C++ IOCP windows??????
???????????? ???????[ 2014/9/3 11:42:19 ] ????????C++ NET ??????
/*******************
?????????
*******************/
void CMYIOCPServer::HandleMessage()
{
printf("thread is %d "??GetCurrentThreadId());
WorkItemBase *pCworkItem = NULL;
byte iCommand = m_byteMsg[0];
switch(iCommand)
{
case '0':
pCworkItem = new CWorkA();
break;
case '1':
pCworkItem = new CWorkB();
break;
case '2':
break;
default:
break;
}
//???????????????
if(NULL != pCworkItem)
{
m_CWorkQueue.InsertWorkItem(pCworkItem);
}
printf("Finish Handle Message ");
}
/**************************
?????????????????
**************************/
void CMYIOCPServer::SendMessage(SOCKET &tSOCKET??char MessAge[MessMaxLen])
{
// ????????????????????????????
WaitForSingleObject(m_hMutex??INFINITE);
send(tSOCKET?? MessAge?? MessMaxLen?? 0); // ???????
ReleaseMutex(m_hMutex);
}
/**************************
?????SOCKET???????????
?????????
**************************/
bool CMYIOCPServer::LoadWindowsSocket()
{
// ????socket????????
WORD wVersionRequested = MAKEWORD(2?? 2); // ????2.2?汾??WinSock??
WSADATA wsaData; // ????Windows Socket??????
DWORD err = WSAStartup(wVersionRequested?? &wsaData);
if (0 != err){ // ??????????????????
m_sError = "Request Windows Socket Library Error! ";
return false;
}
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2){// ????????????????汾????????
WSACleanup();
m_sError ="Request Windows Socket Version 2.2 Error! ";
system("pause");
return false;
}
// ????IOCP????????
/**
* ?????????????????
* HANDLE WINAPI CreateIoCompletionPort(
* __in HANDLE FileHandle?? // ??????????????????????????????????
* __in HANDLE ExistingCompletionPort?? // ????????IOCP???
* __in ULONG_PTR CompletionKey?? // ???????????????I/O????????????
* __in DWORD NumberOfConcurrentThreads // ????????????д????????????????CPU??????*2
* );
**/
m_completionPort = CreateIoCompletionPort( INVALID_HANDLE_VALUE?? NULL?? 0?? 0);
if (NULL == m_completionPort){ // ????IO?????????
m_sError ="CreateIoCompletionPort failed. Error: ";
return false;
}
// ????IOCP???--??????洴??????
// ??????????????????
SYSTEM_INFO mySysInfo;
GetSystemInfo(&mySysInfo);
// ?????????????????????????
for(DWORD i = 0; i < (mySysInfo.dwNumberOfProcessors * 2); ++i){
// ??????????????????????????????????????
HANDLE ThreadHandle = CreateThread(NULL?? 0?? &CMYIOCPServer::ServerWorkThread?? m_completionPort?? 0?? NULL);
if(NULL == ThreadHandle){
m_sError ="Create Thread Handle failed. Error:: ";
}
CloseHandle(ThreadHandle);
}
return true;
}
/*************************
???????????SOCKET???
*************************/
bool CMYIOCPServer::InitServerSocket()
{
// ????????????
m_srvSocket = socket(AF_INET?? SOCK_STREAM?? 0);
// ??SOCKET??????
SOCKADDR_IN srvAddr;
srvAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
srvAddr.sin_family = AF_INET;
srvAddr.sin_port = htons(m_iLisenPoint);
int bindResult = bind(m_srvSocket?? (SOCKADDR*)&srvAddr?? sizeof(SOCKADDR));
if(SOCKET_ERROR == bindResult){
m_sError = "Bind failed. Error:";
return false;
}
return true;
}
/**************************
???????????????????
**************************/
bool CMYIOCPServer::CreateServerSocker()
{
// ??SOCKET???????????
int listenResult = listen(m_srvSocket?? 10);
if(SOCKET_ERROR == listenResult){
m_sError = "Listen failed. Error: " ;
return false;
}
// ???????IO????
cout << "????????????????????????????????... ";
int icount = 0;
while(true){
PER_HANDLE_DATA * PerHandleData = NULL;
SOCKADDR_IN saRemote;
int RemoteLen;
SOCKET acceptSocket;
// ?????????????????????????????AcceptEx()
RemoteLen = sizeof(saRemote);
acceptSocket = accept(m_srvSocket?? (SOCKADDR*)&saRemote?? &RemoteLen);
if(SOCKET_ERROR == acceptSocket){ // ???????????
cerr << "Accept Socket Error: " << GetLastError() << endl;
m_sError = "Accept Socket Error: " ;
icount++;
if(icount > 50)
{
return false;
}
continue;
}
icount = 0;
// ?????????????????????????????????
PerHandleData = (LPPER_HANDLE_DATA)GlobalAlloc(GPTR?? sizeof(PER_HANDLE_DATA)); // ?????????PerHandleData?????????С?????
PerHandleData -> socket = acceptSocket;
memcpy (&PerHandleData -> ClientAddr?? &saRemote?? RemoteLen);
m_vclientGroup.push_back(PerHandleData); // ?????????????????????????????
// ????????????????????
CreateIoCompletionPort((HANDLE)(PerHandleData -> socket)?? m_completionPort?? (DWORD)PerHandleData?? 0);
// ??????????????????I/O??????I/O????
// ??????????????????????????
// WSARecv??WSASend??????ЩI/O???????????????????I/O??????????
// ??I/O????????(I/O???)
LPPER_IO_OPERATION_DATA PerIoData = NULL;
PerIoData = (LPPER_IO_OPERATION_DATA)GlobalAlloc(GPTR?? sizeof(PER_IO_OPERATEION_DATA));
ZeroMemory(&(PerIoData -> overlapped)?? sizeof(OVERLAPPED));
PerIoData->databuff.len = 1024;
PerIoData->databuff.buf = PerIoData->buffer;
PerIoData->operationType = 0; // read
DWORD RecvBytes;
DWORD Flags = 0;
WSARecv(PerHandleData->socket?? &(PerIoData->databuff)?? 1?? &RecvBytes?? &Flags?? &(PerIoData->overlapped)?? NULL);
}
//???????
DWORD dwByteTrans;
PostQueuedCompletionStatus(m_completionPort?? dwByteTrans?? 0?? 0);
closesocket(listenResult);
return true;
}
/*********************
?????????
*********************/
bool CMYIOCPServer::ServerSetUp()
{
if(false == LoadWindowsSocket())
{
return false;
}
if(false == InitServerSocket())
{
return false;
}
if(false == CreateServerSocker())
{
return false;
}
return true;
}
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
??C++????????????C++ lvalue??rvalueC++11????????C++???????????????C++?е?????????????????C++?????????C++???Windows????λ??C/C++???????????????????JAVA??C??C++??????????c++??python???????????????????????????????C++???????C++?е????????C++????????????????C++ ???????????????C++?????????????????????C++????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????