小弟今天研究了一下winsock 2.0中的overlapped io傳輸模型範例, 裡面的WSARecv()和
WSASend()函數都設定了同一個lpCompletionRoutine function
我的問題是, 假設我呼叫了WSASend()並設定 callback
lpCompletionRoutine function,
當該callback function被呼叫時應該是send的io動作全部完成之後吧, 有可能
WSASend()還沒全部傳完訊息callback function就被呼叫嗎?(比如WSASend該傳輸5byte
, 但只傳了3byte就呼叫callback)
假設不可能, 為何範例中的lpCompletionRoutine function
需要加上"上色"的那段code呢?
該function的內容大致如下:
void CALLBACK WorkerRoutine(DWORD Error, DWORD BytesTransferred,
LPWSAOVERLAPPED Overlapped, DWORD InFlags)
{
DWORD SendBytes, RecvBytes;
DWORD Flags;
LPSOCKET_INFORMATION SI;
//先作error handleing..省略
SI = (LPSOCKET_INFORMATION) Overlapped;
if (SI->BytesRECV == 0)
{
printf("SI->BytesRECV == 0\n");
SI->BytesRECV = BytesTransferred;
SI->BytesSEND = 0;
}
else
{
SI->BytesSEND += BytesTransferred;
}
if (SI->BytesRECV > SI->BytesSEND)
{
// Post another WSASend() request.
// Since WSASend() is not guaranteed to send
// all of the bytes requested,
// continue posting WSASend() calls until all received
// bytes are sent
ZeroMemory(&(SI->Overlapped), sizeof(WSAOVERLAPPED));
SI->DataBuf.buf = SI->Buffer + SI->BytesSEND;
SI->DataBuf.len = SI->BytesRECV - SI->BytesSEND;
//利用WSASend()送出訊息..省略
}
else
{
SI->BytesRECV = 0;
// Now that there are no more bytes to send post
// another WSARecv() request
Flags = 0;
ZeroMemory(&(SI->Overlapped), sizeof(WSAOVERLAPPED));
SI->DataBuf.len = DATA_BUFSIZE;
SI->DataBuf.buf = SI->Buffer;
//重新呼叫WSARecv()..省略
}
}
請各位先進給與指教, 謝謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.166.97.136
※ 編輯: hn12303158 來自: 218.166.97.136 (05/10 23:30)
※ 編輯: hn12303158 來自: 218.166.97.136 (05/10 23:31)
※ 編輯: hn12303158 來自: 218.166.97.136 (05/10 23:32)
※ 編輯: hn12303158 來自: 218.166.97.136 (05/10 23:33)
※ 編輯: hn12303158 來自: 218.166.97.136 (05/10 23:34)