diff --git a/src/hx/libs/asys/libuv/net/LibuvTcpServer.cpp b/src/hx/libs/asys/libuv/net/LibuvTcpServer.cpp index 3d1f64ec2..764720edb 100644 --- a/src/hx/libs/asys/libuv/net/LibuvTcpServer.cpp +++ b/src/hx/libs/asys/libuv/net/LibuvTcpServer.cpp @@ -67,7 +67,7 @@ namespace return; } - Dynamic(request->cbSuccess.rooted)(hx::asys::net::TcpSocket(new hx::asys::libuv::net::LibuvTcpSocket(socket.release()))); + Dynamic(request->cbSuccess.rooted)(hx::asys::net::TcpSocket(new hx::asys::libuv::net::LibuvTcpSocket(socket.release(), server->keepAlive))); } } } diff --git a/src/hx/libs/asys/libuv/net/LibuvTcpSocket.cpp b/src/hx/libs/asys/libuv/net/LibuvTcpSocket.cpp index 42ba56a88..85f3d2f8e 100644 --- a/src/hx/libs/asys/libuv/net/LibuvTcpSocket.cpp +++ b/src/hx/libs/asys/libuv/net/LibuvTcpSocket.cpp @@ -9,14 +9,14 @@ namespace { std::unique_ptr tcp; - hx::RootedObject options; + int keepAlive; uv_connect_t connect; ConnectionRequest(Dynamic _cbSuccess, Dynamic _cbFailure) : hx::asys::libuv::BaseRequest(_cbSuccess, _cbFailure) , tcp(std::make_unique()) - , options(nullptr) + , keepAlive(hx::asys::libuv::net::KEEP_ALIVE_VALUE) { connect.data = this; } @@ -61,7 +61,7 @@ namespace return; } - Dynamic(spData->cbSuccess.rooted)(new hx::asys::libuv::net::LibuvTcpSocket(spData->tcp.release())); + Dynamic(spData->cbSuccess.rooted)(new hx::asys::libuv::net::LibuvTcpSocket(spData->tcp.release(), spData->keepAlive)); } void startConnection(hx::asys::libuv::LibuvAsysContext ctx, sockaddr* const address, Dynamic options, Dynamic cbSuccess, Dynamic cbFailure) @@ -76,7 +76,40 @@ namespace return; } - if ((result = uv_tcp_keepalive(request->tcp.get(), true, 10) < 0)) + if (hx::IsNotNull(options)) + { + auto keepAliveValue = options->__Field(HX_CSTRING("keepAlive"), hx::PropertyAccess::paccDynamic); + if (keepAliveValue.isBool()) + { + request->keepAlive = keepAliveValue.asInt() ? hx::asys::libuv::net::KEEP_ALIVE_VALUE : 0; + } + + auto sendSizeValue = options->__Field(HX_CSTRING("sendBuffer"), hx::PropertyAccess::paccDynamic); + if (sendSizeValue.isInt()) + { + auto result = uv_send_buffer_size(reinterpret_cast(request->tcp.get()), &sendSizeValue.valInt); + if (result < 0) + { + cbFailure(hx::asys::libuv::uv_err_to_enum(result)); + + return; + } + } + + auto recvSizeValue = options->__Field(HX_CSTRING("receiveBuffer"), hx::PropertyAccess::paccDynamic); + if (recvSizeValue.isInt()) + { + auto result = uv_recv_buffer_size(reinterpret_cast(request->tcp.get()), &recvSizeValue.valInt); + if (result < 0) + { + cbFailure(hx::asys::libuv::uv_err_to_enum(result)); + + return; + } + } + } + + if ((result = uv_tcp_keepalive(request->tcp.get(), request->keepAlive > 0, hx::asys::libuv::net::KEEP_ALIVE_VALUE) < 0)) { cbFailure(hx::asys::libuv::uv_err_to_enum(result)); @@ -94,8 +127,9 @@ namespace } } -hx::asys::libuv::net::LibuvTcpSocket::LibuvTcpSocket(uv_tcp_t* tcp) +hx::asys::libuv::net::LibuvTcpSocket::LibuvTcpSocket(uv_tcp_t* tcp, const int keepAlive) : tcp(tcp) + , keepAlive(keepAlive) , reader(new hx::asys::libuv::stream::StreamReader_obj(reinterpret_cast(tcp))) , writer(new hx::asys::libuv::stream::StreamWriter_obj(reinterpret_cast(tcp))) { @@ -113,7 +147,7 @@ void hx::asys::libuv::net::LibuvTcpSocket::getKeepAlive(Dynamic cbSuccess, Dynam void hx::asys::libuv::net::LibuvTcpSocket::getSendBufferSize(Dynamic cbSuccess, Dynamic cbFailure) { auto size = 0; - auto result = uv_send_buffer_size(reinterpret_cast(tcp, &size), &size); + auto result = uv_send_buffer_size(reinterpret_cast(tcp), &size); if (result < 0) { diff --git a/src/hx/libs/asys/libuv/net/LibuvTcpSocket.h b/src/hx/libs/asys/libuv/net/LibuvTcpSocket.h index f42a7be04..ceaa140b7 100644 --- a/src/hx/libs/asys/libuv/net/LibuvTcpSocket.h +++ b/src/hx/libs/asys/libuv/net/LibuvTcpSocket.h @@ -16,7 +16,7 @@ namespace hx::asys::libuv::net hx::asys::libuv::stream::StreamReader reader; public: - LibuvTcpSocket(uv_tcp_t* _socket); + LibuvTcpSocket(uv_tcp_t* _socket, int keepAlive); void getKeepAlive(Dynamic cbSuccess, Dynamic cbFailure) override; void getSendBufferSize(Dynamic cbSuccess, Dynamic cbFailure) override;