Ejemplo n.º 1
0
void SSDBMultiClient::sendPing(DataSocket::PTR ds)
{
    DBServerUserData* dbUserData = (DBServerUserData*)ds->getUserData();
    if (dbUserData->pingTime > 0)
    {
        SSDBProtocolRequest sr;
        sr.init();
        sr.appendStr("ping");
        sr.endl();

        ds->send(sr.getResult(), sr.getResultLen());

        /*自动发送的ping操作,也要模拟一个完成回调,不然会导致业务上的请求乱序*/
        queue<std::function<void(const string& response)>>* callbacklist = dbUserData->callbacklist;
        callbacklist->push(nullptr);

        dbUserData->pingTimer = mNetService.getTimerMgr().AddTimer(dbUserData->pingTime, [this, ds](){
            sendPing(ds);
        });
    }
}
Ejemplo n.º 2
0
void SSDBMultiClient::startNetThread(std::function<void(void)> frameCallback)
{
    if (mNetThread == nullptr)
    {
        mRunIOLoop = true;
        mNetThread = new thread([&, frameCallback](){
            while (mRunIOLoop)
            {
                mNetService.loop(1);

                mRequestList.SyncRead(0);
                RequestMsg tmp;
                while (mRequestList.PopFront(&tmp))
                {
                    if (!mBackends.empty())
                    {
                        /*  随机选择一个服务器   */
                        DataSocket::PTR client = mBackends[rand() % mBackends.size()];
                        DBServerUserData* dbUserData = (DBServerUserData*)client->getUserData();
                        queue<std::function<void(const string& response)>>* callbacklist = dbUserData->callbacklist;
                        callbacklist->push(tmp.callback);

                        client->send(tmp.content.c_str(), tmp.content.size());
                    }
                    else
                    {
                        forgeError("no server", tmp.callback);
                    }
                }

                mLogicFunctorMQ.ForceSyncWrite();
                /*  TODO::目前只在通知队列里有数据时,才调用帧回调--可在其中唤醒主线程--就无需把外部mainloop设置到此类   */
                if (mLogicFunctorMQ.SharedListSize() > 0 && frameCallback != nullptr)
                {
                    frameCallback();
                }
            }
        });
    }
}