示例#1
0
void CwxEchoThriftIf::Echo(echo_thrift::EchoData& _return, const std::string& echo_data) {
  EchoTss* tss = (EchoTss*)CwxTss::instance();
  CwxMsgBlock* msg = CwxMsgBlockAlloc::malloc(echo_data.length());
  memcpy(msg->wr_ptr(), echo_data.c_str(), echo_data.length());
  msg->wr_ptr(echo_data.length());
  msg->event().setSvrId(CwxEchoApp::SVR_TYPE_ECHO);
  msg->event().setEvent(CwxEventInfo::RECV_MSG);
  msg->event().setConnUserData(&tss->m_queue);
  tss->m_curId++;
  msg->event().m_ullArg = tss->m_curId;
  m_app->GetThreadPool()->append(msg);
  while(true) {
    if (tss->m_queue.dequeue(msg) != -1) {
      if (msg->event().m_ullArg == tss->m_curId) {
        _return.data.assign(msg->rd_ptr(), msg->length());
        CwxMsgBlockAlloc::free(msg);
        return;
      }
      CwxMsgBlockAlloc::free(msg);
    } else {
      _return.data = "";
    }
  }
}
示例#2
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;
  }
  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;
}