Skip to content

Commit

Permalink
微调
Browse files Browse the repository at this point in the history
Signed-off-by: liuyanghejerry <liuyanghejerry@126.com>
  • Loading branch information
liuyanghejerry committed Nov 22, 2014
1 parent 329d428 commit f581d3c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
35 changes: 16 additions & 19 deletions src/common/network/clientsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,28 @@ ClientSocket::ClientSocket(QObject *parent) :
Socket(parent),
schedualDataLength_(0),
leftDataLength_(0),
poolEnabled_(false),
timer_(new QTimer(this)),
state_(INIT),
roomDelay_(-1),
loopTimer_(new QTimer(this)),
heartBeatTimer_(new QTimer(this)),
archive_(Singleton<ArchiveFile>::instance()),
poolEnabled_(false),
no_save_(false),
remove_after_close_(false),
canceled_(false),
hb_timer_(new QTimer(this)),
state_(INIT),
roomDelay_(-1)
canceled_(false)
{
initRouter();
connect(this, &ClientSocket::newData,
this, &ClientSocket::onPending);
connect(this, &ClientSocket::managerPack,
this, &ClientSocket::onManagerPack);
connect(timer_, &QTimer::timeout,
connect(loopTimer_, &QTimer::timeout,
this, &ClientSocket::processPending);
timer_->start(WAIT_TIME);
loopTimer_->start(WAIT_TIME);

connect(hb_timer_, &QTimer::timeout,
connect(heartBeatTimer_, &QTimer::timeout,
this, &ClientSocket::sendHeartbeat);
connect(hb_timer_, &QTimer::timeout,
connect(heartBeatTimer_, &QTimer::timeout,
this, &ClientSocket::requestOnlinelist);
}

Expand All @@ -68,10 +68,10 @@ void ClientSocket::setPoolEnabled(bool on)
{
poolEnabled_ = on;
if(!poolEnabled_){
timer_->start(WAIT_TIME);
loopTimer_->start(WAIT_TIME);
processPending();
}else{
timer_->stop();
loopTimer_->stop();
}
}

Expand Down Expand Up @@ -147,7 +147,6 @@ void ClientSocket::connectToManager(const QHostAddress &addr, const int port)
connect(this, &ClientSocket::disconnected,
[this]{
// TODO: mannully or suppose to close?
// router_.clear();
});
connectToHost(addr, port);
state_ = CONNECTING_MANAGER;
Expand Down Expand Up @@ -277,7 +276,7 @@ void ClientSocket::onResponseLogin(const QJsonObject &map)
// NOTE: to wait mainwindow, we have to set pool on
setPoolEnabled(true);
requestArchiveSign();
// TODO: checkout if own the room
// checkout if own the room
if(roomKey().length()) {
requestCheckout();
}
Expand All @@ -304,8 +303,6 @@ void ClientSocket::tryJoinRoom(const QHostAddress &addr, const int port)
qDebug()<<"try auto join room";
sendCmdPack(map);
state_ = JOINING_ROOM;

// disconnect(this, &ClientSocket::connected, 0, 0);
});
connect(this, &ClientSocket::cmdPack,
[this](const QJsonObject &map) {
Expand Down Expand Up @@ -540,12 +537,12 @@ bool ClientSocket::requestKickUser(const QString& id)

void ClientSocket::startHeartbeat()
{
hb_timer_->start(1000 * 60 / HEARTBEAT_RATE);
heartBeatTimer_->start(1000 * 60 / HEARTBEAT_RATE);
}

void ClientSocket::stopHeartbeat()
{
hb_timer_->stop();
heartBeatTimer_->stop();
}

void ClientSocket::setClientId(const QString &id)
Expand Down Expand Up @@ -881,7 +878,7 @@ void ClientSocket::reset()
clientid_.clear();
roomname_.clear();
canvassize_ = QSize();
timer_->start(WAIT_TIME);
loopTimer_->start(WAIT_TIME);
pool_.clear();
// mutex_.unlock();
}
Expand Down
16 changes: 7 additions & 9 deletions src/common/network/clientsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "socket.h"
#include "../../common/binary.h"
//#include "../../common/network/packparser.h"
#include "../misc/router.h"
#include <QSize>
#include <QMutex>
Expand Down Expand Up @@ -62,11 +61,9 @@ class ClientSocket : public Socket
QString passwd() const;
void setPasswd(const QString &passwd);
bool isPoolEnabled();
void setSchedualDataLength(quint64 length);
quint64 schedualDataLength();
void reset();
QString archiveSignature() const;
void setArchiveSignature(const QString &as);
quint64 archiveSize() const;
void setPoolEnabled(bool on);
void setRoomCloseFlag();
Expand Down Expand Up @@ -157,7 +154,6 @@ public slots:
void close() Q_DECL_OVERRIDE;
private:
Q_DISABLE_COPY(ClientSocket)
// PackParser parser_;
QString clientid_;
QString username_;
QString roomname_;
Expand All @@ -168,22 +164,24 @@ public slots:
quint64 leftDataLength_;
Router<> router_;
QList<QByteArray> pool_;
bool poolEnabled_;
QTimer *timer_;
State state_;
QAtomicInt roomDelay_;
QTimer *loopTimer_;
QTimer *heartBeatTimer_;
ArchiveFile& archive_;
bool poolEnabled_;
bool no_save_;
bool remove_after_close_;
bool canceled_;
const static int WAIT_TIME = 1000;
const static int HEARTBEAT_RATE = 30; // sends 30 heartbeat packs per min
QTimer *hb_timer_;
State state_;
QAtomicInt roomDelay_;
private slots:
void initRouter();
void setClientId(const QString &id);
void setRoomName(const QString &name);
void setCanvasSize(const QSize &size);
void setArchiveSignature(const QString &as);
void setSchedualDataLength(quint64 length);
ParserResult parserPack(const QByteArray& data);
QByteArray assamblePack(bool compress, PACK_TYPE pt, const QByteArray& bytes);
void onPending(const QByteArray& bytes);
Expand Down
2 changes: 1 addition & 1 deletion src/common/network/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
Socket::Socket(QObject *parent) :
QObject(parent),
dataSize(0),
socket(new QTcpSocket(this)),
commandStarted(false)
{
socket = new QTcpSocket(this);
socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
connect(socket,&QTcpSocket::readyRead,
Expand Down
2 changes: 1 addition & 1 deletion src/common/network/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ protected slots:
QByteArray pack(const QByteArray &content);
void unpack(const QByteArray &content);
quint32 dataSize;
bool commandStarted;
QTcpSocket *socket;
bool commandStarted;
private:
Q_DISABLE_COPY(Socket)
};
Expand Down

0 comments on commit f581d3c

Please sign in to comment.