monotone

monotone Mtn Source Tree

Root/netxx/stream.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 for the Netxx::Stream.
35**/
36
37// common header
38#include "common.h"
39
40// Netxx includes
41#include "netxx/stream.h"
42#include "netxx/probeinfo.h"
43#include "socket.h"
44
45// standard includes
46#include <memory>
47#include <algorithm>
48
49//####################################################################
50struct Netxx::Stream::pimpl
51{
52 pimpl (void) {}
53 explicit pimpl (socket_type socketfd) : socket_(socketfd) {}
54 pimpl (const pimpl &other) : socket_(other.socket_), pi_(other.pi_) {}
55
56 Socket socket_;
57 ProbeInfo pi_;
58};
59//####################################################################
60Netxx::Stream::Stream (const Address &address, const Timeout &timeout)
61 : StreamBase(timeout)
62{
63 std::auto_ptr<pimpl> ap(pimpl_ = new pimpl);
64
65 make_connection(pimpl_->socket_, address);
66 pimpl_->pi_.add_socket(pimpl_->socket_.get_socketfd());
67
68 ap.release();
69}
70//####################################################################
71Netxx::Stream::Stream (socket_type socketfd, const Timeout &timeout)
72 : StreamBase(timeout)
73{
74 std::auto_ptr<pimpl> ap(pimpl_ = new pimpl(socketfd));
75
76 pimpl_->pi_.add_socket(pimpl_->socket_.get_socketfd());
77
78 ap.release();
79}
80//####################################################################
81Netxx::Stream::Stream (const char *uri, port_type default_port, const Timeout &timeout)
82 : StreamBase(timeout)
83{
84 Address addr(uri, default_port);
85 std::auto_ptr<pimpl> ap(pimpl_ = new pimpl);
86
87 make_connection(pimpl_->socket_, addr);
88 pimpl_->pi_.add_socket(pimpl_->socket_.get_socketfd());
89
90 ap.release();
91}
92//####################################################################
93Netxx::Stream::Stream (const Stream &other)
94 : StreamBase(other.get_timeout())
95{
96 pimpl_ = new pimpl(*(other.pimpl_));
97}
98//####################################################################
99Netxx::Stream& Netxx::Stream::operator= (const Stream &other)
100{
101 Stream tmp(other);
102 swap(tmp);
103
104 return *this;
105}
106//####################################################################
107void Netxx::Stream::swap (Stream &other)
108{
109 pimpl_->socket_.swap(other.pimpl_->socket_);
110 pimpl_->pi_.swap(other.pimpl_->pi_);
111 swap_base(other);
112}
113//####################################################################
114Netxx::Stream::~Stream (void)
115{
116 delete pimpl_;
117}
118//####################################################################
119void Netxx::Stream::close (void)
120{
121 pimpl_->socket_.close();
122 pimpl_->pi_.clear();
123}
124//####################################################################
125int Netxx::Stream::get_socketfd (void) const
126{
127 return pimpl_->socket_.get_socketfd();
128}
129//####################################################################
130Netxx::signed_size_type Netxx::Stream::write (const void *buffer, size_type length)
131{
132 return pimpl_->socket_.write(buffer, length, get_timeout());
133}
134//####################################################################
135Netxx::signed_size_type Netxx::Stream::read (void *buffer, size_type length)
136{
137 return pimpl_->socket_.read(buffer, length, get_timeout());
138}
139//####################################################################
140const Netxx::ProbeInfo* Netxx::Stream::get_probe_info (void) const
141{
142 return &(pimpl_->pi_);
143}
144//####################################################################

Archive Download this file

Branches

Tags

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