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 "streamserver.h"
42 
43 // Netxx includes
44 #include "address.h"
45 #include "types.h"
46 #include "probeinfo.h"
47 #include "serverbase.h"
48 #include "socket.h"
49 #include "accept.h"
50 
51 // standard includes
52 #include <memory>
53 
54 //####################################################################
55 namespace
56 {
57     void call_listen (Netxx::Socket *sockets, Netxx::size_type sockets_size, int backlog);
58 }
59 //####################################################################
StreamServer(port_type port,const Timeout & timeout,int listen_backlog)60 Netxx::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 //####################################################################
StreamServer(const Address & addr,const Timeout & timeout,int listen_backlog)71 Netxx::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 //####################################################################
~StreamServer(void)80 Netxx::StreamServer::~StreamServer (void)
81 {
82     delete pimpl_;
83 }
84 //####################################################################
accept_connection(void)85 Netxx::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 //####################################################################
get_probe_info(void) const92 const Netxx::ProbeInfo* Netxx::StreamServer::get_probe_info (void) const
93 {
94     return pimpl_->get_probe_info();
95 }
96 //####################################################################
init(const Address & addr,int backlog)97 void 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 //####################################################################
108 namespace
109 {
110     //####################################################################
call_listen(Netxx::Socket * sockets,Netxx::size_type sockets_size,int backlog)111     void call_listen (Netxx::Socket *sockets, Netxx::size_type sockets_size, int backlog)
112     {
113 	for (Netxx::size_type i=0; i<sockets_size; ++i) {
114 	    if (listen(sockets[i].get_socketfd(), backlog) != 0) {
115 		std::string error("listen(2) error: ");
116 		error += Netxx::str_error(Netxx::get_last_error());
117 		throw Netxx::Exception(error);
118 	    }
119 	}
120     }
121     //####################################################################
122 } // end anonymous namespace
123 //####################################################################
124 namespace Netxx
125 {
126     //####################################################################
operator ==(const StreamServer & ss,socket_type fd)127     bool operator== (const StreamServer &ss, socket_type fd)
128     { return ss.pimpl_->has_socket(fd); }
129     //####################################################################
operator ==(socket_type fd,const StreamServer & ss)130     bool operator== (socket_type fd, const StreamServer &ss)
131     { return ss == fd; }
132     //####################################################################
operator !=(const StreamServer & ss,socket_type fd)133     bool operator!= (const StreamServer &ss, socket_type fd)
134     { return !(ss == fd); }
135     //####################################################################
operator !=(socket_type fd,const StreamServer & ss)136     bool operator!= (socket_type fd, const StreamServer &ss)
137     { return !(ss == fd); }
138     //####################################################################
139 } // end Netxx namespace
140