HRESULT CServer::OnDataRecieved() { if (!m_pSocketServer) { return Error(L"Server hasn't been initialised - programming error!"); } if (!m_pIDataInit || !m_pIData) { return Error(L"Internal error: failed to create Data object"); } CCOMSocketServer::Socket *pSocket = m_pSocketServer->GetSocket(); CIOBuffer *pBuffer = m_pSocketServer->GetBuffer(); if (!pSocket || !pBuffer) { return Error(L"Internal error: pSocket or pBuffer is 0"); } ISocket *pISocket = reinterpret_cast<ISocket*>(pSocket->GetUserPtr()); HRESULT hr = m_pIDataInit->Init(pBuffer->GetBuffer(), pBuffer->GetUsed()); if (SUCCEEDED(hr)) { Fire_OnDataReceived(pISocket, m_pIData); // this can stall if the handler doesnt return - // the ATL implementation is difficult to multi thread... } return hr; }
void CSocketClient::Write( const char *pData, size_t dataLength ) { if ( INVALID_SOCKET != m_connectSocket && dataLength > 0 && pData ) { CIOBuffer *pBuffer = Allocate(); /* * Call to unqualified virtual function */ PreWrite( pBuffer, pData, dataLength ); pBuffer->AddData( pData, dataLength ); /* * Begin to send data */ pBuffer->SetupWrite(); DWORD dwFlags = 0; DWORD dwSendNumBytes = 0; if ( SOCKET_ERROR == ::WSASend( m_connectSocket, pBuffer->GetWSABUF(), 1, &dwSendNumBytes, dwFlags, pBuffer, NULL) ) { DWORD lastError = ::WSAGetLastError(); if ( ERROR_IO_PENDING != lastError ) { Output( _T("CSocketClient::Write() - WSASend: ") + GetLastErrorMessage( lastError ) ); if ( lastError == WSAECONNABORTED || lastError == WSAECONNRESET || lastError == WSAEDISCON) { StopConnections(); } } } if ( pBuffer->GetUsed() != pBuffer->GetWSABUF()->len ) { /* * Call to unqualified virtual function */ //OnError(_T("CSocketClient::WriteCompleted - Socket write where not all data was written")); } pBuffer->Release(); } }