PIEthernet fix tcp-server close (properly delete all clients)
PIEthernet::stopThreadedListen() method decompose client to 2 implementations - server-side and client-side
This commit is contained in:
57
main.cpp
57
main.cpp
@@ -1,6 +1,6 @@
|
||||
#include "pibytearray.h"
|
||||
#include "piclient_server_client.h"
|
||||
#include "piclient_server_server.h"
|
||||
#include "piclientserver_client.h"
|
||||
#include "piclientserver_server.h"
|
||||
#include "picodeparser.h"
|
||||
#include "piiostream.h"
|
||||
#include "pijson.h"
|
||||
@@ -21,10 +21,32 @@ enum MyEvent {
|
||||
PIKbdListener kbd;
|
||||
|
||||
|
||||
class MyServerClient: public PIClientServer::ServerClient {
|
||||
protected:
|
||||
void readed(PIByteArray data) override { piCout << "readed" << (data.size()); }
|
||||
void aboutDelete() override { piCout << "aboutDelete"; }
|
||||
void disconnected() override { piCout << "disconnected"; }
|
||||
void connected() override {
|
||||
piCout << "connected";
|
||||
send_thread.start(
|
||||
[this] {
|
||||
// write((PIString::fromNumber(++counter)).toUTF8());
|
||||
PIByteArray ba(64_KiB);
|
||||
write(ba);
|
||||
},
|
||||
2_Hz);
|
||||
}
|
||||
PIThread send_thread;
|
||||
int counter = 0;
|
||||
};
|
||||
|
||||
|
||||
class MyClient: public PIClientServer::Client {
|
||||
protected:
|
||||
void readed(PIByteArray data) override { piCout << "readed" << (data.size()); }
|
||||
void disconnected() override { piCout << "disconnected"; }
|
||||
void connected() override {
|
||||
piCout << "connected";
|
||||
send_thread.start(
|
||||
[this] {
|
||||
// write((PIString::fromNumber(++counter)).toUTF8());
|
||||
@@ -43,26 +65,43 @@ int main(int argc, char * argv[]) {
|
||||
|
||||
|
||||
PIClientServer::Server * s = nullptr;
|
||||
PIClientServer::Client * c = nullptr;
|
||||
PIThread s_thread;
|
||||
PIVector<PIClientServer::Client *> cv;
|
||||
|
||||
if (argc > 1) {
|
||||
piCout << "Server";
|
||||
s = new PIClientServer::Server();
|
||||
s->setClientFactory([] { return new MyClient(); });
|
||||
s->setClientFactory([] { return new MyServerClient(); });
|
||||
s->enableSymmetricEncryption("1122334455667788"_hex);
|
||||
s->listenAll(12345);
|
||||
s_thread.start(
|
||||
[s] {
|
||||
piCout << "*** clients" << s->clientsCount();
|
||||
int i = 0;
|
||||
s->forEachClient([&i](PIClientServer::ServerClient * c) {
|
||||
piCout << "client" << ++i << c;
|
||||
c->write(PIByteArray(16_KiB));
|
||||
piMSleep(200);
|
||||
});
|
||||
},
|
||||
0.5_Hz);
|
||||
} else {
|
||||
piCout << "Client";
|
||||
c = new MyClient();
|
||||
c->createNew();
|
||||
c->enableSymmetricEncryption("1122334455667788"_hex);
|
||||
c->connect(PINetworkAddress::resolve("127.0.0.1", 12345));
|
||||
piForTimes(5) {
|
||||
piMSleep(50);
|
||||
auto c = new MyClient();
|
||||
c->enableSymmetricEncryption("1122334455667788"_hex);
|
||||
c->connect(PINetworkAddress::resolve("127.0.0.1", 12345));
|
||||
cv << c;
|
||||
}
|
||||
}
|
||||
|
||||
WAIT_FOR_EXIT;
|
||||
|
||||
s_thread.stopAndWait();
|
||||
|
||||
piDeleteSafety(s);
|
||||
piDeleteSafety(c);
|
||||
piDeleteAllAndClear(cv);
|
||||
|
||||
return 0;
|
||||
/*PIPackedTCP * tcp_s =
|
||||
|
||||
Reference in New Issue
Block a user