monotone

monotone Mtn Source Tree

Root/netxx/streambase.h

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 definition of the Netxx::StreamBase class.
35**/
36
37#ifndef _netxx_streambase_h_
38#define _netxx_streambase_h_
39
40// Netxx includes
41#include <netxx/types.h>
42#include <netxx/address.h>
43#include <netxx/timeout.h>
44
45namespace Netxx {
46 // forward declarations
47 class Socket;
48 class ProbeInfo;
49
50/**
51 * The Netxx::StreamBase class acts as a base class for the various stream
52 * classes. You cannot directly instianciate an object of this class because
53 * it has pure virtual funtions.
54**/
55class StreamBase {
56public:
57 //####################################################################
58 /**
59 * Create a new StreamBase and set the timeout for stream operations.
60 *
61 * @param timeout The timeout to use for stream operations.
62 * @author Peter Jones
63 **/
64 //####################################################################
65 explicit StreamBase (const Timeout &timeout=Timeout());
66
67 //####################################################################
68 /**
69 * StreamBase virutal destructor. Cleanup the streambase.
70 *
71 * @author Peter Jones
72 **/
73 //####################################################################
74 virtual ~StreamBase (void);
75
76 //####################################################################
77 /**
78 * Set the timeout that is used for various stream operations.
79 *
80 * @param timeout The timeout to use.
81 * @author Peter Jones
82 **/
83 //####################################################################
84 void set_timeout (const Timeout &timeout);
85
86 //####################################################################
87 /**
88 * Get the current timeout value that is used for stream operations.
89 *
90 * @return The current timeout value.
91 * @author Peter Jones
92 **/
93 //####################################################################
94 const Timeout& get_timeout (void) const;
95
96 //####################################################################
97 /**
98 * Read data from the stream into the given buffer. This operation will
99 * place the read data into the buffer unless there is an error or
100 * timeout. If a stream fatal error occures an exception is throw.
101 *
102 * @param buffer The buffer to place read data into.
103 * @param length The avaliable size for the given buffer.
104 * @return Greater than 0: For the bytes read into the buffer.
105 * @return 0 If the peer closed the connection
106 * @return -1: For a timeout
107 * @author Peter Jones
108 **/
109 //####################################################################
110 virtual signed_size_type read (void *buffer, size_type length) = 0;
111
112 //####################################################################
113 /**
114 * Write data from a buffer into the stream. If a stream fatal error
115 * occures an exception is thrown.
116 *
117 * @param buffer The data to write to the stream.
118 * @param length The amount of data to use from the buffer.
119 * @return Greater than 0: The number of bytes written to the stream.
120 * @return 0 If the peer closed the connection.
121 * @return -1: for a timeout.
122 * @author Peter Jones
123 **/
124 //####################################################################
125 virtual signed_size_type write (const void *buffer, size_type length) = 0;
126
127 //####################################################################
128 /**
129 * Close the stream connection with the peer. After the connection is
130 * closed you should not use any of the other stream member functions.
131 *
132 * @author Peter Jones
133 **/
134 //####################################################################
135 virtual void close (void) = 0;
136
137 //####################################################################
138 /**
139 * Return the file descriptor that is being used for the stream
140 * connection.
141 *
142 * @return The file descriptor for this connection.
143 * @author Peter Jones
144 **/
145 //####################################################################
146 virtual socket_type get_socketfd (void) const = 0;
147
148 //####################################################################
149 /**
150 * Get information about how this StreamBase should be probed from
151 * the Netxx::Probe class.
152 *
153 * @return A Netxx::ProbeInfo object.
154 * @author Peter Jones
155 **/
156 //####################################################################
157 virtual const ProbeInfo* get_probe_info (void) const = 0;
158protected:
159 //####################################################################
160 /**
161 * This protected member function is used to establish a connection with
162 * one of the addresses given. This function will throw an exception if
163 * it cannot make a connection with any of the given addresses.
164 *
165 * @param socket On successful return this will be the socket for the conection
166 * @param address The list of addresses to try
167 * @author Peter Jones
168 **/
169 //####################################################################
170 void make_connection (Socket &socket, const Address &address);
171
172 //####################################################################
173 /**
174 * Used by the stream classes so they can implement a swap() member
175 * function.
176 *
177 * @param other The other StreamBase to swap with.
178 * @author Peter Jones
179 **/
180 //####################################################################
181 void swap_base (StreamBase &other);
182
183private:
184 Timeout timeout_;
185
186}; // end Netxx::StreamBase class
187} // end Netxx namespace
188#endif

Archive Download this file

Branches

Tags

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