From 5e5e15cc8cf40e0deefb8c0a9f4155c750c871bc Mon Sep 17 00:00:00 2001 From: liuyanghejerry Date: Sun, 23 Nov 2014 01:12:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E9=AA=8C=E6=80=A7=E6=96=AD=E7=BA=BF?= =?UTF-8?q?=E9=87=8D=E8=BF=9E=20#70?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuyanghejerry --- src/common/network/clientsocket.cpp | 122 +++++++++++++------ src/common/network/clientsocket.h | 16 ++- src/painttyDesktop/widgets/canvasbackend.cpp | 6 +- 3 files changed, 105 insertions(+), 39 deletions(-) diff --git a/src/common/network/clientsocket.cpp b/src/common/network/clientsocket.cpp index b48eb83..ca98de1 100644 --- a/src/common/network/clientsocket.cpp +++ b/src/common/network/clientsocket.cpp @@ -46,11 +46,18 @@ ClientSocket::ClientSocket(QObject *parent) : { initRouter(); connect(this, &ClientSocket::newData, - this, &ClientSocket::onPending); + this, &ClientSocket::onInputPending); connect(this, &ClientSocket::managerPack, this, &ClientSocket::onManagerPack); + connect(this, &ClientSocket::disconnected, + this, &ClientSocket::onServerDisconnected); + connect(this, &ClientSocket::cmdPack, + [this](const QJsonObject &map) { + qDebug()<<"onCmdData"<start(WAIT_TIME); connect(heartBeatTimer_, &QTimer::timeout, @@ -69,7 +76,7 @@ void ClientSocket::setPoolEnabled(bool on) poolEnabled_ = on; if(!poolEnabled_){ loopTimer_->start(WAIT_TIME); - processPending(); + processInputPending(); }else{ loopTimer_->stop(); } @@ -171,13 +178,6 @@ void ClientSocket::requestNewRoom(const QJsonObject &m) state_ = REQUESTING_NEWROOM; } -void ClientSocket::tryJoinRoom(const QString &url) -{ - auto decoded_info = decodeRoomUrl(url); - setPasswd(decoded_info.passwd); - tryJoinRoom(QHostAddress(decoded_info.addr), decoded_info.port); -} - void ClientSocket::onResponseRoomList(const QJsonObject &obj) { if(!obj["result"].toBool()) @@ -287,27 +287,26 @@ void ClientSocket::onResponseLogin(const QJsonObject &map) // } +void ClientSocket::tryJoinRoom(const QString &url) +{ + auto decoded_info = decodeRoomUrl(url); + setPasswd(decoded_info.passwd); + tryJoinRoom(QHostAddress(decoded_info.addr), decoded_info.port); +} + void ClientSocket::tryJoinRoom(const QHostAddress &addr, const int port) { qDebug()<<"tryJoinRoom"<start(WAIT_TIME); - pool_.clear(); + inputPool_.clear(); // mutex_.unlock(); } diff --git a/src/common/network/clientsocket.h b/src/common/network/clientsocket.h index 258e99c..8714e62 100644 --- a/src/common/network/clientsocket.h +++ b/src/common/network/clientsocket.h @@ -47,7 +47,7 @@ class ClientSocket : public Socket JOINING_ROOM, ROOM_JOINED, ROOM_EXITED, - ROOM_DISCONNECTED, + ROOM_OFFLINE, ROOM_KICKED }; @@ -124,6 +124,7 @@ class ClientSocket : public Socket void roomListFetched(QHash); void roomCreated(); void roomJoined(); + void roomOfflined(); void roomAboutToClose(); void layerAllCleared(); @@ -132,6 +133,8 @@ class ClientSocket : public Socket void getKicked(); void delayGet(int); + void newClientId(const QString&); + void dataPack(const QJsonObject&); void msgPack(const QJsonObject&); void cmdPack(const QJsonObject&); @@ -163,7 +166,8 @@ public slots: quint64 schedualDataLength_; quint64 leftDataLength_; Router<> router_; - QList pool_; + QList inputPool_; + QList outputPool_; State state_; QAtomicInt roomDelay_; QTimer *loopTimer_; @@ -184,11 +188,15 @@ private slots: void setSchedualDataLength(quint64 length); ParserResult parserPack(const QByteArray& data); QByteArray assamblePack(bool compress, PACK_TYPE pt, const QByteArray& bytes); - void onPending(const QByteArray& bytes); - void processPending(); + void onInputPending(const QByteArray& bytes); + void processInputPending(); bool dispatch(const QByteArray& bytes); void onNewMessage(const QJsonObject &map); void onManagerPack(const QJsonObject &data); + void trySendData(const QByteArray &content); + void processOutputPending(); + void onServerDisconnected(); + void tryRejoinRoom(); }; #endif // CLIENTSOCKET_H diff --git a/src/painttyDesktop/widgets/canvasbackend.cpp b/src/painttyDesktop/widgets/canvasbackend.cpp index 29dfdec..7988833 100644 --- a/src/painttyDesktop/widgets/canvasbackend.cpp +++ b/src/painttyDesktop/widgets/canvasbackend.cpp @@ -29,7 +29,11 @@ CanvasBackend::CanvasBackend(QObject *parent) fullspeed_replay = settings.value("canvas/fullspeed_replay", true).toBool(); - + // when re-join one room, clientId should be refreshed + connect(&client_socket, &ClientSocket::newClientId, + [this] (const QString& clientId){ + cached_clientid_ = clientId; + }); connect(&client_socket, &ClientSocket::dataPack, this, &CanvasBackend::onIncomingData); connect(this, &CanvasBackend::newDataGroup,