This commit is contained in:
2020-08-27 19:40:11 +03:00
parent e1f2c90790
commit 234d4e73be
9 changed files with 101 additions and 20 deletions

View File

@@ -19,10 +19,24 @@
#include "picloudtcp.h" #include "picloudtcp.h"
#include "picrypt.h" #include "picrypt.h"
#include "pichunkstream.h"
const char hash_def_key[] = "_picrypt_"; const char hash_def_key[] = "_picrypt_";
PIByteArray & operator <<(PIByteArray & s, const PICloud::Header & v) {
s << v.version << v.type << v.sname;
return s;
}
PIByteArray & operator >>(PIByteArray & s, PICloud::Header & v) {
s >> v.version >> v.type >> v.sname;
return s;
}
PICloudTCP::PICloudTCP() { PICloudTCP::PICloudTCP() {
} }

View File

@@ -26,6 +26,30 @@
#include "pip_cloud_export.h" #include "pip_cloud_export.h"
#include "pistring.h" #include "pistring.h"
namespace PICloud {
enum Version {
Version_1 = 1,
};
enum HeaderType {
Server = 1,
Client = 2,
};
struct PIP_CLOUD_EXPORT Header {
Header() {
version = Version_1;
}
uchar version; // PICloud::Version
uchar type; // PICloud::HeaderType
PIString sname; // server name
};
}
PIP_CLOUD_EXPORT PIByteArray & operator <<(PIByteArray & s, const PICloud::Header & v);
PIP_CLOUD_EXPORT PIByteArray & operator >>(PIByteArray & s, PICloud::Header & v);
class PIP_CLOUD_EXPORT PICloudTCP { class PIP_CLOUD_EXPORT PICloudTCP {
public: public:

View File

@@ -2,17 +2,7 @@
int main() { int main() {
PICloudServer s("127.0.0.1:10101"); PICloudServer s("127.0.0.1:10101");
// for (int i=0; i<3; ++i) {
// s.open();
// piCout << "opened";
// piSleep(10);
// s.close();
// piCout << "closed";
// piSleep(1);
// }
// s.open();
s.startThreadedRead(); s.startThreadedRead();
piSleep(2); piSleep(10);
// s.stopThreadedRead();
return 0; return 0;
} }

View File

@@ -1,5 +1,24 @@
#include "cloudserver.h" #include "cloudserver.h"
CloudServer::CloudServer(DispatcherClient * client) { CloudServer::CloudServer(DispatcherClient * c) : server(c) {
} }
CloudServer::~CloudServer() {
for (auto c :clients) {
c->close();
}
}
void CloudServer::addClient(DispatcherClient * c) {
clients << c;
}
void CloudServer::printStatus() {
piCout << " " << "Clients for" << server->address() << ":";
for (auto c: clients) {
piCout << " " << c->address();
}
}

View File

@@ -3,10 +3,18 @@
#include "dispatcherclient.h" #include "dispatcherclient.h"
class CloudServer
{ class CloudServer : public PIObject {
PIOBJECT(CloudServer)
public: public:
CloudServer(DispatcherClient * client); CloudServer(DispatcherClient * c);
~CloudServer();
void addClient(DispatcherClient * c);
EVENT_HANDLER0(void, printStatus);
private:
DispatcherClient * server;
PIVector<DispatcherClient*> clients;
}; };
#endif // CLOUDSERVER_H #endif // CLOUDSERVER_H

View File

@@ -1,8 +1,8 @@
#include "dispatcherclient.h" #include "dispatcherclient.h"
#include "picloudtcp.h"
DispatcherClient::DispatcherClient(PIEthernet * eth_) { DispatcherClient::DispatcherClient(PIEthernet * eth_) : eth(eth_), authorised(false) {
eth = eth_;
CONNECTU(&disconnect_tm, tickEvent, eth, close); CONNECTU(&disconnect_tm, tickEvent, eth, close);
eth->startThreadedRead(); eth->startThreadedRead();
CONNECTU(eth, threadedReadEvent, this, readed); CONNECTU(eth, threadedReadEvent, this, readed);
@@ -21,6 +21,10 @@ PIString DispatcherClient::address() {
return eth->path(); return eth->path();
} }
void DispatcherClient::close() {
eth->close();
}
void DispatcherClient::disconnected(bool withError) { void DispatcherClient::disconnected(bool withError) {
piCoutObj << "client disconnected" << eth->sendAddress(); piCoutObj << "client disconnected" << eth->sendAddress();
@@ -30,7 +34,18 @@ void DispatcherClient::disconnected(bool withError) {
void DispatcherClient::readed(uchar *data, int size) { void DispatcherClient::readed(uchar *data, int size) {
PIByteArray ba(data, size); PIByteArray ba(data, size);
piCoutObj << "readed" << ba.toHex(); if (authorised) {
eth->write(ba); dataReaded(ba);
} else {
if (ba.size() < 4) return;
PICloud::Header hdr;
ba >> hdr;
if ((PICloud::HeaderType)hdr.type == PICloud::Server) {
registerServer(hdr.sname, this);
}
if ((PICloud::HeaderType)hdr.type == PICloud::Client) {
registerClient(hdr.sname, this);
}
}
} }

View File

@@ -11,7 +11,10 @@ public:
~DispatcherClient(); ~DispatcherClient();
EVENT1(disconnectEvent, DispatcherClient *, client) EVENT1(disconnectEvent, DispatcherClient *, client)
EVENT2(registerServer, PIString, sname, DispatcherClient *, client) EVENT2(registerServer, PIString, sname, DispatcherClient *, client)
EVENT2(registerClient, PIString, sname, DispatcherClient *, client)
PIString address(); PIString address();
void close();
EVENT1(dataReaded, PIByteArray, ba)
private: private:
EVENT_HANDLER2(void, readed, uchar * , data, int, size); EVENT_HANDLER2(void, readed, uchar * , data, int, size);
@@ -19,6 +22,7 @@ private:
PIEthernet * eth; PIEthernet * eth;
PITimer disconnect_tm; PITimer disconnect_tm;
bool authorised;
}; };

View File

@@ -28,6 +28,7 @@ void DispatcherServer::printStatus() {
auto it = c_servers.makeIterator(); auto it = c_servers.makeIterator();
while(it.next()){ while(it.next()){
piCout << " " << it.key(); piCout << " " << it.key();
it.value()->printStatus();
} }
map_mutex.unlock(); map_mutex.unlock();
} }
@@ -50,6 +51,13 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
c_servers.insert(sname, new CloudServer(c)); c_servers.insert(sname, new CloudServer(c));
map_mutex.unlock(); map_mutex.unlock();
}); });
CONNECTL(client, registerClient, [this](PIString sname, DispatcherClient * c){
map_mutex.lock();
if (c_servers.contains(sname)) {
c_servers[sname]->addClient(c);
}
map_mutex.unlock();
});
piCoutObj << "add client" << client; piCoutObj << "add client" << client;
map_mutex.lock(); map_mutex.lock();
clients.push_back(client); clients.push_back(client);

View File

@@ -1,7 +1,6 @@
#ifndef DISPATCHERSERVER_H #ifndef DISPATCHERSERVER_H
#define DISPATCHERSERVER_H #define DISPATCHERSERVER_H
#include "dispatcherclient.h"
#include "cloudserver.h" #include "cloudserver.h"