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

#include <nat-helper.hpp>

Collaboration diagram for NatHelper:
Collaboration graph

Public Member Functions

 NatHelper ()
 
 NatHelper (int port)
 
 ~NatHelper ()=default
 
void run (bool async=true)
 
void stop ()
 

Private Member Functions

void cleanEmptyRooms ()
 
void onClientDesc (Client &client, CompletionStatus status)
 
void onConnectionAccepted (const std::error_code &ec)
 
void worker ()
 

Private Attributes

asio::ip::tcp::acceptor acceptor
 
std::map< Client *, std::shared_ptr< Client > > incomingClients
 
asio::io_service io_service
 
asio::ip::tcp::socket pendingSocket
 
int port
 
std::map< std::string, std::shared_ptr< Room > > rooms
 
std::thread worker_thread
 

Detailed Description

Definition at line 47 of file nat-helper.hpp.

Constructor & Destructor Documentation

◆ NatHelper() [1/2]

NatHelper::NatHelper ( )

Definition at line 50 of file nat-helper.cpp.

50 : NatHelper(12558)
51{
52
53}

◆ NatHelper() [2/2]

NatHelper::NatHelper ( int port)

Definition at line 41 of file nat-helper.cpp.

41 : port(port),
42 io_service(),
43 //work_guard(io_service.get_executor()),
46{
47
48}
asio::ip::tcp::acceptor acceptor
asio::io_service io_service
asio::ip::tcp::socket pendingSocket

◆ ~NatHelper()

NatHelper::~NatHelper ( )
default

Member Function Documentation

◆ cleanEmptyRooms()

void NatHelper::cleanEmptyRooms ( )
private

Definition at line 104 of file nat-helper.cpp.

104 {
105 for(auto it = rooms.begin(); it != rooms.end(); ){
106 if(it->second->isEmpty()){
107 std::cout << "Removing empty room " << it->first << "\n";
108 it = rooms.erase(it);
109 } else {
110 it++;
111 }
112 }
113}
std::map< std::string, std::shared_ptr< Room > > rooms

References rooms.

Referenced by onClientDesc().

◆ onClientDesc()

void NatHelper::onClientDesc ( Client & client,
CompletionStatus status )
private

Definition at line 115 of file nat-helper.cpp.

115 {
117 std::cerr << "Error reading client description" << std::endl;
118 incomingClients.erase(&client);
119 return;
120 }
121
122 const auto& roomName = client.getRoomName();
123 std::cout << "Moving client " << client.getClientName()
124 << " to room " << roomName
125 << std::endl;
126
127 auto roomIt = rooms.find(roomName);
128 if(roomIt == rooms.end()){
130 std::cout << "Creating room " << roomName << std::endl;
131 roomIt = rooms.insert({roomName, std::make_shared<Room>(roomName)}).first;
132 }
133
134 auto clientIt = incomingClients.find(&client);
135 auto& room = roomIt->second;
136 if(room->isFull()){
137 std::cerr << "Room " << roomName << " is full, dropping client" << std::endl;
138 } else {
139 roomIt->second->addClient(std::move(clientIt->second));
140 }
141 incomingClients.erase(clientIt);
142}
void cleanEmptyRooms()
std::map< Client *, std::shared_ptr< Client > > incomingClients
codec_t first
Definition sdl2.c:423

References cleanEmptyRooms(), first, incomingClients, rooms, and Success.

Referenced by onConnectionAccepted().

Here is the call graph for this function:

◆ onConnectionAccepted()

void NatHelper::onConnectionAccepted ( const std::error_code & ec)
private

Definition at line 87 of file nat-helper.cpp.

87 {
88 if(ec){
89 std::cerr << "Error accepting client connection: " << ec.message() << std::endl;
90 return;
91 }
92
93 using namespace std::placeholders;
94
95 auto client = std::make_shared<Client>(std::move(pendingSocket));
96 client->readDescription(
97 std::bind(&NatHelper::onClientDesc, this, _1, _2));
98 incomingClients.insert({client.get(), std::move(client)});
99
100 acceptor.async_accept(pendingSocket,
101 std::bind(&NatHelper::onConnectionAccepted, this, _1));
102}
void onClientDesc(Client &client, CompletionStatus status)
void onConnectionAccepted(const std::error_code &ec)

References acceptor, incomingClients, onClientDesc(), onConnectionAccepted(), and pendingSocket.

Referenced by onConnectionAccepted(), and run().

Here is the call graph for this function:

◆ run()

void NatHelper::run ( bool async = true)

Definition at line 62 of file nat-helper.cpp.

62 {
63 using namespace std::placeholders;
64
65 asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port);
66 acceptor.open(endpoint.protocol());
67 acceptor.bind(endpoint);
68 acceptor.listen(asio::socket_base::max_connections);
69 acceptor.set_option(asio::ip::tcp::acceptor::reuse_address(false));
70 acceptor.async_accept(pendingSocket,
71 std::bind(&NatHelper::onConnectionAccepted, this, _1));
72
73 if(async){
74 std::cout << "Starting thread" << std::endl;
75 worker_thread = std::thread(&NatHelper::worker, this);
76 } else {
77 worker();
78 }
79}
std::thread worker_thread
void worker()

References acceptor, onConnectionAccepted(), pendingSocket, port, worker(), and worker_thread.

Referenced by main(), and worker().

Here is the call graph for this function:

◆ stop()

void NatHelper::stop ( )

Definition at line 81 of file nat-helper.cpp.

81 {
82 std::cout << "Stopping..." << std::endl;
83 io_service.stop();
84 worker_thread.join();
85}

References io_service, and worker_thread.

◆ worker()

void NatHelper::worker ( )
private

Definition at line 56 of file nat-helper.cpp.

56 {
57 std::cout << "Running" << std::endl;
58 io_service.run();
59 std::cout << "End" << std::endl;
60}

References io_service, and run().

Referenced by run().

Here is the call graph for this function:

Member Data Documentation

◆ acceptor

asio::ip::tcp::acceptor NatHelper::acceptor
private

Definition at line 70 of file nat-helper.hpp.

Referenced by onConnectionAccepted(), and run().

◆ incomingClients

std::map<Client *, std::shared_ptr<Client> > NatHelper::incomingClients
private

Definition at line 73 of file nat-helper.hpp.

Referenced by onClientDesc(), and onConnectionAccepted().

◆ io_service

asio::io_service NatHelper::io_service
private

Definition at line 66 of file nat-helper.hpp.

Referenced by stop(), and worker().

◆ pendingSocket

asio::ip::tcp::socket NatHelper::pendingSocket
private

Definition at line 71 of file nat-helper.hpp.

Referenced by onConnectionAccepted(), and run().

◆ port

int NatHelper::port
private

Definition at line 64 of file nat-helper.hpp.

Referenced by run().

◆ rooms

std::map<std::string, std::shared_ptr<Room> > NatHelper::rooms
private

Definition at line 74 of file nat-helper.hpp.

Referenced by cleanEmptyRooms(), and onClientDesc().

◆ worker_thread

std::thread NatHelper::worker_thread
private

Definition at line 68 of file nat-helper.hpp.

Referenced by run(), and stop().


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