PIObject::deleted now has 1 argument
PIIODevice small refactoring new PIIODevice virtual methods: threadedReadTerminated() and threadedWriteTerminated() PIIODevice::stop now accept bool "hard" instead of "wait" PIStreamPacker new features: packet size crypt and aggressive optimization
This commit is contained in:
@@ -47,6 +47,12 @@ void PICloudServer::setServerName(const PIString & server_name) {
|
||||
}
|
||||
|
||||
|
||||
PIVector<PICloudServer::Client *> PICloudServer::clients() const {
|
||||
PIMutexLocker _ml(clients_mutex);
|
||||
return clients_;
|
||||
}
|
||||
|
||||
|
||||
bool PICloudServer::openDevice() {
|
||||
piCout << "PICloudServer open device" << path();
|
||||
bool op = eth.connect(path(), false);
|
||||
@@ -61,10 +67,12 @@ bool PICloudServer::openDevice() {
|
||||
|
||||
bool PICloudServer::closeDevice() {
|
||||
eth.stop();
|
||||
for (auto c : clients) {
|
||||
clients_mutex.lock();
|
||||
for (auto c : clients_) {
|
||||
c->stop();
|
||||
c->close();
|
||||
}
|
||||
clients_mutex.unlock();
|
||||
eth.close();
|
||||
return true;
|
||||
}
|
||||
@@ -103,6 +111,7 @@ PICloudServer::Client::~Client() {
|
||||
is_connected = false;
|
||||
cond_buff.notifyOne();
|
||||
}
|
||||
stop();
|
||||
close();
|
||||
}
|
||||
|
||||
@@ -156,32 +165,56 @@ void PICloudServer::_readed(PIByteArray & ba) {
|
||||
switch (hdr.first) {
|
||||
case PICloud::TCP::Connect: {
|
||||
uint id = tcp.parseConnect(ba);
|
||||
clients_mutex.lock();
|
||||
Client * oc = index_clients.value(id, nullptr);
|
||||
if (oc) {
|
||||
clients_mutex.unlock();
|
||||
tcp.sendDisconnected(id);
|
||||
} else {
|
||||
piCoutObj << "new Client" << id;
|
||||
Client * c = new Client(this, id);
|
||||
clients << c;
|
||||
CONNECTU(c, deleted, this, clientDeleted)
|
||||
clients_ << c;
|
||||
index_clients.insert(id, c);
|
||||
clients_mutex.unlock();
|
||||
newConnection(c);
|
||||
}
|
||||
} break;
|
||||
case PICloud::TCP::Disconnect: {
|
||||
uint id = tcp.parseDisconnect(ba);
|
||||
clients_mutex.lock();
|
||||
Client * oc = index_clients.value(id, nullptr);
|
||||
if (oc) {
|
||||
oc->is_connected = false;
|
||||
oc->close();
|
||||
}
|
||||
clients_mutex.unlock();
|
||||
} break;
|
||||
case PICloud::TCP::Data: {
|
||||
PIPair<uint, PIByteArray> d = tcp.parseDataServer(ba);
|
||||
clients_mutex.lock();
|
||||
Client * oc = index_clients.value(d.first, nullptr);
|
||||
if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
|
||||
clients_mutex.unlock();
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PICloudServer::clientDeleted(PIObject * o) {
|
||||
PICloudServer::Client * c = (PICloudServer::Client*)o;
|
||||
clients_mutex.lock();
|
||||
clients_.removeOne(c);
|
||||
auto it = index_clients.makeIterator();
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
if (it.value() == c) {
|
||||
index_clients.remove(it.key());
|
||||
break;
|
||||
}
|
||||
}
|
||||
clients_mutex.unlock();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user