Beispiel #1
0
int CwxAppFramework::onFailConnect(CwxAppHandler4Msg& conn)
{
    CWX_DEBUG(("Failure to connect, svr_id=%u, host_id=%u, conn_id=%u",
        conn.getConnInfo().getSvrId(),
        conn.getConnInfo().getHostId(),
        conn.getConnInfo().getConnId()));
    return 0;
}
Beispiel #2
0
int CwxAppFramework::onConnClosed(CwxAppHandler4Msg& conn)
{
    CWX_DEBUG(("connect closed, svr_id=%u, host_id=%u, conn_id=%u",
        conn.getConnInfo().getSvrId(),
        conn.getConnInfo().getHostId(),
        conn.getConnInfo().getConnId()));
    return 0;

}
Beispiel #3
0
int CwxAppFramework::onStartSendMsg(CwxMsgBlock* msg,
                                    CwxAppHandler4Msg& conn)
{
    CWX_UNUSED_ARG(msg);
    CWX_DEBUG(("begin send msg, svr_id=%u, host_id=%u, conn_id=%u",
        conn.getConnInfo().getSvrId(),
        conn.getConnInfo().getHostId(),
        conn.getConnInfo().getConnId()));
    return 0;
}
Beispiel #4
0
CWX_UINT32 CwxAppFramework::onEndSendMsg(CwxMsgBlock*& msg,
                                         CwxAppHandler4Msg& conn)
{
    CWX_UNUSED_ARG(msg);
    CWX_DEBUG(("finish send msg, svr_id=%u, host_id=%u, conn_id=%u",
        conn.getConnInfo().getSvrId(),
        conn.getConnInfo().getHostId(),
        conn.getConnInfo().getConnId()));
    return CwxMsgSendCtrl::UNDO_CONN;
}
Beispiel #5
0
///notice close connect
void CwxAppFramework::innerNoticeCloseConnect(CwxAppFramework* pApp, CwxAppNotice* pNotice)
{
    CWX_UINT32 uiConnId = (CWX_UINT32)((size_t)(pNotice->m_noticeArg));
    CwxAppHandler4Msg* pConn = (CwxAppHandler4Msg*)pApp->m_pReactor->getHandlerByConnId(uiConnId);
    if (!pConn)
    {
        CWX_ERROR(("Execute closing connect by conn_id[%u], it doesn't exist", uiConnId));
        return ;
    }
    pConn->getConnInfo().setActiveClose(true);
    pConn->close();
}
Beispiel #6
0
///连接关闭
int CwxProProxyApp::onConnClosed(CwxAppHandler4Msg& ,
    CwxAppHandler4Msg& conn,
    CwxMsgHead const& ,
    bool& ) {
  CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0);
  pBlock->event().setSvrId(conn.getConnInfo().getSvrId());
  pBlock->event().setHostId(conn.getConnInfo().getHostId());
  pBlock->event().setConnId(conn.getConnInfo().getConnId());
  pBlock->event().setEvent(CwxEventInfo::CONN_CLOSED);
  m_mqThreadPool->append(pBlock);
  return 0;
}
Beispiel #7
0
int CwxMqApp::monitorStats(char const* buf,
                           CWX_UINT32 uiDataLen,
                           CwxAppHandler4Msg& conn)
{
  string* strCmd = (string*) conn.getConnInfo().getUserData();
  strCmd->append(buf, uiDataLen);
  CwxMsgBlock* msg = NULL;
  string::size_type end = 0;
  do {
    CwxCommon::trim(*strCmd);
    end = strCmd->find('\n');
    if (string::npos == end) {
      if (strCmd->length() > 10) { //无效的命令
        strCmd->erase(); ///清空接受到的命令
        ///回复信息
        msg = CwxMsgBlockAlloc::malloc(1024);
        strcpy(msg->wr_ptr(), "ERROR\r\n");
        msg->wr_ptr(strlen(msg->wr_ptr()));
      } else {
        return 0;
      }
    } else {
      if (memcmp(strCmd->c_str(), "stats", 5) == 0) {
        strCmd->erase(); ///清空接受到的命令
        CWX_UINT32 uiLen = packMonitorInfo();
        msg = CwxMsgBlockAlloc::malloc(uiLen);
        memcpy(msg->wr_ptr(), m_szBuf, uiLen);
        msg->wr_ptr(uiLen);
      } else if (memcmp(strCmd->c_str(), "quit", 4) == 0) {
        return -1;
      } else { //无效的命令
        strCmd->erase(); ///清空接受到的命令
        ///回复信息
        msg = CwxMsgBlockAlloc::malloc(1024);
        strcpy(msg->wr_ptr(), "ERROR\r\n");
        msg->wr_ptr(strlen(msg->wr_ptr()));
      }
    }
  } while (0);

  msg->send_ctrl().setConnId(conn.getConnInfo().getConnId());
  msg->send_ctrl().setSvrId(CwxMqApp::SVR_TYPE_MONITOR);
  msg->send_ctrl().setHostId(0);
  msg->send_ctrl().setMsgAttr(CwxMsgSendCtrl::NONE);
  if (-1 == sendMsgByConn(msg)) {
    CWX_ERROR(("Failure to send monitor reply"));
    CwxMsgBlockAlloc::free(msg);
    return -1;
  }
  return 0;
}
Beispiel #8
0
///收到消息
int CwxProProxyApp::onRecvMsg(CwxMsgBlock* msg,
                        CwxAppHandler4Msg& conn,
                        CwxMsgHead const& header,
                        bool&)
{
  msg->event().setSvrId(conn.getConnInfo().getSvrId());
  msg->event().setHostId(conn.getConnInfo().getHostId());
  msg->event().setConnId(conn.getConnInfo().getConnId());
  msg->event().setEvent(CwxEventInfo::RECV_MSG);
  msg->event().setMsgHeader(header);
  msg->event().setTimestamp(CwxDate::getTimestamp());
  m_mqThreadPool->append(msg);
  return 0;
}
Beispiel #9
0
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;
    
}
Beispiel #10
0
///notice noticeReconnect 
void CwxAppFramework::innerNoticeReconnect(CwxAppFramework* pApp, CwxAppNotice* pNotice)
{
    CWX_UINT32 uiConnId = (CWX_UINT32)((size_t)(pNotice->m_noticeArg));
    CWX_UINT32 uiDelay = (CWX_UINT32)((size_t)(pNotice->m_noticeArg1));
    CwxAppHandler4Msg * pConn = (CwxAppHandler4Msg*)pApp->m_pReactor->getHandlerByConnId(uiConnId);
    if (!pConn)
    {
        CWX_ERROR(("Execute closing connect by conn_id[%u], it doesn't exist", uiConnId));
        return ;
    }
    if (!pConn->getConnInfo().isActiveConn())
    {
        CWX_ERROR(("Execute closing connect by conn_id[%u], it's not active conn", uiConnId));
        return ;
    }
    if (CwxAppConnInfo::CONNECTING == pConn->getConnInfo().getState())
    {
        CWX_DEBUG(("Conn[%s] is waiting connect, don't reconnect"));
        return;
    }
    pConn->getConnInfo().setActiveClose(false);
    pConn->getConnInfo().setReconn(true);
    pConn->getConnInfo().setReconnDelay(uiDelay);
    pConn->close();
}
Beispiel #11
0
///收到消息的响应函数
int CwxMqApp::onRecvMsg(CwxAppHandler4Msg& conn, bool&) {
  if (SVR_TYPE_MONITOR == conn.getConnInfo().getSvrId()) {
    char szBuf[1024];
    ssize_t recv_size = CwxSocket::recv(conn.getHandle(), szBuf, 1024);
    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;
      }
    }
    ///监控消息
    return monitorStats(szBuf, (CWX_UINT32) recv_size, conn);
  } else {
    CWX_ASSERT(0);
  }
  return -1;
}
Beispiel #12
0
///echo请求的请求消息
int CwxEchoApp::onRecvMsg(CwxMsgBlock* msg, CwxAppHandler4Msg& conn, CwxMsgHead const& header, bool& bSuspendConn){

    msg->event().setSvrId(conn.getConnInfo().getSvrId());
    msg->event().setHostId(conn.getConnInfo().getHostId());
    msg->event().setConnId(conn.getConnInfo().getConnId());
    msg->event().setIoHandle(conn.getHandle());
    msg->event().setConnUserData(NULL);
    msg->event().setMsgHeader(header);
    msg->event().setEvent(CwxEventInfo::RECV_MSG);
    msg->event().setTimestamp(CwxDate::getTimestamp());
    ///不停止继续接受
    bSuspendConn = false;
    CWX_ASSERT (msg);
    ///将消息放到线程池队列中,有内部的线程调用其处理handle来处理
    m_threadPool->append(msg);
    return 0;

}
Beispiel #13
0
///收到消息
int CwxMqApp::onRecvMsg(CwxMsgBlock* msg,
                        CwxAppHandler4Msg& conn,
                        CwxMsgHead const& header,
                        bool&)
{
  if ((SVR_TYPE_RECV == conn.getConnInfo().getSvrId())
    || (SVR_TYPE_MASTER == conn.getConnInfo().getSvrId()))
  {
    msg->event().setSvrId(conn.getConnInfo().getSvrId());
    msg->event().setHostId(conn.getConnInfo().getHostId());
    msg->event().setConnId(conn.getConnInfo().getConnId());
    ///保存消息头
    msg->event().setMsgHeader(header);
    ///设置事件类型
    msg->event().setEvent(CwxEventInfo::RECV_MSG);
    ///将消息放到线程池队列中,有内部的线程调用其处理handle来处理
    m_recvThreadPool->append(msg);
    return 0;
  } else {
    CWX_ASSERT(0);
  }
  return 0;
}
Beispiel #14
0
int CwxAppFramework::onRecvMsg(CwxAppHandler4Msg& conn,
                      bool& )
{
    CWX_DEBUG(("recv msg, svr_id=%u, host_id=%u, conn_id=%u",
        conn.getConnInfo().getSvrId(),
        conn.getConnInfo().getHostId(),
        conn.getConnInfo().getConnId()));
    char szBuf[4096];
    int ret = 0;
    while(1)
    {
        ret = CwxSocket::read(conn.getHandle(), szBuf, 4096);
        if (0>=ret)
        {
            if ((0 == ret) || (errno != EWOULDBLOCK))
            {
                return -1; //error
            }
            return 0;
        }
        if (ret < 4096) return 0;
    }
    return ret;
}
Beispiel #15
0
///notice send msg
void CwxAppFramework::innerNoticeSendMsgByConn(CwxAppFramework* pApp,
                                               CwxAppNotice* pNotice)
{
    CwxMsgBlock* msg = (CwxMsgBlock*)pNotice->m_noticeArg;
    CwxAppHandler4Msg* pConn = (CwxAppHandler4Msg*)pApp->m_pReactor->getHandlerByConnId(msg->send_ctrl().getConnId());
    if (!pConn || 
        (CwxAppConnInfo::ESTABLISHED != pConn->getConnInfo().getState()))
    {
        if (msg->send_ctrl().isFailNotice())
        {
            pApp->onFailSendMsg(msg);
        }
        if (msg) CwxMsgBlockAlloc::free(msg);
        pNotice->m_noticeArg = NULL;
        return;
    }
    if (!msg->send_ctrl().isUndoConn())
    {
        int ret = 0;
        if (msg->send_ctrl().isResumeConn())
        {
            ret = pConn->reactor()->resumeHandler(pConn, CwxAppHandler4Base::READ_MASK);
            if (0 != ret)
            {
                CWX_ERROR(("Failure to resume handler, conn_id=[%u]", msg->send_ctrl().getConnId()));
                pConn->close();
            }
        }
        else if (msg->send_ctrl().isSuspendConn())
        {
            ret = pConn->reactor()->suspendHandler(pConn, CwxAppHandler4Base::READ_MASK);
            if (0 != ret)
            {
                CWX_ERROR(("Failure to resume handler, conn_id=[%u]", msg->send_ctrl().getConnId()));
                pConn->close();
            }
        }
    }
    if (!pConn->putMsg(msg))
    {
        if (msg->send_ctrl().isFailNotice())
        {
            pApp->onFailSendMsg(msg);
        }
        if (msg) CwxMsgBlockAlloc::free(msg);
        pNotice->m_noticeArg = NULL;
    }
}
Beispiel #16
0
///连接关闭
int CwxMqApp::onConnClosed(CwxAppHandler4Msg& conn) {
  if ((SVR_TYPE_RECV == conn.getConnInfo().getSvrId())
    || (SVR_TYPE_MASTER == conn.getConnInfo().getSvrId()))
  {
    CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0);
    pBlock->event().setSvrId(conn.getConnInfo().getSvrId());
    pBlock->event().setHostId(conn.getConnInfo().getHostId());
    pBlock->event().setConnId(conn.getConnInfo().getConnId());
    ///设置事件类型
    pBlock->event().setEvent(CwxEventInfo::CONN_CLOSED);
    m_recvThreadPool->append(pBlock);
  } else if (SVR_TYPE_MONITOR == conn.getConnInfo().getSvrId()) { ///若是监控的连接关闭,则必须释放先前所创建的string对象。
    if (conn.getConnInfo().getUserData()) {
      delete (string*) conn.getConnInfo().getUserData();
      conn.getConnInfo().setUserData(NULL);
    }
  } else {
    CWX_ASSERT(0);
  }
  return 0;
}
Beispiel #17
0
///连接建立
int CwxMqApp::onConnCreated(CwxAppHandler4Msg& conn,
                            bool&,
                            bool&)
{
  if ((SVR_TYPE_RECV == conn.getConnInfo().getSvrId())
    || (SVR_TYPE_MASTER == conn.getConnInfo().getSvrId()))
  {
    CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0);
    pBlock->event().setSvrId(conn.getConnInfo().getSvrId());
    pBlock->event().setHostId(conn.getConnInfo().getHostId());
    pBlock->event().setConnId(conn.getConnInfo().getConnId());
    ///设置事件类型
    pBlock->event().setEvent(CwxEventInfo::CONN_CREATED);
    ///将事件添加到消息队列
    m_recvThreadPool->append(pBlock);
  } else if (SVR_TYPE_MONITOR == conn.getConnInfo().getSvrId()) { ///如果是监控的连接建立,则建立一个string的buf,用于缓存不完整的命令
    string* buf = new string();
    conn.getConnInfo().setUserData(buf);
  } else {
    CWX_ASSERT(0);
  }
  return 0;
}