void CTcpPullClient::DoFetch(BYTE* pBuffer, int iLength) { int iIndex = 0; int iRemain = iLength; while(iRemain > 0) { CBufferPtr* pItem = m_pullBuffers->front(); int iItemSize = (int)pItem->Size(); int iCopySize = min(iItemSize, iRemain); memcpy(pBuffer + iIndex, pItem->Ptr(), iCopySize); if(iItemSize > iRemain) { int iLeft = iItemSize - iCopySize; memcpy(pItem->Ptr(), pItem->Ptr() + iCopySize, iLeft); pItem->Realloc(iLeft); } else { m_pullBuffers->pop_front(); delete pItem; } iRemain -= iCopySize; iIndex += iCopySize; } }
BOOL CClientSocket::DoSendData(CBufferPtr& sndData) { while(sndData.Size() > 0) { int rc = send(m_soClient, (char*)(BYTE*)sndData, min((int)sndData.Size(), (int)m_dwSocketBufferSize), 0); if(rc > 0) { if(FireSend(m_dwConnID, sndData, rc) == ISocketListener::HR_ERROR) { TRACE1("<CNNID: %Iu> OnSend() event should not return 'HR_ERROR' !!\n", m_dwConnID); ASSERT(FALSE); } int left = (int)sndData.Size() - rc; memcpy((BYTE*)sndData, (BYTE*)sndData + rc, left); sndData.Realloc(left); } else if(rc == SOCKET_ERROR) { int code = ::WSAGetLastError(); if(code == WSAEWOULDBLOCK) break; else { SetLastError(ISC_NETWORK_ERROR, _T(__FUNCTION__), code); FireError(m_dwConnID, SO_SEND, code); return FALSE; } } else ASSERT(FALSE); } return TRUE; }
void OnCmdStart(CCommandParser* pParser) { if(!s_vtClients.empty()) { ::LogClientStartFail(SE_ILLEGAL_STATE, ::HP_GetSocketErrorDesc(SE_ILLEGAL_STATE)); return; } s_stat.Reset(); for(DWORD i = 0; i < g_app_arg.conn_count; i++) { CSSLClientPtr pSocket(&s_listener); if(!pSocket->SetupSSLContext(g_c_iVerifyMode, g_c_lpszPemCertFile, g_c_lpszPemKeyFile, g_c_lpszKeyPasswod, g_c_lpszCAPemCertFileOrPath)) { int iErrCode = ::SYS_GetLastError(); ::LogClientStartFail(iErrCode, _T("initialize SSL env fail")); ClearHPSocketPtrSet(s_vtClients); EXIT(EX_CONFIG, iErrCode); } pSocket->SetKeepAliveTime(g_app_arg.keep_alive ? TCP_KEEPALIVE_TIME : 0); if(pSocket->Start(g_app_arg.remote_addr, g_app_arg.port, g_app_arg.async)) s_vtClients.push_back(pSocket.Detach()); else { ::LogClientStartFail(pSocket->GetLastError(), pSocket->GetLastErrorDesc()); ClearHPSocketPtrSet(s_vtClients); return; } } ::LogClientStart(g_app_arg.remote_addr, g_app_arg.port); DWORD dwSendDelay = 3; CString strMsg; strMsg.Format(_T("*** willing to send data after %d seconds ***"), dwSendDelay); ::LogMsg(strMsg); ::WaitFor(dwSendDelay * 500); while(true) { int iConnected = s_stat.GetConnected(); if(iConnected < 0) { ClearHPSocketPtrSet(s_vtClients); ::LogClientStartFail(ERROR_CONNREFUSED, ::HP_GetSocketErrorDesc(SE_CONNECT_SERVER)); return; } else if(iConnected == (int)g_app_arg.conn_count) break; ::WaitFor(3); } ::WaitFor(dwSendDelay * 500); s_sendBuffer.Malloc(g_app_arg.data_length, true); ::LogMsg(_T("*** Go Now ! ***")); s_stat.StartTest(); BOOL bTerminated = FALSE; for(DWORD i = 0; i < g_app_arg.test_times; i++) { for(DWORD j = 0; j < g_app_arg.conn_count; j++) { ITcpClient* pSocket = s_vtClients[j]; if(!pSocket->Send(s_sendBuffer, (int)s_sendBuffer.Size())) { ::LogClientSendFail(i + 1, j + 1, ::SYS_GetLastError(), ::HP_GetSocketErrorDesc(SE_DATA_SEND)); bTerminated = TRUE; break; } } if(bTerminated) break; if(g_app_arg.test_interval > 0 && i + 1 < g_app_arg.test_times) ::WaitFor(g_app_arg.test_interval); } if(bTerminated) ClearHPSocketPtrSet(s_vtClients); s_sendBuffer.Free(); }
En_HP_HandleResult CServerDlg::OnWSMessageComplete(HP_HttpServer pSender, HP_CONNID dwConnID) { ::PostOnWSMessageComplete(dwConnID, m_spThis->GetSenderName(pSender)); CBufferPtr* pBuffer = nullptr; ::HP_Server_GetConnectionExtra(pSender, dwConnID, (PVOID*)&pBuffer); VERIFY(pBuffer); BOOL bFinal; BYTE iReserved, iOperationCode; VERIFY(::HP_HttpServer_GetWSMessageState(pSender, dwConnID, &bFinal, &iReserved, &iOperationCode, nullptr, nullptr, nullptr)); ::HP_HttpServer_SendWSMessage(pSender, dwConnID, bFinal, iReserved, iOperationCode, nullptr, pBuffer->Ptr(), (int)pBuffer->Size(), 0); pBuffer->Free(); if(iOperationCode == 0x8) ::HP_Server_Disconnect(pSender, dwConnID, TRUE); return HR_OK; }
EnHandleResult CHttpServerListenerImpl::OnWSMessageComplete(IHttpServer* pSender, CONNID dwConnID) { ::PostOnWSMessageComplete(dwConnID, m_strName); CBufferPtr* pBuffer = nullptr; pSender->GetConnectionExtra(dwConnID, (PVOID*)&pBuffer); VERIFY(pBuffer); BOOL bFinal; BYTE iReserved, iOperationCode; VERIFY(pSender->GetWSMessageState(dwConnID, &bFinal, &iReserved, &iOperationCode, nullptr, nullptr, nullptr)); pSender->SendWSMessage(dwConnID, bFinal, iReserved, iOperationCode, nullptr, pBuffer->Ptr(), (int)pBuffer->Size()); pBuffer->Free(); if(iOperationCode == 0x8) pSender->Disconnect(dwConnID); return HR_OK; }