JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
jsonrpc::transport::FramedTransport Class Reference

Base class for framed transport mechanisms. More...

#include <framed_transport.hpp>

Inheritance diagram for jsonrpc::transport::FramedTransport:
Collaboration diagram for jsonrpc::transport::FramedTransport:

Static Protected Member Functions

static void FrameMessage (std::ostream &output, const std::string &message)
 Constructs a framed message.
 
static auto ReadHeadersFromStream (std::istream &input) -> HeaderMap
 
static auto ReadContentLengthFromStream (std::istream &input) -> int
 
static auto ReadContent (std::istream &input, std::size_t content_length) -> std::string
 Reads content from the input stream based on the content length.
 
static auto ReceiveFramedMessage (std::istream &input) -> std::string
 Receives a framed message.
 

Static Protected Attributes

static constexpr const char * kHeaderDelimiter = "\r\n\r\n"
 The delimiter used to separate headers from the message content.
 

Friends

class FramedTransportTest
 

Detailed Description

Base class for framed transport mechanisms.

Provides modular functionality for sending and receiving framed messages.

Definition at line 18 of file framed_transport.hpp.

Member Function Documentation

◆ FrameMessage()

void jsonrpc::transport::FramedTransport::FrameMessage ( std::ostream & output,
const std::string & message )
staticprotected

Constructs a framed message.

Constructs a JSON message as a string with additional headers for Content-Length and Content-Type, similar to HTTP.

Parameters
outputThe output stream to write the framed message.
messageThe message to be framed.

Definition at line 9 of file framed_transport.cpp.

10 {
11 output << "Content-Length: " << message.size() << "\r\n"
12 << "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n"
13 << "\r\n"
14 << message;
15}

Referenced by jsonrpc::transport::FramedPipeTransport::SendMessage(), jsonrpc::transport::FramedSocketTransport::SendMessage(), and jsonrpc::transport::FramedStdioTransport::SendMessage().

Here is the caller graph for this function:

◆ ReadContent()

auto jsonrpc::transport::FramedTransport::ReadContent ( std::istream & input,
std::size_t content_length ) -> std::string
staticprotected

Reads content from the input stream based on the content length.

Parameters
inputThe input stream to read the content from.
content_lengthThe length of the content to be read.
Returns
The content as a string.

Definition at line 47 of file framed_transport.cpp.

48 {
49 std::string content(content_length, '\0');
50 input.read(content.data(), static_cast<std::streamsize>(content_length));
51 if (input.gcount() != static_cast<std::streamsize>(content_length)) {
52 throw std::runtime_error("Failed to read the expected content length");
53 }
54 return content;
55}

◆ ReadContentLengthFromStream()

auto jsonrpc::transport::FramedTransport::ReadContentLengthFromStream ( std::istream & input) -> int
staticprotected

Definition at line 38 of file framed_transport.cpp.

38 {
39 auto headers = ReadHeadersFromStream(input);
40 auto it = headers.find("Content-Length");
41 if (it == headers.end()) {
42 throw std::runtime_error("Content-Length header missing");
43 }
44 return ParseContentLength(it->second);
45}
static auto ReadHeadersFromStream(std::istream &input) -> HeaderMap

◆ ReadHeadersFromStream()

auto jsonrpc::transport::FramedTransport::ReadHeadersFromStream ( std::istream & input) -> HeaderMap
staticprotected

Definition at line 17 of file framed_transport.cpp.

18 {
19 HeaderMap headers;
20 std::string line;
21
22 while (std::getline(input, line) && !line.empty() && line != "\r") {
23 auto colon_pos = line.find(':');
24 if (colon_pos != std::string::npos) {
25 std::string header_key = utils::Trim(line.substr(0, colon_pos));
26 std::string header_value = utils::Trim(line.substr(colon_pos + 1));
27 headers[header_key] = header_value;
28 }
29 }
30
31 if (headers.empty()) {
32 throw std::runtime_error("Failed to read headers");
33 }
34
35 return headers;
36}
auto Trim(std::string_view in) -> std::string

References jsonrpc::utils::Trim().

Here is the call graph for this function:

◆ ReceiveFramedMessage()

auto jsonrpc::transport::FramedTransport::ReceiveFramedMessage ( std::istream & input) -> std::string
staticprotected

Receives a framed message.

Reads headers to determine the content length, then reads the message content based on that length.

Parameters
inputThe input stream to read the framed message.
Returns
The received message content.

Definition at line 57 of file framed_transport.cpp.

57 {
58 int content_length = ReadContentLengthFromStream(input);
59 return ReadContent(input, content_length);
60}
static auto ReadContentLengthFromStream(std::istream &input) -> int
static auto ReadContent(std::istream &input, std::size_t content_length) -> std::string
Reads content from the input stream based on the content length.

Friends And Related Symbol Documentation

◆ FramedTransportTest

friend class FramedTransportTest
friend

Definition at line 70 of file framed_transport.hpp.

Member Data Documentation

◆ kHeaderDelimiter

const char* jsonrpc::transport::FramedTransport::kHeaderDelimiter = "\r\n\r\n"
staticconstexprprotected

The delimiter used to separate headers from the message content.

Definition at line 24 of file framed_transport.hpp.


The documentation for this class was generated from the following files: