Merge pull request 'cloud_debug' (#78) from cloud_debug into master
Reviewed-on: https://git.shs.tools/SHS/pip/pulls/78
This commit was merged in pull request #78.
This commit is contained in:
@@ -25,23 +25,35 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
|
|||||||
tcp.setRole(PICloud::TCP::Client);
|
tcp.setRole(PICloud::TCP::Client);
|
||||||
setName("cloud_client");
|
setName("cloud_client");
|
||||||
is_connected = false;
|
is_connected = false;
|
||||||
|
is_deleted = false;
|
||||||
|
// setReopenEnabled(false);
|
||||||
CONNECTL(ð, connected, [this](){opened_ = true; tcp.sendStart();});
|
CONNECTL(ð, connected, [this](){opened_ = true; tcp.sendStart();});
|
||||||
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
|
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
|
||||||
CONNECTL(ð, disconnected, [this](bool){
|
CONNECTL(ð, disconnected, [this](bool){
|
||||||
// piCoutObj << "disconnected";
|
if (is_deleted) return;
|
||||||
|
//piCoutObj << "eth disconnected";
|
||||||
|
static_cast<PIThread*>(ð)->stop();
|
||||||
|
opened_ = false;
|
||||||
if (is_connected) disconnected();
|
if (is_connected) disconnected();
|
||||||
internalDisconnect();
|
internalDisconnect();
|
||||||
piMSleep(100);
|
//piCoutObj << "eth disconnected done";
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PICloudClient::~PICloudClient() {
|
PICloudClient::~PICloudClient() {
|
||||||
eth.close();
|
//piCoutObj << "~PICloudClient()";
|
||||||
|
PIThread::stop();
|
||||||
|
//eth.close();
|
||||||
if (is_connected) disconnected();
|
if (is_connected) disconnected();
|
||||||
internalDisconnect();
|
is_connected = false;
|
||||||
close();
|
close();
|
||||||
stop();
|
//piCoutObj << "~PICloudClient() closed";
|
||||||
|
internalDisconnect();
|
||||||
|
// stop(false);
|
||||||
|
is_deleted = true;
|
||||||
|
internalDisconnect();
|
||||||
|
//piCoutObj << "~PICloudClient() done";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -57,41 +69,44 @@ void PICloudClient::setKeepConnection(bool on) {
|
|||||||
|
|
||||||
|
|
||||||
bool PICloudClient::openDevice() {
|
bool PICloudClient::openDevice() {
|
||||||
// piCout << "PICloudClient open device" << path();
|
//piCoutObj << "open";// << path();
|
||||||
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
|
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
|
||||||
if (op) {
|
if (op) {
|
||||||
mutex_buff.lock();
|
mutex_connect.lock();
|
||||||
eth.startThreadedRead();
|
eth.startThreadedRead();
|
||||||
bool conn_ok = cond_connect.waitFor(mutex_buff, (int)eth.readTimeout());
|
//piCoutObj << "connecting...";
|
||||||
// piCoutObj << "conn_ok" << conn_ok;
|
bool conn_ok = cond_connect.waitFor(mutex_connect, (int)eth.readTimeout());
|
||||||
mutex_buff.unlock();
|
//piCoutObj << "conn_ok" << conn_ok << is_connected;
|
||||||
|
mutex_connect.unlock();
|
||||||
if (!conn_ok) {
|
if (!conn_ok) {
|
||||||
|
mutex_connect.lock();
|
||||||
eth.stop();
|
eth.stop();
|
||||||
eth.close();
|
eth.close();
|
||||||
piMSleep(100);
|
mutex_connect.unlock();
|
||||||
}
|
}
|
||||||
return isConnected();
|
return is_connected;
|
||||||
} else {
|
} else {
|
||||||
eth.close();
|
//eth.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PICloudClient::closeDevice() {
|
bool PICloudClient::closeDevice() {
|
||||||
PIThread::stop(false);
|
//PIThread::stop();
|
||||||
if (is_connected) {
|
if (is_connected) {
|
||||||
internalDisconnect();
|
internalDisconnect();
|
||||||
}
|
}
|
||||||
eth.stop();
|
eth.stop();
|
||||||
if (eth.isOpened()) eth.close();
|
eth.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int PICloudClient::readDevice(void * read_to, int max_size) {
|
int PICloudClient::readDevice(void * read_to, int max_size) {
|
||||||
// piCoutObj << "readDevice";
|
if (is_deleted) return -1;
|
||||||
if (!is_connected) return -1;
|
//piCoutObj << "readDevice";
|
||||||
|
if (!is_connected && eth.isClosed()) openDevice();
|
||||||
int sz = -1;
|
int sz = -1;
|
||||||
mutex_buff.lock();
|
mutex_buff.lock();
|
||||||
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;});
|
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;});
|
||||||
@@ -101,11 +116,14 @@ int PICloudClient::readDevice(void * read_to, int max_size) {
|
|||||||
buff.remove(0, sz);
|
buff.remove(0, sz);
|
||||||
}
|
}
|
||||||
mutex_buff.unlock();
|
mutex_buff.unlock();
|
||||||
|
if (!is_connected) opened_ = false;
|
||||||
|
//piCoutObj << "readDevice done" << sz;
|
||||||
return sz;
|
return sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int PICloudClient::writeDevice(const void * data, int size) {
|
int PICloudClient::writeDevice(const void * data, int size) {
|
||||||
|
if (is_deleted) return -1;
|
||||||
// piCoutObj << "writeDevice";
|
// piCoutObj << "writeDevice";
|
||||||
return tcp.sendData(PIByteArray(data, size));
|
return tcp.sendData(PIByteArray(data, size));
|
||||||
}
|
}
|
||||||
@@ -119,25 +137,30 @@ void PICloudClient::internalDisconnect() {
|
|||||||
|
|
||||||
|
|
||||||
void PICloudClient::_readed(PIByteArray & ba) {
|
void PICloudClient::_readed(PIByteArray & ba) {
|
||||||
mutex_buff.lock();
|
if (is_deleted) return;
|
||||||
|
//piCoutObj << "_readed";
|
||||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||||
if (hdr.second == tcp.role()) {
|
if (hdr.second == tcp.role()) {
|
||||||
switch (hdr.first) {
|
switch (hdr.first) {
|
||||||
case PICloud::TCP::Connect:
|
case PICloud::TCP::Connect:
|
||||||
if (tcp.parseConnect(ba) == 1) {
|
if (tcp.parseConnect(ba) == 1) {
|
||||||
|
mutex_connect.lock();
|
||||||
is_connected = true;
|
is_connected = true;
|
||||||
connected();
|
connected();
|
||||||
|
mutex_connect.unlock();
|
||||||
cond_connect.notifyOne();
|
cond_connect.notifyOne();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case PICloud::TCP::Disconnect:
|
case PICloud::TCP::Disconnect:
|
||||||
is_connected = false;
|
|
||||||
static_cast<PIThread*>(ð)->stop();
|
static_cast<PIThread*>(ð)->stop();
|
||||||
|
opened_ = false;
|
||||||
eth.close();
|
eth.close();
|
||||||
break;
|
break;
|
||||||
case PICloud::TCP::Data:
|
case PICloud::TCP::Data:
|
||||||
if (is_connected) {
|
if (is_connected) {
|
||||||
|
mutex_buff.lock();
|
||||||
buff.append(ba);
|
buff.append(ba);
|
||||||
|
mutex_buff.unlock();
|
||||||
cond_buff.notifyOne();
|
cond_buff.notifyOne();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -146,7 +169,7 @@ void PICloudClient::_readed(PIByteArray & ba) {
|
|||||||
}
|
}
|
||||||
//piCoutObj << "readed" << ba.toHex();
|
//piCoutObj << "readed" << ba.toHex();
|
||||||
}
|
}
|
||||||
mutex_buff.unlock();
|
|
||||||
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
|
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
|
||||||
|
//piCoutObj << "_readed done";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,10 +26,12 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
|
|||||||
tcp.setServerName(server_name);
|
tcp.setServerName(server_name);
|
||||||
setName("cloud_server__" + server_name);
|
setName("cloud_server__" + server_name);
|
||||||
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
|
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
|
||||||
CONNECTL(ð, connected, [this](){tcp.sendStart();});
|
CONNECTL(ð, connected, [this](){opened_ = true; tcp.sendStart();});
|
||||||
CONNECTL(ð, disconnected, [this](bool){
|
CONNECTL(ð, disconnected, [this](bool){
|
||||||
piCoutObj << "disconnected";
|
//piCoutObj << "disconnected";
|
||||||
|
static_cast<PIThread*>(ð)->stop();
|
||||||
opened_ = false;
|
opened_ = false;
|
||||||
|
ping_timer.stop(false);
|
||||||
piMSleep(100);
|
piMSleep(100);
|
||||||
});
|
});
|
||||||
CONNECTL(&ping_timer, tickEvent, [this] (void *, int){
|
CONNECTL(&ping_timer, tickEvent, [this] (void *, int){
|
||||||
@@ -57,14 +59,14 @@ PIVector<PICloudServer::Client *> PICloudServer::clients() const {
|
|||||||
|
|
||||||
|
|
||||||
bool PICloudServer::openDevice() {
|
bool PICloudServer::openDevice() {
|
||||||
piCout << "PICloudServer open device" << path();
|
//piCout << "PICloudServer open device" << path();
|
||||||
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
|
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
|
||||||
if (op) {
|
if (op) {
|
||||||
eth.startThreadedRead();
|
eth.startThreadedRead();
|
||||||
ping_timer.start(5000);
|
ping_timer.start(5000);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
ping_timer.stop();
|
ping_timer.stop(false);
|
||||||
eth.close();
|
eth.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -72,6 +74,7 @@ bool PICloudServer::openDevice() {
|
|||||||
|
|
||||||
bool PICloudServer::closeDevice() {
|
bool PICloudServer::closeDevice() {
|
||||||
eth.stop();
|
eth.stop();
|
||||||
|
ping_timer.stop(false);
|
||||||
clients_mutex.lock();
|
clients_mutex.lock();
|
||||||
for (auto c : clients_) {
|
for (auto c : clients_) {
|
||||||
c->close();
|
c->close();
|
||||||
@@ -87,7 +90,8 @@ bool PICloudServer::closeDevice() {
|
|||||||
|
|
||||||
int PICloudServer::readDevice(void * read_to, int max_size) {
|
int PICloudServer::readDevice(void * read_to, int max_size) {
|
||||||
//piCoutObj << "readDevice";
|
//piCoutObj << "readDevice";
|
||||||
piMSleep(eth.readTimeout());
|
if (!opened_) openDevice();
|
||||||
|
else piMSleep(eth.readTimeout());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,7 +187,7 @@ void PICloudServer::_readed(PIByteArray & ba) {
|
|||||||
if (oc) {
|
if (oc) {
|
||||||
tcp.sendDisconnected(id);
|
tcp.sendDisconnected(id);
|
||||||
} else {
|
} else {
|
||||||
piCoutObj << "new Client" << id;
|
//piCoutObj << "new Client" << id;
|
||||||
Client * c = new Client(this, id);
|
Client * c = new Client(this, id);
|
||||||
CONNECTU(c, deleted, this, clientDeleted);
|
CONNECTU(c, deleted, this, clientDeleted);
|
||||||
clients_mutex.lock();
|
clients_mutex.lock();
|
||||||
@@ -195,7 +199,7 @@ void PICloudServer::_readed(PIByteArray & ba) {
|
|||||||
} break;
|
} break;
|
||||||
case PICloud::TCP::Disconnect: {
|
case PICloud::TCP::Disconnect: {
|
||||||
uint id = tcp.parseDisconnect(ba);
|
uint id = tcp.parseDisconnect(ba);
|
||||||
piCoutObj << "remove Client" << id;
|
//piCoutObj << "remove Client" << id;
|
||||||
clients_mutex.lock();
|
clients_mutex.lock();
|
||||||
Client * oc = index_clients.value(id, nullptr);
|
Client * oc = index_clients.value(id, nullptr);
|
||||||
clients_mutex.unlock();
|
clients_mutex.unlock();
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ private:
|
|||||||
PIConditionVariable cond_buff;
|
PIConditionVariable cond_buff;
|
||||||
PIConditionVariable cond_connect;
|
PIConditionVariable cond_connect;
|
||||||
std::atomic_bool is_connected;
|
std::atomic_bool is_connected;
|
||||||
|
std::atomic_bool is_deleted;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PICLOUDCLIENT_H
|
#endif // PICLOUDCLIENT_H
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
|
|
||||||
#include "pithread.h"
|
#include "pithread.h"
|
||||||
|
|
||||||
#define WAIT_FOR_EXIT while (!PIKbdListener::exiting) piMSleep(PIP_MIN_MSLEEP*5);
|
#define WAIT_FOR_EXIT while (!PIKbdListener::exiting) piMSleep(PIP_MIN_MSLEEP*5); // TODO: rewrite with condvar
|
||||||
|
|
||||||
|
|
||||||
class PIP_EXPORT PIKbdListener: public PIThread
|
class PIP_EXPORT PIKbdListener: public PIThread
|
||||||
|
|||||||
@@ -700,8 +700,7 @@ PIObject::Deleter::Deleter() {
|
|||||||
stopping = started = posted = false;
|
stopping = started = posted = false;
|
||||||
CONNECTL(&(PRIVATE->thread), started, [this](){proc();});
|
CONNECTL(&(PRIVATE->thread), started, [this](){proc();});
|
||||||
PRIVATE->thread.startOnce();
|
PRIVATE->thread.startOnce();
|
||||||
while (!started)
|
while (!started) piMSleep(1);
|
||||||
piMSleep(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -710,8 +709,7 @@ PIObject::Deleter::~Deleter() {
|
|||||||
stopping = true;
|
stopping = true;
|
||||||
PRIVATE->cond_var.notifyAll();
|
PRIVATE->cond_var.notifyAll();
|
||||||
#ifndef WINDOWS
|
#ifndef WINDOWS
|
||||||
while (PRIVATE->thread.isRunning())
|
while (PRIVATE->thread.isRunning()) piMSleep(1);
|
||||||
piMSleep(1);
|
|
||||||
#endif
|
#endif
|
||||||
deleteAll();
|
deleteAll();
|
||||||
//piCout << "~Deleter ok";
|
//piCout << "~Deleter ok";
|
||||||
@@ -774,12 +772,9 @@ void PIObject::Deleter::deleteObject(PIObject * o) {
|
|||||||
//piCout << "[Deleter] delete" << (uintptr_t)o << "...";
|
//piCout << "[Deleter] delete" << (uintptr_t)o << "...";
|
||||||
if (o->isPIObject()) {
|
if (o->isPIObject()) {
|
||||||
//piCout << "[Deleter] delete" << (uintptr_t)o << "wait atomic ...";
|
//piCout << "[Deleter] delete" << (uintptr_t)o << "wait atomic ...";
|
||||||
while (o->isInEvent()) {
|
while (o->isInEvent()) piMSleep(1);
|
||||||
piMSleep(1);
|
|
||||||
}
|
|
||||||
//piCout << "[Deleter] delete" << (uintptr_t)o << "wait atomic done";
|
//piCout << "[Deleter] delete" << (uintptr_t)o << "wait atomic done";
|
||||||
if (o->isPIObject())
|
if (o->isPIObject()) delete o;
|
||||||
delete o;
|
|
||||||
}
|
}
|
||||||
//piCout << "[Deleter] delete" << (uintptr_t)o << "done";
|
//piCout << "[Deleter] delete" << (uintptr_t)o << "done";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ bool PIBinaryLog::threadedRead(uchar *readed, int size) {
|
|||||||
pausemutex.lock();
|
pausemutex.lock();
|
||||||
if (is_pause) {
|
if (is_pause) {
|
||||||
pausemutex.unlock();
|
pausemutex.unlock();
|
||||||
piMSleep(100);
|
piMSleep(100); // TODO: rewrite with condvar
|
||||||
return false;
|
return false;
|
||||||
} else if (pause_time > PISystemTime()) {
|
} else if (pause_time > PISystemTime()) {
|
||||||
startlogtime += pause_time;
|
startlogtime += pause_time;
|
||||||
@@ -204,22 +204,22 @@ bool PIBinaryLog::threadedRead(uchar *readed, int size) {
|
|||||||
int dtc;
|
int dtc;
|
||||||
if (is_started) {
|
if (is_started) {
|
||||||
if (is_pause) {
|
if (is_pause) {
|
||||||
piMSleep(100);
|
piMSleep(100); // TODO: rewrite with condvar
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (delay > 0) {
|
if (delay > 0) {
|
||||||
cdelay = delay * play_speed;
|
cdelay = delay * play_speed;// TODO: rewrite with condvar
|
||||||
dtc = int(cdelay) /100;
|
dtc = int(cdelay) / 100;// TODO: rewrite with condvar
|
||||||
if (play_speed <= 0.) dtc = 2;
|
if (play_speed <= 0.) dtc = 2;
|
||||||
//piCout << play_speed << dtc;
|
//piCout << play_speed << dtc;
|
||||||
for (int j=0; j<dtc; j++) {
|
for (int j=0; j<dtc; j++) {
|
||||||
cdelay = delay * play_speed;
|
cdelay = delay * play_speed;// TODO: rewrite with condvar
|
||||||
dtc = int(cdelay) /100;
|
dtc = int(cdelay) / 100;// TODO: rewrite with condvar
|
||||||
piMSleep(100);
|
piMSleep(100);// TODO: rewrite with condvar
|
||||||
if (play_speed <= 0.) {dtc = 2; j = 0;}
|
if (play_speed <= 0.) {dtc = 2; j = 0;}
|
||||||
//piCout << " " << play_speed << dtc << j;
|
//piCout << " " << play_speed << dtc << j;
|
||||||
}
|
}
|
||||||
cdelay = cdelay - dtc*100;
|
cdelay = cdelay - dtc*100;// TODO: rewrite with condvar
|
||||||
PISystemTime::fromMilliseconds(cdelay).sleep();
|
PISystemTime::fromMilliseconds(cdelay).sleep();
|
||||||
}
|
}
|
||||||
} else is_started = true;
|
} else is_started = true;
|
||||||
@@ -228,7 +228,7 @@ bool PIBinaryLog::threadedRead(uchar *readed, int size) {
|
|||||||
case PlayStaticDelay:
|
case PlayStaticDelay:
|
||||||
if (is_started) {
|
if (is_started) {
|
||||||
if (is_pause) {
|
if (is_pause) {
|
||||||
piMSleep(100);
|
piMSleep(100);// TODO: rewrite with condvar
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
play_delay.sleep();
|
play_delay.sleep();
|
||||||
|
|||||||
@@ -239,7 +239,7 @@ void PIIODevice::check_start(void * data, int delim) {
|
|||||||
//cout << "check " << tread_started_ << endl;
|
//cout << "check " << tread_started_ << endl;
|
||||||
if (open()) {
|
if (open()) {
|
||||||
thread_started_ = true;
|
thread_started_ = true;
|
||||||
timer.stop();
|
timer.stop(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
main.cpp
4
main.cpp
@@ -32,8 +32,8 @@ int main(int argc, char * argv[]) {
|
|||||||
piCout << "[Client] data:" << str;
|
piCout << "[Client] data:" << str;
|
||||||
if (str == "ping_S") c.write(PIString("pong_S").toByteArray());
|
if (str == "ping_S") c.write(PIString("pong_S").toByteArray());
|
||||||
}));
|
}));
|
||||||
CONNECTL(&c, connected, ([&](){piCout << "connected";}));
|
CONNECTL(&c, connected, ([](){piCout << "connected";}));
|
||||||
CONNECTL(&c, disconnected, ([&](){piCout << "disconnected";}));
|
CONNECTL(&c, disconnected, ([](){piCout << "disconnected";}));
|
||||||
CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){
|
CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){
|
||||||
piCout << "[Server] new client:" << cl;
|
piCout << "[Server] new client:" << cl;
|
||||||
clients << cl;
|
clients << cl;
|
||||||
|
|||||||
Reference in New Issue
Block a user