From 265a8231c8f6843489de769e37219b0dca035f7a Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 28 Mar 2012 15:56:39 +0200 Subject: [PATCH] Implemented support for sending reply messages. --- base/tcpclient.cpp | 8 +++++-- jsonrpc/jsonrpcmessage.cpp | 43 ++++++++++++++++++++++++++++++++------ jsonrpc/jsonrpcmessage.h | 14 ++++++++----- jsonrpc/netstring.cpp | 6 ++++++ jsonrpc/netstring.h | 1 + miniapp/miniapp.cpp | 8 ++++++- 6 files changed, 66 insertions(+), 14 deletions(-) diff --git a/base/tcpclient.cpp b/base/tcpclient.cpp index c353b50da..b8c5e1f60 100644 --- a/base/tcpclient.cpp +++ b/base/tcpclient.cpp @@ -32,7 +32,9 @@ FIFO::RefType TCPClient::GetRecvQueue(void) int TCPClient::ReadableEventHandler(EventArgs::RefType ea) { char buffer[4096]; - int rc; + int read_total, rc; + + read_total = 0; while (true) { rc = recv(GetFD(), buffer, sizeof(buffer), 0); @@ -50,8 +52,10 @@ int TCPClient::ReadableEventHandler(EventArgs::RefType ea) } m_RecvQueue->Write(buffer, rc); + read_total += rc; - if (m_RecvQueue->GetSize() > 1024 * 1024) + /* make sure we don't starve other sockets */ + if (read_total > 128 * 1024) break; } diff --git a/jsonrpc/jsonrpcmessage.cpp b/jsonrpc/jsonrpcmessage.cpp index 9d866cfe0..5f8c93363 100644 --- a/jsonrpc/jsonrpcmessage.cpp +++ b/jsonrpc/jsonrpcmessage.cpp @@ -21,11 +21,27 @@ JsonRpcMessage::RefType JsonRpcMessage::FromNetstring(Netstring::RefType ns) Netstring::RefType JsonRpcMessage::ToNetstring(void) { - return Netstring::RefType(); + Netstring::RefType ns = new_object(); + char *msg = cJSON_Print(m_JSON); + ns->SetString(msg); + return ns; +} + +void JsonRpcMessage::SetFieldString(const char *field, const string& value) +{ + if (m_JSON == NULL) + m_JSON = cJSON_CreateObject(); + + cJSON *object = cJSON_CreateString(value.c_str()); + cJSON_DeleteItemFromObject(m_JSON, field); + cJSON_AddItemToObject(m_JSON, field, object); } string JsonRpcMessage::GetFieldString(const char *field) { + if (m_JSON == NULL) + m_JSON = cJSON_CreateObject(); + cJSON *idObject = cJSON_GetObjectItem(m_JSON, field); if (idObject == NULL || idObject->type != cJSON_String) @@ -34,8 +50,19 @@ string JsonRpcMessage::GetFieldString(const char *field) return string(idObject->valuestring); } -void JsonRpcMessage::SetID(string id) +void JsonRpcMessage::SetVersion(const string& version) { + SetFieldString("version", version); +} + +string JsonRpcMessage::GetVersion(void) +{ + return GetFieldString("jsonrpc"); +} + +void JsonRpcMessage::SetID(const string& id) +{ + SetFieldString("id", id); } string JsonRpcMessage::GetID(void) @@ -43,8 +70,9 @@ string JsonRpcMessage::GetID(void) return GetFieldString("id"); } -void JsonRpcMessage::SetMethod(string method) +void JsonRpcMessage::SetMethod(const string& method) { + SetFieldString("method", method); } string JsonRpcMessage::GetMethod(void) @@ -52,8 +80,9 @@ string JsonRpcMessage::GetMethod(void) return GetFieldString("method"); } -void JsonRpcMessage::SetParams(string params) +void JsonRpcMessage::SetParams(const string& params) { + SetFieldString("params", params); } string JsonRpcMessage::GetParams(void) @@ -61,8 +90,9 @@ string JsonRpcMessage::GetParams(void) return GetFieldString("params"); } -void JsonRpcMessage::SetResult(string result) +void JsonRpcMessage::SetResult(const string& result) { + SetFieldString("result", result); } string JsonRpcMessage::GetResult(void) @@ -70,8 +100,9 @@ string JsonRpcMessage::GetResult(void) return GetFieldString("result"); } -void JsonRpcMessage::SetError(string error) +void JsonRpcMessage::SetError(const string& error) { + SetFieldString("error", error); } string JsonRpcMessage::GetError(void) diff --git a/jsonrpc/jsonrpcmessage.h b/jsonrpc/jsonrpcmessage.h index 867d7a474..683d9dd0c 100644 --- a/jsonrpc/jsonrpcmessage.h +++ b/jsonrpc/jsonrpcmessage.h @@ -9,6 +9,7 @@ class JsonRpcMessage : public Object private: cJSON *m_JSON; + void SetFieldString(const char *field, const string& value); string GetFieldString(const char *field); public: @@ -21,19 +22,22 @@ public: static JsonRpcMessage::RefType FromNetstring(Netstring::RefType ns); Netstring::RefType ToNetstring(void); - void SetID(string id); + void SetVersion(const string& version); + string GetVersion(void); + + void SetID(const string& id); string GetID(void); - void SetMethod(string method); + void SetMethod(const string& method); string GetMethod(void); - void SetParams(string params); + void SetParams(const string& params); string GetParams(void); - void SetResult(string result); + void SetResult(const string& result); string GetResult(void); - void SetError(string error); + void SetError(const string& error); string GetError(void); }; diff --git a/jsonrpc/netstring.cpp b/jsonrpc/netstring.cpp index c3f5ac982..00b5636b1 100644 --- a/jsonrpc/netstring.cpp +++ b/jsonrpc/netstring.cpp @@ -85,6 +85,12 @@ 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 diff --git a/jsonrpc/netstring.h b/jsonrpc/netstring.h index f7d7ead19..abcdfd020 100644 --- a/jsonrpc/netstring.h +++ b/jsonrpc/netstring.h @@ -23,6 +23,7 @@ public: size_t GetSize(void) const; const void *GetData(void) const; + void SetString(char *str); const char *ToString(void); }; diff --git a/miniapp/miniapp.cpp b/miniapp/miniapp.cpp index d5f2986a5..40a5e75ae 100644 --- a/miniapp/miniapp.cpp +++ b/miniapp/miniapp.cpp @@ -50,9 +50,15 @@ public: int MessageHandler(NewMessageEventArgs::RefType nea) { + JsonRpcClient::RefType client = static_pointer_cast(nea->Source); JsonRpcMessage::RefType msg = nea->Message; - //cout << "Message received: " << msg->GetID() << ": " << msg->GetMethod() << endl; + JsonRpcMessage::RefType response = new_object(); + response->SetVersion("2.0"); + response->SetID(msg->GetID()); + response->SetResult("moo"); + client->SendMessage(response); + return 0; }