monotone

monotone Mtn Source Tree

Root/netxx/socket.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::Socket class.
35**/
36
37#ifndef _netxx_socket_h_
38#define _netxx_socket_h_
39
40// Netxx includes
41#include <netxx/types.h>
42#include <netxx/address.h>
43#include "probe_impl.h"
44
45namespace Netxx {
46 class Timeout;
47
48/**
49 * The Socket class is a wrapper around the operating system socket
50 * functions. It is the low level socket interface for the Netxx code, due
51 * to the fact that there are at least two higher level classes (Server and
52 * Client).
53**/
54class Socket {
55public:
56 enum Type {
57TCP,///< TCP IPv4 Address
58TCP6,///< TCP IPv6 Address
59UDP,///< UDP IPv4 Address
60UDP6,///< UDP IPv6 Address
61LOCALSTREAM,///< Unix Domain TCP
62LOCALDGRAM///< Unix Domain UDP
63 };
64
65 Socket (void);
66
67 explicit Socket (Type type);
68
69 //####################################################################
70 /**
71 * Socket class ctor that creates a socket from a already connected
72 * socket file descriptor.
73 *
74 * @param sockedfd The socket file descriptor to use
75 * @author Peter Jones
76 **/
77 //####################################################################
78 explicit Socket (socket_type socketfd);
79
80 //####################################################################
81 /**
82 * Socket class copy ctor. The ctor will copy the other socket, so
83 * you will have two sockets with the same file desc.
84 *
85 * @param other The other Socket
86 * @author Peter Jones
87 **/
88 //####################################################################
89 Socket (const Socket &other);
90
91 //####################################################################
92 /**
93 * Assignment operator
94 *
95 * @param other The other Socket
96 * @author Peter Jones
97 **/
98 //####################################################################
99 Socket& operator= (const Socket& other);
100
101 //####################################################################
102 /**
103 * Swap this socket for another one.
104 *
105 * @param other The other Socket to swap with
106 * @author Peter Jones
107 **/
108 //####################################################################
109 void swap (Socket &other);
110
111 //####################################################################
112 /**
113 * Socket class dtor
114 *
115 * @author Peter Jones
116 **/
117 //####################################################################
118 ~Socket (void);
119
120 //####################################################################
121 /**
122 * Read data from the socket and place it into the user buffer. If the given
123 * timeout is not a null timeout then this method will only wait for data
124 * until the timeout expires, at which time it will return -1. If you give a
125 * null timeout, this method will block until data arives.
126 *
127 * @param buffer The user buffer to write data into
128 * @param length The max bytes to place into buffer
129 * @param timeout How long to wait for data to arive
130 * @return The number of bytes read from the socket, or -1 for timeout
131 * @author Peter Jones
132 **/
133 //####################################################################
134 signed_size_type read (void *buffer, size_type length, const Timeout &timeout);
135
136 //####################################################################
137 /**
138 * Write data from a user buffer to the socket. If the given timeout
139 * is not a null timeout then this method will only wait for the
140 * socket to become writable until the timeout expires, at which
141 * point it will return -1. If you give a null timeout then this
142 * method will block until all data has been written to the socket.
143 *
144 * @param buffer The user buffer to read from
145 * @param length The number of bytes to read from the user buffer
146 * @return The number of bytes written, -1 for timeout
147 * @author Peter Jones
148 **/
149 //####################################################################
150 signed_size_type write (const void *buffer, size_type length, const Timeout &timeout);
151
152 //####################################################################
153 /**
154 * Check to see if there is data to read from a socket. If you give a valid
155 * timeout then this method will only block for that timeout.
156 *
157 * @param timeout The amount of time to wait for the socket to become
158 * readable
159 * @return True if there is data to read, false otherwise
160 * @author Peter Jones
161 **/
162 //####################################################################
163 bool readable (const Timeout &timeout);
164
165 //####################################################################
166 /**
167 * Check to see if a socket can be written to. If you give a timeout
168 * value then this method will not block longer then the timeout.
169 *
170 * @param timeout How long to wait for the socket to become writable.
171 * @return True if you can write data to the socket, false otherwise
172 * @author Peter Jones
173 **/
174 //####################################################################
175 bool writable (const Timeout &timeout);
176
177 //####################################################################
178 /**
179 * Check to see if a socket can be read from or written to. If you give
180 * a timeout value then this method will not block longer then the
181 * timeout.
182 *
183 * @param timeout How long to wait for the socket to become writable.
184 * @return True if you can read from or write to the socket.
185 * @reutrn False if there is a timeout.
186 * @author Peter Jones
187 **/
188 //####################################################################
189 bool readable_or_writable (const Timeout &timeout);
190
191 //####################################################################
192 /**
193 * Close the socket.
194 *
195 * @author Peter Jones
196 **/
197 //####################################################################
198 void close (void);
199
200 //####################################################################
201 /**
202 * Release the socket fd so that it won't be closed by the close
203 * member function or the dtor.
204 *
205 * @author Peter Jones
206 **/
207 //####################################################################
208 void release (void);
209
210 //####################################################################
211 /**
212 * Get the socket type. This value is only valid when a socket is
213 * constructed with a type or with another socket that was constructed
214 * with a type.
215 *
216 * @return The socket type.
217 * @author Peter Jones
218 **/
219 //####################################################################
220 Type get_type (void) const;
221
222 //####################################################################
223 /**
224 * Get the file descriptor for this socket.
225 *
226 * @return The fd for this socket
227 * @author Peter Jones
228 **/
229 //####################################################################
230 socket_type get_socketfd (void) const;
231
232 //####################################################################
233 /**
234 * Set the socket file descriptor. This will close the current
235 * descriptor if needed and reset this Socket to control the new socket
236 * file descriptor. That means that the Socket destructor will close the
237 * given file descriptor unless you call the release function.
238 *
239 * @param socketfd The socket file descriptor to take over
240 * @author Peter Jones
241 **/
242 //####################################################################
243 void set_socketfd (socket_type socketfd);
244
245 //####################################################################
246 /**
247 * Find out if the socket is open and ready
248 *
249 * @return True if socket is NOT ready
250 * @author Peter Jones
251 **/
252 //####################################################################
253 bool operator! (void) const;
254
255 bool operator< (const Socket &other) const;
256 bool operator< (socket_type socketfd) const;
257
258private:
259 socket_type socketfd_;
260 bool probe_ready_;
261 bool type_ready_;
262 Probe_impl probe_;
263 Type type_;
264
265}; // end Netxx::Socket
266
267bool operator== (const Socket &first, const Socket &second);
268bool operator!= (const Socket &first, const Socket &second);
269
270} // end Netxx namespace
271#endif

Archive Download this file

Branches

Tags

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