Hi everybody, this text turned out to be rather long and maybe not so clear, please excuse me.
I'm currently using the winsock library to send and receive data in an application made with WinAPI, I'm not using MFC. I'm using asynchronous sockets (I think that's what it's called) which means that I will get a userdefined message posted in my message loop whenever there is data to read, a connection to accept etc. I'm sending my data using TCP.
Now I want to write a dynamic recv handler and by that I mean a function that can receive (almost) any amount of bytes and convert those bytes into something which my program can interpret. In this case the first 4 bytes are the type of the packet, the next 4 bytes are the size of the packet and the next size bytes are the actual data.
Because I don't want to allocate a lot of excessive memory each time I receive something (which may be as small as 10 bytes or even less) I start by allocating 256B if that's not enough my function will enter a for loop and during each iteration it will receive another 256B and check if that's all for this time. Since I don't want and really can't make that loop limitless I've currently limited it to 9 iterations.
My problem is this, what if there are more bytes to recv than current maximum? My attempt at a solution was to check when that limit was reach and then handle all the packets that where in the buffer. But then I reached another problem, what if there are 6B left when I've processed all the packets, that means that I only got a partial packet. I tried to save those bytes that were left and add them to the beginning of the data returned by the next recv call which I know should contain the rest of the data (I know this because I know exactly how many bytes I've sent at the other end) But I do not get valid numbers when copying data from the char buffer into the size and type variables.
I know I could probably send a packet before the huge one and tell the client to handle next packet received with a much bigger buffer to copy to but that's not very good either in my opinion and what if I need to allocate so much memory I will get a std::bad_alloc?
That's not very dynamic in my eyes.
That's my problem I might have some bug in my code but I have looked at it a lot or maybe the computer's memory just doesn't work as I think it does.
TL DR version, I don't know if this is enough info though
To sum it up and maybe clarify a bit my basic problem is this: I receive 100B of data and after processing that data I see that I've got 6B left in the buffer, I can't make a valid packet out of that data since the bare minimum in my program is 8B (Type 4B and Size 4B). I've tried to save those six bytes and "add" them to the beginning of the data returned by the next Recv call but I've not gotten any valid output when I've copied that data from the char buffer into their respective variables.
I've probably missed some crucial information or been too unclear, either way I hope someone can help me with this.