From: Gunnar Beutner Date: Fri, 22 Jun 2012 09:20:48 +0000 (+0200) Subject: Performance improvements for sockets. X-Git-Tag: v0.0.1~375 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e2253b862420711a1283077e2189f8579c31cb9d;p=icinga2 Performance improvements for sockets. --- diff --git a/jsonrpc/jsonrpcclient.cpp b/jsonrpc/jsonrpcclient.cpp index c95b049b1..76ecc483a 100644 --- a/jsonrpc/jsonrpcclient.cpp +++ b/jsonrpc/jsonrpcclient.cpp @@ -40,7 +40,7 @@ JsonRpcClient::JsonRpcClient(TcpClientRole role, shared_ptr sslContext) */ void JsonRpcClient::SendMessage(const MessagePart& message) { - Netstring::WriteStringToFIFO(GetSendQueue(), message.ToJsonString()); + Netstring::WriteStringToSocket(GetSelf(), message.ToJsonString()); } /** @@ -53,7 +53,7 @@ void JsonRpcClient::DataAvailableHandler(void) string jsonString; MessagePart message; - if (!Netstring::ReadStringFromFIFO(GetRecvQueue(), &jsonString)) + if (!Netstring::ReadStringFromSocket(GetSelf(), &jsonString)) return; message = MessagePart(jsonString); diff --git a/jsonrpc/netstring.cpp b/jsonrpc/netstring.cpp index de1104544..9b9c4428f 100644 --- a/jsonrpc/netstring.cpp +++ b/jsonrpc/netstring.cpp @@ -22,7 +22,7 @@ using namespace icinga; /** - * Reads data from a FIFO in netstring format. + * Reads data from a TCP client in netstring format. * * @param fifo The FIFO to read from. * @param[out] str The string that has been read from the FIFO. @@ -30,8 +30,9 @@ using namespace icinga; * @exception InvalidNetstringException The input stream is invalid. * @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c */ -bool Netstring::ReadStringFromFIFO(FIFO::Ptr fifo, string *str) +bool Netstring::ReadStringFromSocket(const TcpClient::Ptr& client, string *str) { + FIFO::Ptr fifo = client->GetRecvQueue(); size_t buffer_length = fifo->GetSize(); char *buffer = (char *)fifo->GetReadBuffer(); @@ -75,13 +76,15 @@ bool Netstring::ReadStringFromFIFO(FIFO::Ptr fifo, string *str) } /** - * Writes data into a FIFO using the netstring format. + * Writes data into a TCP client's send buffer using the netstring format. * * @param fifo The FIFO. * @param str The string that is to be written. */ -void Netstring::WriteStringToFIFO(FIFO::Ptr fifo, const string& str) +void Netstring::WriteStringToSocket(const TcpClient::Ptr& client, const string& str) { + FIFO::Ptr fifo = client->GetSendQueue(); + stringstream prefixbuf; prefixbuf << str.size() << ":"; @@ -90,4 +93,6 @@ void Netstring::WriteStringToFIFO(FIFO::Ptr fifo, const string& str) fifo->Write(str.c_str(), str.size()); fifo->Write(",", 1); + + client->Flush(); } diff --git a/jsonrpc/netstring.h b/jsonrpc/netstring.h index 66b2bc23c..e59540bcf 100644 --- a/jsonrpc/netstring.h +++ b/jsonrpc/netstring.h @@ -24,7 +24,8 @@ namespace icinga { /** - * Thrown when an invalid netstring was encountered while reading from a FIFO. + * Thrown when an invalid netstring was encountered while reading from a + * TCP client. * * @ingroup jsonrpc */ @@ -40,8 +41,8 @@ DEFINE_EXCEPTION_CLASS(InvalidNetstringException); class I2_JSONRPC_API Netstring { public: - static bool ReadStringFromFIFO(FIFO::Ptr fifo, string *message); - static void WriteStringToFIFO(FIFO::Ptr fifo, const string& message); + static bool ReadStringFromSocket(const TcpClient::Ptr& client, string *message); + static void WriteStringToSocket(const TcpClient::Ptr& client, const string& message); private: Netstring(void);