Return bytes sent/written in SendMessage() and WriteStringToStream()

refs #5509
This commit is contained in:
Noah Hilverling 2017-11-13 16:26:21 +01:00
parent 2acaccd028
commit 054faa54d5
4 changed files with 12 additions and 7 deletions

View File

@ -102,18 +102,21 @@ StreamReadStatus NetString::ReadStringFromStream(const Stream::Ptr& stream, Stri
}
/**
* Writes data into a stream using the netstring format.
* Writes data into a stream using the netstring format and returns bytes written.
*
* @param stream The stream.
* @param str The String that is to be written.
*
* @return The amount of bytes written.
*/
void NetString::WriteStringToStream(const Stream::Ptr& stream, const String& str)
size_t NetString::WriteStringToStream(const Stream::Ptr& stream, const String& str)
{
std::ostringstream msgbuf;
WriteStringToStream(msgbuf, str);
String msg = msgbuf.str();
stream->Write(msg.CStr(), msg.GetLength());
return msg.GetLength();
}
/**

View File

@ -39,7 +39,7 @@ class I2_BASE_API NetString
{
public:
static StreamReadStatus ReadStringFromStream(const Stream::Ptr& stream, String *message, StreamReadContext& context, bool may_wait = false);
static void WriteStringToStream(const Stream::Ptr& stream, const String& message);
static size_t WriteStringToStream(const Stream::Ptr& stream, const String& message);
static void WriteStringToStream(std::ostream& stream, const String& message);
private:

View File

@ -54,11 +54,13 @@ static bool GetDebugJsonRpcCached(void)
#endif /* I2_DEBUG */
/**
* Sends a message to the connected peer.
* Sends a message to the connected peer and returns the bytes sent.
*
* @param message The message.
*
* @return The amount of bytes sent.
*/
void JsonRpc::SendMessage(const Stream::Ptr& stream, const Dictionary::Ptr& message)
size_t JsonRpc::SendMessage(const Stream::Ptr& stream, const Dictionary::Ptr& message)
{
String json = JsonEncode(message);
@ -67,7 +69,7 @@ void JsonRpc::SendMessage(const Stream::Ptr& stream, const Dictionary::Ptr& mess
std::cerr << ConsoleColorTag(Console_ForegroundBlue) << ">> " << json << ConsoleColorTag(Console_Normal) << "\n";
#endif /* I2_DEBUG */
NetString::WriteStringToStream(stream, json);
return NetString::WriteStringToStream(stream, json);
}
StreamReadStatus JsonRpc::ReadMessage(const Stream::Ptr& stream, String *message, StreamReadContext& src, bool may_wait)

View File

@ -35,7 +35,7 @@ namespace icinga
class I2_REMOTE_API JsonRpc
{
public:
static void SendMessage(const Stream::Ptr& stream, const Dictionary::Ptr& message);
static size_t SendMessage(const Stream::Ptr& stream, const Dictionary::Ptr& message);
static StreamReadStatus ReadMessage(const Stream::Ptr& stream, String *message, StreamReadContext& src, bool may_wait = false);
static Dictionary::Ptr DecodeMessage(const String& message);