hey people i need some help with a winsock thing

the problem is i have multiple connections (i think it's called multi-threading) like a webserver has
but only one global data buffer thing
when data comes in on seperate connections, it all goes into the one buffer, should it be like that ?
i want to be able to receive a certain amout of data in each seperate buffer (one seperate buffer for every connection),
and then later process the data whole (eg recv a whole file, then work out what to do with it)
how would i do that ?

also another thing i was wondering, if i don't get all the data in one recv (ie 1024 bytes being sent, but only say 512 bytes are received into buffer) does that ever happen ? and if so what should i do ?

i'm new to asm, so these are probably really stupid questions but any help would be much appreciated
thanks
Posted on 2004-02-05 06:21:31 by someone
Heysaho.

If you're using CreateThread, you're doing multithreading - otherwise you're not :). However, if you're following Iczelion's tutorials with async sockets (those posting windows messages), you'll probably have to handle multiple concurrent connections anyway.

You'll need to have a buffer per connection. You will probably need some state information per connection anyway, so figure out a way to associate your sockets with some information (some array will probably do). You can either recv directly into the per-connection buffer, or you can use a smal local variable and copy to the buffer later - your choice. Anyway be sure to be paranoid about length checking so you don't get buffer overflows...

Yes, you will have to gracefully handle not getting all the bytes you ask for on a recv, or sending on a send. This is the rule rather than the exception. Have a look at www.madwizard.org , Thomas made some nice texts about socket programming.
Posted on 2004-02-05 06:52:58 by f0dder
hey
i'm not using create thread, it's the posting messages way.

i tried using a local variable, but when i recv data it terminates the app with errors and stuff, could it be something to do with using addr instead of offset ? or the way i declared the variable (LOCAL dwBuff:DWORD)

also if i use a small local buffer and copy it to the main buffer later, whats the best way to copy it ? (probably a stupid question, but i can't work it out)

also how do i create an array, i havn't worked with arrays in asm yet, so it's all new to me

thanks
Posted on 2004-02-05 07:42:04 by someone
If you have a "LOCAL dwBuff:DWORD" and use "addr dwBuff", you will only be able to recv four bytes :) - if you have used HeapAlloc (or, grrr, GlobalAlloc) to allocate memory, and stored the pointer in dwBuff, pass "" to recv, not "addr dwBuff".

You can use just about any method of copying the local buffer to a larger buffer, the data amounts you will be dealing with are small, so even a "rep movsb" will be fine. Then again, there probably isn't much point in not recv'ing directly to the correct buffer.
Posted on 2004-02-05 08:07:45 by f0dder
ok, i'll try using HeapAlloc, and passing ""
also if i recv directly into the larger buffer (with data already in it), will it overwrite what i've already got in that buffer, or will it add on to the end of it
Posted on 2004-02-05 08:18:14 by someone
it will overwrite, so you should not just pass the buffer, but add keep adjusting the offset to recv to based on how many bytes you have received - and be sure that you're not overflowing :p
Posted on 2004-02-05 08:20:29 by f0dder
ok i'm stuck again, having trouble with HeapAlloc

i've got the local variables:

LOCAL dwAvalData:DWORD
LOCAL hMem:DWORD
LOCAL szBuff:DWORD
LOCAL dwDataRead:DWORD

and then the code:

invoke HeapAlloc, szBuff, HEAP_ZERO_MEMORY, dwAvalData
mov hMem, eax
invoke HeapLock, hMem
mov szBuff, eax
invoke recv, wParam, szBuff, dwAvalData, 0
mov dwDataRead, eax
invoke MessageBox, 0, addr szBuff, 0, 0
invoke HeapUnlock, hMem
invoke HeapFree, szBuff, 0, hMem

i'm pretty sure i did the whole allocating memory thing wrong, any ideas ?
Posted on 2004-02-05 08:47:16 by someone

invoke GetProcessHeap
invoke HeapAlloc, eax, HEAP_ZERO_MEMORY, how_many_bytes_I_want
mov , eax

- too tired and busy to help you with the rest right now, sorry :)
Posted on 2004-02-05 09:06:52 by f0dder
thats fine, you've already helped heaps, thanks
Posted on 2004-02-05 23:48:09 by someone
Here's the malloc macro from Ultrano's ATC oop support:

malloc_func proc uses ebx ecx edx esi edi How
ifndef HEAP1
.data
HEAP1 dd 0
.code
endif
.if !HEAP1
invoke HeapCreate,0,10000,0
mov HEAP1,eax
.endif
invoke HeapAlloc,HEAP1,HEAP_ZERO_MEMORY,How
ret
malloc_func endp

malloc MACRO How:REQ
invoke malloc_func,How
exitm <eax>
ENDM

ok then, usage:
include this in your asm source, and use it like this...

mov pMem, malloc (cbSize)

later, to free a block of memory, you'll need this macro:
free_func proc uses eax ebx ecx edx esi edi What
invoke HeapFree,HEAP1,0,What
ret
free_func endp

free MACRO What
invoke free_func,What
ENDM

which is as simple to use as:
free pMem

Have a nice day :)


ps You won't need to destroy the Heap created by the malloc macro, it'll be destroyed with the process which created it.
Posted on 2004-02-06 08:00:10 by Homer
cool, thanks heaps
Posted on 2004-02-07 06:05:05 by someone