picloud add server logics
This commit is contained in:
@@ -29,11 +29,12 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
|
||||
|
||||
|
||||
PICloudServer::~PICloudServer() {
|
||||
for (auto & c : clients) {
|
||||
c.stop();
|
||||
c.close();
|
||||
}
|
||||
stop();
|
||||
eth.stop();
|
||||
for (auto & c : clients) {
|
||||
c->stop();
|
||||
c->close();
|
||||
}
|
||||
close();
|
||||
}
|
||||
|
||||
@@ -66,9 +67,9 @@ bool PICloudServer::openDevice() {
|
||||
|
||||
bool PICloudServer::closeDevice() {
|
||||
eth.stop();
|
||||
for (auto & c : clients) {
|
||||
c.stop();
|
||||
c.close();
|
||||
for (auto c : clients) {
|
||||
c->stop();
|
||||
c->close();
|
||||
}
|
||||
eth.close();
|
||||
return true;
|
||||
@@ -85,7 +86,17 @@ int PICloudServer::writeDevice(const void * data, int max_size) {
|
||||
}
|
||||
|
||||
|
||||
PICloudServer::Client::Client(PICloudServer * srv) : server(srv) {
|
||||
void PICloudServer::clientDisconnect(uint client_id) {
|
||||
tcp.sendDisconnected(ð, client_id);
|
||||
}
|
||||
|
||||
|
||||
int PICloudServer::sendData(const PIByteArray & data, uint client_id) {
|
||||
return tcp.sendData(ð, data, client_id);
|
||||
}
|
||||
|
||||
|
||||
PICloudServer::Client::Client(PICloudServer * srv, uint id) : server(srv), client_id(id) {
|
||||
|
||||
}
|
||||
|
||||
@@ -95,10 +106,67 @@ bool PICloudServer::Client::openDevice() {
|
||||
}
|
||||
|
||||
|
||||
bool PICloudServer::Client::closeDevice() {
|
||||
server->clientDisconnect(client_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int PICloudServer::Client::readDevice(void * read_to, int max_size) {
|
||||
//piCoutObj << "readDevice";
|
||||
mutex_buff.lock();
|
||||
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
||||
int sz = piMini(max_size, buff.size());
|
||||
memcpy(read_to, buff.data(), sz);
|
||||
buff.remove(0, sz);
|
||||
mutex_buff.unlock();
|
||||
return sz;
|
||||
}
|
||||
|
||||
|
||||
int PICloudServer::Client::writeDevice(const void * data, int max_size) {
|
||||
return server->sendData(PIByteArray(data, max_size), client_id);
|
||||
}
|
||||
|
||||
|
||||
void PICloudServer::Client::pushBuffer(const PIByteArray & ba) {
|
||||
mutex_buff.lock();
|
||||
buff.append(ba);
|
||||
cond_buff.notifyOne();
|
||||
mutex_buff.unlock();
|
||||
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100);
|
||||
}
|
||||
|
||||
|
||||
void PICloudServer::readed(uchar *data, int size) {
|
||||
PIByteArray ba(data, size);
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||
if (hdr.second == tcp.role()) {
|
||||
piCoutObj << "readed" << ba.toHex();
|
||||
switch (hdr.first) {
|
||||
case PICloud::TCP::Connect: {
|
||||
uint id = tcp.parseConnect(ba);
|
||||
Client * oc = index_clients.value(id, nullptr);
|
||||
if (oc) {
|
||||
tcp.sendDisconnected(ð, id);
|
||||
} else {
|
||||
Client * c = new Client(this, id);
|
||||
clients << c;
|
||||
index_clients.insert(id, c);
|
||||
newConnection(c);
|
||||
}
|
||||
} break;
|
||||
case PICloud::TCP::Disconnect: {
|
||||
uint id = tcp.parseDisconnect(ba);
|
||||
Client * oc = index_clients.value(id, nullptr);
|
||||
if (oc) oc->close();
|
||||
} break;
|
||||
case PICloud::TCP::Data: {
|
||||
PIPair<uint, PIByteArray> d = tcp.parseDataServer(ba);
|
||||
Client * oc = index_clients.value(d.first, nullptr);
|
||||
if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user