/[resiprocate]/main/reTurn/TcpServer.cxx
ViewVC logotype

Contents of /main/reTurn/TcpServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10468 - (show annotations) (download)
Wed Sep 11 09:56:05 2013 UTC (6 years, 2 months ago) by Dpocock
File MIME type: text/plain
File size: 3828 byte(s)
reTurn: listen on IPv6 as well as IPv4
1 #include "TcpServer.hxx"
2 #include <boost/bind.hpp>
3 #include <rutil/WinLeakCheck.hxx>
4 #include <rutil/Logger.hxx>
5 #include "ReTurnSubsystem.hxx"
6
7 #define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
8
9 namespace reTurn {
10
11 TcpServer::TcpServer(asio::io_service& ioService, RequestHandler& requestHandler, const asio::ip::address& address, unsigned short port)
12 : mIOService(ioService),
13 mAcceptor(ioService),
14 mConnectionManager(),
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
38 void
39 TcpServer::start()
40 {
41 mAcceptor.async_accept(((TcpConnection*)mNewConnection.get())->socket(), boost::bind(&TcpServer::handleAccept, this, asio::placeholders::error));
42 }
43
44 void
45 TcpServer::handleAccept(const asio::error_code& e)
46 {
47 if (!e)
48 {
49 mConnectionManager.start(mNewConnection);
50
51 mNewConnection.reset(new TcpConnection(mIOService, mConnectionManager, mRequestHandler));
52 mAcceptor.async_accept(((TcpConnection*)mNewConnection.get())->socket(), boost::bind(&TcpServer::handleAccept, this, asio::placeholders::error));
53 }
54 else
55 {
56 ErrLog(<< "Error in handleAccept: " << e.value() << "-" << e.message());
57 if(e == asio::error::no_descriptors)
58 {
59 // Retry if too many open files (ie. out of socket descriptors)
60 mNewConnection.reset(new TcpConnection(mIOService, mConnectionManager, mRequestHandler));
61 mAcceptor.async_accept(((TcpConnection*)mNewConnection.get())->socket(), boost::bind(&TcpServer::handleAccept, this, asio::placeholders::error));
62 }
63 }
64 }
65
66 }
67
68
69 /* ====================================================================
70
71 Copyright (c) 2007-2008, Plantronics, Inc.
72 All rights reserved.
73
74 Redistribution and use in source and binary forms, with or without
75 modification, are permitted provided that the following conditions are
76 met:
77
78 1. Redistributions of source code must retain the above copyright
79 notice, this list of conditions and the following disclaimer.
80
81 2. Redistributions in binary form must reproduce the above copyright
82 notice, this list of conditions and the following disclaimer in the
83 documentation and/or other materials provided with the distribution.
84
85 3. Neither the name of Plantronics nor the names of its contributors
86 may be used to endorse or promote products derived from this
87 software without specific prior written permission.
88
89 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
90 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
91 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
92 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
93 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
94 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
95 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
96 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
97 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
98 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
99 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100
101 ==================================================================== */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27