x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to disable Nagle algorithm / enable TCP_NoDelay?

Hi, Dear Developers! I am trying to make TCP client on C++, by using this example: link:( https://wiki.unrealengine.com/Third_Party_Socket_Server_Connection ). When I send messages to the Server with small intervals, on Server I receive merged Posts like this:

 MOVE|0.0|0.0|0.0|0.0 
 MOVE|0.0|0.0|0.0|0.0 
 MOVE|0.0|0.0|0.0|0.0MOVE|0.0|0.0|0.0|0.0 
 MOVE|0.0|0.0|0.0|0.0

To solve this problem I was going to disable Nagle algorithm / enable TCP_NoDelay, but don't know how. I was looking for method FSocket.setNoDelay(true) or something similar, but have nothing. Who know, where I can find this option for TCP Sockets?

Product Version: UE 4.8
Tags:
more ▼

asked Jun 29 '15 at 09:38 PM in C++ Programming

avatar image

Feliss
18 1 2 5

avatar image Fleischer Aug 04 '15 at 09:04 PM

You can consider using delimiting symbol to mark end of message for example instead of trying to find way to disable Nagle's algorithm. You could use ~ in the end of each line, so the end of message can be easily parsed.

 MOVE|0.0|0.0|0.0|0.0~
 MOVE|0.0|0.0|0.0|0.0~
 MOVE|0.0|0.0|0.0|0.0~MOVE|0.0|0.0|0.0|0.0~
 MOVE|0.0|0.0|0.0|0.0~
avatar image Kafumanto Mar 09 '16 at 07:13 PM

Unfortunately (as of v4.10) there is still no way to specify that flag without modifying the Engine source code (look at "Engine\Source\Runtime\Sockets\Private").

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

This is for windows only:

If you can get the Winsock from your FSocket instance then you can disable Nagle's algorithm by yourself. Unfortunately the Winsock is not a member of FSocket, but it is a member of FSocket's Windows specific derivation FSocketBSD. But Unreal Engine does not expose this type so you can't use it in your code without changing the engine source.

So here's how to disable Nagle's algorithm without changing the engine source :)

 // Build listener socket...
 FSocket *listener = ....;

 // Move the listener pointer to right after your FSocket instance and cast it to a UINT_PTR
 UINT_PTR nativeSocket = *((UINT_PTR*)(listener + 1));

 // Disable Nagle's algorithm
 int flag = 1;
 int result = setsockopt(nativeSocket, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
 int err = WSAGetLastError();

 // In case you messed something up
 if (result < 0)
     UE_LOG(LogTemp, Warning, TEXT("Error code %d"), err);

Here are the Winsock error codes in case you need to debug something. This works because FSocketBSD is derived from FSocket and the Winsock is the first member of FSocketBSD. Since member variables are laid out in memory in the order they are defined (*most of the time), the first value in memory after your FSocket will be the first member of FSocketBSD. I said *most of the time because the layout of memory can depend on the compiler and compiler options. This will also not work if future Unreal version move the Winsock so that it's not the first member of FSocketBSD, though this can be fixed by simply moving the pointer to it's new location.

more ▼

answered Feb 01 '19 at 12:58 PM

avatar image

Waranoi
53 1 5 5

avatar image Fleischer Feb 01 '19 at 01:26 PM

Thanks for such a detailed answer! I still believe that it does not make much sense to alter Engine by ourselves as it could make future updates difficult, but it is a great chance to understand the Engine internals better

(fun fact, we have worked on this question for TCP networking PoC back in 2015 and abandoned the project a couple of weeks later, so never had a chance to dive deep enough into it)

avatar image Waranoi Feb 01 '19 at 01:59 PM

Not sure if I misunderstand your comment or if I explained poorly, but by doing this you can disable Nagle's algorithm without altering the Engine. I'll edit my asnwer anyways to make it more clear.

avatar image Fleischer Feb 01 '19 at 02:20 PM

oh, sorry I understood the wrong way after seeing

This will also not work if future Unreal version move the Winsock so that it's not the first member of FSocketBSD

I have not worked with UE4 more than three years, so I just read the overall text quickly enough to skip some important details. My thoughts here generally are that it is not a good idea in most cases to be dependent on some specifics of the current version of the Engine if it could be subject to change in the future.

avatar image Waranoi Feb 01 '19 at 02:56 PM

This is true, but also easily patched by adding preprocessor macros for different versions if they decide to restructure FSocketBSD in the future.

 #if ENGINE_MINOR_VERSION < 22
 // Move pointer to right location
 #endif
 #if ENGINE_MINOR_VERSION >= 22
 // Move pointer to new right location
 #endif
(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question