File: skstream.h

package info (click to toggle)
skstream 0.3.9-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 1,896 kB
  • sloc: sh: 10,997; cpp: 2,605; makefile: 64
file content (409 lines) | stat: -rw-r--r-- 11,644 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**************************************************************************
 FreeSockets - Portable C++ classes for IP(sockets) applications. (v0.3)
 Copyright (C) 2000-2001 Rafael Guterres Jeffman
           (C) 2002-2006 Alistair Riddoch

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

**************************************************************************/

/**
 * This software package has been extensively modified by members of the
 * Worldforge Project. See the file ChangeLog for details.
 *
 * $Id$
 *
 */
#ifndef RGJ_FREE_STREAM_H_
#define RGJ_FREE_STREAM_H_

#include <iostream>

#include <skstream/sksocket.h>

/////////////////////////////////////////////////////////////////////////////
// class socketbuf
/////////////////////////////////////////////////////////////////////////////

/// A base class for stream buffers that handle sockets
class socketbuf : public std::streambuf {
private:
  std::streambuf::char_type *_buffer;

protected:
  SOCKET_TYPE _socket;

  timeval _underflow_timeout;
  timeval _overflow_timeout;

private:
  /// Not implemented. Copying a socket buffer is not permited.
  socketbuf(const socketbuf&);
  /// Not implemented. Copying a socket buffer is not permited.
  socketbuf& operator=(const socketbuf&);

protected:
  bool Timeout;

public:
  /** Make a new socket buffer from an existing socket, with optional
   *  buffer sizes.
   */
  explicit socketbuf(SOCKET_TYPE sock, std::streamsize insize = 0x8000,
                                       std::streamsize outsize = 0x8000);
  /** Make a new socket buffer from an existing socket, with an existing
   *  buffer.
   */
  socketbuf(SOCKET_TYPE sock, std::streambuf::char_type * buf,
                              std::streamsize length);

  /// Destroy the socket buffer.
  virtual ~socketbuf();

  /// Set the existing socket that this buffer should use.
  void setSocket(SOCKET_TYPE sock);

  /// Get the socket that this buffer uses.
  SOCKET_TYPE getSocket() const {
      return _socket; 
  }

  /** Set up a timeout value after which an error flag is set if the socket
   *  is not ready for a read.
   */
  void setReadTimeout(unsigned sec, unsigned usec=0) {
    _underflow_timeout.tv_sec  = sec;
    _underflow_timeout.tv_usec = usec;
  }

  /** Set up a timeout value after which an error flag is set if the socket
   *  is not ready for a write.
   */
  void setWriteTimeout(unsigned sec, unsigned usec=0) {
    _overflow_timeout.tv_sec  = sec;
    _overflow_timeout.tv_usec = usec;
  }

  void setTimeout(unsigned sec, unsigned usec=0) {
    setWriteTimeout(sec, usec);
  }

  /// Return the flag indicating whether a timeout has occured.
  bool timeout() const {
    return Timeout;
  }

protected:
  /// Handle writing data from the buffer to the socket.
  virtual int_type overflow(int_type nCh = traits_type::eof()) = 0;
  /// Handle reading data from the socket to the buffer.
  virtual int_type underflow() = 0;

  /// Flush the output buffer.
  virtual int sync();

  /** Set the buffer area this stream buffer uses. Only works if not already
   *  set.
   */
  std::streambuf * setbuf(std::streambuf::char_type * buf, std::streamsize len);
};

/// A stream buffer class that handles stream sockets
class stream_socketbuf : public socketbuf {
public:
  /** Make a new socket buffer from an existing socket, with optional
   *  buffer sizes.
   */
  explicit stream_socketbuf(SOCKET_TYPE sock,
                            std::streamsize insize = 0x8000,
                            std::streamsize outsize = 0x8000);
  /** Make a new socket buffer from an existing socket, with an existing
   *  buffer.
   */
  stream_socketbuf(SOCKET_TYPE sock, std::streambuf::char_type * buf,
                                     std::streamsize length);

  /// Destroy the socket buffer.
  virtual ~stream_socketbuf();

protected:
  /// Handle writing data from the buffer to the socket.
  virtual int_type overflow(int_type nCh = traits_type::eof());
  /// Handle reading data from the socket to the buffer.
  virtual int_type underflow();

};

/// A stream buffer class that handles datagram sockets
class dgram_socketbuf : public socketbuf {
public:
  /** Make a new socket buffer from an existing socket, with optional
   *  buffer sizes.
   */
  explicit dgram_socketbuf(SOCKET_TYPE sock, std::streamsize insize = 0x8000,
                                             std::streamsize outsize = 0x8000);
  /** Make a new socket buffer from an existing socket, with an existing
   *  buffer.
   */
  dgram_socketbuf(SOCKET_TYPE sock,
                  std::streambuf::char_type * buf,
                  std::streamsize length);

  /// Destroy the socket buffer.
  virtual ~dgram_socketbuf();

  bool setTarget(const std::string& address, unsigned port, int proto);

  void setOutpeer(const sockaddr_storage & peer) { 
    out_peer = peer; 
  }

  const sockaddr_storage & getOutpeer() const {
    return out_peer; 
  }

  const sockaddr_storage & getInpeer() const { 
    return in_peer; 
  }

  SOCKLEN getOutpeerSize() const {
    return out_p_size;
  }

  SOCKLEN getInpeerSize() const {
    return in_p_size;
  }

protected:
  /// Target address of datagrams sent via this stream
  sockaddr_storage out_peer;
  /// Source address of last datagram received by this stream
  sockaddr_storage in_peer;
  /// Size of target address
  SOCKLEN out_p_size;
  /// Size of source address
  SOCKLEN in_p_size;

  /// Handle writing data from the buffer to the socket.
  virtual int_type overflow(int_type nCh = traits_type::eof());
  /// Handle reading data from the socket to the buffer.
  virtual int_type underflow();

};

/////////////////////////////////////////////////////////////////////////////
// class socket_stream
/////////////////////////////////////////////////////////////////////////////

/// A base class for iostreams that handles stream sockets
class basic_socket_stream : public basic_socket, public std::iostream {
protected:
  socketbuf & _sockbuf;
  int m_protocol;

public:
  /// Make a socket stream.
  basic_socket_stream(socketbuf & buffer, int proto = FreeSockets::proto_IP);

  // Destructor
  virtual ~basic_socket_stream();

  bool fail();

  bool operator!() {
    return fail();
  }

  bool timeout() const {
    return _sockbuf.timeout();
  }

  virtual SOCKET_TYPE getSocket() const;

  // Needs to be virtual to handle in-progress connect()'s for
  // tcp sockets
  virtual void close();

  void shutdown(bool wr_only = false);

  void setSocket(SOCKET_TYPE sock) {
    _sockbuf.setSocket(sock);
  }

  void setTimeout(unsigned sec, unsigned usec=0) { 
    _sockbuf.setTimeout(sec,usec); 
  }

  int getProtocol() const { 
    return m_protocol; 
  }
};

/////////////////////////////////////////////////////////////////////////////
// class stream_socket_stream
/////////////////////////////////////////////////////////////////////////////

class stream_socket_stream : public basic_socket_stream {
private:
  stream_socket_stream(const stream_socket_stream&);
  stream_socket_stream& operator=(const stream_socket_stream& socket);

protected:
  SOCKET_TYPE _connecting_socket;

  stream_socket_stream();
  stream_socket_stream(SOCKET_TYPE socket);
public:
  virtual ~stream_socket_stream();
  
  virtual void close();
  virtual SOCKET_TYPE getSocket() const;

  bool connect_pending() const {
    return (_connecting_socket != INVALID_SOCKET);
  }
};

/////////////////////////////////////////////////////////////////////////////
// class tcp_socket_stream
/////////////////////////////////////////////////////////////////////////////

struct addrinfo;

/// An iostream class that handle TCP sockets
class tcp_socket_stream : public stream_socket_stream {
private:
  tcp_socket_stream(const tcp_socket_stream&);

  tcp_socket_stream& operator=(const tcp_socket_stream& socket);

  struct addrinfo * _connecting_address;
  struct addrinfo * _connecting_addrlist;

public:
  tcp_socket_stream();
  tcp_socket_stream(SOCKET_TYPE socket);

  tcp_socket_stream(const std::string& address, int service,
                    bool nonblock = false);

  tcp_socket_stream(const std::string& address, int service,
                    unsigned int milliseconds);

  virtual ~tcp_socket_stream();

  int open(const std::string& address, int service, bool nonblock = false);
  int open(const std::string& address, int service, unsigned int milliseconds);
  int open(struct addrinfo *, bool nonblock = false);
  int open_next();

  const std::string getRemoteHost(bool lookup = false) const;
  const std::string getRemoteService(bool lookup = false) const;
  bool isReady(unsigned int milliseconds = 0);
};

/// An iostream class that handle IP datagram sockets
class dgram_socket_stream : public basic_socket_stream {
private:
  dgram_socket_stream(const dgram_socket_stream&);

  dgram_socket_stream& operator=(const dgram_socket_stream& socket);

protected:
  dgram_socketbuf & dgram_sockbuf;

  int bindToIpService(int service, int type, int protocol);

public:
  dgram_socket_stream();

  virtual ~dgram_socket_stream();

  bool setTarget(const std::string& address, unsigned port) { 
    return dgram_sockbuf.setTarget(address, port, m_protocol); 
  }

  void setOutpeer(const sockaddr_storage& peer) { 
    return dgram_sockbuf.setOutpeer(peer); 
  }

  const sockaddr_storage & getOutpeer() const { 
    return dgram_sockbuf.getOutpeer(); 
  }

  const sockaddr_storage & getInpeer() const { 
    return dgram_sockbuf.getInpeer(); 
  }

  SOCKLEN getOutpeerSize() const {
    return dgram_sockbuf.getOutpeerSize();
  }

  SOCKLEN getInpeerSize() const {
    return dgram_sockbuf.getInpeerSize();
  }
};


/////////////////////////////////////////////////////////////////////////////
// class udp_socket_stream
/////////////////////////////////////////////////////////////////////////////

/// An iostream class that handle UDP sockets
class udp_socket_stream : public dgram_socket_stream {
private:
  udp_socket_stream(const udp_socket_stream&);

  udp_socket_stream& operator=(const udp_socket_stream& socket);

public:
  udp_socket_stream();

  virtual ~udp_socket_stream();

  int open(int service);
};

#ifdef SOCK_RAW

/////////////////////////////////////////////////////////////////////////////
// class raw_socket_stream
/////////////////////////////////////////////////////////////////////////////

/// An iostream class that handle raw IP sockets
class raw_socket_stream : public dgram_socket_stream {
private:
  raw_socket_stream(const raw_socket_stream&);

  raw_socket_stream& operator=(const raw_socket_stream& socket);

public:
  raw_socket_stream(FreeSockets::IP_Protocol proto=FreeSockets::proto_RAW);

  virtual ~raw_socket_stream();

  void setProtocol(FreeSockets::IP_Protocol proto);

  bool setTarget(const std::string& address, unsigned port) { 
    return dgram_sockbuf.setTarget(address, port, m_protocol); 
  }

  bool setBroadcast(bool opt=false);
};

#endif // SOCK_RAW

#endif // RGJ_FREE_STREAM_H_