From 17b902ebcca24cca9c7cf169ce0f461dd17e449c Mon Sep 17 00:00:00 2001 From: peri4 Date: Fri, 13 Sep 2024 11:08:32 +0300 Subject: [PATCH] add PIClientServer::Config, common configuration type for both sides, stream packer and encryption settings --- libs/client_server/piclientserver_client.cpp | 1 + .../piclientserver_client_base.cpp | 9 --- libs/client_server/piclientserver_config.cpp | 50 ++++++++++++++++ libs/client_server/piclientserver_server.cpp | 7 +-- .../piclientserver_client_base.h | 6 +- .../client_server/piclientserver_config.h | 60 +++++++++++++++++++ .../client_server/piclientserver_server.h | 5 +- main.cpp | 12 ++-- 8 files changed, 125 insertions(+), 25 deletions(-) create mode 100644 libs/client_server/piclientserver_config.cpp create mode 100644 libs/main/client_server/piclientserver_config.h diff --git a/libs/client_server/piclientserver_client.cpp b/libs/client_server/piclientserver_client.cpp index 8f139b07..7c81d86b 100644 --- a/libs/client_server/piclientserver_client.cpp +++ b/libs/client_server/piclientserver_client.cpp @@ -47,6 +47,7 @@ PIClientServer::Client::~Client() { void PIClientServer::Client::connect(PINetworkAddress addr) { if (!tcp || !own_tcp) return; close(); + config.apply(this); tcp->connect(addr, true); tcp->startThreadedRead(); piCout << "Connect to" << addr.toString(); diff --git a/libs/client_server/piclientserver_client_base.cpp b/libs/client_server/piclientserver_client_base.cpp index 41b6f800..faa99b97 100644 --- a/libs/client_server/piclientserver_client_base.cpp +++ b/libs/client_server/piclientserver_client_base.cpp @@ -53,15 +53,6 @@ int PIClientServer::ClientBase::write(const void * d, const size_t s) { } -void PIClientServer::ClientBase::enableSymmetricEncryption(const PIByteArray & key) { - if (key.isNotEmpty()) { - stream.setCryptEnabled(true); - stream.setCryptKey(key); - } else - stream.setCryptEnabled(false); -} - - void PIClientServer::ClientBase::init() { if (!tcp) return; CONNECTL(&stream, sendRequest, [this](const PIByteArray & ba) { diff --git a/libs/client_server/piclientserver_config.cpp b/libs/client_server/piclientserver_config.cpp new file mode 100644 index 00000000..a8d190a0 --- /dev/null +++ b/libs/client_server/piclientserver_config.cpp @@ -0,0 +1,50 @@ +/* + PIP - Platform Independent Primitives + Ivan Pelipenko peri4ko@yandex.ru + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#include "piclientserver_config.h" + +#include "piclientserver_client_base.h" + + +void PIClientServer::Config::setPacketSign(ushort sign) { + packet_sign = sign; +} + + +void PIClientServer::Config::setPacketSize(int bytes) { + packet_size = bytes; +} + + +void PIClientServer::Config::enableSymmetricEncryption(const PIByteArray & key) { + crypt_key = key; +} + + +void PIClientServer::Config::apply(ClientBase * client) { + auto & s(client->stream); + + s.setPacketSign(packet_sign); + s.setMaxPacketSize(packet_size); + + if (crypt_key.isNotEmpty()) { + s.setCryptEnabled(true); + s.setCryptKey(crypt_key); + } else + s.setCryptEnabled(false); +} diff --git a/libs/client_server/piclientserver_server.cpp b/libs/client_server/piclientserver_server.cpp index 501c1564..164f5dfa 100644 --- a/libs/client_server/piclientserver_server.cpp +++ b/libs/client_server/piclientserver_server.cpp @@ -110,11 +110,6 @@ void PIClientServer::Server::forEachClient(std::function f } -void PIClientServer::Server::enableSymmetricEncryption(const PIByteArray & key) { - crypt_key = key; -} - - void PIClientServer::Server::stopServer() { if (!tcp_server) return; is_closing = true; @@ -125,7 +120,7 @@ void PIClientServer::Server::stopServer() { void PIClientServer::Server::newClient(ServerClient * c) { clients << c; - c->enableSymmetricEncryption(crypt_key); + config.apply(c); c->tcp->startThreadedRead(); c->connected(); piCout << "New client"; diff --git a/libs/main/client_server/piclientserver_client_base.h b/libs/main/client_server/piclientserver_client_base.h index 249b0d6f..3c1105ed 100644 --- a/libs/main/client_server/piclientserver_client_base.h +++ b/libs/main/client_server/piclientserver_client_base.h @@ -25,7 +25,7 @@ #ifndef piclientserver_client_base_H #define piclientserver_client_base_H -#include "pinetworkaddress.h" +#include "piclientserver_config.h" #include "pip_client_server_export.h" #include "pistreampacker.h" @@ -36,6 +36,7 @@ namespace PIClientServer { class Server; class PIP_CLIENT_SERVER_EXPORT ClientBase { + friend class Config; friend class Server; NO_COPY_CLASS(ClientBase); @@ -50,7 +51,7 @@ public: int write(const void * d, const size_t s); int write(const PIByteArray & ba) { return write(ba.data(), ba.size()); } - void enableSymmetricEncryption(const PIByteArray & key); + Config & configuration() { return config; } protected: virtual void readed(PIByteArray data) {} @@ -62,6 +63,7 @@ protected: bool own_tcp = false; std::atomic_bool can_write = {true}; PIEthernet * tcp = nullptr; + Config config; private: void destroy(); diff --git a/libs/main/client_server/piclientserver_config.h b/libs/main/client_server/piclientserver_config.h new file mode 100644 index 00000000..6ed6b5f4 --- /dev/null +++ b/libs/main/client_server/piclientserver_config.h @@ -0,0 +1,60 @@ +/*! \file piclientserver_config.h + * \ingroup ClientServer + * \~\brief + * \~english + * \~russian + */ +/* + PIP - Platform Independent Primitives + Ivan Pelipenko peri4ko@yandex.ru + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#ifndef piclientserver_config_H +#define piclientserver_config_H + +#include "pibytearray.h" +#include "pip_client_server_export.h" + + +namespace PIClientServer { + +class Server; +class Client; +class ClientBase; + +class PIP_CLIENT_SERVER_EXPORT Config { + friend class Server; + friend class Client; + +public: + void setPacketSign(ushort sign); + void setPacketSize(int bytes); + + void enableSymmetricEncryption(const PIByteArray & key); + +protected: + void apply(ClientBase * client); + + PIByteArray crypt_key; + ushort packet_sign = 0xAFBE; + int packet_size = 1400; + +private: +}; + +} // namespace PIClientServer + +#endif diff --git a/libs/main/client_server/piclientserver_server.h b/libs/main/client_server/piclientserver_server.h index 10e1b051..37a380d1 100644 --- a/libs/main/client_server/piclientserver_server.h +++ b/libs/main/client_server/piclientserver_server.h @@ -25,6 +25,7 @@ #ifndef piclientserver_server_H #define piclientserver_server_H +#include "piclientserver_config.h" #include "pimutex.h" #include "pinetworkaddress.h" #include "pip_client_server_export.h" @@ -55,7 +56,7 @@ public: void setClientFactory(std::function f) { client_factory = f; } - void enableSymmetricEncryption(const PIByteArray & key); + Config & configuration() { return config; } private: void stopServer(); @@ -67,7 +68,7 @@ private: PIEthernet * tcp_server = nullptr; PIThread * clean_thread = nullptr; PIThreadNotifier clean_notifier; - PIByteArray crypt_key; + Config config; PIVector clients; mutable PIMutex clients_mutex; diff --git a/main.cpp b/main.cpp index 2bab34c9..a9c31875 100644 --- a/main.cpp +++ b/main.cpp @@ -27,7 +27,7 @@ public: ~MyServerClient() { send_thread.stopAndWait(); } protected: - // void readed(PIByteArray data) override { piCout << "readed" << (data.size()); } + void readed(PIByteArray data) override { piCout << "readed" << (data.size()); } // void aboutDelete() override { piCout << "aboutDelete"; } // void disconnected() override { piCout << "disconnected"; } void connected() override { @@ -50,7 +50,7 @@ public: ~MyClient() { send_thread.stopAndWait(); } protected: - // void readed(PIByteArray data) override { piCout << "readed" << (data.size()); } + void readed(PIByteArray data) override { piCout << "readed" << (data.size()); } void disconnected() override { piCout << "disconnected"; } void connected() override { piCout << "connected"; @@ -101,7 +101,7 @@ int main(int argc, char * argv[]) { piCout << "Server"; s = new PIClientServer::Server(); s->setClientFactory([] { return new MyServerClient(); }); - s->enableSymmetricEncryption("1122334455667788"_hex); + s->configuration().enableSymmetricEncryption("1122334455667788"_hex); s->listenAll(12345); s_thread->start( [s] { @@ -110,17 +110,17 @@ int main(int argc, char * argv[]) { s->forEachClient([&i](PIClientServer::ServerClient * c) { // piCout << "client" << ++i << c; c->write(PIByteArray(1_KiB)); - c->close(); + // c->close(); // piMSleep(200); }); }, 1._Hz); } else { piCout << "Client"; - piForTimes(20) { + piForTimes(2) { piMSleep(25); auto c = new MyClient(); - c->enableSymmetricEncryption("1122334455667788"_hex); + c->configuration().enableSymmetricEncryption("1122334455667788"_hex); c->connect(PINetworkAddress::resolve("127.0.0.1", 12345)); cv << c; }