chronoxor / CppServer

Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
MIT License
1.44k stars 284 forks source link

TCPSession::onReceived only receives part of the buffer sent by client #63

Closed wsyOverflow closed 3 years ago

wsyOverflow commented 3 years ago

I create a TCPSeesion followed by the TCP Chat example. However, I found my TCPSession can not received the whole buffer in one call of onReceived function when my client sent a large amount of data. The remaining data sent by client will be received by the next call of onReceived function. It seems receiving data by chuncks even the client sends the data in one sending.

How can I know my server has received all the data sent by client at a sending?

chronoxor commented 3 years ago

Out of box TCPSession provides streaming protocol. What you want is called messaging protocol. You should apply messaging protocol in your user code. Very simple example is to send data length in first 4 bytes (int length) and then receive, collect in array and wait for a whole data. When you receive the message then process it in your code.

You for message based protocol you might look into my FastBinaryEncoding project. Idea that I described above is implemented here - https://github.com/chronoxor/FastBinaryEncoding/blob/master/proto/fbe_protocol.cpp

wsyOverflow commented 3 years ago

Thanks for your explanation, I will implement that you suggested