UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
client.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 <iostream>
38#include "client.hpp"
39
40Client::Client(asio::ip::tcp::socket&& socket) : socket(std::move(socket)) { }
41
43 using namespace std::placeholders;
45 std::bind(&Client::readNameComplete,
46 shared_from_this(), std::ref(socket), onComplete, _1));
47}
48
49void Client::readNameComplete(asio::ip::tcp::socket& socket,
50 CompletionCallback onComplete,
52{
54 onComplete(*this, status);
55 return;
56 }
57
58 clientName = std::string(inMsg.getStr());
59 using namespace std::placeholders;
61 std::bind(&Client::readRoomComplete,
62 shared_from_this(), std::ref(socket), onComplete, _1));
63}
64
65void Client::readRoomComplete(asio::ip::tcp::socket& socket,
66 CompletionCallback onComplete,
68{
70 onComplete(*this, status);
71 return;
72 }
73
74 roomName = std::string(inMsg.getStr());
75 using namespace std::placeholders;
77 std::bind(&Client::readDescComplete, shared_from_this(), onComplete, _1));
78}
79
81 CompletionCallback onComplete,
83{
85 onComplete(*this, status);
86 return;
87 }
88
89 sdpDesc = std::string(inMsg.getStr());
90 onComplete(*this, CompletionStatus::Success);
91}
92
94 using namespace std::placeholders;
97 shared_from_this(), onComplete, _1));
98}
99
101 CompletionCallback onComplete,
103{
105 onComplete(*this, status);
106 return;
107 }
108
109 if(inMsg.getStr() == "disconnect"){
110 onComplete(*this, CompletionStatus::GracefulDisconnect);
111 return;
112 }
113
114 candidates.emplace_back(inMsg.getStr());
115 onComplete(*this, CompletionStatus::Success);
116}
117
119 return !sendQueue.empty() || outMsg.isSendingNow();
120}
121
122
123void Client::sendMsg(std::string_view msg){
125 sendQueue.emplace(msg);
126 return;
127 }
128
130 using namespace std::placeholders;
131 outMsg.async_sendMsg(socket, std::bind(&Client::onMsgSent, shared_from_this(), _1));
132}
133
136 std::cerr << "Failed to send a message\n";
137 }
138
139 if(sendQueue.empty())
140 return;
141
142 using namespace std::placeholders;
143 outMsg.setStr(sendQueue.front());
144 sendQueue.pop();
145 outMsg.async_sendMsg(socket, std::bind(&Client::onMsgSent, shared_from_this(), _1));
146}
asio::ip::tcp::socket socket
Definition client.hpp:96
void sendMsg(std::string_view msg)
Definition client.cpp:123
void readDescription(CompletionCallback onComplete)
Definition client.cpp:42
std::queue< std::string > sendQueue
Definition client.hpp:92
void readNameComplete(asio::ip::tcp::socket &socket, CompletionCallback onComplete, CompletionStatus status)
Definition client.cpp:49
Client(asio::ip::tcp::socket &&socket)
Definition client.cpp:40
Message inMsg
Definition client.hpp:94
Message outMsg
Definition client.hpp:95
void readCandidateComplete(CompletionCallback onComplete, CompletionStatus status)
Definition client.cpp:100
std::vector< std::string > candidates
Definition client.hpp:90
std::string roomName
Definition client.hpp:88
void readRoomComplete(asio::ip::tcp::socket &socket, CompletionCallback onComplete, CompletionStatus status)
Definition client.cpp:65
void readDescComplete(CompletionCallback onComplete, CompletionStatus status)
Definition client.cpp:80
std::function< void(Client &, CompletionStatus)> CompletionCallback
Definition client.hpp:51
std::string sdpDesc
Definition client.hpp:89
void readCandidate(CompletionCallback onComplete)
Definition client.cpp:93
bool isSendCallbackPending() const
Definition client.cpp:118
void onMsgSent(CompletionStatus status)
Definition client.cpp:134
std::string clientName
Definition client.hpp:87
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_sendMsg(asio::ip::tcp::socket &socket, CompletionCallback onComplete)
Definition message.cpp:67
bool isSendingNow() const
Definition message.hpp:61
void setStr(std::string_view msg)
Definition message.cpp:57
CompletionStatus