socket_options.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_SOCKET_SOCKET_OPTIONS_H_
  5. #define NET_SOCKET_SOCKET_OPTIONS_H_
  6. #include <stdint.h>
  7. #include "net/base/net_export.h"
  8. #include "net/socket/socket_descriptor.h"
  9. namespace net {
  10. // This function enables/disables buffering in the kernel. By default, on Linux,
  11. // TCP sockets will wait up to 200ms for more data to complete a packet before
  12. // transmitting. After calling this function, the kernel will not wait. See
  13. // TCP_NODELAY in `man 7 tcp`.
  14. //
  15. // For Windows:
  16. //
  17. // The Nagle implementation on Windows is governed by RFC 896. The idea
  18. // behind Nagle is to reduce small packets on the network. When Nagle is
  19. // enabled, if a partial packet has been sent, the TCP stack will disallow
  20. // further *partial* packets until an ACK has been received from the other
  21. // side. Good applications should always strive to send as much data as
  22. // possible and avoid partial-packet sends. However, in most real world
  23. // applications, there are edge cases where this does not happen, and two
  24. // partial packets may be sent back to back. For a browser, it is NEVER
  25. // a benefit to delay for an RTT before the second packet is sent.
  26. //
  27. // As a practical example in Chromium today, consider the case of a small
  28. // POST. I have verified this:
  29. // Client writes 649 bytes of header (partial packet #1)
  30. // Client writes 50 bytes of POST data (partial packet #2)
  31. // In the above example, with Nagle, a RTT delay is inserted between these
  32. // two sends due to nagle. RTTs can easily be 100ms or more. The best
  33. // fix is to make sure that for POSTing data, we write as much data as
  34. // possible and minimize partial packets. We will fix that. But disabling
  35. // Nagle also ensure we don't run into this delay in other edge cases.
  36. // See also:
  37. // http://technet.microsoft.com/en-us/library/bb726981.aspx
  38. //
  39. // SetTCPNoDelay() sets the TCP_NODELAY option. Use |no_delay| to enable or
  40. // disable it. On error returns a net error code, on success returns OK.
  41. int SetTCPNoDelay(SocketDescriptor fd, bool no_delay);
  42. // SetReuseAddr() sets the SO_REUSEADDR socket option. Use |reuse| to enable or
  43. // disable it. On error returns a net error code, on success returns OK.
  44. int SetReuseAddr(SocketDescriptor fd, bool reuse);
  45. // SetSocketReceiveBufferSize() sets the SO_RCVBUF socket option. On error
  46. // returns a net error code, on success returns OK.
  47. int SetSocketReceiveBufferSize(SocketDescriptor fd, int32_t size);
  48. // SetSocketSendBufferSize() sets the SO_SNDBUF socket option. On error
  49. // returns a net error code, on success returns OK.
  50. int SetSocketSendBufferSize(SocketDescriptor fd, int32_t size);
  51. } // namespace net
  52. #endif // NET_SOCKET_SOCKET_OPTIONS_H_