Skip to content

Commit

Permalink
Added "UDP/IP Proxy Socket".
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Feb 14, 2024
1 parent d225e59 commit 263a969
Show file tree
Hide file tree
Showing 20 changed files with 1,056 additions and 113 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ option (CC_TOOLS_QT_BUILD_PLUGIN_TCP_CLIENT_SOCKET "Build TCP client socket plug
option (CC_TOOLS_QT_BUILD_PLUGIN_TCP_PROXY_SOCKET "Build TCP proxy socket plugin." ${CC_TOOLS_QT_BUILD_PLUGINS})
option (CC_TOOLS_QT_BUILD_PLUGIN_TCP_SERVER_SOCKET "Build TCP server socket plugin." ${CC_TOOLS_QT_BUILD_PLUGINS})
option (CC_TOOLS_QT_BUILD_PLUGIN_UDP_SOCKET "Build UDP socket plugin." ${CC_TOOLS_QT_BUILD_PLUGINS})
option (CC_TOOLS_QT_BUILD_PLUGIN_UDP_PROXY_SOCKET "Build UDP proxy socket plugin." ${CC_TOOLS_QT_BUILD_PLUGINS})
option (CC_TOOLS_QT_BUILD_PLUGIN_RAW_DATA_PROTOCOL "Build raw data protocol plugin." ${CC_TOOLS_QT_BUILD_PLUGINS})
option (CC_TOOLS_QT_BUILD_PLUGIN_DEMO_PROTOCOL "Build demo protocol plugin." OFF)

Expand Down
57 changes: 2 additions & 55 deletions plugin/udp_socket/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,55 +1,2 @@
if (NOT CC_TOOLS_QT_BUILD_PLUGIN_UDP_SOCKET)
return()
endif ()

######################################################################

function (plugin_udp_client_socket)
set (name "cc_tools_plugin_udp_client_socket")

if (NOT TARGET Qt::Network)
message(WARNING "Can NOT build ${name} due to missing Qt::Network library")
return()
endif ()

set (meta_file "${CMAKE_CURRENT_SOURCE_DIR}/udp_socket.json")
set (stamp_file "${CMAKE_CURRENT_BINARY_DIR}/refresh_stamp.txt")

set (refresh_plugin_header TRUE)
if ((NOT EXISTS ${stamp_file}) OR (${meta_file} IS_NEWER_THAN ${stamp_file}))
execute_process(
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_SOURCE_DIR}/UdpSocketPlugin.h)
execute_process(
COMMAND ${CMAKE_COMMAND} -E touch ${stamp_file})
endif ()

set (
ui
UdpSocketConfigWidget.ui
)

set (src
UdpSocket.cpp
UdpSocketPlugin.h
UdpSocketPlugin.cpp
UdpSocketConfigWidget.cpp
)

add_library (${name} MODULE ${ui} ${src})
target_link_libraries(${name} PRIVATE cc::${PROJECT_NAME} Qt::Network Qt::Widgets Qt::Core)

install (
TARGETS ${name}
DESTINATION ${PLUGIN_INSTALL_DIR})

endfunction()

######################################################################

cc_find_qt_components (Network)

include_directories (
${CMAKE_CURRENT_BINARY_DIR}
)

plugin_udp_client_socket ()
add_subdirectory (generic)
add_subdirectory (proxy)
55 changes: 55 additions & 0 deletions plugin/udp_socket/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
if (NOT CC_TOOLS_QT_BUILD_PLUGIN_UDP_SOCKET)
return()
endif ()

######################################################################

function (plugin_udp_generic_socket)
set (name "cc_tools_plugin_udp_generic_socket")

if (NOT TARGET Qt::Network)
message(WARNING "Can NOT build ${name} due to missing Qt::Network library")
return()
endif ()

set (meta_file "${CMAKE_CURRENT_SOURCE_DIR}/udp_socket.json")
set (stamp_file "${CMAKE_CURRENT_BINARY_DIR}/refresh_stamp.txt")

set (refresh_plugin_header TRUE)
if ((NOT EXISTS ${stamp_file}) OR (${meta_file} IS_NEWER_THAN ${stamp_file}))
execute_process(
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_SOURCE_DIR}/UdpGenericSocketPlugin.h)
execute_process(
COMMAND ${CMAKE_COMMAND} -E touch ${stamp_file})
endif ()

set (
ui
UdpGenericSocketConfigWidget.ui
)

set (src
UdpGenericSocket.cpp
UdpGenericSocketPlugin.h
UdpGenericSocketPlugin.cpp
UdpGenericSocketConfigWidget.cpp
)

add_library (${name} MODULE ${ui} ${src})
target_link_libraries(${name} PRIVATE cc::${PROJECT_NAME} Qt::Network Qt::Widgets Qt::Core)

install (
TARGETS ${name}
DESTINATION ${PLUGIN_INSTALL_DIR})

endfunction()

######################################################################

cc_find_qt_components (Network)

include_directories (
${CMAKE_CURRENT_BINARY_DIR}
)

plugin_udp_generic_socket ()
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <QtCore/QtGlobal>
#include <QtNetwork/QHostAddress>

#include "UdpSocket.h"
#include "UdpGenericSocket.h"

namespace cc_tools_qt
{
Expand All @@ -41,27 +41,27 @@ const QString ToPropName("udp.to");
} // namespace


UdpSocket::UdpSocket()
UdpGenericSocket::UdpGenericSocket()
: m_host(DefaultHost)
{
connect(
&m_socket, &QUdpSocket::disconnected,
this, &UdpSocket::socketDisconnected);
this, &UdpGenericSocket::socketDisconnected);
connect(
&m_socket, &QUdpSocket::readyRead,
this, &UdpSocket::readFromSocket);
this, &UdpGenericSocket::readFromSocket);
connect(
&m_broadcastSocket, &QUdpSocket::readyRead,
this, &UdpSocket::readFromBroadcastSocket);
this, &UdpGenericSocket::readFromBroadcastSocket);

#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
connect(
&m_socket, &QUdpSocket::errorOccurred,
this, &UdpSocket::socketErrorOccurred);
this, &UdpGenericSocket::socketErrorOccurred);

connect(
&m_broadcastSocket, &QUdpSocket::errorOccurred,
this, &UdpSocket::socketErrorOccurred);
this, &UdpGenericSocket::socketErrorOccurred);
#else // #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
connect(
&m_socket, SIGNAL(error(QAbstractSocket::SocketError)),
Expand All @@ -72,12 +72,13 @@ UdpSocket::UdpSocket()
#endif // #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
}

UdpSocket::~UdpSocket() noexcept
UdpGenericSocket::~UdpGenericSocket() noexcept
{
m_socket.blockSignals(true);
m_broadcastSocket.blockSignals(true);
}

bool UdpSocket::socketConnectImpl()
bool UdpGenericSocket::socketConnectImpl()
{
if ((!m_host.isEmpty()) && (m_port == 0)) {
static const QString Error =
Expand Down Expand Up @@ -125,7 +126,7 @@ bool UdpSocket::socketConnectImpl()
return true;
}

void UdpSocket::socketDisconnectImpl()
void UdpGenericSocket::socketDisconnectImpl()
{
m_socket.blockSignals(true);
m_socket.close();
Expand All @@ -134,7 +135,7 @@ void UdpSocket::socketDisconnectImpl()
m_socket.blockSignals(false);
}

void UdpSocket::sendDataImpl(DataInfoPtr dataPtr)
void UdpGenericSocket::sendDataImpl(DataInfoPtr dataPtr)
{
assert(dataPtr);
QString from =
Expand Down Expand Up @@ -205,27 +206,27 @@ void UdpSocket::sendDataImpl(DataInfoPtr dataPtr)

}

void UdpSocket::socketDisconnected()
void UdpGenericSocket::socketDisconnected()
{
reportDisconnected();
}

void UdpSocket::readFromSocket()
void UdpGenericSocket::readFromSocket()
{
readData(m_socket);
}

void UdpSocket::readFromBroadcastSocket()
void UdpGenericSocket::readFromBroadcastSocket()
{
readData(m_broadcastSocket);
}

void UdpSocket::socketErrorOccurred([[maybe_unused]] QAbstractSocket::SocketError err)
void UdpGenericSocket::socketErrorOccurred([[maybe_unused]] QAbstractSocket::SocketError err)
{
std::cout << "ERROR: UDP Socket: " << m_socket.errorString().toStdString() << std::endl;
}

void UdpSocket::readData(QUdpSocket& socket)
void UdpGenericSocket::readData(QUdpSocket& socket)
{
while (socket.hasPendingDatagrams()) {
QHostAddress senderAddress;
Expand Down Expand Up @@ -260,7 +261,7 @@ void UdpSocket::readData(QUdpSocket& socket)
}
}

bool UdpSocket::bindSocket(QUdpSocket& socket)
bool UdpGenericSocket::bindSocket(QUdpSocket& socket)
{
if (!socket.bind(QHostAddress::AnyIPv4, m_localPort, QUdpSocket::ShareAddress)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ namespace cc_tools_qt
namespace plugin
{

class UdpSocket : public QObject, public cc_tools_qt::Socket
class UdpGenericSocket : public QObject, public cc_tools_qt::Socket
{
Q_OBJECT
using Base = cc_tools_qt::Socket;

public:
typedef unsigned short PortType;

UdpSocket();
~UdpSocket() noexcept;
UdpGenericSocket();
~UdpGenericSocket() noexcept;

void setHost(const QString& value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "UdpSocketConfigWidget.h"
#include "UdpGenericSocketConfigWidget.h"

#include <limits>

Expand All @@ -25,8 +25,8 @@ namespace cc_tools_qt
namespace plugin
{

UdpSocketConfigWidget::UdpSocketConfigWidget(
UdpSocket& socket,
UdpGenericSocketConfigWidget::UdpGenericSocketConfigWidget(
UdpGenericSocket& socket,
QWidget* parentObj)
: Base(parentObj),
m_socket(socket)
Expand All @@ -53,40 +53,40 @@ UdpSocketConfigWidget::UdpSocketConfigWidget(

connect(
m_ui.m_hostLineEdit, &QLineEdit::textChanged,
this, &UdpSocketConfigWidget::hostValueChanged);
this, &UdpGenericSocketConfigWidget::hostValueChanged);

connect(
m_ui.m_portSpinBox, qOverload<int>(&QSpinBox::valueChanged),
this, &UdpSocketConfigWidget::portValueChanged);
this, &UdpGenericSocketConfigWidget::portValueChanged);

connect(
m_ui.m_localPortSpinBox, qOverload<int>(&QSpinBox::valueChanged),
this, &UdpSocketConfigWidget::localPortValueChanged);
this, &UdpGenericSocketConfigWidget::localPortValueChanged);

connect(
m_ui.m_broadcastMaskLineEdit, &QLineEdit::textChanged,
this, &UdpSocketConfigWidget::broadcastMaskValueChanged);
this, &UdpGenericSocketConfigWidget::broadcastMaskValueChanged);

}

UdpSocketConfigWidget::~UdpSocketConfigWidget() noexcept = default;
UdpGenericSocketConfigWidget::~UdpGenericSocketConfigWidget() noexcept = default;

void UdpSocketConfigWidget::hostValueChanged(const QString& value)
void UdpGenericSocketConfigWidget::hostValueChanged(const QString& value)
{
m_socket.setHost(value);
}

void UdpSocketConfigWidget::portValueChanged(int value)
void UdpGenericSocketConfigWidget::portValueChanged(int value)
{
m_socket.setPort(static_cast<PortType>(value));
}

void UdpSocketConfigWidget::localPortValueChanged(int value)
void UdpGenericSocketConfigWidget::localPortValueChanged(int value)
{
m_socket.setLocalPort(static_cast<PortType>(value));
}

void UdpSocketConfigWidget::broadcastMaskValueChanged(const QString& value)
void UdpGenericSocketConfigWidget::broadcastMaskValueChanged(const QString& value)
{
m_socket.setBroadcastMask(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@
#pragma once

#include <QtWidgets/QWidget>
#include "ui_UdpSocketConfigWidget.h"
#include "ui_UdpGenericSocketConfigWidget.h"

#include "UdpSocket.h"
#include "UdpGenericSocket.h"

namespace cc_tools_qt
{

namespace plugin
{

class UdpSocketConfigWidget : public QWidget
class UdpGenericSocketConfigWidget : public QWidget
{
Q_OBJECT
typedef QWidget Base;
public:
typedef UdpSocket::PortType PortType;
typedef UdpGenericSocket::PortType PortType;

explicit UdpSocketConfigWidget(
UdpSocket& socket,
explicit UdpGenericSocketConfigWidget(
UdpGenericSocket& socket,
QWidget* parentObj = nullptr);

~UdpSocketConfigWidget() noexcept;
~UdpGenericSocketConfigWidget() noexcept;

private slots:
void hostValueChanged(const QString& value);
Expand All @@ -49,8 +49,8 @@ private slots:
void broadcastMaskValueChanged(const QString& value);

private:
UdpSocket& m_socket;
Ui::UdpSocketConfigWidget m_ui;
UdpGenericSocket& m_socket;
Ui::UdpGenericSocketConfigWidget m_ui;
};

} // namespace plugin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UdpSocketConfigWidget</class>
<widget class="QWidget" name="UdpSocketConfigWidget">
<class>UdpGenericSocketConfigWidget</class>
<widget class="QWidget" name="UdpGenericSocketConfigWidget">
<property name="geometry">
<rect>
<x>0</x>
Expand Down
Loading

0 comments on commit 263a969

Please sign in to comment.