void CSSLClientAsync::UnInitSSL() { if (m_ssl) { int32_t nRet = SSL_shutdown(m_ssl); if (nRet == 0) { int32_t nErrorCode = SSL_get_error(GetSSL(), nRet); SOCKET_IO_WARN("ssl shutdown not finished, errno: %d.", nErrorCode); } else if (nRet == 1) { SOCKET_IO_DEBUG("ssl shutdown successed."); } else if (nRet < 0) { int32_t nErrorCode = SSL_get_error(GetSSL(), nRet); SOCKET_IO_ERROR("ssl shutdown failed, errno: %d.", nErrorCode); } SSL_free(m_ssl); m_ssl = NULL; } if (m_ctx) { SSL_CTX_free(m_ctx); m_ctx = NULL; } }
void CSSLClientAsync::OnRecv() { if (GetSSLConnectStatus() == TRUE) { char szBuf[TCP_RECV_SIZE] = {0}; int32_t nRet = SSL_read(GetSSL(), szBuf, TCP_RECV_SIZE); if (nRet > 0) { int32_t nBufSize = nRet; char szIP[32] = {0}; int32_t nPort = 0; S_GetPeerName(GetSocket(), szIP, &nPort); DoRecv(GetSocketID(), szBuf, nBufSize, szIP, nPort); } else if (nRet == 0) { int32_t nErrorCode = SSL_get_error(GetSSL(), nRet); if (SSL_ERROR_ZERO_RETURN == nErrorCode) { //对方关闭socket SOCKET_IO_WARN("recv ssl data error, peer closed."); DoException(GetSocketID(), SOCKET_IO_SSL_RECV_FAILED); } else { SOCKET_IO_ERROR("recv ssl data error."); DoException(GetSocketID(), SOCKET_IO_SSL_RECV_FAILED); } } else { int32_t nErrorCode = SSL_get_error(GetSSL(), nRet); if (SSL_ERROR_WANT_READ == nErrorCode || SSL_ERROR_WANT_WRITE == nErrorCode) { //用select/epoll/iocp的方式应该很少会有这个情况出现 SOCKET_IO_DEBUG("recv ssl data error, buffer is blocking."); } else { SOCKET_IO_ERROR("recv ssl data error, errno: %d.", nErrorCode); DoException(GetSocketID(), SOCKET_IO_SSL_RECV_FAILED); } } } else { SSLConnect(); } }
/** @fn int32_t CTCPClientAsync::SendBufferAsync() * @brief 将待发送队列中的数据发送出去 * @return */ int32_t CTCPClientAsync::SendBufferAsync() { int32_t nErrorCode = 0; m_sendqueuemutex.Lock(); if (m_sendqueue.size() == 0) { //待发送队列中为空,则删除写事件的注册,改成读事件 m_pio->Remove_WriteEvent(this); m_sendqueuemutex.Unlock(); if (_GetWaitForCloseStatus() == TRUE) { //待发送内容发送完毕,则关闭链接 _Close(); } return nErrorCode; } CSimpleBuffer* pBufferLoop = m_sendqueue.front(); m_sendqueuemutex.Unlock(); int32_t nRet = S_Send(GetSocket(), (void*)pBufferLoop->GetBuffer(), pBufferLoop->GetWriteOffset()); if ( nRet < 0) { #if (defined(_WIN32) || defined(_WIN64)) int32_t nError = ::GetLastError(); if (WSAEWOULDBLOCK == nError) #else int32_t nError = errno; if (EAGAIN == nError) #endif { SOCKET_IO_DEBUG("send tcp data, buffer is blocking.") } else { _ClearSendBuffer(); SOCKET_IO_ERROR("send tcp data error, errno: %d.", nError); DoException(GetSocketID(), SOCKET_IO_TCP_SEND_FAILED); } }
int32_t CSSLClientAsync::SendBufferAsync() { int32_t nErrorCode = SOCKET_IO_RESULT_OK; m_sendqueuemutex.Lock(); if (m_sendqueue.size() == 0) { SOCKET_IO_DEBUG("ssl send queue is empty."); //待发送队列中为空,则删除写事件的注册,改成读事件 m_pio->Remove_WriteEvent(this); m_sendqueuemutex.Unlock(); if (_GetWaitForCloseStatus() == TRUE) { //待发送内容发送完毕,则关闭链接 _Close(); } return nErrorCode; } CSimpleBuffer* pBufferLoop = m_sendqueue.front(); m_sendqueuemutex.Unlock(); int32_t nRet = SSL_write(GetSSL(), (void*)pBufferLoop->GetBuffer(), pBufferLoop->GetWriteOffset()); if ( nRet < 0) { int32_t nError = SSL_get_error(GetSSL(), nRet); if (SSL_ERROR_WANT_WRITE == nError || SSL_ERROR_WANT_READ == nError) { SOCKET_IO_INFO("send ssl data, buffer is blocking, errno: %d.", nError); } else { _ClearSendBuffer(); SOCKET_IO_ERROR("send ssl data error, errno: %d.", nError); DoException(GetSocketID(), SOCKET_IO_SSL_SEND_FAILED); } } else if (nRet == 0) { int32_t nError = SSL_get_error(GetSSL(), nRet); if (SSL_ERROR_ZERO_RETURN == nError) { SOCKET_IO_WARN("send ssl data error, peer closed."); } else { SOCKET_IO_ERROR("send ssl data error, errno: %d.", nError); } _ClearSendBuffer(); DoException(GetSocketID(), SOCKET_IO_SSL_SEND_FAILED); } else if (nRet != pBufferLoop->GetWriteOffset()) { //将未成功的数据重新放置buffer loop中,待下次发送 //对于ssl来说,应该不会出现此种情况 int32_t nSize = 0; pBufferLoop->Read(NULL, nRet); SOCKET_IO_WARN("send ssl data, send size: %d, less than %d.", nRet, pBufferLoop->GetWriteOffset()); } else { SOCKET_IO_INFO("send ssl data from buffer successed."); m_sendqueuemutex.Lock(); delete pBufferLoop; pBufferLoop = NULL; m_sendqueue.pop(); m_sendqueuemutex.Unlock(); } return nErrorCode; }
int32_t CSSLClientAsync::SendMsgAsync(const char *szBuf, int32_t nBufSize) { CSimpleBuffer* pBufferLoop = new CSimpleBuffer(); pBufferLoop->Write(szBuf, nBufSize); m_sendqueuemutex.Lock(); if (m_sendqueue.size() != 0) { if (_GetWaitForCloseStatus() == TRUE) { SOCKET_IO_DEBUG("send ssl data error, socket will be closed."); delete pBufferLoop; pBufferLoop = NULL; } else { if (m_sendqueue.size() >= MAX_SEND_QUEUE_SIZE) { SOCKET_IO_WARN("send ssl data error, buffer is overload."); delete pBufferLoop; pBufferLoop = NULL; } else { SOCKET_IO_INFO("send ssl data, push data to buffer."); m_sendqueue.push(pBufferLoop); //m_pio->Add_WriteEvent(this); } } m_sendqueuemutex.Unlock(); return SOCKET_IO_RESULT_OK; } m_sendqueuemutex.Unlock(); int32_t nRet = SSL_write(GetSSL(), (void*)pBufferLoop->GetBuffer(), pBufferLoop->GetWriteOffset()); if ( nRet < 0) { int32_t nError = SSL_get_error(GetSSL(), nRet); if (SSL_ERROR_WANT_WRITE == nError || SSL_ERROR_WANT_READ == nError) { m_sendqueuemutex.Lock(); m_sendqueue.push(pBufferLoop); m_sendqueuemutex.Unlock(); //有数据放入待发送队列,则注册为写事件 m_pio->Add_WriteEvent(this); SOCKET_IO_INFO("send ssl data, buffer is blocking, errno: %d.", nError); } else { delete pBufferLoop; pBufferLoop = NULL; SOCKET_IO_ERROR("send ssl data error, errno: %d.", nError); DoException(GetSocketID(), SOCKET_IO_SSL_SEND_FAILED); } } else if (nRet == 0) { int32_t nError = SSL_get_error(GetSSL(), nRet); if (SSL_ERROR_ZERO_RETURN == nError) { SOCKET_IO_WARN("send ssl data error, peer closed."); } else { SOCKET_IO_ERROR("send ssl data error, errno: %d.", nError); } delete pBufferLoop; pBufferLoop = NULL; DoException(GetSocketID(), SOCKET_IO_SSL_SEND_FAILED); } else if (nRet != nBufSize) { int32_t nRest = nBufSize - nRet; pBufferLoop->Read(NULL, nRet); m_sendqueuemutex.Lock(); m_sendqueue.push(pBufferLoop); m_sendqueuemutex.Unlock(); //有数据放入待发送队列,则注册为写事件 //对于ssl来说,应该不会出现此种情况 m_pio->Add_WriteEvent(this); SOCKET_IO_WARN("send ssl data, send size: %d, less than %d.", nRet, nBufSize); } else if (nRet == nBufSize) { delete pBufferLoop; pBufferLoop = NULL; SOCKET_IO_DEBUG("send ssl data successed."); } else { delete pBufferLoop; pBufferLoop = NULL; } return SOCKET_IO_RESULT_OK; }