本文整理汇总了C++中SocketServer::write方法的典型用法代码示例。如果您正苦于以下问题:C++ SocketServer::write方法的具体用法?C++ SocketServer::write怎么用?C++ SocketServer::write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SocketServer
的用法示例。
在下文中一共展示了SocketServer::write方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: handle
int PsCacheSetHandler::handle(PcMsg * msg , SocketServer & server , int peer)
{
PcMsgSetReq * set = (PcMsgSetReq *)msg;
PcMsgSetRes res;
COMMON_LOGGER_DEBUG("set command , %s %s\n" , set->getKey().c_str() , set->getValue().c_str());
int len = 0;
#ifndef __STORAGE_FASTDB__
PsData * data = PsDataMgr::instance()->set(const_cast<char *>(set->getKey().c_str()) ,
const_cast<char *>(set->getValue().c_str()) , strlen(set->getValue().c_str()));
if (data)
{
res.setCode(0);
res.setDesc("success");
res.setValue(data->value);
len += strlen(data->value);
len += 4;
}
else
{
res.setCode(1);
res.setDesc("failed");
}
#else
bool ret = PsDataMgr::instance()->set(const_cast<char *>(set->getKey().c_str()) ,
const_cast<char *>(set->getValue().c_str()) , strlen(set->getValue().c_str()) , -1);
if (ret)
{
res.setCode(0);
res.setDesc("success");
res.setValue(set->getValue());
len += strlen(set->getValue().c_str());
len += 4;
}
else
{
res.setCode(1);
res.setDesc("failed");
}
#endif
res.setType(PSCACHE_MSG_SET_RES);
res.setLength(80 + len);
res.setVersion(msg->getVersion());
PscacheMsgResParser parser;
char * buffer = new char[80 + len];
(void)memset(buffer , 0x00 , 80 + len);
parser.compile(res , buffer);
server.write(peer , buffer , 80 + len);
delete [] buffer;
return 0;
}
示例2: socketCallback
int socketCallback(int sock , char * buffer)
{
PcMsg * pmsg = 0;
unsigned int type = 0;
(void)PcMsgUtil::parse(buffer + 4 , type);
COMMON_LOGGER_DEBUG("type is %04X\n" , type);
if (type == PSCACHE_MSG_GET_REQ)
{
pmsg = new PcMsgGetReq;
pscache_get_counter ++;
}
else if (type == PSCACHE_MSG_SET_REQ)
{
pmsg = new PcMsgSetReq;
pscache_set_counter ++;
}
else if (type == PSCACHE_MSG_DEL_REQ)
{
pmsg = new PcMsgDelReq;
pscache_del_counter ++;
}
else if (type == PSCACHE_MSG_REP_REQ)
{
unsigned int number = 0;
PcMsgUtil::parse(buffer + 8 , number);
unsigned int subType = 0;
PcMsgUtil::parse(buffer + 12 + number * 36 + 4 , subType);
if (subType == PSCACHE_MSG_SET_REQ)
{
pmsg = new PcMsgSetReq;
}
else if (subType == PSCACHE_MSG_DEL_REQ)
{
pmsg = new PcMsgDelReq;
}
else
{
return 0;
}
PscacheMsgReqParser parser;
parser.parse(buffer + 12 + number * 36 , *pmsg);
if (pmsg->getType() == PSCACHE_MSG_SET_REQ)
{
PcMsgSetReq * set = (PcMsgSetReq *)pmsg;
#ifndef __STORAGE_FASTDB__
PsDataMgr::instance()->set(const_cast<char *>(set->getKey().c_str()) ,
const_cast<char *>(set->getValue().c_str()) , strlen(set->getValue().c_str()));
#else
PsDataMgr::instance()->set(const_cast<char *>(set->getKey().c_str()) ,
const_cast<char *>(set->getValue().c_str()) , strlen(set->getValue().c_str()) , -1);
#endif
COMMON_LOGGER_DEBUG("replica set command , %s %s\n" , set->getKey().c_str() , set->getValue().c_str());
}
else if (pmsg->getType() == PSCACHE_MSG_DEL_REQ)
{
PcMsgDelReq * del = (PcMsgDelReq *)pmsg;
PsDataMgr::instance()->del(const_cast<char *>(del->getKey().c_str()));
COMMON_LOGGER_DEBUG("replica del command , %s\n" , del->getKey().c_str());
}
pscache_rep_counter ++;
delete pmsg;
char res[12] = {0};
unsigned int length = 12;
int offset = 0;
offset += PcMsgUtil::compile(res + offset , length);
unsigned int type = PSCACHE_MSG_REP_RES;
offset += PcMsgUtil::compile(res + offset , type);
server.write(sock , res , 12);
PcMsgUtil::parse(buffer , length);
PsrHandlerMgr::instance()->handle(sock , buffer , length);
return 0;
}
else if (type == PSCACHE_MSG_HB_REQ)
{
COMMON_LOGGER_INFO("get heart beat request from %d\n" , sock);
char res[12] = {0};
PcMsgHBRes resp;
int offset = 0;
offset += PcMsgUtil::compile(res + offset , resp.getLength());
offset += PcMsgUtil::compile(res + offset , resp.getType());
//.........这里部分代码省略.........