UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
message.cpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2021 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37#include <charconv>
38#include <cassert>
39#include <cstring>
40#include <iostream>
41#include "message.hpp"
42
43std::string_view Message::getStr() const{
44 return std::string_view(data + headerSize, size);
45}
46
47void Message::async_readMsg(asio::ip::tcp::socket& socket,
48 CompletionCallback onComplete)
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}
56
57void Message::setStr(std::string_view msg){
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}
66
67void Message::async_sendMsg(asio::ip::tcp::socket& socket,
68 CompletionCallback onComplete)
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}
77
78void Message::async_readBody(asio::ip::tcp::socket& socket,
79 CompletionCallback onComplete,
80 const asio::error_code& ec, [[maybe_unused]] size_t readLen)
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}
105
107 const std::error_code& ec, size_t readLen)
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}
120
122 const std::error_code& ec, [[maybe_unused]] size_t sentLen)
123{
124 sendingNow = false;
126}
std::string_view getStr() const
Definition message.cpp:43
void async_readMsg(asio::ip::tcp::socket &socket, CompletionCallback onComplete)
Definition message.cpp:47
void async_sendComplete(CompletionCallback onComplete, const std::error_code &ec, size_t sentLen)
Definition message.cpp:121
size_t size
Definition message.hpp:75
void async_sendMsg(asio::ip::tcp::socket &socket, CompletionCallback onComplete)
Definition message.cpp:67
static constexpr size_t bodySize
Definition message.hpp:80
bool sendingNow
Definition message.hpp:77
static constexpr size_t headerSize
Definition message.hpp:79
void async_readBody(asio::ip::tcp::socket &socket, CompletionCallback onComplete, const std::error_code &ec, size_t readLen)
Definition message.cpp:78
char data[headerSize+bodySize]
Definition message.hpp:81
void setStr(std::string_view msg)
Definition message.cpp:57
std::function< void(CompletionStatus)> CompletionCallback
Definition message.hpp:47
void async_readBodyComplete(CompletionCallback onComplete, const std::error_code &ec, size_t readLen)
Definition message.cpp:106