Performance improvements for sockets.

This commit is contained in:
Gunnar Beutner 2012-06-22 11:20:48 +02:00
parent d2332c8fd4
commit e2253b8624
3 changed files with 15 additions and 9 deletions

View 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);

View 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();
}

View 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);