void CUdpServer::HandleZeroBytes(CONNID dwConnID, TUdpBufferObj* pBufferObj) { if(pBufferObj->operation == SO_RECEIVE) { if(dwConnID == 0) dwConnID = HandleAccept(pBufferObj); if(dwConnID != 0) { TRACE("<S-CNNID: %Iu> recv 0 bytes (detect package)\n", dwConnID); TUdpSocketObj* pSocketObj = FindSocketObj(dwConnID); if(TUdpSocketObj::IsValid(pSocketObj)) { pSocketObj->detectFails = 0; SendDetectPackage(dwConnID, pSocketObj); } } DoReceive(pBufferObj); } else VERIFY(FALSE); }
void CTcpServer::HandleReceive(CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj) { if(m_bMarkSilence) pSocketObj->activeTime = ::TimeGetTime(); EnHandleResult hr = TriggerFireReceive(pSocketObj, pBufferObj); if(hr == HR_OK || hr == HR_IGNORE) { if(ContinueReceive(pSocketObj, pBufferObj, hr)) { { CSpinLock locallock(pSocketObj->sgPause); pSocketObj->recving = FALSE; } DoReceive(pSocketObj, pBufferObj); } } if(hr == HR_CLOSED) { AddFreeBufferObj(pBufferObj); } else if(hr == HR_ERROR) { TRACE("<S-CNNID: %Iu> OnReceive() event return 'HR_ERROR', connection will be closed !\n", dwConnID); AddFreeSocketObj(pSocketObj, SCF_ERROR, SO_RECEIVE, ENSURE_ERROR_CANCELLED); AddFreeBufferObj(pBufferObj); } }
void CUdpServer::HandleZeroBytes(CONNID dwConnID, TUdpBufferObj* pBufferObj) { if(pBufferObj->operation == SO_RECEIVE) { if(dwConnID == 0) dwConnID = HandleAccept(pBufferObj); if(dwConnID != 0) { TUdpSocketObj* pSocketObj = FindSocketObj(dwConnID); if(TUdpSocketObj::IsValid(pSocketObj)) { pSocketObj->detectFails = 0; DoSend(pSocketObj, nullptr, 0); } TRACE("<S-CNNID: %Iu> recv 0 bytes (detect package)\n", dwConnID); } DoReceive(pBufferObj); } else { ASSERT(dwConnID != 0); TRACE("<S-CNNID: %Iu> send 0 bytes (detect package)\n", dwConnID); } }
DWORD CTcpAgent::ConnectToServer(CONNID dwConnID, SOCKET& soClient, LPCTSTR pszRemoteAddress, USHORT usPort) { TCHAR szAddress[40]; int iAddressLen = sizeof(szAddress) / sizeof(TCHAR); if(!::GetIPAddress(pszRemoteAddress, szAddress, iAddressLen)) return ERROR_INVALID_ADDRESS; SOCKADDR_IN addr; if(!::sockaddr_A_2_IN(AF_INET, szAddress, usPort, addr)) return ERROR_INVALID_ADDRESS; TBufferObj* pBufferObj = GetFreeBufferObj(); TSocketObj* pSocketObj = GetFreeSocketObj(dwConnID, soClient); memcpy(&pSocketObj->remoteAddr, &addr, sizeof(SOCKADDR_IN)); AddClientSocketObj(dwConnID, pSocketObj); DWORD result = NO_ERROR; if(m_bAsyncConnect) { if(::CreateIoCompletionPort((HANDLE)soClient, m_hCompletePort, (ULONG_PTR)pSocketObj, 0)) result = ::PostConnect(m_pfnConnectEx, soClient, addr, pBufferObj); else result = ::GetLastError(); } else { if(::connect(soClient, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN)) != SOCKET_ERROR) { if(::CreateIoCompletionPort((HANDLE)soClient, m_hCompletePort, (ULONG_PTR)pSocketObj, 0)) { BOOL bOnOff = (m_dwKeepAliveTime > 0 && m_dwKeepAliveInterval > 0); VERIFY(::SSO_KeepAliveVals(pSocketObj->socket, bOnOff, m_dwKeepAliveTime, m_dwKeepAliveInterval) == NO_ERROR); if(FireConnect(dwConnID) != HR_ERROR) result = DoReceive(dwConnID, pSocketObj, pBufferObj); else result = ERROR_FUNCTION_FAILED; } else result = ::GetLastError(); } else result = ::WSAGetLastError(); } if(result != NO_ERROR) { AddFreeSocketObj(dwConnID, SCF_NONE); AddFreeBufferObj(pBufferObj); soClient = INVALID_SOCKET; } return result; }
void CUdpServer::HandleError(CONNID dwConnID, TUdpBufferObj* pBufferObj, DWORD dwErrorCode) { if(dwConnID != 0) AddFreeSocketObj(dwConnID, SCF_ERROR, pBufferObj->operation, dwErrorCode); if(pBufferObj->operation == SO_RECEIVE) DoReceive(pBufferObj); else AddFreeBufferObj(pBufferObj); }
void CTcpAgent::HandleReceive(CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj) { if(FireReceive(dwConnID, (BYTE*)pBufferObj->buff.buf, pBufferObj->buff.len) != HR_ERROR) DoReceive(dwConnID, pSocketObj, pBufferObj); else { TRACE("<A-CNNID: %Iu> OnReceive() event return 'HR_ERROR', connection will be closed !\n", dwConnID); AddFreeSocketObj(dwConnID, SCF_ERROR, SO_RECEIVE, ERROR_FUNCTION_FAILED); AddFreeBufferObj(pBufferObj); } }
void Run() { for (uint8_t i = 1; i <= boost::thread::hardware_concurrency(); ++i) { threads_.create_thread([&]() { io_service_.run(); }); } boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::udp::v4(), 5354); socket_.open(endpoint.protocol()); socket_.bind(endpoint); DoReceive(); }
void CTcpAgent::HandleConnect(CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj) { ::SSO_UpdateConnectContext(pBufferObj->client, 0); if(TriggerFireConnect(pSocketObj) != HR_ERROR) DoReceive(dwConnID, pSocketObj, pBufferObj); else { AddFreeSocketObj(pSocketObj, SCF_NONE); AddFreeBufferObj(pBufferObj); } }
BOOL CConnection::HandleRecvEvent(UINT32 dwBytes) { #ifdef WIN32 m_dwDataLen += dwBytes; ExtractBuffer(); if (!DoReceive()) { return FALSE; } #else if (!DoReceive()) { return FALSE; } ExtractBuffer(); #endif return TRUE; }
void CIocpServer::HandleReceive(TSocketObj* pSocketObj, TBufferObj* pBufferObj) { //处理接收数据 处理完成后重新投递 if(FireReceive(pSocketObj->connID, (BYTE*)pBufferObj->buff.buf, pBufferObj->buff.len) != ISocketListener::HR_ERROR) DoReceive(pSocketObj, pBufferObj); else { TRACE1("<CNNID: %d> FireReceive() return HR_ERROR, connection will be closed", pSocketObj->connID); FireError(pSocketObj->connID, SO_RECEIVE, WSAEPROTOTYPE); AddFreeSocketObj(pSocketObj->connID); AddFreeBufferObj(pBufferObj); } }
void CTcpServer::HandleAccept(SOCKET soListen, TBufferObj* pBufferObj) { VERIFY(::PostIocpAccept(m_hCompletePort)); int iLocalSockaddrLen; int iRemoteSockaddrLen; HP_PSOCKADDR pLocalSockAddr; HP_PSOCKADDR pRemoteSockAddr; int iAddrLen = HP_SOCKADDR::AddrSize(m_usFamily) + 16; m_pfnGetAcceptExSockaddrs ( pBufferObj->buff.buf, 0, iAddrLen, iAddrLen, (SOCKADDR**)&pLocalSockAddr, &iLocalSockaddrLen, (SOCKADDR**)&pRemoteSockAddr, &iRemoteSockaddrLen ); CONNID dwConnID = 0; SOCKET socket = pBufferObj->client; if(!HasStarted() || !m_bfActiveSockets.AcquireLock(dwConnID)) { ::ManualCloseSocket(socket, SD_BOTH); AddFreeBufferObj(pBufferObj); return; } TSocketObj* pSocketObj = GetFreeSocketObj(dwConnID, socket); pRemoteSockAddr->Copy(pSocketObj->remoteAddr); AddClientSocketObj(dwConnID, pSocketObj); ::SSO_UpdateAcceptContext(socket, soListen); ::CreateIoCompletionPort((HANDLE)socket, m_hCompletePort, (ULONG_PTR)pSocketObj, 0); if(TriggerFireAccept(pSocketObj) != HR_ERROR) DoReceive(pSocketObj, pBufferObj); else { AddFreeSocketObj(pSocketObj, SCF_NONE); AddFreeBufferObj(pBufferObj); } }
void CHttpControlSocket::OnClose(int error) { LogMessage(Debug_Verbose, _T("CHttpControlSocket::OnClose(%d)"), error); if (error) { LogMessage(::Error, _("Disconnected from server: %s"), CSocket::GetErrorDescription(error).c_str()); ResetOperation(FZ_REPLY_ERROR | FZ_REPLY_DISCONNECTED); return; } if (DoReceive() == FZ_REPLY_REDIRECTED) return; // Socket got closed already // HTTP socket isn't connected outside operations if (!m_pCurOpData) return; if (m_pCurOpData->pNextOpData) { ResetOperation(FZ_REPLY_ERROR | FZ_REPLY_DISCONNECTED); return; } if (!m_pHttpOpData->m_gotHeader) { ResetOperation(FZ_REPLY_ERROR | FZ_REPLY_DISCONNECTED); return; } if (m_pHttpOpData->m_transferEncoding == CHttpOpData::chunked) { if (!m_pHttpOpData->m_chunkData.getTrailer) { ResetOperation(FZ_REPLY_ERROR | FZ_REPLY_DISCONNECTED); return; } } else { if (m_pHttpOpData->m_totalSize != -1 && m_pHttpOpData->m_receivedData != m_pHttpOpData->m_totalSize) { ResetOperation(FZ_REPLY_ERROR | FZ_REPLY_DISCONNECTED); return; } } ProcessData(0, 0); }
void CIocpServer::HandleAccept(SOCKET soListen, TBufferObj* pBufferObj) { DeleteAcceptSocket(pBufferObj->client); int iLocalSockaddrLen; int iRemoteSockaddrLen; SOCKADDR* pLocalSockAddr; SOCKADDR* pRemoteSockAddr; //将AcceptEx接受的第一块数据中的本地和远程机器的地址返回给用户 m_pfnGetAcceptExSockaddrs ( pBufferObj->buff.buf, 0, sizeof(SOCKADDR_IN) + 16, sizeof(SOCKADDR_IN) + 16, (SOCKADDR **)&pLocalSockAddr, &iLocalSockaddrLen, (SOCKADDR **)&pRemoteSockAddr, &iRemoteSockaddrLen ); TSocketObj* pSocketObj = GetFreeSocketObj(); pSocketObj->socket = pBufferObj->client; pSocketObj->connID = ::InterlockedIncrement((LONG*)&m_dwConnID); //InterlockedIncrement 对m_dwConnID加1 在对m_dwConnID访问的时候其他线程不能访问这个变量 memcpy(&pSocketObj->clientAddr, pRemoteSockAddr, sizeof(SOCKADDR_IN)); //远程机器的地址 AddClientSocketObj(pSocketObj->connID, pSocketObj); int result = 0; result = ::SSO_UpdateAcceptContext(pSocketObj->socket, soListen); ASSERT(result == 0); if(m_dwKeepAliveTimes > 0 && m_dwKeepAliveInterval > 0) { result = ::SSO_KeepAliveVals(pSocketObj->socket, TRUE, m_dwKeepAliveTimes, m_dwKeepAliveInterval); ASSERT(result == 0); } //result = ::SSO_NoDelay(pSocketObj->socket, TRUE); //ASSERT(result == 0); VERIFY(::CreateIoCompletionPort((HANDLE)pSocketObj->socket, m_hCompletePort, (ULONG_PTR)pSocketObj, 0)); FireAccept(pSocketObj->connID); //打印连接 //接受连接成功后 投递一个接收 DoReceive(pSocketObj, pBufferObj); }
void CTcpAgent::HandleConnect(CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj) { VERIFY(::SSO_UpdateConnectContext(pSocketObj->socket, 0) == NO_ERROR); BOOL bOnOff = (m_dwKeepAliveTime > 0 && m_dwKeepAliveInterval > 0); VERIFY(::SSO_KeepAliveVals(pSocketObj->socket, bOnOff, m_dwKeepAliveTime, m_dwKeepAliveInterval) == NO_ERROR); if(FireConnect(dwConnID) != HR_ERROR) DoReceive(dwConnID, pSocketObj, pBufferObj); else { AddFreeSocketObj(dwConnID, SCF_NONE); AddFreeBufferObj(pBufferObj); } }
void CUdpServer::HandleReceive(CONNID dwConnID, TUdpBufferObj* pBufferObj) { if(dwConnID == 0) dwConnID = HandleAccept(pBufferObj); if(dwConnID != 0) { if(FireReceive(dwConnID, (BYTE*)pBufferObj->buff.buf, pBufferObj->buff.len) == ISocketListener::HR_ERROR) { TRACE1("<S-CNNID: %Iu> OnReceive() event return 'HR_ERROR', connection will be closed !\n", dwConnID); AddFreeSocketObj(dwConnID, SCF_ERROR, SO_RECEIVE, ERROR_FUNCTION_FAILED); } } DoReceive(pBufferObj); }
void CTcpServer::HandleReceive(CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj) { if(m_bMarkSilence) pSocketObj->activeTime = ::TimeGetTime(); EnHandleResult hr = TriggerFireReceive(pSocketObj, pBufferObj); if(hr == HR_OK || hr == HR_IGNORE) DoReceive(dwConnID, pSocketObj, pBufferObj); else if(hr == HR_CLOSED) { AddFreeBufferObj(pBufferObj); } else { AddFreeSocketObj(pSocketObj, SCF_ERROR, SO_RECEIVE, ERROR_CANCELLED); AddFreeBufferObj(pBufferObj); } }
void CTcpAgent::HandleReceive(CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj) { if(m_bMarkSilence) pSocketObj->activeTime = ::TimeGetTime(); EnHandleResult hr = TriggerFireReceive(pSocketObj, pBufferObj); if(hr == HR_OK || hr == HR_IGNORE) DoReceive(dwConnID, pSocketObj, pBufferObj); else if(hr == HR_CLOSED) { AddFreeBufferObj(pBufferObj); } else { TRACE("<A-CNNID: %Iu> OnReceive() event return 'HR_ERROR', connection will be closed !\n", dwConnID); AddFreeSocketObj(pSocketObj, SCF_ERROR, SO_RECEIVE, ERROR_CANCELLED); AddFreeBufferObj(pBufferObj); } }
void OneBufferSentHandler( std::shared_ptr<boost::asio::ip::udp::endpoint> p_endpoint, std::shared_ptr<size_t> p_size, const boost::system::error_code& ec, size_t length) { if (ec.value() == ssf::error::message_too_long) { { boost::recursive_mutex::scoped_lock lock(one_buffer_mutex_); one_buffer_.resize(one_buffer_.size() / 2); } if (one_buffer_.size()) { SizeReceivedHandler(p_endpoint, p_size, boost::system::error_code(), 0); return; } } else { DoReceive(); return; } }
void CTcpServer::HandleReceive(CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj) { EnHandleResult hr = FireReceive(pSocketObj, (BYTE*)pBufferObj->buff.buf, pBufferObj->buff.len); if(hr == HR_OK || hr == HR_IGNORE) DoReceive(dwConnID, pSocketObj, pBufferObj); else if(hr == HR_CLOSED) { ASSERT(m_enRecvPolicy == RP_SERIAL); AddFreeBufferObj(pBufferObj); } else { TRACE("<S-CNNID: %Iu> OnReceive() event return 'HR_ERROR', connection will be closed !\n", dwConnID); AddFreeSocketObj(pSocketObj, SCF_ERROR, SO_RECEIVE, ERROR_CANCELLED); AddFreeBufferObj(pBufferObj); } }
void CTcpServer::HandleAccept(SOCKET soListen, TBufferObj* pBufferObj) { ::PostQueuedCompletionStatus(m_hCompletePort, IOCP_SI_ACCEPT, 0, nullptr); int iLocalSockaddrLen; int iRemoteSockaddrLen; SOCKADDR* pLocalSockAddr; SOCKADDR* pRemoteSockAddr; m_pfnGetAcceptExSockaddrs ( pBufferObj->buff.buf, 0, sizeof(SOCKADDR_IN) + 16, sizeof(SOCKADDR_IN) + 16, (SOCKADDR **)&pLocalSockAddr, &iLocalSockaddrLen, (SOCKADDR **)&pRemoteSockAddr, &iRemoteSockaddrLen ); SOCKET socket = pBufferObj->client; CONNID dwConnID = ::GenerateConnectionID(m_dwConnID); TSocketObj* pSocketObj = GetFreeSocketObj(dwConnID, socket); memcpy(&pSocketObj->clientAddr, pRemoteSockAddr, sizeof(SOCKADDR_IN)); AddClientSocketObj(dwConnID, pSocketObj); VERIFY(::SSO_UpdateAcceptContext(socket, soListen) == NO_ERROR); BOOL bOnOff = (m_dwKeepAliveTime > 0 && m_dwKeepAliveInterval > 0); VERIFY(::SSO_KeepAliveVals(socket, bOnOff, m_dwKeepAliveTime, m_dwKeepAliveInterval) == NO_ERROR); if(FireAccept(dwConnID, socket) != ISocketListener::HR_ERROR) { VERIFY(::CreateIoCompletionPort((HANDLE)socket, m_hCompletePort, (ULONG_PTR)pSocketObj, 0)); DoReceive(dwConnID, pSocketObj, pBufferObj); } else { AddFreeSocketObj(dwConnID, SCF_NONE); AddFreeBufferObj(pBufferObj); } }
int CTcpServer::DoUnpause(CONNID dwConnID) { TSocketObj* pSocketObj = FindSocketObj(dwConnID); if(!TSocketObj::IsValid(pSocketObj)) return ERROR_OBJECT_NOT_FOUND; EnHandleResult hr = BeforeUnpause(pSocketObj); if(hr != HR_OK && hr != HR_IGNORE) { if(hr == HR_ERROR) AddFreeSocketObj(pSocketObj, SCF_ERROR, SO_RECEIVE, ENSURE_ERROR_CANCELLED); return ERROR_CANCELLED; } return DoReceive(pSocketObj, GetFreeBufferObj()); }
void CTcpServer::HandleAccept(SOCKET soListen, TBufferObj* pBufferObj) { VERIFY(::PostIocpAccept(m_hCompletePort)); int iLocalSockaddrLen; int iRemoteSockaddrLen; SOCKADDR* pLocalSockAddr; SOCKADDR* pRemoteSockAddr; m_pfnGetAcceptExSockaddrs ( pBufferObj->buff.buf, 0, sizeof(SOCKADDR_IN) + 16, sizeof(SOCKADDR_IN) + 16, (SOCKADDR **)&pLocalSockAddr, &iLocalSockaddrLen, (SOCKADDR **)&pRemoteSockAddr, &iRemoteSockaddrLen ); SOCKET socket = pBufferObj->client; CONNID dwConnID = ::GenerateConnectionID(); TSocketObj* pSocketObj = GetFreeSocketObj(dwConnID, socket); memcpy(&pSocketObj->remoteAddr, pRemoteSockAddr, sizeof(SOCKADDR_IN)); AddClientSocketObj(dwConnID, pSocketObj); ::SSO_UpdateAcceptContext(socket, soListen); if(TriggerFireAccept(pSocketObj) != HR_ERROR) { ::CreateIoCompletionPort((HANDLE)socket, m_hCompletePort, (ULONG_PTR)pSocketObj, 0); DoReceive(dwConnID, pSocketObj, pBufferObj); } else { AddFreeSocketObj(pSocketObj, SCF_NONE); AddFreeBufferObj(pBufferObj); } }
// --------------------------------------------------------------------------- // From class CActive // Handles completion of receive. // --------------------------------------------------------------------------- // void CIkev1Receiver::RunL() { if ( iStatus.Int() == KErrNone ) { __ASSERT_DEBUG( iUdpData != NULL, User::Invariant() ); const ThdrISAKMP* ikeHdr = ThdrISAKMP::Ptr( iUdpData->Des() ); TInt msgLth = iUdpData->Length(); // Ignore possible <non-ESP marker> in the beginning of IKE message. TUint32 ikeMsgHdrOctets = GET32( ikeHdr ); if ( ikeMsgHdrOctets == NON_ESP_MARKER ) { ikeHdr = ikeHdr->GotoOffset( NON_ESP_MARKER_SIZE ); msgLth -= NON_ESP_MARKER_SIZE; } iCallback.IkeMsgReceivedL( *ikeHdr, iSrcAddr, iLocalPort ); } else { iReceiving = EFalse; iCallback.ReceiveError( iStatus.Int() ); } delete iUdpData; iUdpData = NULL; if ( iReceiving && iStatus.Int() == KErrNone ) { // Continue receiving. DoReceive(); } }
void CHttpControlSocket::OnReceive() { DoReceive(); }
BOOL CUdpServer::DoAccept() { TUdpBufferObj* pBufferObj = GetFreeBufferObj(); return (DoReceive(pBufferObj) == NO_ERROR); }
// --------------------------------------------------------------------------- // Starts receive. // --------------------------------------------------------------------------- // void CIkev1Receiver::StartReceive() { iReceiving = ETrue; DoReceive(); }
BOOL CUdpServer::DoAccept() { TUdpBufferObj* pBufferObj = GetFreeBufferObj(); return DoReceive(pBufferObj); }
DWORD CTcpAgent::ConnectToServer(CONNID dwConnID, SOCKET& soClient, LPCTSTR lpszRemoteAddress, USHORT usPort) { TCHAR szAddress[40]; int iAddressLen = sizeof(szAddress) / sizeof(TCHAR); if(!::GetIPAddress(lpszRemoteAddress, szAddress, iAddressLen)) return WSAEADDRNOTAVAIL; SOCKADDR_IN addr; if(!::sockaddr_A_2_IN(AF_INET, szAddress, usPort, addr)) return WSAEADDRNOTAVAIL; TBufferObj* pBufferObj = GetFreeBufferObj(); TSocketObj* pSocketObj = GetFreeSocketObj(dwConnID, soClient); memcpy(&pSocketObj->remoteAddr, &addr, sizeof(SOCKADDR_IN)); AddClientSocketObj(dwConnID, pSocketObj); DWORD result = NO_ERROR; BOOL bNeedFree = TRUE; if(m_bAsyncConnect) { if(::CreateIoCompletionPort((HANDLE)soClient, m_hCompletePort, (ULONG_PTR)pSocketObj, 0)) { result = DoConnect(dwConnID, pSocketObj, pBufferObj); bNeedFree = FALSE; } else result = ::GetLastError(); } else { if(::connect(soClient, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN)) != SOCKET_ERROR) { if(::CreateIoCompletionPort((HANDLE)soClient, m_hCompletePort, (ULONG_PTR)pSocketObj, 0)) { if(TriggerFireConnect(pSocketObj) != HR_ERROR) { result = DoReceive(dwConnID, pSocketObj, pBufferObj); bNeedFree = FALSE; } else result = ERROR_CANCELLED; } else result = ::GetLastError(); } else result = ::WSAGetLastError(); } if(result != NO_ERROR) { if(bNeedFree) { AddFreeSocketObj(pSocketObj, SCF_NONE); AddFreeBufferObj(pBufferObj); } soClient = INVALID_SOCKET; } return result; }