File: serverbase.cxx

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (218 lines) | stat: -rw-r--r-- 7,160 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (C) 2001-2004 Peter J Jones (pjones@pmade.org)
 * All Rights Reserved
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of the Author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/** @file
 * This file contains the implementation of the Netxx::ServerBase class.
**/

// common header
#include "common.h"

// Netxx includes
#include "serverbase.h"
#include "netxx/address.h"
#include "netxx/sockopt.h"
#include "netxx/probeinfo.h"
#include "socket.h"

// standard includes
#include <map>
#include <vector>
#include <algorithm>
#include <functional>

//####################################################################
namespace 
{
#   ifndef WIN32
	struct unlink_functor : public std::unary_function<std::string, void> 
	{
	    void operator() (const std::string &file)
	    { unlink(file.c_str()); }
	};
#   endif
}
//####################################################################
Netxx::ServerBase::ServerBase (const Timeout &timeout)
    : timeout_(timeout), sockets_(0), sockets_size_(0)
{ /* must not call bind_to from here */ }
//####################################################################
Netxx::ServerBase::~ServerBase (void) 
{
#   ifndef WIN32
	if (sockets_ && !files_.empty()) {
	    std::for_each(sockets_, sockets_ + sockets_size_, std::mem_fun_ref(&Socket::close));
	    std::for_each(files_.begin(), files_.end(), unlink_functor());
	}
#   endif

    if (sockets_) delete [] sockets_;
}
//####################################################################
void Netxx::ServerBase::bind_to(const Address &addr, bool stream_server) 
{
    if (sockets_) throw Exception("bug in Netxx, ServerBase::sockets_ != 0");

    /*
     * it is safe to allocate this block of memory because the destructor
     * will free it even if this function dies with an exception.
     */
    sockets_size_ = addr.size();
    sockets_ = new Socket[sockets_size_];

    /*
     * now that we have an array of sockets we need to walk through each one
     * and set it up. We will use a temporay socket and make it call
     * socket() for us. Then we can set the reuse address socket option and
     * call bind.
     */
    Address::const_iterator addr_it=addr.begin(), addr_end=addr.end();
    for (size_type current_socket=0; addr_it != addr_end; ++addr_it, ++current_socket) {
	const sockaddr *sa = static_cast<const sockaddr*>(addr_it->get_sa());
	size_type sa_size = addr_it->get_sa_size();

	Socket::Type stype;
	switch (sa->sa_family) {
	    case AF_INET:
		stype = stream_server ? Socket::TCP : Socket::UDP;
		break;

#	ifndef NETXX_NO_INET6
	    case AF_INET6:
		stype = stream_server ? Socket::TCP6 : Socket::UDP6;
		break;
#	endif

#	ifndef WIN32
	    case AF_LOCAL:
		stype = stream_server ? Socket::LOCALSTREAM : Socket::LOCALDGRAM;
		break;
#	endif

	    default:
		stype = stream_server ? Socket::TCP : Socket::UDP;
		break;
	}

	Socket tmp_socket(stype);
	socket_type socketfd = tmp_socket.get_socketfd();
	tmp_socket.swap(sockets_[current_socket]);

	SockOpt socket_opt(socketfd);
	socket_opt.set_reuse_address();

#	ifndef NETXX_NO_INET6
	if (sa->sa_family == AF_INET6)
	    socket_opt.set_ipv6_listen_for_v6_only();
#	endif

	if (bind(socketfd, sa, sa_size) != 0) {
	    std::string error("bind(2) error: ");
	    error += Netxx::str_error(Netxx::get_last_error());
	    throw NetworkException(error);
	}

	sockets_map_[socketfd] = &(sockets_[current_socket]);
	pi_.add_socket(socketfd);

#	ifndef WIN32
	    /*
	     * check to see if we need to record a filename. we would need
	     * to record a filename for local domain sockets in order to
	     * remove the file when the server is done with it.
	     *
	     * also take this time to set the mode for the socket file to
	     * something sane. this may be a bad assumption here but I think
	     * this is a good value for a domain socket.
	     */
	    if (sa->sa_family == AF_LOCAL) {
		const sockaddr_un *saun = reinterpret_cast<const sockaddr_un*>(sa);

		/*
		 * This will make a local domain socket more like a real
		 * socket since anyone with local file system access can
		 * connect to it.
		 */
		chmod(saun->sun_path, 0666);

		if (saun->sun_path[0] == '/') {
		    files_.push_back(saun->sun_path);
		} else {
		    char buffer[MAXPATHLEN];

		    if (getcwd(buffer, sizeof(buffer))) {
 			std::string fullpath = buffer; fullpath += '/'; fullpath += saun->sun_path;
			files_.push_back(fullpath);
		    } else {
			files_.push_back(saun->sun_path);
		    }
		}
	    }
#	endif
    }

    probe_.add(*this);
}
//####################################################################
void Netxx::ServerBase::get_socket_list (Socket *&sockets, size_type &size) 
{
    sockets = sockets_;
    size = sockets_size_;
}
//####################################################################
Netxx::Socket* Netxx::ServerBase::get_readable_socket (void) 
{
    Probe::result_type rt = probe_.ready(timeout_, Probe::ready_read);
    if (rt.first == -1) return 0;
    return sockets_map_[rt.first];
}
//####################################################################
void Netxx::ServerBase::set_timeout (const Timeout &timeout) 
{
    timeout_ = timeout;
}
//####################################################################
const Netxx::Timeout& Netxx::ServerBase::get_timeout (void) const 
{
    return timeout_;
}
//####################################################################
const Netxx::ProbeInfo* Netxx::ServerBase::get_probe_info (void) const 
{
    return &pi_;
}
//####################################################################
bool Netxx::ServerBase::has_socket (socket_type socketfd) const 
{
    return sockets_map_.find(socketfd) != sockets_map_.end();
}
//####################################################################