From 8b334fe25917c31aa8a60c9e45fbc0990c3eebef Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Tue, 27 Feb 2018 14:49:15 +0100 Subject: [PATCH] Use cork in tlsstream and HTTP connection --- lib/base/tlsstream.cpp | 21 +++++++++++++++++++-- lib/base/tlsstream.hpp | 2 ++ lib/remote/httpserverconnection.cpp | 4 +++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/base/tlsstream.cpp b/lib/base/tlsstream.cpp index 1fda60382..403daf336 100644 --- a/lib/base/tlsstream.cpp +++ b/lib/base/tlsstream.cpp @@ -152,12 +152,17 @@ void TlsStream::OnEvent(int revents) char buffer[64 * 1024]; if (m_CurrentAction == TlsActionNone) { - if (revents & (POLLIN | POLLERR | POLLHUP)) + bool corked = IsCorked(); + if (!corked && (revents & (POLLIN | POLLERR | POLLHUP))) m_CurrentAction = TlsActionRead; else if (m_SendQ->GetAvailableBytes() > 0 && (revents & POLLOUT)) m_CurrentAction = TlsActionWrite; else { - ChangeEvents(POLLIN); + if (corked) + ChangeEvents(0); + else + ChangeEvents(POLLIN); + return; } } @@ -404,3 +409,15 @@ Socket::Ptr TlsStream::GetSocket(void) const { return m_Socket; } + +void TlsStream::SetCorked(bool corked) +{ + Stream::SetCorked(corked); + + boost::mutex::scoped_lock lock(m_Mutex); + + if (corked) + m_CurrentAction = TlsActionNone; + else + ChangeEvents(POLLIN | POLLOUT); +} diff --git a/lib/base/tlsstream.hpp b/lib/base/tlsstream.hpp index 6f7f71738..5c475500f 100644 --- a/lib/base/tlsstream.hpp +++ b/lib/base/tlsstream.hpp @@ -73,6 +73,8 @@ public: bool IsVerifyOK(void) const; String GetVerifyError(void) const; + virtual void SetCorked(bool corked) override; + private: boost::shared_ptr m_SSL; bool m_Eof; diff --git a/lib/remote/httpserverconnection.cpp b/lib/remote/httpserverconnection.cpp index 550778751..e087437e6 100644 --- a/lib/remote/httpserverconnection.cpp +++ b/lib/remote/httpserverconnection.cpp @@ -97,7 +97,6 @@ void HttpServerConnection::Disconnect(void) bool HttpServerConnection::ProcessMessage(void) { - bool res; HttpResponse response(m_Stream, m_CurrentRequest); @@ -175,6 +174,8 @@ bool HttpServerConnection::ProcessMessage(void) return res; } + m_Stream->SetCorked(true); + m_RequestQueue.Enqueue(std::bind(&HttpServerConnection::ProcessMessageAsync, HttpServerConnection::Ptr(this), m_CurrentRequest, response, m_AuthenticatedUser)); @@ -328,6 +329,7 @@ void HttpServerConnection::ProcessMessageAsync(HttpRequest& request, HttpRespons response.Finish(); m_PendingRequests--; + m_Stream->SetCorked(false); } void HttpServerConnection::DataAvailableHandler(void)