ssize_t CwxSocket::recv (CWX_HANDLE handle, CwxMsgHead& head, CwxMsgBlock*& msg, CwxTimeouter *timeout) { char szHead[CwxMsgHead::MSG_HEAD_LEN]; ssize_t ret = read_n(handle, szHead, CwxMsgHead::MSG_HEAD_LEN, timeout, NULL); if (ret != CwxMsgHead::MSG_HEAD_LEN) return ret; if (!head.fromNet(szHead)) { errno = EBADMSG; return -1; } msg = CwxMsgBlockAlloc::malloc(head.getDataLen()); if (!msg) { errno = ENOMEM; return -1; } ret = recv_n(handle, msg->wr_ptr(), head.getDataLen(), timeout, NULL); if (ret != (ssize_t)head.getDataLen()) { CwxMsgBlockAlloc::free(msg); msg = NULL; return ret; } msg->wr_ptr(head.getDataLen()); return CwxMsgHead::MSG_HEAD_LEN + head.getDataLen(); }
int CwxAppFramework::onRecvMsg(CwxMsgBlock* msg, CwxAppHandler4Msg& conn, CwxMsgHead const& header, bool& bSuspendConn) { CWX_DEBUG(("recv msg, svr_id=%u, host_id=%u, conn_id=%u, msg_type=%u", conn.getConnInfo().getSvrId(), conn.getConnInfo().getHostId(), conn.getConnInfo().getConnId(), header.getMsgType())); if (msg) CwxMsgBlockAlloc::free(msg); bSuspendConn = false; return 0; }
int main(int argc ,char** argv){ int iRet = parseArg(argc, argv); if (0 == iRet) return 0; if (-1 == iRet) return 1; CwxSockStream stream; CwxINetAddr addr(g_unPort, g_strHost.c_str()); CwxSockConnector conn; if (0 != conn.connect(stream, addr)){ printf("Failure to connect ip:port: %s:%u, errno=%d\n", g_strHost.c_str(), g_unPort, errno); return 1; } CwxPackageWriterEx writer; CwxPackageReaderEx reader; CwxMsgHead head; CwxMsgBlock* block=NULL; char szErr2K[2048]; char const* pErrMsg=NULL; CwxKeyValueItemEx item; CWX_UINT32 uiBufLen = UNISTOR_MAX_DATA_SIZE ; char* szBuf = (char*)malloc(uiBufLen); CwxEncodeXml xmlEncode; CwxXmlPackageConv xmlConv(&xmlEncode); CWX_UINT32 uiVersion=0; CWX_UINT32 uiFieldNum=0; if (g_file.length()){ string strContent; if (!CwxFile::readTxtFile(g_file, strContent)){ printf("Failure to read xml file:%s\n", g_file.c_str()); free(szBuf); return -1; } if (!xmlConv.xmlToPackage(strContent.c_str(), szBuf, uiBufLen)){ printf("Failure to convert xml, err:%s\n", xmlConv.getErrMsg()); free(szBuf); return -1; } if (!reader.unpack(szBuf, uiBufLen)){ printf("Failure to unpack msg, err:%s\n", reader.getErrMsg()); free(szBuf); return -1; } CWX_UINT32 index=0; item.m_szData = reader.getKey(index)->m_szData; item.m_uiDataLen = reader.getKey(index)->m_uiDataLen; item.m_bKeyValue = reader.getKey(index)->m_bKeyValue; }else{ item.m_szData = g_data.c_str(); item.m_uiDataLen = g_data.length(); item.m_bKeyValue = false; } CwxKeyValueItemEx key; key.m_szData = g_key.c_str(); key.m_uiDataLen = g_key.length(); key.m_bKeyValue = false; CwxKeyValueItemEx field; field.m_szData = g_field.c_str(); field.m_uiDataLen = g_field.length(); field.m_bKeyValue = false; CwxKeyValueItemEx extra; extra.m_szData = g_extra.c_str(); extra.m_uiDataLen = g_extra.length(); extra.m_bKeyValue = false; do { if (UNISTOR_ERR_SUCCESS != UnistorPoco::packRecvAdd( &writer, block, 100, key, g_field.length()?&field:NULL, g_extra.length()?&extra:NULL, item, g_uiExpire, g_sign, g_uiVersion, g_bCache, g_user.c_str(), g_passwd.c_str(), szErr2K)) { printf("failure to pack add key package, err=%s\n", szErr2K); iRet = 1; break; } if (block->length() != (CWX_UINT32)CwxSocket::write_n(stream.getHandle(), block->rd_ptr(), block->length())) { printf("failure to send message, errno=%d\n", errno); iRet = 1; break; } CwxMsgBlockAlloc::free(block); block = NULL; //recv msg if (0 >= CwxSocket::read(stream.getHandle(), head, block)) { printf("failure to read the reply, errno=%d\n", errno); iRet = 1; break; } if (UnistorPoco::MSG_TYPE_RECV_ADD_REPLY != head.getMsgType()) { printf("recv a unknow msg type, msg_type=%u\n", head.getMsgType()); iRet = 1; break; } if (UNISTOR_ERR_SUCCESS != UnistorPoco::parseRecvReply(&reader, block, iRet, uiVersion, uiFieldNum, pErrMsg, szErr2K)) { printf("failure to unpack reply msg, err=%s\n", szErr2K); iRet = 1; break; } if (UNISTOR_ERR_SUCCESS != iRet) { printf("failure to add key, task_id=%u, err_code=%d, err=%s\n", head.getTaskId(), iRet, pErrMsg); iRet = 1; break; } iRet = 0; printf("success to add key[%s], data=%s, version=%u, fieldnum=%u, taskid=%u\n", g_key.c_str(), g_data.c_str(), uiVersion, uiFieldNum, head.getTaskId()); } while(0); if (block) CwxMsgBlockAlloc::free(block); stream.close(); free(szBuf); return iRet; }
int CwxAppHandler4Channel::recvPackage(CWX_HANDLE handle, CWX_UINT32& uiRecvHeadLen, CWX_UINT32& uiRecvDataLen, char* szRecvHead, CwxMsgHead& header, CwxMsgBlock*& msg) { ssize_t recv_size = 0; ssize_t need_size = 0; need_size = CwxMsgHead::MSG_HEAD_LEN - uiRecvHeadLen; if (need_size > 0 ) {//not get complete head recv_size = CwxSocket::recv(handle, szRecvHead + uiRecvHeadLen, need_size); if (recv_size <=0 ) { //error or signal if ((0==recv_size) || ((errno != EWOULDBLOCK) && (errno != EINTR))) { return -1; //error } else {//signal or no data return 0; } } uiRecvHeadLen += recv_size; if (recv_size < need_size) { return 0; } szRecvHead[uiRecvHeadLen] = 0x00; if (!header.fromNet(szRecvHead)) { CWX_ERROR(("Msg header is error.")); return -1; } msg = CwxMsgBlockAlloc::malloc(header.getDataLen()); uiRecvDataLen = 0; }//end if (need_size > 0) //recv data need_size = header.getDataLen() - uiRecvDataLen; if (need_size > 0) {//not get complete data recv_size = CwxSocket::recv(handle, msg->wr_ptr(), need_size); if (recv_size <=0 ) { //error or signal if ((errno != EWOULDBLOCK)&&(errno != EINTR)) { return -1; //error } else {//signal or no data return 0; } } //move write pointer msg->wr_ptr(recv_size); uiRecvDataLen += recv_size; if (recv_size < need_size) { return 0; } } return 1; }
int main(int argc, char** argv) { int iRet = parseArg(argc, argv); if (0 == iRet) return 0; if (-1 == iRet) return 1; CwxSockStream stream; CwxINetAddr addr(g_unPort, g_strHost.c_str()); CwxSockConnector conn; if (0 != conn.connect(stream, addr)) { printf("failure to connect ip:port: %s:%u, errno=%d\n", g_strHost.c_str(), g_unPort, errno); return 1; } CwxPackageWriter writer; CwxPackageReader reader; CwxMsgHead head; CwxMsgBlock* block = NULL; char szErr2K[2048]; char const* pErrMsg = NULL; CwxKeyValueItem item; if (g_file.length()) { item.m_szData = g_szData; item.m_uiDataLen = g_uiDataLen; } else { item.m_szData = (char*) g_data.c_str(); item.m_uiDataLen = g_data.length(); } do { if (CWX_MQ_ERR_SUCCESS != CwxMqPoco::packRecvData(&writer, block, 0, item, g_user.c_str(), g_passwd.c_str(), g_zip, szErr2K)) { printf("failure to pack message package, err=%s\n", szErr2K); iRet = 1; break; } if (block->length() != (CWX_UINT32) CwxSocket::write_n(stream.getHandle(), block->rd_ptr(), block->length())) { printf("failure to send message, errno=%d\n", errno); iRet = 1; break; } CwxMsgBlockAlloc::free(block); block = NULL; //recv msg if (0 >= CwxSocket::read(stream.getHandle(), head, block)) { printf("failure to read the reply, errno=%d\n", errno); iRet = 1; break; } if (CwxMqPoco::MSG_TYPE_RECV_DATA_REPLY != head.getMsgType()) { printf("recv a unknow msg type, msg_type=%u\n", head.getMsgType()); iRet = 1; break; } CWX_UINT64 ullSid; if (CWX_MQ_ERR_SUCCESS != CwxMqPoco::parseRecvDataReply(&reader, block, iRet, ullSid, pErrMsg, szErr2K)) { printf("failure to unpack reply msg, err=%s\n", szErr2K); iRet = 1; break; } if (CWX_MQ_ERR_SUCCESS != iRet) { printf("failure to send message, err_code=%d, err=%s\n", iRet, pErrMsg); iRet = 1; break; } iRet = 0; printf("success to send msg, data's sid=%s\n", CwxCommon::toString(ullSid, szErr2K, 10)); } while (0); if (g_szData) free(g_szData); if (block) CwxMsgBlockAlloc::free(block); stream.close(); return iRet; }
int main(int argc ,char** argv) { int iRet = parseArg(argc, argv); if (0 == iRet) return 0; if (-1 == iRet) return 1; CwxSockStream stream; CwxINetAddr addr(g_unPort, g_strHost.c_str()); CwxSockConnector conn; if (0 != conn.connect(stream, addr)) { printf("Failure to connect ip:port: %s:%u, errno=%d\n", g_strHost.c_str(), g_unPort, errno); return 1; } CwxPackageWriterEx writer; CwxPackageReaderEx reader; CwxMsgHead head; CwxMsgBlock* block=NULL; char szErr2K[2048]; char const* pErrMsg=NULL; CWX_UINT32 uiVersion=0; CWX_UINT32 uiFieldNum=0; CwxKeyValueItemEx key; key.m_szData = g_key.c_str(); key.m_uiDataLen = g_key.length(); key.m_bKeyValue = false; CwxKeyValueItemEx field; field.m_szData = g_field.c_str(); field.m_uiDataLen = g_field.length(); field.m_bKeyValue = false; CwxKeyValueItemEx extra; extra.m_szData = g_extra.c_str(); extra.m_uiDataLen = g_extra.length(); extra.m_bKeyValue = false; do { if (UNISTOR_ERR_SUCCESS != UnistorPoco::packRecvDel( &writer, block, 0, key, g_field.length()?&field:NULL, g_extra.length()?&extra:NULL, g_version, g_user.c_str(), g_passwd.c_str(), szErr2K)) { printf("failure to pack delete key package, err=%s\n", szErr2K); iRet = 1; break; } if (block->length() != (CWX_UINT32)CwxSocket::write_n(stream.getHandle(), block->rd_ptr(), block->length())) { printf("failure to send message, errno=%d\n", errno); iRet = 1; break; } CwxMsgBlockAlloc::free(block); block = NULL; //recv msg if (0 >= CwxSocket::read(stream.getHandle(), head, block)) { printf("failure to read the reply, errno=%d\n", errno); iRet = 1; break; } if (UnistorPoco::MSG_TYPE_RECV_DEL_REPLY != head.getMsgType()) { printf("recv a unknow msg type, msg_type=%u\n", head.getMsgType()); iRet = 1; break; } if (UNISTOR_ERR_SUCCESS != UnistorPoco::parseRecvReply(&reader, block, iRet, uiVersion, uiFieldNum, pErrMsg, szErr2K)) { printf("failure to unpack reply msg, err=%s\n", szErr2K); iRet = 1; break; } if (UNISTOR_ERR_SUCCESS != iRet) { printf("failure to delete key, err_code=%d, err=%s\n", iRet, pErrMsg); iRet = 1; break; } iRet = 0; printf("success to delete key[%s], version=%u, field_num=%u\n", g_key.c_str(), uiVersion, uiFieldNum); } while(0); if (block) CwxMsgBlockAlloc::free(block); stream.close(); return iRet; }
int main(int argc ,char** argv) { int iRet = parseArg(argc, argv); if (0 == iRet) return 0; if (-1 == iRet) return 1; CwxSockStream stream; CwxINetAddr addr(g_unPort, g_strHost.c_str()); CwxSockConnector conn; if (0 != conn.connect(stream, addr)) { printf("Failure to connect ip:port: %s:%u, errno=%d\n", g_strHost.c_str(), g_unPort, errno); return 1; } CwxPackageWriterEx writer; CwxPackageWriterEx writer1; CwxPackageReaderEx reader; CwxMsgHead head; CwxMsgBlock* block=NULL; char szErr2K[2048]; CWX_UINT32 output_buf_len = UNISTOR_MAX_KVS_SIZE; char* output_buf = (char*)malloc(output_buf_len); bool bGets=g_key.size()>1?true:false; do { string strField; list<string>::iterator iter = g_field.begin(); while(iter != g_field.end()){ if (!strField.length()){ strField = *iter; }else{ strField += "\n"; strField += *iter; } iter++; } CwxKeyValueItemEx field; field.m_szData = strField.c_str(); field.m_uiDataLen = strField.length(); field.m_bKeyValue = false; CwxKeyValueItemEx extra; extra.m_szData = g_extra.c_str(); extra.m_uiDataLen = g_extra.length(); extra.m_bKeyValue = false; if (!bGets){ CwxKeyValueItemEx key; key.m_szData = g_key.begin()->c_str(); key.m_uiDataLen = g_key.begin()->length(); key.m_bKeyValue = false; if (UNISTOR_ERR_SUCCESS != UnistorPoco::packGetKey(&writer, block, 100, key, strField.length()?&field:NULL, g_extra.length()?&extra:NULL, g_bVer, g_user.c_str(), g_passwd.c_str(), g_bMaster, g_ucKeyInfo, szErr2K)) { printf("failure to pack get key package, err=%s\n", szErr2K); iRet = 1; break; } }else{ list<pair<char const*, CWX_UINT16> > keys; list<string>::iterator iter = g_key.begin(); while(iter != g_key.end()){ keys.push_back(pair<char const*, CWX_UINT16>(iter->c_str(), iter->length())); iter++; } if (UNISTOR_ERR_SUCCESS != UnistorPoco::packGetKeys(&writer, &writer1, block, 100, keys, strField.length()?&field:NULL, g_extra.length()?&extra:NULL, g_user.c_str(), g_passwd.c_str(), g_bMaster, g_ucKeyInfo, szErr2K)) { printf("failure to pack gets key package, err=%s\n", szErr2K); iRet = 1; break; } } //send if (block->length() != (CWX_UINT32)CwxSocket::write_n(stream.getHandle(), block->rd_ptr(), block->length())) { printf("failure to send message, errno=%d\n", errno); iRet = 1; break; } CwxMsgBlockAlloc::free(block); block = NULL; //recv msg if (0 >= CwxSocket::read(stream.getHandle(), head, block)) { printf("failure to read the reply, errno=%d\n", errno); iRet = 1; break; } if ((bGets?UnistorPoco::MSG_TYPE_RECV_GETS_REPLY:UnistorPoco::MSG_TYPE_RECV_GET_REPLY) != head.getMsgType()) { printf("recv a unknow msg type, task_id=%u, msg_type=%u\n", head.getTaskId(), head.getMsgType()); iRet = 1; break; } printf("query result, task_id=%u, len=%u, msg_len=%u\n", head.getTaskId(), head.getDataLen(), block->length()); CwxPackageEx::dump(block->rd_ptr(), block->length(), output_buf, output_buf_len, " "); printf("dump len:%u\n", output_buf_len); output_buf[output_buf_len] = 0x00; for (CWX_UINT32 i=0; i<output_buf_len; i++){ if (0 == output_buf[i]) output_buf[i]=' '; } printf("%s\n", output_buf); } while(0); if (block) CwxMsgBlockAlloc::free(block); if (output_buf) free(output_buf); stream.close(); return iRet; }