UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
Message Class Reference

#include <message.hpp>

Collaboration diagram for Message:
Collaboration graph

Public Types

using CompletionCallback = std::function<void(CompletionStatus)>
 

Public Member Functions

 Message ()=default
 
void async_readMsg (asio::ip::tcp::socket &socket, CompletionCallback onComplete)
 
void async_sendMsg (asio::ip::tcp::socket &socket, CompletionCallback onComplete)
 
void clear ()
 
bool empty () const
 
size_t getSize () const
 
std::string_view getStr () const
 
bool isSendingNow () const
 
void setStr (std::string_view msg)
 

Private Member Functions

void async_readBody (asio::ip::tcp::socket &socket, CompletionCallback onComplete, const std::error_code &ec, size_t readLen)
 
void async_readBodyComplete (CompletionCallback onComplete, const std::error_code &ec, size_t readLen)
 
void async_sendComplete (CompletionCallback onComplete, const std::error_code &ec, size_t sentLen)
 

Private Attributes

char data [headerSize+bodySize]
 
bool sendingNow = false
 
size_t size = 0
 

Static Private Attributes

static constexpr size_t bodySize = 2048
 
static constexpr size_t headerSize = 5
 

Detailed Description

Definition at line 45 of file message.hpp.

Member Typedef Documentation

◆ CompletionCallback

using Message::CompletionCallback = std::function<void(CompletionStatus)>

Definition at line 47 of file message.hpp.

Constructor & Destructor Documentation

◆ Message()

Message::Message ( )
default

Member Function Documentation

◆ async_readBody()

void Message::async_readBody ( asio::ip::tcp::socket & socket,
CompletionCallback onComplete,
const std::error_code & ec,
size_t readLen )
private

Definition at line 78 of file message.cpp.

81{
82 if(ec){
83 std::cerr << "Msg error " << ec.message() << "\n";
84 onComplete(ec == asio::error::eof ?
86 return;
87 }
88
89 assert(readLen == headerSize);
90 size_t expectedSize = 0;
91
92 auto res = std::from_chars(data, data + headerSize, expectedSize);
93 if(res.ec != std::errc()){
94 onComplete(CompletionStatus::Error);
95 return;
96 }
97
98 using namespace std::placeholders;
99 asio::async_read(socket,
100 asio::buffer(data + headerSize, expectedSize),
102 this, onComplete, _1, _2));
103
104}
static constexpr size_t headerSize
Definition message.hpp:79
char data[headerSize+bodySize]
Definition message.hpp:81
void async_readBodyComplete(CompletionCallback onComplete, const std::error_code &ec, size_t readLen)
Definition message.cpp:106

References async_readBodyComplete(), data, Error, headerSize, and UnexpectedDisconnect.

Referenced by async_readMsg().

Here is the call graph for this function:

◆ async_readBodyComplete()

void Message::async_readBodyComplete ( CompletionCallback onComplete,
const std::error_code & ec,
size_t readLen )
private

Definition at line 106 of file message.cpp.

108{
109 if(ec){
110 std::cerr << "Msg error: " << ec.message() << "\n";
111 onComplete(ec == asio::error::eof ?
113 return;
114 }
115
116 size = readLen;
117
118 onComplete(CompletionStatus::Success);
119}
size_t size
Definition message.hpp:75

References Error, size, Success, and UnexpectedDisconnect.

Referenced by async_readBody().

◆ async_readMsg()

void Message::async_readMsg ( asio::ip::tcp::socket & socket,
CompletionCallback onComplete )

Definition at line 47 of file message.cpp.

49{
50 using namespace std::placeholders;
51 asio::async_read(socket,
52 asio::buffer(data, headerSize),
53 std::bind(&Message::async_readBody,
54 this, std::ref(socket), onComplete, _1, _2));
55}
void async_readBody(asio::ip::tcp::socket &socket, CompletionCallback onComplete, const std::error_code &ec, size_t readLen)
Definition message.cpp:78

References async_readBody(), data, and headerSize.

Referenced by Client::readCandidate(), Client::readDescription(), Client::readNameComplete(), and Client::readRoomComplete().

Here is the call graph for this function:

◆ async_sendComplete()

void Message::async_sendComplete ( CompletionCallback onComplete,
const std::error_code & ec,
size_t sentLen )
private

Definition at line 121 of file message.cpp.

123{
124 sendingNow = false;
126}
bool sendingNow
Definition message.hpp:77

References Error, sendingNow, and Success.

Referenced by async_sendMsg().

◆ async_sendMsg()

void Message::async_sendMsg ( asio::ip::tcp::socket & socket,
CompletionCallback onComplete )

Definition at line 67 of file message.cpp.

69{
70 using namespace std::placeholders;
71 sendingNow = true;
72 asio::async_write(socket,
73 asio::buffer(data, headerSize + size),
75 this, onComplete, _1, _2));
76}
void async_sendComplete(CompletionCallback onComplete, const std::error_code &ec, size_t sentLen)
Definition message.cpp:121

References async_sendComplete(), data, headerSize, sendingNow, and size.

Referenced by Client::onMsgSent(), and Client::sendMsg().

Here is the call graph for this function:

◆ clear()

void Message::clear ( )

◆ empty()

bool Message::empty ( ) const
inline

Definition at line 60 of file message.hpp.

60{ return size == 0; }

References size.

◆ getSize()

size_t Message::getSize ( ) const
inline

Definition at line 59 of file message.hpp.

59{ return size; }

References size.

◆ getStr()

std::string_view Message::getStr ( ) const

Definition at line 43 of file message.cpp.

43 {
44 return std::string_view(data + headerSize, size);
45}

References data, headerSize, and size.

Referenced by Client::readCandidateComplete(), Client::readDescComplete(), Client::readNameComplete(), and Client::readRoomComplete().

◆ isSendingNow()

bool Message::isSendingNow ( ) const
inline

Definition at line 61 of file message.hpp.

61{ return sendingNow; }

References sendingNow.

Referenced by Client::isSendCallbackPending().

◆ setStr()

void Message::setStr ( std::string_view msg)

Definition at line 57 of file message.cpp.

57 {
58 assert(!sendingNow);
59 size = std::min(bodySize, msg.size());
60
61 memset(data, ' ', headerSize);
62 std::to_chars(data, data + headerSize, size);
63
64 memcpy(data + headerSize, msg.data(), size);
65}
static constexpr size_t bodySize
Definition message.hpp:80

References bodySize, data, headerSize, sendingNow, and size.

Referenced by Client::onMsgSent(), and Client::sendMsg().

Member Data Documentation

◆ bodySize

size_t Message::bodySize = 2048
staticconstexprprivate

Definition at line 80 of file message.hpp.

Referenced by setStr().

◆ data

char Message::data[headerSize+bodySize]
private

Definition at line 81 of file message.hpp.

Referenced by async_readBody(), async_readMsg(), async_sendMsg(), getStr(), and setStr().

◆ headerSize

size_t Message::headerSize = 5
staticconstexprprivate

Definition at line 79 of file message.hpp.

Referenced by async_readBody(), async_readMsg(), async_sendMsg(), getStr(), and setStr().

◆ sendingNow

bool Message::sendingNow = false
private

Definition at line 77 of file message.hpp.

Referenced by async_sendComplete(), async_sendMsg(), isSendingNow(), and setStr().

◆ size

size_t Message::size = 0
private

Definition at line 75 of file message.hpp.

Referenced by async_readBodyComplete(), async_sendMsg(), empty(), getSize(), getStr(), and setStr().


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