void CSSLClientAsync::_Close() { if (m_ssl) { UnInitSSL(); } if (GetSocket() != S_INVALID_SOCKET) { if (m_pio) { m_pio->Remove_Handler(this); } SetSSLConnectStatus(FALSE); S_CloseSocket(GetSocket()); SOCKET_IO_WARN("close ssl socket, sock %d, real sock: %d.", GetSocketID(), GetSocket()); m_socket = S_INVALID_SOCKET; DoClose(GetSocketID()); _ClearSendBuffer(); } }
/** @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; }