Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Recv - Dynamic Buffer

Skilled Illusionist
Joined
Oct 31, 2008
Messages
341
Reaction score
294
Hello, is there any way to make the Buffer for Recv Dynamic ?

this is the way I actually use it ..

PHP:
bool CClient::ClientThread()
{
    std::vector<char> szBuffer(65535);
    int iReceivedBytes;
    do
    {
        iReceivedBytes = recv(this->ClientSocket, szBuffer.data(), szBuffer.size(), 0);
        
        if (iReceivedBytes <= 0)
        {
            closesocket(this->ClientSocket);
            return 0;
        }
        this->PacketControl( szBuffer.data() );

    } while (this->ClientSocket != INVALID_SOCKET);

    return 0;
}
 
Junior Spellweaver
Joined
Oct 27, 2008
Messages
165
Reaction score
89
Most packets have a header, wich in most cases have a data size, I don't know what you receive but reading the fulll data in the TCP stream, not too good Idea, you can break the recv in 2 , 1 to read the header, and 1 to read th data.

Here are 2 scenarios:
- If you receive 2 or more packets while you process one it will be stored in the TCP stream, and when you use recv to read from the TCP stream it will read the buffer size you put in recv.
- If the packet is too big while the data is send, the packet is broken into 2 packets or more packets, while you recv the TCP stream, you would read a incomplete packet.

It is simple to recv the header and data, and continue to processing if you have a complete packet(this depends on how your application works, and if the packet contains a header to specify the data size)
 
Skilled Illusionist
Joined
Oct 31, 2008
Messages
341
Reaction score
294
well Packet[0}] contains a unsigned short ( WORD ) with the Size of Packet also..

unsigned short sSize = *(unsigned short)*&Packet[0];
 
Junior Spellweaver
Joined
Oct 27, 2008
Messages
165
Reaction score
89
It is possible to make the buffer dynamic, but not recommended, since if you're using pointers, pointers are not too safe when allocating memory, plus it uses the heap, which is slow if you have to do lots of calculations. vector is a manage version of an array, but it still allocates memory every the capacity of the vector is reached(the STL vector allocates 1.5x - 2x more memory then it is needed).

For example a vector with capacity of 5, when you insert 6 elements it will reallocate memory for 6 elements, and when you add another element it will reallocate again(assuming the allocation capacity is 1x), reallocation's are slow when dealing with lots of calculations.
 
Back
Top