如何将下面的C++函数改成写delphi 谢谢(100)

  • 主题发起人 主题发起人 edsionchen002
  • 开始时间 开始时间
E

edsionchen002

Unregistered / Unconfirmed
GUEST, unregistred user!
typedef struct { unsigned short usType; unsigned short usLength;} ST_PUSHHEAD;void handlePushRecv(SOCKET sock){ ST_PUSHHEAD stPushHead; // push header int nHeaderLen = sizeof(ST_PUSHHEAD); // push header length int nDataLen = 0; // recv data length int s_nReceived = 0; // static para int nType; // command type int nRead = 0; // counter of recv char char szRecvBuffer[MSG_MAXLEN] = ""; // recv buffer // begin recv || just recv a part of header if (s_nReceived < nHeaderLen) { nDataLen = 0; nRead = ::recv (sock, (char *) &stPushHead + s_nReceived, nHeaderLen - s_nReceived, 0); if (nRead <= 0) { // sock error occurs, must close if (GetLastError() != WSAEWOULDBLOCK) { s_nReceived = 0; return; } } s_nReceived += nRead; if (s_nReceived < nHeaderLen) return; nDataLen = ntohs(stPushHead.usLength); nType = ntohs(stPushHead.usType); } if (nDataLen > MSG_MAXLEN || nDataLen < 0) { s_nReceived = 0; return; } // just recv a part of whole pack if (s_nReceived < nHeaderLen + nDataLen) { nRead = ::recv (sock, szRecvBuffer + s_nReceived - nHeaderLen, nDataLen + nHeaderLen - s_nReceived, 0); if (nRead <= 0) { if (GetLastError() != WSAEWOULDBLOCK) { s_nReceived = 0; return; } } s_nReceived += nRead; } // recv whole pack if (s_nReceived == nHeaderLen + nDataLen) { // print message printf ("receive from server: [%d]%s/n", nType, szRecvBuffer); // reset param s_nReceived = 0; nDataLen = 0; memset(szRecvBuffer, 0, sizeof(MSG_MAXLEN)); }}
 
ST_PUSHHEAD=Record usType:Cardinal; usLength:Cardinal;end;procedure handlePushRecv(sock:TSOCKET)var stPushHead:ST_PUSHHEAD; nHeaderLen:integer; s_nReceived:integer ; nRead:integer; szRecvBuffer:array[0..MSG_MAXLEN]of char;begin nHeaderLen:= SizeOf(ST_PUSHHEAD); fillchar(szRecvBuffer,0,MSG_MAXLEN); if(s_nReceived< nHeaderLen) then begin ndataLen :=0 ; nread:=Recv(sock,(pchar)stPushHead + s_nReceived, nHeaderLen - s_nReceived, 0); if(nRead<=0 ) then begin // sock error occurs, must close if (GetLastError()<>WSAEWOULDBLOCK) then begin s_nReceived := 0; exit; end; end; s_nReceived :=s_nReceived+ nRead; if (s_nReceived < nHeaderLen) then exit; nDataLen := ntohs(stPushHead.usLength); nType := ntohs(stPushHead.usType); end; if (nDataLen > MSG_MAXLEN) or (nDataLen < 0) then begin s_nReceived := 0; exit; end; // just recv a part of whole pack if (s_nReceived < nHeaderLen + nDataLen) then begin nRead := recv (sock, szRecvBuffer + s_nReceived - nHeaderLen, nDataLen + nHeaderLen - s_nReceived, 0); if (nRead <= 0)then begin if (GetLastError() <> WSAEWOULDBLOCK) then begin s_nReceived = 0; exit; end; end; s_nReceived := nRead +s_nReceived; end; // recv whole pack if (s_nReceived = nHeaderLen + nDataLen) then begin // print message printf ("receive from server: [%d]%s/n", nType, szRecvBuffer); OutputDebugString(format('receive from server: [%d]%s '+#13, [nType, szRecvBuffer])); // reset param s_nReceived := 0; nDataLen := 0; fillchar(szRecvBuffer, 0, sizeof(MSG_MAXLEN)); end;end;
 

Similar threads

后退
顶部