add PIClientServer::Config, common configuration type for both sides, stream packer and encryption settings
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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) {
|
||||
|
||||
50
libs/client_server/piclientserver_config.cpp
Normal file
50
libs/client_server/piclientserver_config.cpp
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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);
|
||||
}
|
||||
@@ -110,11 +110,6 @@ void PIClientServer::Server::forEachClient(std::function<void(ServerClient *)> 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";
|
||||
|
||||
Reference in New Issue
Block a user