#include "dispatcherclient.h" #include "picloudtcp.h" DispatcherClient::DispatcherClient(PIEthernet * eth_, int id) : eth(eth_), authorised(false), client_id(id) { CONNECTU(&disconnect_tm, tickEvent, eth, close); CONNECTU(eth, threadedReadEvent, this, readed); CONNECTU(eth, disconnected, this, disconnected); piCoutObj << "client connected" << eth->sendAddress(); } void DispatcherClient::start() { eth->startThreadedRead(); //disconnect_tm.start(10000); } DispatcherClient::~DispatcherClient() { // delete eth; } PIString DispatcherClient::address() { return eth->path(); } void DispatcherClient::close() { eth->close(); } void DispatcherClient::sendConnected() { tcp.sendConnected(eth, client_id); } void DispatcherClient::sendData(const PIByteArray & data) { tcp.sendData(eth, data); } void DispatcherClient::disconnected(bool withError) { piCoutObj << "client disconnected" << eth->sendAddress(); disconnectEvent(this); } void DispatcherClient::readed(uchar *data, int size) { // piCout << size; PIByteArray ba(data, size); PIPair hdr = tcp.parseHeader(ba); if (hdr.first == PICloud::TCP::Invalid) { disconnected(true); return; } if (authorised) { switch (hdr.first) { case PICloud::TCP::Connect: return; case PICloud::TCP::Disconnect: disconnected(false); return; case PICloud::TCP::Data: dataReaded(tcp.parseData(ba)); return; default: disconnected(true); return; } } else { switch (hdr.first) { case PICloud::TCP::Connect: { tcp.setRole(hdr.second); PIString sn = tcp.parseConnect(ba); if (hdr.second == PICloud::TCP::Server) registerServer(sn, this); if (hdr.second == PICloud::TCP::Client) registerClient(sn, this); return;} case PICloud::TCP::Disconnect: disconnected(false); return; default: disconnected(true); return; } } }