Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierLDff committed May 5, 2024
1 parent fcb6d5b commit 410929f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 80 deletions.
4 changes: 2 additions & 2 deletions docs/GettingStart.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ protected:
const auto s = new MySocket(parent);
connect(s, &MySocket::stringReceived, [this, s](const QString& string)
{
qInfo("RX \"%s\" from client %s:%d", qPrintable(string), qPrintable(s->peerAddress()), signed(s->peerPort()));
qInfo("RX \"%s\" from client %s:%d", qPrintable(string), qPrintable(s->peerAddress()), int(s->peerPort()));
Q_EMIT s->sendString(string);
});
return s;
Expand Down Expand Up @@ -321,4 +321,4 @@ Options:

You can also check `NetTcp_EchoServer` that implement only the server code that reply echo to client that will connect.

And check `NetTcp_EchoClient`, example of a client that will connect to a server and send a string.
And check `NetTcp_EchoClient`, example of a client that will connect to a server and send a string.
14 changes: 6 additions & 8 deletions examples/EchoClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ class App
[this](const QString value)
{
clientLog->info("Rx \"{}\" from server {}:{}", qPrintable(value), qPrintable(client.peerAddress()),
signed(client.peerPort()));
int(client.peerPort()));
});
QObject::connect(
&client, &net::tcp::Socket::isRunningChanged, [](bool value) { clientLog->info("isRunning : {}", value); });
QObject::connect(&client, &net::tcp::Socket::isRunningChanged,
[](bool value) { clientLog->info("isRunning : {}", value); });
QObject::connect(&client, &net::tcp::Socket::isConnectedChanged,
[this](bool value)
{
Expand All @@ -74,7 +74,7 @@ class App
[](quint64 total) { clientLog->info("Sent bytes : {}", total); });

client.setUseWorkerThread(multiThreaded);
clientLog->info("Start client to connect to address {}, on port {}", qPrintable(ip), signed(port));
clientLog->info("Start client to connect to address {}, on port {}", qPrintable(ip), int(port));
client.start(ip, port);

appLog->info("Start application");
Expand Down Expand Up @@ -115,15 +115,13 @@ int main(int argc, char* argv[])
QCoreApplication::translate("main", "Make the worker live in a different thread. Default false"));
parser.addOption(multiThreadOption);

QCommandLineOption portOption(QStringList() << "s"
<< "src",
QCommandLineOption portOption(QStringList() << "s" << "src",
QCoreApplication::translate("main", "Port for rx packet. Default \"9999\"."),
QCoreApplication::translate("main", "port"));
portOption.setDefaultValue("9999");
parser.addOption(portOption);

QCommandLineOption ipOption(QStringList() << "i"
<< "ip",
QCommandLineOption ipOption(QStringList() << "i" << "ip",
QCoreApplication::translate("main", "Ip address of multicast group. Default \"127.0.0.1\""),
QCoreApplication::translate("main", "ip"));
ipOption.setDefaultValue(QStringLiteral("127.0.0.1"));
Expand Down
30 changes: 13 additions & 17 deletions examples/EchoClientServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ class App
[this](const QString value)
{
clientLog->info("Rx \"{}\" from server {}:{}", qPrintable(value), qPrintable(client.peerAddress()),
signed(client.peerPort()));
int(client.peerPort()));
});
// Print the message that received from client socket
QObject::connect(&server, &MyServer::stringReceived,
[](const QString value, const QString address, const quint16 port)
{ serverLog->info("Rx \"{}\" from server {}:{}", qPrintable(value), qPrintable(address), port); });

QObject::connect(
&server, &net::tcp::Server::isRunningChanged, [](bool value) { serverLog->info("isRunning : {}", value); });
QObject::connect(&server, &net::tcp::Server::isRunningChanged,
[](bool value) { serverLog->info("isRunning : {}", value); });
QObject::connect(&server, &net::tcp::Server::isListeningChanged,
[](bool value) { serverLog->info("isBounded : {}", value); });
QObject::connect(
&client, &net::tcp::Socket::isRunningChanged, [](bool value) { clientLog->info("isRunning : {}", value); });
QObject::connect(&client, &net::tcp::Socket::isRunningChanged,
[](bool value) { clientLog->info("isRunning : {}", value); });
QObject::connect(&client, &net::tcp::Socket::isConnectedChanged,
[this](bool value)
{
Expand All @@ -87,21 +87,19 @@ class App
[](int value, const QString& error) { serverLog->error("accept error : {}", error.toStdString()); });
QObject::connect(&client, &net::tcp::Socket::socketError,
[](int value, const QString& error) { clientLog->error("socket error : {}", error.toStdString()); });
QObject::connect(&server, &net::tcp::Server::newClient,
[](const QString& address, const quint16 port)
{ serverLog->info("New Client {}:{}", qPrintable(address), signed(port)); });
QObject::connect(&server, &net::tcp::Server::clientLost,
[](const QString& address, const quint16 port)
{ serverLog->info("Client Disconnected {}:{}", qPrintable(address), signed(port)); });
QObject::connect(&server, &net::tcp::Server::newClient, [](const QString& address, const quint16 port)
{ serverLog->info("New Client {}:{}", qPrintable(address), int(port)); });
QObject::connect(&server, &net::tcp::Server::clientLost, [](const QString& address, const quint16 port)
{ serverLog->info("Client Disconnected {}:{}", qPrintable(address), int(port)); });
QObject::connect(&client, &net::tcp::Socket::txBytesTotalChanged,
[](quint64 total) { clientLog->info("Sent bytes : {}", total); });

serverLog->info("Start server on address {}:{}", qPrintable(ip), signed(port));
serverLog->info("Start server on address {}:{}", qPrintable(ip), int(port));
// server.start(port) can be called to listen from every interfaces
server.start(ip, port);

client.setUseWorkerThread(multiThreaded);
clientLog->info("Start client to connect to address {}, on port {}", qPrintable(ip), signed(port));
clientLog->info("Start client to connect to address {}, on port {}", qPrintable(ip), int(port));
client.start(ip, port);

appLog->info("Start application");
Expand Down Expand Up @@ -144,15 +142,13 @@ int main(int argc, char* argv[])
QCoreApplication::translate("main", "Make the worker live in a different thread. Default false"));
parser.addOption(multiThreadOption);

QCommandLineOption portOption(QStringList() << "s"
<< "src",
QCommandLineOption portOption(QStringList() << "s" << "src",
QCoreApplication::translate("main", "Port for rx packet. Default \"9999\"."),
QCoreApplication::translate("main", "port"));
portOption.setDefaultValue("9999");
parser.addOption(portOption);

QCommandLineOption ipOption(QStringList() << "i"
<< "ip",
QCommandLineOption ipOption(QStringList() << "i" << "ip",
QCoreApplication::translate("main", "Ip address of multicast group. Default \"127.0.0.1\""),
QCoreApplication::translate("main", "ip"));
ipOption.setDefaultValue(QStringLiteral("127.0.0.1"));
Expand Down
24 changes: 10 additions & 14 deletions examples/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,18 @@ class App
[](const QString value, const QString address, const quint16 port)
{ serverLog->info("Rx \"{}\" from server {}:{}", qPrintable(value), qPrintable(address), port); });

QObject::connect(
&server, &net::tcp::Server::isRunningChanged, [](bool value) { serverLog->info("isRunning : {}", value); });
QObject::connect(&server, &net::tcp::Server::isRunningChanged,
[](bool value) { serverLog->info("isRunning : {}", value); });
QObject::connect(&server, &net::tcp::Server::isListeningChanged,
[](bool value) { serverLog->info("isBounded : {}", value); });
QObject::connect(&server, &net::tcp::Server::acceptError,
[](int value, const QString& error) { serverLog->error("accept error : {}", error.toStdString()); });
QObject::connect(&server, &net::tcp::Server::newClient,
[](const QString& address, const quint16 port)
{ serverLog->info("New Client {}:{}", qPrintable(address), signed(port)); });
QObject::connect(&server, &net::tcp::Server::clientLost,
[](const QString& address, const quint16 port)
{ serverLog->info("Client Disconnected {}:{}", qPrintable(address), signed(port)); });

serverLog->info("Start server on address {}:{}", qPrintable(ip), signed(port));
QObject::connect(&server, &net::tcp::Server::newClient, [](const QString& address, const quint16 port)
{ serverLog->info("New Client {}:{}", qPrintable(address), int(port)); });
QObject::connect(&server, &net::tcp::Server::clientLost, [](const QString& address, const quint16 port)
{ serverLog->info("Client Disconnected {}:{}", qPrintable(address), int(port)); });

serverLog->info("Start server on address {}:{}", qPrintable(ip), int(port));
// server.start(port) can be called to listen from every interfaces
server.start(ip, port);

Expand Down Expand Up @@ -100,15 +98,13 @@ int main(int argc, char* argv[])
QCoreApplication::translate("main", "Make the worker live in a different thread. Default false"));
parser.addOption(multiThreadOption);

QCommandLineOption portOption(QStringList() << "s"
<< "src",
QCommandLineOption portOption(QStringList() << "s" << "src",
QCoreApplication::translate("main", "Port for rx packet. Default \"9999\"."),
QCoreApplication::translate("main", "port"));
portOption.setDefaultValue("9999");
parser.addOption(portOption);

QCommandLineOption ipOption(QStringList() << "i"
<< "ip",
QCommandLineOption ipOption(QStringList() << "i" << "ip",
QCoreApplication::translate("main", "Ip address of multicast group. Default \"127.0.0.1\""),
QCoreApplication::translate("main", "ip"));
ipOption.setDefaultValue(QStringLiteral("127.0.0.1"));
Expand Down
28 changes: 12 additions & 16 deletions examples/FuzzDisconnectionClientServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class App
}
});

QObject::connect(
&server, &net::tcp::Server::isRunningChanged, [](bool value) { serverLog->info("isRunning : {}", value); });
QObject::connect(&server, &net::tcp::Server::isRunningChanged,
[](bool value) { serverLog->info("isRunning : {}", value); });
QObject::connect(&server, &net::tcp::Server::isListeningChanged,
[](bool value) { serverLog->info("isBounded : {}", value); });
QObject::connect(
&client, &net::tcp::Socket::isRunningChanged, [](bool value) { clientLog->info("isRunning : {}", value); });
QObject::connect(&client, &net::tcp::Socket::isRunningChanged,
[](bool value) { clientLog->info("isRunning : {}", value); });
QObject::connect(&client, &net::tcp::Socket::isConnectedChanged,
[this](bool value)
{
Expand All @@ -76,22 +76,20 @@ class App
[](int value, const QString& error) { serverLog->error("accept error : {}", error.toStdString()); });
QObject::connect(&client, &net::tcp::Socket::socketError,
[](int value, const QString& error) { clientLog->error("socket error : {}", error.toStdString()); });
QObject::connect(&server, &net::tcp::Server::newClient,
[](const QString& address, const quint16 port)
{ serverLog->info("New Client {}:{}", qPrintable(address), signed(port)); });
QObject::connect(&server, &net::tcp::Server::clientLost,
[](const QString& address, const quint16 port)
{ serverLog->info("Client Disconnected {}:{}", qPrintable(address), signed(port)); });
QObject::connect(&server, &net::tcp::Server::newClient, [](const QString& address, const quint16 port)
{ serverLog->info("New Client {}:{}", qPrintable(address), int(port)); });
QObject::connect(&server, &net::tcp::Server::clientLost, [](const QString& address, const quint16 port)
{ serverLog->info("Client Disconnected {}:{}", qPrintable(address), int(port)); });
QObject::connect(&client, &net::tcp::Socket::txBytesTotalChanged,
[](quint64 total) { clientLog->info("Sent bytes : {}", total); });

serverLog->info("Start server on address {}:{}", qPrintable(ip), signed(port));
serverLog->info("Start server on address {}:{}", qPrintable(ip), int(port));
// server.start(port) can be called to listen from every interfaces
server.start(ip, port);

client.setWatchdogPeriod(1);
client.setUseWorkerThread(multiThreaded);
clientLog->info("Start client to connect to address {}, on port {}", qPrintable(ip), signed(port));
clientLog->info("Start client to connect to address {}, on port {}", qPrintable(ip), int(port));
client.start(ip, port);

appLog->info("Start application");
Expand Down Expand Up @@ -134,15 +132,13 @@ int main(int argc, char* argv[])
QCoreApplication::translate("main", "Make the worker live in a different thread. Default false"));
parser.addOption(multiThreadOption);

QCommandLineOption portOption(QStringList() << "s"
<< "src",
QCommandLineOption portOption(QStringList() << "s" << "src",
QCoreApplication::translate("main", "Port for rx packet. Default \"9999\"."),
QCoreApplication::translate("main", "port"));
portOption.setDefaultValue("9999");
parser.addOption(portOption);

QCommandLineOption ipOption(QStringList() << "i"
<< "ip",
QCommandLineOption ipOption(QStringList() << "i" << "ip",
QCoreApplication::translate("main", "Ip address of multicast group. Default \"127.0.0.1\""),
QCoreApplication::translate("main", "ip"));
ipOption.setDefaultValue(QStringLiteral("127.0.0.1"));
Expand Down
28 changes: 12 additions & 16 deletions examples/FuzzDisconnectionServerClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ class App
}
});

QObject::connect(
&server, &net::tcp::Server::isRunningChanged, [](bool value) { serverLog->info("isRunning : {}", value); });
QObject::connect(&server, &net::tcp::Server::isRunningChanged,
[](bool value) { serverLog->info("isRunning : {}", value); });
QObject::connect(&server, &net::tcp::Server::isListeningChanged,
[](bool value) { serverLog->info("isBounded : {}", value); });
QObject::connect(
&client, &net::tcp::Socket::isRunningChanged, [](bool value) { clientLog->info("isRunning : {}", value); });
QObject::connect(&client, &net::tcp::Socket::isRunningChanged,
[](bool value) { clientLog->info("isRunning : {}", value); });
QObject::connect(&client, &net::tcp::Socket::isConnectedChanged,
[this](bool value)
{
Expand All @@ -78,22 +78,20 @@ class App
[](int value, const QString& error) { serverLog->error("accept error : {}", error.toStdString()); });
QObject::connect(&client, &net::tcp::Socket::socketError,
[](int value, const QString& error) { clientLog->error("socket error : {}", error.toStdString()); });
QObject::connect(&server, &net::tcp::Server::newClient,
[](const QString& address, const quint16 port)
{ serverLog->info("New Client {}:{}", qPrintable(address), signed(port)); });
QObject::connect(&server, &net::tcp::Server::clientLost,
[](const QString& address, const quint16 port)
{ serverLog->info("Client Disconnected {}:{}", qPrintable(address), signed(port)); });
QObject::connect(&server, &net::tcp::Server::newClient, [](const QString& address, const quint16 port)
{ serverLog->info("New Client {}:{}", qPrintable(address), int(port)); });
QObject::connect(&server, &net::tcp::Server::clientLost, [](const QString& address, const quint16 port)
{ serverLog->info("Client Disconnected {}:{}", qPrintable(address), int(port)); });
QObject::connect(&client, &net::tcp::Socket::txBytesTotalChanged,
[](quint64 total) { clientLog->info("Sent bytes : {}", total); });

serverLog->info("Start server on address {}:{}", qPrintable(ip), signed(port));
serverLog->info("Start server on address {}:{}", qPrintable(ip), int(port));
// server.start(port) can be called to listen from every interfaces
server.start(ip, port);

client.setWatchdogPeriod(1);
client.setUseWorkerThread(multiThreaded);
clientLog->info("Start client to connect to address {}, on port {}", qPrintable(ip), signed(port));
clientLog->info("Start client to connect to address {}, on port {}", qPrintable(ip), int(port));
client.start(ip, port);

appLog->info("Start application");
Expand Down Expand Up @@ -136,15 +134,13 @@ int main(int argc, char* argv[])
QCoreApplication::translate("main", "Make the worker live in a different thread. Default false"));
parser.addOption(multiThreadOption);

QCommandLineOption portOption(QStringList() << "s"
<< "src",
QCommandLineOption portOption(QStringList() << "s" << "src",
QCoreApplication::translate("main", "Port for rx packet. Default \"9999\"."),
QCoreApplication::translate("main", "port"));
portOption.setDefaultValue("9999");
parser.addOption(portOption);

QCommandLineOption ipOption(QStringList() << "i"
<< "ip",
QCommandLineOption ipOption(QStringList() << "i" << "ip",
QCoreApplication::translate("main", "Ip address of multicast group. Default \"127.0.0.1\""),
QCoreApplication::translate("main", "ip"));
ipOption.setDefaultValue(QStringLiteral("127.0.0.1"));
Expand Down
8 changes: 4 additions & 4 deletions src/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ bool Socket::start()

if(socketDescriptor())
{
LOG_INFO("Start tcp socket via socketDescriptor {}", signed(socketDescriptor()));
LOG_INFO("Start tcp socket via socketDescriptor {}", int(socketDescriptor()));
}
else
{
LOG_INFO("Start tcp socket to {}:{}", qPrintable(peerAddress()), signed(peerPort()));
LOG_INFO("Start tcp socket to {}:{}", qPrintable(peerAddress()), int(peerPort()));
}

Q_ASSERT(_worker == nullptr);
Expand Down Expand Up @@ -283,8 +283,8 @@ void Socket::killWorker()
}
}

void Socket::onStartSuccess(
const QString& peerAddress, const quint16 peerPort, const QString& localAddress, const quint16 localPort)
void Socket::onStartSuccess(const QString& peerAddress, const quint16 peerPort, const QString& localAddress,
const quint16 localPort)
{
if(socketDescriptor())
{
Expand Down
6 changes: 3 additions & 3 deletions src/SocketWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void SocketWorker::onStart()
if(_socketDescriptor)
LOG_DEV_INFO("Start worker {}", std::uintptr_t(_socketDescriptor));
else
LOG_DEV_INFO("Start worker {}:{}", qPrintable(_address), signed(_port));
LOG_DEV_INFO("Start worker {}:{}", qPrintable(_address), int(_port));

Q_ASSERT(!_socket);
_socket = new QTcpSocket(this);
Expand Down Expand Up @@ -118,7 +118,7 @@ void SocketWorker::closeSocket()
if(_socketDescriptor)
LOG_DEV_INFO("Close socket worker {}", _socketDescriptor);
else
LOG_DEV_INFO("Close socket worker {}:{}", qPrintable(_address), signed(_port));
LOG_DEV_INFO("Close socket worker {}:{}", qPrintable(_address), int(_port));

Q_ASSERT(_socket);
disconnect(this, nullptr, _socket, nullptr);
Expand Down Expand Up @@ -311,7 +311,7 @@ void SocketWorker::closeAndRestart()
_watchdog->setSingleShot(true);
}
_watchdog->start(_watchdogPeriod);
LOG_INFO("Start Watchdog to attempt reconnection in {} ms", signed(_watchdogPeriod));
LOG_INFO("Start Watchdog to attempt reconnection in {} ms", int(_watchdogPeriod));
}

void SocketWorker::setNoDelay(bool value)
Expand Down

0 comments on commit 410929f

Please sign in to comment.