reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
TcpServer.cxx
Go to the documentation of this file.
1#include "TcpServer.hxx"
2#include <functional>
4#include <rutil/Logger.hxx>
5#include "ReTurnSubsystem.hxx"
6
7#define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
8
9namespace reTurn {
10
11TcpServer::TcpServer(asio::io_context& ioService, RequestHandler& requestHandler, const asio::ip::address& address, unsigned short port)
12: mIOService(ioService),
13 mAcceptor(ioService),
14 mConnectionManager(requestHandler.getTurnManager()),
15 mNewConnection(new TcpConnection(ioService, mConnectionManager, requestHandler)),
16 mRequestHandler(requestHandler)
17{
18 // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
19 asio::ip::tcp::endpoint endpoint(address, port);
20
21 mAcceptor.open(endpoint.protocol());
22 mAcceptor.set_option(asio::ip::tcp::acceptor::reuse_address(true));
23#ifdef USE_IPV6
24#ifdef __linux__
25 if(address.is_v6())
26 {
27 asio::ip::v6_only v6_opt(true);
28 mAcceptor.set_option(v6_opt);
29 }
30#endif
31#endif
32 mAcceptor.bind(endpoint);
33 mAcceptor.listen();
34
35 InfoLog(<< "TcpServer started. Listening on " << address.to_string() << ":" << port);
36}
37
38void
40{
41 mAcceptor.async_accept(((TcpConnection*)mNewConnection.get())->socket(), std::bind(&TcpServer::handleAccept, this, std::placeholders::_1));
42}
43
44void
45TcpServer::handleAccept(const asio::error_code& e)
46{
47 if (!e)
48 {
49 // Started if its client's address doesn't already have too many connections
50 asio::error_code ec;
51 const asio::ip::address remoteAddress = ((TcpConnection*)mNewConnection.get())->socket().remote_endpoint(ec).address();
52 if(!ec)
53 {
54 mConnectionManager.start(mNewConnection, remoteAddress);
55 }
56 else
57 {
58 mNewConnection->stop(); // eg. the client has gone already
59 }
60
61 mNewConnection.reset(new TcpConnection(mIOService, mConnectionManager, mRequestHandler));
62 mAcceptor.async_accept(((TcpConnection*)mNewConnection.get())->socket(), std::bind(&TcpServer::handleAccept, this, std::placeholders::_1));
63 }
64 else
65 {
66 ErrLog(<< "Error in handleAccept: " << e.value() << "-" << e.message());
67 if(e == asio::error::no_descriptors)
68 {
69 // Retry if too many open files (ie. out of socket descriptors)
70 mNewConnection.reset(new TcpConnection(mIOService, mConnectionManager, mRequestHandler));
71 mAcceptor.async_accept(((TcpConnection*)mNewConnection.get())->socket(), std::bind(&TcpServer::handleAccept, this, std::placeholders::_1));
72 }
73 }
74}
75
76}
77
78
79/* ====================================================================
80
81 Copyright (c) 2008-2026 SIP Spectrum, Inc. https://www.sipspectrum.com
82 Copyright (c) 2007-2008, Plantronics, Inc.
83 All rights reserved.
84
85 Redistribution and use in source and binary forms, with or without
86 modification, are permitted provided that the following conditions are
87 met:
88
89 1. Redistributions of source code must retain the above copyright
90 notice, this list of conditions and the following disclaimer.
91
92 2. Redistributions in binary form must reproduce the above copyright
93 notice, this list of conditions and the following disclaimer in the
94 documentation and/or other materials provided with the distribution.
95
96 3. Neither the name of Plantronics nor the names of its contributors
97 may be used to endorse or promote products derived from this
98 software without specific prior written permission.
99
100 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
101 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
102 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
103 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
104 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
105 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
106 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
107 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
108 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
109 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
110 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
111
112 ==================================================================== */
#define InfoLog(args_)
Definition Logger.hxx:131
#define ErrLog(args_)
Definition Logger.hxx:137
void start(ConnectionPtr c, const asio::ip::address &remoteAddress)
Add the specified connection, from remoteAddress, to the manager and start it - or,...
The common handler for all incoming requests.
Represents a single connection from a client.
TcpServer(const TcpServer &)=delete
void list()
Definition userAdmin.cxx:66