monotone

monotone Mtn Source Tree

Root/netxx/datagramserver.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::DatagramServer class.
35**/
36
37// common header
38#include "common.h"
39
40// Netxx includes
41#include "netxx/datagramserver.h"
42#include "netxx/datagram.h"
43#include "netxx/address.h"
44#include "netxx/probeinfo.h"
45#include "serverbase.h"
46#include "socket.h"
47#include "recvfrom.h"
48
49// standard includes
50#include <memory>
51#include <utility>
52
53//####################################################################
54Netxx::DatagramServer::DatagramServer (port_type port, const Timeout &timeout)
55{
56 std::auto_ptr<ServerBase> ap(pimpl_ = new ServerBase(timeout));
57
58 Address addr;
59 addr.add_all_addresses(port);
60 pimpl_->bind_to(addr, false);
61
62 ap.release();
63}
64//####################################################################
65Netxx::DatagramServer::DatagramServer (const Address &addr, const Timeout &timeout)
66{
67 std::auto_ptr<ServerBase> ap(pimpl_ = new ServerBase(timeout));
68
69 pimpl_->bind_to(addr, false);
70
71 ap.release();
72}
73//####################################################################
74Netxx::DatagramServer::~DatagramServer (void)
75{
76 delete pimpl_;
77}
78//####################################################################
79Netxx::DatagramServer::receive_type Netxx::DatagramServer::receive (void *buffer, size_type length)
80{
81 Socket *ready_socket = pimpl_->get_readable_socket();
82 if (!ready_socket) return std::make_pair(static_cast<signed_size_type>(-1), Peer());
83 return call_recvfrom(*ready_socket, buffer, length);
84}
85//####################################################################
86Netxx::signed_size_type Netxx::DatagramServer::send (const Peer &peer, const void *buffer, size_type length)
87{
88 /*
89 * We will create a Netxx::Datagram to do the dirty work for us. We take
90 * care that the Datagram class does not close our server socket! We
91 * give the Datagram peer.get_socketfd() because that is the server
92 * socket that recevived the datagram.
93 */
94 Datagram client(peer.get_socketfd(), pimpl_->get_timeout());
95 client.release();
96 return client.send(peer, buffer, length);
97}
98//####################################################################
99const Netxx::ProbeInfo* Netxx::DatagramServer::get_probe_info (void) const
100{
101 return pimpl_->get_probe_info();
102}
103//####################################################################
104namespace Netxx
105{
106 //####################################################################
107 bool operator== (const DatagramServer &ds, socket_type fd)
108 { return ds.pimpl_->has_socket(fd); }
109 //####################################################################
110 bool operator== (socket_type fd, const DatagramServer &ds)
111 { return ds == fd; }
112 //####################################################################
113 bool operator!= (const DatagramServer &ds, socket_type fd)
114 { return !(ds == fd); }
115 //####################################################################
116 bool operator!= (socket_type fd, const DatagramServer &ds)
117 { return !(ds == fd); }
118 //####################################################################
119} // end Netxx namespace

Archive Download this file

Branches

Tags

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