From badb51338c77aef5608f36e76165b3e1c1c38bc8 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 28 Mar 2012 21:20:13 +0200 Subject: [PATCH] Further performance improvements. --- jsonrpc/jsonrpcclient.cpp | 12 +++---- jsonrpc/jsonrpcmessage.cpp | 14 +++----- jsonrpc/jsonrpcmessage.h | 6 ++-- jsonrpc/netstring.cpp | 69 +++++++++++--------------------------- jsonrpc/netstring.h | 13 ++----- 5 files changed, 36 insertions(+), 78 deletions(-) diff --git a/jsonrpc/jsonrpcclient.cpp b/jsonrpc/jsonrpcclient.cpp index 8bf28dd44..2ecd7b2e2 100644 --- a/jsonrpc/jsonrpcclient.cpp +++ b/jsonrpc/jsonrpcclient.cpp @@ -11,27 +11,27 @@ void JsonRpcClient::Start(void) void JsonRpcClient::SendMessage(JsonRpcMessage::RefType message) { - Netstring::RefType ns = message->ToNetstring(); - ns->WriteToFIFO(GetSendQueue()); + Netstring::WriteJSONToFIFO(GetSendQueue(), message->GetJSON()); } int JsonRpcClient::DataAvailableHandler(EventArgs::RefType ea) { - Netstring::RefType ns; + cJSON *json; while (true) { try { - ns = Netstring::ReadFromFIFO(GetRecvQueue()); + json = Netstring::ReadJSONFromFIFO(GetRecvQueue()); } catch (const exception&) { Close(); return 1; } - if (ns == NULL) + if (json == NULL) break; - JsonRpcMessage::RefType msg = JsonRpcMessage::FromNetstring(ns); + JsonRpcMessage::RefType msg = new_object(); + msg->SetJSON(json); NewMessageEventArgs::RefType nea = new_object(); nea->Source = shared_from_this(); nea->Message = msg; diff --git a/jsonrpc/jsonrpcmessage.cpp b/jsonrpc/jsonrpcmessage.cpp index 5f8c93363..84839b28b 100644 --- a/jsonrpc/jsonrpcmessage.cpp +++ b/jsonrpc/jsonrpcmessage.cpp @@ -12,19 +12,15 @@ JsonRpcMessage::~JsonRpcMessage(void) cJSON_Delete(m_JSON); } -JsonRpcMessage::RefType JsonRpcMessage::FromNetstring(Netstring::RefType ns) +void JsonRpcMessage::SetJSON(cJSON *object) { - JsonRpcMessage::RefType msg = new_object(); - msg->m_JSON = cJSON_Parse(ns->ToString()); - return msg; + cJSON_Delete(m_JSON); + m_JSON = object; } -Netstring::RefType JsonRpcMessage::ToNetstring(void) +cJSON *JsonRpcMessage::GetJSON(void) { - Netstring::RefType ns = new_object(); - char *msg = cJSON_Print(m_JSON); - ns->SetString(msg); - return ns; + return m_JSON; } void JsonRpcMessage::SetFieldString(const char *field, const string& value) diff --git a/jsonrpc/jsonrpcmessage.h b/jsonrpc/jsonrpcmessage.h index 683d9dd0c..36296e8d6 100644 --- a/jsonrpc/jsonrpcmessage.h +++ b/jsonrpc/jsonrpcmessage.h @@ -19,9 +19,9 @@ public: JsonRpcMessage(void); ~JsonRpcMessage(void); - static JsonRpcMessage::RefType FromNetstring(Netstring::RefType ns); - Netstring::RefType ToNetstring(void); - + void SetJSON(cJSON *object); + cJSON *GetJSON(void); + void SetVersion(const string& version); string GetVersion(void); diff --git a/jsonrpc/netstring.cpp b/jsonrpc/netstring.cpp index 827aa76e4..3762f754c 100644 --- a/jsonrpc/netstring.cpp +++ b/jsonrpc/netstring.cpp @@ -4,26 +4,15 @@ using namespace icinga; using std::sprintf; -Netstring::Netstring(void) -{ - m_Length = 0; - m_Data = NULL; -} - -Netstring::~Netstring(void) -{ - Memory::Free(m_Data); -} - /* based on https://github.com/PeterScott/netstring-c/blob/master/netstring.c */ -Netstring::RefType Netstring::ReadFromFIFO(FIFO::RefType fifo) +cJSON *Netstring::ReadJSONFromFIFO(FIFO::RefType fifo) { size_t buffer_length = fifo->GetSize(); - const char *buffer = (const char *)fifo->GetReadBuffer(); + char *buffer = (char *)fifo->GetReadBuffer(); /* minimum netstring length is 3 */ if (buffer_length < 3) - return Netstring::RefType(); + return NULL; /* no leading zeros allowed */ if (buffer[0] == '0' && isdigit(buffer[1])) @@ -35,14 +24,14 @@ Netstring::RefType Netstring::ReadFromFIFO(FIFO::RefType fifo) for (i = 0; i < buffer_length && isdigit(buffer[i]); i++) { /* length specifier must have at most 9 characters */ if (i >= 9) - return Netstring::RefType(); + return NULL; len = len * 10 + (buffer[i] - '0'); } /* make sure the buffer is large enough */ if (i + len + 1 >= buffer_length) - return Netstring::RefType(); + return NULL; /* check for the colon delimiter */ if (buffer[i++] != ':') @@ -52,48 +41,30 @@ Netstring::RefType Netstring::ReadFromFIFO(FIFO::RefType fifo) if (buffer[i + len] != ',') throw exception(/*"Invalid Netstring (missing ,)"*/); - Netstring::RefType ns = new_object(); - ns->m_Length = len; - ns->m_Data = Memory::Allocate(len + 1); - memcpy(ns->m_Data, &buffer[i], len); - ((char *)ns->m_Data)[len] = '\0'; + /* nuke the comma delimiter */ + buffer[i + len] = '\0'; + cJSON *object = cJSON_Parse(&buffer[i]); + + if (object == NULL) { + /* restore the comma */ + buffer[i + len] = ','; + throw exception(/*"Invalid JSON string"*/); + } /* remove the data from the fifo */ fifo->Read(NULL, i + len + 1); - return ns; + return object; } -bool Netstring::WriteToFIFO(FIFO::RefType fifo) const +void Netstring::WriteJSONToFIFO(FIFO::RefType fifo, cJSON *object) { + char *json = cJSON_Print(object); + size_t len = strlen(json); char strLength[50]; - sprintf(strLength, "%lu", (unsigned long)m_Length); + sprintf(strLength, "%lu", (unsigned long)len); fifo->Write(strLength, strlen(strLength)); - fifo->Write(m_Data, m_Length); + fifo->Write(json, len); fifo->Write(",", 1); - - return true; -} - -size_t Netstring::GetSize(void) const { - return m_Length; -} - -const void *Netstring::GetData(void) const -{ - return m_Data; -} - -void Netstring::SetString(char *str) -{ - m_Data = str; - m_Length = strlen(str); -} - -const char *Netstring::ToString(void) -{ - /* our implementation already guarantees that there's a NUL char at - the end of the string - so we can just return m_Data here */ - return (const char *)m_Data; } diff --git a/jsonrpc/netstring.h b/jsonrpc/netstring.h index abcdfd020..f44e3f61d 100644 --- a/jsonrpc/netstring.h +++ b/jsonrpc/netstring.h @@ -14,17 +14,8 @@ public: typedef shared_ptr RefType; typedef weak_ptr WeakRefType; - Netstring(void); - ~Netstring(void); - - static Netstring::RefType ReadFromFIFO(FIFO::RefType fifo); - bool WriteToFIFO(FIFO::RefType fifo) const; - - size_t GetSize(void) const; - const void *GetData(void) const; - - void SetString(char *str); - const char *ToString(void); + static cJSON *ReadJSONFromFIFO(FIFO::RefType fifo); + static void WriteJSONToFIFO(FIFO::RefType fifo, cJSON *object); }; } -- 2.40.0