monotone

monotone Mtn Source Tree

Root/netxx/streamserver.cxx

1/*
2 * Copyright (C) 2001-2004 Peter J Jones (pjones@pmade.org)
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of the Author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/** @file
34 * This file contains the implementation of the Netxx::StreamServer class.
35**/
36
37// common header
38#include "common.h"
39
40// definition include
41#include "netxx/streamserver.h"
42
43// Netxx includes
44#include "netxx/address.h"
45#include "netxx/types.h"
46#include "netxx/probeinfo.h"
47#include "serverbase.h"
48#include "socket.h"
49#include "accept.h"
50
51// standard includes
52#include <memory>
53
54//####################################################################
55namespace
56{
57 void call_listen (Netxx::Socket *sockets, Netxx::size_type sockets_size, int backlog);
58}
59//####################################################################
60Netxx::StreamServer::StreamServer (port_type port, const Timeout &timeout, int listen_backlog)
61{
62 std::auto_ptr<ServerBase> ap(pimpl_ = new ServerBase(timeout));
63
64 Address addr;
65 addr.add_all_addresses(port);
66 init(addr, listen_backlog);
67
68 ap.release();
69}
70//####################################################################
71Netxx::StreamServer::StreamServer (const Address &addr, const Timeout &timeout, int listen_backlog)
72{
73 std::auto_ptr<ServerBase> ap(pimpl_ = new ServerBase(timeout));
74
75 init(addr, listen_backlog);
76
77 ap.release();
78}
79//####################################################################
80Netxx::StreamServer::~StreamServer (void)
81{
82 delete pimpl_;
83}
84//####################################################################
85Netxx::Peer Netxx::StreamServer::accept_connection (void)
86{
87 Socket *ready_socket = pimpl_->get_readable_socket();
88 if (!ready_socket) return Peer();
89 return call_accept(*ready_socket, pimpl_->get_timeout());
90}
91//####################################################################
92const Netxx::ProbeInfo* Netxx::StreamServer::get_probe_info (void) const
93{
94 return pimpl_->get_probe_info();
95}
96//####################################################################
97void Netxx::StreamServer::init (const Address &addr, int backlog)
98{
99 pimpl_->bind_to(addr, true);
100
101 Socket *sockets;
102 size_type sockets_size;
103
104 pimpl_->get_socket_list(sockets, sockets_size);
105 call_listen(sockets, sockets_size, backlog);
106}
107//####################################################################
108namespace
109{
110 //####################################################################
111 void call_listen (Netxx::Socket *sockets, Netxx::size_type sockets_size, int backlog)
112 {
113for (Netxx::size_type i=0; i<sockets_size; ++i) {
114 if (listen(sockets[i].get_socketfd(), backlog) != 0) {
115std::string error("listen(2) error: ");
116error += Netxx::str_error(Netxx::get_last_error());
117throw Netxx::Exception(error);
118 }
119}
120 }
121 //####################################################################
122} // end anonymous namespace
123//####################################################################
124namespace Netxx
125{
126 //####################################################################
127 bool operator== (const StreamServer &ss, socket_type fd)
128 { return ss.pimpl_->has_socket(fd); }
129 //####################################################################
130 bool operator== (socket_type fd, const StreamServer &ss)
131 { return ss == fd; }
132 //####################################################################
133 bool operator!= (const StreamServer &ss, socket_type fd)
134 { return !(ss == fd); }
135 //####################################################################
136 bool operator!= (socket_type fd, const StreamServer &ss)
137 { return !(ss == fd); }
138 //####################################################################
139} // end Netxx namespace

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status