From 905721d2b9eae59fde76efe87ccb20fd100362c1 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 1 Jul 2016 14:48:45 +0200 Subject: [PATCH] [DEV] try to have fast tcp socket --- enet/Tcp.cpp | 12 ++++++++++++ enet/Tcp.h | 2 ++ enet/TcpClient.cpp | 1 + enet/WebSocket.cpp | 2 ++ 4 files changed, 17 insertions(+) diff --git a/enet/Tcp.cpp b/enet/Tcp.cpp index 97c60d9..9b9a84a 100644 --- a/enet/Tcp.cpp +++ b/enet/Tcp.cpp @@ -14,6 +14,18 @@ #include #include #include +#include + +bool enet::Tcp::setTCPNoDelay(bool _enabled) { + if (m_socketId >= 0) { + int flag = _enabled==true?1:0; + if(setsockopt(m_socketId, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(flag)) == -1) { + return false; + } + return true; + } + return false; +} enet::Tcp::Tcp() : diff --git a/enet/Tcp.h b/enet/Tcp.h index 6c395d4..224b977 100644 --- a/enet/Tcp.h +++ b/enet/Tcp.h @@ -107,6 +107,8 @@ namespace enet { } return ret/sizeof(T); } + + bool setTCPNoDelay(bool _enabled); }; } diff --git a/enet/TcpClient.cpp b/enet/TcpClient.cpp index 1292eb4..8cc554b 100644 --- a/enet/TcpClient.cpp +++ b/enet/TcpClient.cpp @@ -15,6 +15,7 @@ #include #include #include +#include enet::Tcp enet::connectTcpClient(uint8_t _ip1, uint8_t _ip2, uint8_t _ip3, uint8_t _ip4, uint16_t _port, uint32_t _numberRetry) { std::string tmpname; diff --git a/enet/WebSocket.cpp b/enet/WebSocket.cpp index facd994..c12b622 100644 --- a/enet/WebSocket.cpp +++ b/enet/WebSocket.cpp @@ -37,6 +37,7 @@ enet::WebSocket::WebSocket(enet::Tcp _connection, bool _isServer) : m_interface(), m_observer(nullptr), m_observerUriCheck(nullptr) { + _connection.setTCPNoDelay(true); if (_isServer == true) { ememory::SharedPtr interface = std::make_shared(std::move(_connection)); interface->connectHeader(this, &enet::WebSocket::onReceiveRequest); @@ -50,6 +51,7 @@ enet::WebSocket::WebSocket(enet::Tcp _connection, bool _isServer) : } void enet::WebSocket::setInterface(enet::Tcp _connection, bool _isServer) { + _connection.setTCPNoDelay(true); if (_isServer == true) { ememory::SharedPtr interface = std::make_shared(std::move(_connection)); interface->connectHeader(this, &enet::WebSocket::onReceiveRequest);