]> granicus.if.org Git - icinga2/commitdiff
Performance improvements for sockets.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 22 Jun 2012 09:20:48 +0000 (11:20 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 22 Jun 2012 09:20:48 +0000 (11:20 +0200)
jsonrpc/jsonrpcclient.cpp
jsonrpc/netstring.cpp
jsonrpc/netstring.h

index c95b049b119b40ea6a26b0ce31eac2141050a61d..76ecc483a050def5f12175e7b6f5d988de829422 100644 (file)
@@ -40,7 +40,7 @@ JsonRpcClient::JsonRpcClient(TcpClientRole role, shared_ptr<SSL_CTX> 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);
index de11045448d456c8b7a370c7c130c170071a43a8..9b9c4428fd25093f3978da31e4924bf535753f06 100644 (file)
@@ -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();
 }
index 66b2bc23ca932ecae8a223b106d989bb805079e4..e59540bcf43b3f609818db6669aaf733f935c0fa 100644 (file)
@@ -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);