Browse Source

Enable TCP_NODELAY for WebSocket connection.

Tatsuhiro Tsujikawa 13 năm trước cách đây
mục cha
commit
332bda83b4
5 tập tin đã thay đổi với 15 bổ sung0 xóa
  1. 1 0
      configure.ac
  2. 1 0
      src/HttpServerCommand.cc
  3. 6 0
      src/SocketCore.cc
  4. 3 0
      src/SocketCore.h
  5. 4 0
      src/a2netcompat.h

+ 1 - 0
configure.ac

@@ -300,6 +300,7 @@ AC_CHECK_HEADERS([argz.h \
                   malloc.h \
                   netdb.h \
                   netinet/in.h \
+                  netinet/tcp.h \
 		  poll.h \
 		  port.h \
                   stddef.h \

+ 1 - 0
src/HttpServerCommand.cc

@@ -163,6 +163,7 @@ bool HttpServerCommand::execute()
         httpServer_->feedUpgradeResponse("websocket",
                                          fmt("Sec-WebSocket-Accept: %s\r\n",
                                              serverKey.c_str()));
+        httpServer_->getSocket()->setTcpNodelay(true);
         Command* command =
           new rpc::WebSocketResponseCommand(getCuid(), httpServer_, e_,
                                             socket_);

+ 6 - 0
src/SocketCore.cc

@@ -506,6 +506,12 @@ void SocketCore::joinMulticastGroup
   setSockOpt(IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
 }
 
+void SocketCore::setTcpNodelay(bool f)
+{
+  int val = f;
+  setSockOpt(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
+}
+
 void SocketCore::setNonBlockingMode()
 {
 #ifdef __MINGW32__

+ 3 - 0
src/SocketCore.h

@@ -125,6 +125,9 @@ public:
   (const std::string& multicastAddr, uint16_t multicastPort,
    const std::string& localAddr);
   
+  // Enables TCP_NODELAY socket option if f == true.
+  void setTcpNodelay(bool f);
+
   void create(int family, int protocol = 0);
 
   void bindWithFamily(uint16_t port, int family, int flags = AI_PASSIVE);

+ 4 - 0
src/a2netcompat.h

@@ -73,6 +73,10 @@
 # include <netinet/in.h>
 #endif // HAVE_NETINET_IN_H
 
+#ifdef HAVE_NETINET_TCP_H
+# include <netinet/tcp.h>
+#endif // HAVE_NETINET_TCP_H
+
 #ifdef HAVE_ARPA_INET_H
 # include <arpa/inet.h>
 #endif // HAVE_ARPA_INET_H