Send/Recieve Functions Input Requested (they are written)

Started by Max_Power, January 16, 2007, 06:53:57 AM

Previous topic - Next topic

Max_Power

This code is on the receiving end:


DownloadFile proc uses esi nLen:DWORD
LOCAL lpData:DWORD
LOCAL nChunks:DWORD
LOCAL nLenFinalChunk:DWORD
LOCAL dwSocket:DWORD
LOCAL nDataLen:DWORD

lea esi,sckClients
invoke SendMessage,hClientList,LB_GETCURSEL,0,0
.IF eax==LB_ERR
ret
.ELSE
mov eax,[esi+4*eax]
mov dwSocket,eax
.ENDIF

;Calculate the required number of chunks
xor edx,edx
mov eax,nLen
mov ecx,MAX_CHUNK_SIZE
div ecx

.IF edx!=0
mov nLenFinalChunk,edx
inc eax
.ELSE
mov nLenFinalChunk,MAX_CHUNK_SIZE
.ENDIF

mov nChunks,eax

;Get all of the chunks
mov lpData,alloc(nLen)
mov esi,eax
xor ecx,ecx
.WHILE ecx<nChunks
push ecx

invoke ioctlsocket,dwSocket,FIONREAD,ADDR nDataLen
mov eax,nDataLen
.IF eax>=nLenFinalChunk
inc ecx
.IF ecx==nChunks
invoke recv,dwSocket,esi,nLenFinalChunk,0
.ELSE
invoke recv,dwSocket,esi,MAX_CHUNK_SIZE,0
.ENDIF
pop ecx

.IF eax!=SOCKET_ERROR
inc ecx
add esi,MAX_CHUNK_SIZE
.ELSE
free lpData
ret
.ENDIF

.ELSE
invoke Sleep,100
pop ecx

.ENDIF
.ENDW

;Write the chunks to a file
mov esi,FUNC(CreateFile,ADDR szTarget,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL)
invoke WriteFile,esi,lpData,nLen,ADDR nLen,NULL
invoke CloseHandle,esi

free lpData

ret
DownloadFile endp


This code is on the ending end:


SendFile proc uses esi edi lpszFile:DWORD
LOCAL nLen:DWORD
LOCAL hFile:DWORD
LOCAL lpData:DWORD
LOCAL nChunks:DWORD
LOCAL nLenFinalChunk:DWORD

mov hFile,FUNC(CreateFile,lpszFile,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL)
.IF eax!=INVALID_HANDLE_VALUE
;Let the reciever know how big the file is
mov nLen,FUNC(GetFileSize,hFile,NULL)
mov lpData,alloc(eax)
mov esi,lpData
invoke wsprintf,esi,ADDR szComUp,nLen
invoke SendCommand,esi

;Calculate the required number of chunks
xor edx,edx
mov eax,nLen
mov ecx,MAX_CHUNK_SIZE
div ecx

;Calculate the size of the last chunk
.IF edx!=0
mov nLenFinalChunk,edx
inc eax
.ELSE
mov nLenFinalChunk,MAX_CHUNK_SIZE
.ENDIF

mov nChunks,eax

;Read the entire file
invoke ReadFile,hFile,esi,nLen,ADDR nLen,NULL
invoke CloseHandle,hFile

;If we don't make the sockets block here
;and in the server we will get a lot of
;notifications and windows will decide
;to kill the connections
mov nLen,0
invoke WSAAsyncSelect,dwSocket,hMainWindow,0,0
invoke ioctlsocket,dwSocket,FIONBIO,ADDR nLen

;Send all of the chunks
xor ecx,ecx
.WHILE ecx<nChunks
push ecx
inc ecx
.IF ecx==nChunks
invoke send,dwSocket,esi,nLenFinalChunk,0
.ELSE
invoke send,dwSocket,esi,MAX_CHUNK_SIZE,0
.ENDIF
pop ecx

.IF eax!=SOCKET_ERROR
inc ecx
add esi,MAX_CHUNK_SIZE
.ELSE
free lpData
ret
.ENDIF
.ENDW
.ENDIF

free lpData

invoke WSAAsyncSelect,dwSocket,hMainWindow,WM_SOCKET,FD_READ or FD_CLOSE

ret
SendFile endp


They seem to be really hit or miss. Sometimes they work fine, other times they don't for no explainable hardware reason. Any input?