libevent异步TCP连接,判断是否连接成功



libevent异步TCP连接服务器的过程:

1)创建nonblock的socket,

2)调用connect连接服务器,检查返回值,判断是否是阻塞了,如果是,就设置等待 EV_WRITE事件:

     event_set(&m_evSocket, m_iSockFd, EV_WRITE, onConnect, this);

3)EV_WRITE返回后,判断event是否为EV_WRITE,

关键一点:如果EV_WRITE,还要进一步判断sockopt是否正确。


代码片段如下:

bool CClientSocket::connectSocket(string szHostAddr, int nHostPort){
 if (m_iSockFd == -1){
  createSocket();
 }

 struct sockaddr_in Ser_addr;
 memset(&Ser_addr, 0, sizeof(struct sockaddr_in));

 Ser_addr.sin_family = AF_INET;
 Ser_addr.sin_addr.s_addr = inet_addr(szHostAddr.c_str());
 Ser_addr.sin_port = htons(nHostPort);

 if(connect(m_iSockFd, (const struct sockaddr *) &Ser_addr,sizeof(Ser_addr)) == -1){ 
  if ( errno == EINPROGRESS || errno == EWOULDBLOCK){
   event_set(&m_evSocket, m_iSockFd, EV_WRITE, onConnect, this);
   event_base_set(&m_eventbase, &m_evSocket);
   struct timeval tv1;
   tv1.tv_sec = 5;
   tv1.tv_usec = 0;
   event_add(&m_evSocket, &tv1);
   m_connSCRStatus.sockRequestSended();
   printf("connect would block\n");
   return true;
  }
  else{     
   printf("fail to connect\n");
   closeSocket();
   return false;
  }
 }
 else{
  printf("connected directly...\n");
  // connected directlly
  event_set(&m_evSocket, m_iSockFd, EV_READ|EV_PERSIST, onRecv, this); 
  event_base_set(&m_eventbase, &m_evSocket);
  event_add(&m_evSocket, NULL);
  onConnect(); 
  return true;
 }

}

void CClientSocket::onConnect(int fd, short event, void *arg){
 CClientSocket *pThis = (CClientSocket*)arg;
 if (event == EV_WRITE){ 
  int err;
  socklen_t len = sizeof(err);
  getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
  if (err) {
   printf("connect return ev_write, but check failed\n");
   pThis->closeSocket();
  }
  else{
   event_set(&pThis->m_evSocket, pThis->m_iSockFd, EV_READ|EV_PERSIST, onRecv, arg);
   event_base_set(&pThis->m_eventbase, &pThis->m_evSocket);
   event_add(&pThis->m_evSocket, NULL);
   pThis->onConnect();
   printf("connect return ev_write, check ok\n");
  }
 }
 else{  // timeout
  printf("connect return failed\n");
  pThis->closeSocket();
 }
}

void CClientSocket::onConnect(){
 logMessage("connect ok!");
 //

}

参考:

http://stackoverflow.com/questions/16074981/how-to-check-if-connection-is-established-when-use-libev-with-non-block-socket

你可能感兴趣的:(Linux,C++)