void SSDBMultiSetWaitReply::mergeAndSend(ClientLogicSession* client) { if (mIsError) { SSDBProtocolRequest errorResponse; errorResponse.writev("error"); errorResponse.endl(); client->send(errorResponse.getResult(), errorResponse.getResultLen()); } else { if (mWaitResponses.size() == 1) { client->send(mWaitResponses.front().reply->c_str(), mWaitResponses.front().reply->size()); } else { string* errorReply = nullptr; int64_t num = 0; SSDBProtocolRequest megreResponse; for (auto& v : mWaitResponses) { int64_t tmp; if (read_int64(v.ssdbReply, &tmp).ok()) { num += tmp; } else { errorReply = v.reply; break; } } if (errorReply != nullptr) { client->send(errorReply->c_str(), errorReply->size()); } else { SSDBProtocolRequest megreResponse; megreResponse.writev("ok", num); megreResponse.endl(); client->send(megreResponse.getResult(), megreResponse.getResultLen()); } } } }
void SSDBMultiClient::forgeError(const string& error, std::function<void(const string&)>& callback) { if (callback != nullptr) { SSDBProtocolRequest err; err.appendStr(error); err.endl(); std::shared_ptr<string > response = std::make_shared<string>(err.getResult(), err.getResultLen()); mLogicFunctorMQ.Push([callback, response](){ callback(*response); }); } }
void SSDBSingleWaitReply::mergeAndSend(ClientLogicSession* client) { assert(mWaitResponses.size() == 1); if (!mIsError) { client->send(mWaitResponses.front().reply->c_str(), mWaitResponses.front().reply->size()); } else { /* TODO::缓存错误的response对象,进行多次使用,避免多次构造 */ SSDBProtocolRequest errorResponse; errorResponse.writev("error"); errorResponse.endl(); client->send(errorResponse.getResult(), errorResponse.getResultLen()); } }
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); }); } }