Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #119 #130

Closed
wants to merge 1 commit into from
Closed

Fix for issue #119 #130

wants to merge 1 commit into from

Conversation

nitrix
Copy link

@nitrix nitrix commented Dec 7, 2011

Fix for issue #119: Two packets are sent when using sf::TcpSocket::Send(sf::Packet);

Brainstorm:

  • Of course with TCP you need a way to tell how long the data is.
  • Sending two packets is a "working" solution, but the TCP/IP overhead is extremely huge. Most games needs short/quick real-time updates and that header is often bigger than the actual data sent...
  • The best way to fix this is to prepend the data with an Uint32 telling how long the following data is. (Just like the operator<<(std::string) function does, but for the entire packet data.
  • We can't put too much complexity into Packet::onSend() because it's a virtual function.
  • Creating a temporary buffer to push the size and also copy the packet's data is counter-intuitive and CPU intensive way.

Resolution:

  • Every Packet object is now created with a data buffer containing an Uint32 (the size of the packet).
  • A few pointers arithmetic have been introduced, but the class internals is fairly simple. Basically, when you call GetDataSize(), you get the internal buffer size minus the Uint32 placeholder... when you read/write data, you do so after the placeholder... basically we're just ignoring the new placeholder, but it's still there in the buffer.
  • The placeholder for the size is written only when the packet needs to be sent.
  • The current API isn't broken in any way. To achieve that, I needed to do a const_cast<> on Packet::GetData()

Result/Note:

I think everything should be fine. UDPSocket doesn't need a size due to datagrams and other functions doesn't use a Packet object.

…cpSocket::Send(sf::Packet); You can read the issue on the tracker for more informations.
@@ -36,7 +36,8 @@
////////////////////////////////////////////////////////////
Packet::Packet() :
myReadPos(0),
myIsValid(true)
myIsValid(true),
myData(sizeof(Uint32), 0)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Create the size placeholder at the begining of the data buffer (initialized to 0)

@nitrix nitrix closed this Dec 10, 2011
@nitrix nitrix reopened this Dec 10, 2011
@nitrix
Copy link
Author

nitrix commented Dec 10, 2011

I made a much better patch for my own project. Pulling very soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants