libassa
3.5.1
assa
Socket.h
Go to the documentation of this file.
1
// -*- c++ -*-
2
//------------------------------------------------------------------------------
3
// Socket.h
4
//------------------------------------------------------------------------------
5
// Copyright (C) 1997-2002,2005 Vladislav Grinchenko
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Library General Public
9
// License as published by the Free Software Foundation; either
10
// version 2 of the License, or (at your option) any later version.
11
//------------------------------------------------------------------------------
12
//
13
// This class is a direct derivative from my Unix Network Programming
14
// class work on generalizing object-oriented network interfaces.
15
//
16
//------------------------------------------------------------------------------
17
// Created: 03/22/1999
18
//------------------------------------------------------------------------------
19
20
#ifndef SOCKET_H
21
#define SOCKET_H
22
23
#include <sys/stat.h>
24
#include <sys/time.h>
25
#include <limits.h>
// for INT_MAX
26
#include <stdio.h>
// for EOF
27
#include <sys/types.h>
28
#include <unistd.h>
29
#include <fcntl.h>
// for fcntl(2)
30
31
#ifdef linux
32
# include <sys/ioctl.h>
// ioctl(2)
33
#endif
34
35
#ifdef sun // ioctl(2)
36
# include <unistd.h>
37
# include <stropts.h>
38
# include <sys/filio.h>
39
#endif
40
41
#include "
assa/Address.h
"
42
52
#define BYTES_LEFT_IN_SOCKBUF(s) ((s).eof () ? -1 : (s).in_avail ())
53
59
#define BYTES_LEFT_IN_SIN (cin.eof () ? -1 : cin.rdbuf ()->in_avail ())
60
61
62
namespace
ASSA
{
63
64
class
Streambuf;
// Forward declaration
65
71
class
Socket
{
72
public
:
74
static
const
int
PGSIZE
;
75
80
enum
io_state_t
{
81
goodbit
= 0,
82
eofbit
= 1,
84
failbit
= 2,
87
badbit
= 4
90
};
91
92
typedef
int
iostate
;
93
typedef
unsigned
char
IOState
;
94
98
enum
opt_t
99
{
100
reuseaddr
,
102
rcvlowat
,
108
sndlowat
,
115
nonblocking
143
};
144
146
Socket
();
147
149
virtual
~Socket
();
150
152
virtual
bool
open
(
const
int
domain_) =0;
153
155
virtual
bool
close
() =0;
156
161
virtual
bool
connect
(
const
Address
& address_);
162
168
virtual
bool
bind
(
const
Address
& my_address_) =0;
169
175
virtual
int
write
(
const
char
* buf_,
const
u_int
size_);
176
179
int
getBytesAvail
(
void
)
const
;
180
185
virtual
int
read
(
char
* buf_,
const
u_int
size_);
186
224
int
ignore
(
int
n_ = INT_MAX,
int
delim_ = EOF);
225
227
virtual
handler_t
getHandler
()
const
= 0;
228
230
virtual
const
int
getDomain
()
const
= 0;
231
240
virtual
Streambuf
*
rdbuf
() {
return
0; }
241
247
virtual
Streambuf
*
rdbuf
(
Streambuf
*
/*sb_*/
) {
return
0; }
248
256
virtual
int
in_avail
()
const
= 0;
257
264
virtual
Socket
&
flush
();
265
270
bool
turnOptionOn
(
opt_t
opt_);
271
276
bool
turnOptionOff
(
opt_t
opt_);
277
284
bool
setOption
(
opt_t
opt_,
int
arg_);
285
291
int
getOption
(
opt_t
opt_)
const
;
292
294
operator
void
* ()
const
;
295
297
bool
operator!
()
const
;
298
302
iostate
rdstate
()
const
{
return
m_state
; }
303
305
void
clear
(iostate state_ =
Socket::goodbit
);
306
310
void
setstate
(iostate flag_);
311
315
bool
good
()
const
{
return
m_state
== 0; }
316
321
bool
eof
()
const
{
return
m_state
&
Socket::eofbit
; }
322
328
bool
fail
()
const
329
{
330
return
m_state
& (
Socket::failbit
|
Socket::badbit
);
331
}
332
337
bool
bad
()
const
{
return
m_state
&
Socket::badbit
; }
338
340
void
dumpState
()
const
;
341
343
static
size_t
xdr_length
(
const
std::string& s_)
344
{
345
return
(4 + s_.length () + s_.length () % 4);
346
}
347
349
Socket
&
operator>>
(
char
& c);
350
352
Socket
&
operator>>
(
unsigned
char
& c_)
353
{
354
return
operator>>
((
char
&) c_);
355
}
356
358
Socket
&
operator>>
(
signed
char
& c_)
359
{
360
return
operator>>
((
char
&) c_);
361
}
362
364
Socket
&
operator>>
(std::string& s_);
365
367
Socket
&
operator>>
(
short
& n_);
368
370
Socket
&
operator>>
(
unsigned
short
& n_);
371
373
Socket
&
operator>>
(
int
& n_);
374
376
Socket
&
operator>>
(
unsigned
int
& n_);
377
379
Socket
&
operator>>
(
long
& n_);
380
382
Socket
&
operator>>
(
unsigned
long
& n_);
383
385
Socket
&
operator>>
(
float
& n_);
386
388
Socket
&
operator>>
(
double
& n_);
389
391
Socket
&
operator<<
(
char
c);
392
394
Socket
&
operator<<
(
unsigned
char
c_)
395
{
396
return
(*
this
) << (char) c_;
397
}
398
400
Socket
&
operator<<
(
signed
char
c_)
401
{
402
return
(*
this
) << (char) c_;
403
}
404
406
Socket
&
operator<<
(
const
std::string& s_);
407
409
Socket
&
operator<<
(
short
n_);
410
412
Socket
&
operator<<
(
unsigned
short
n_);
413
415
Socket
&
operator<<
(
int
n_);
416
418
Socket
&
operator<<
(
unsigned
int
n_);
419
421
Socket
&
operator<<
(
long
n_);
422
424
Socket
&
operator<<
(
unsigned
long
n_);
425
427
Socket
&
operator<<
(
float
n_);
428
430
Socket
&
operator<<
(
double
n_);
431
433
Socket
&
operator<<
(
Socket
& (*f) (
Socket
&))
434
{
435
return
(f (*
this
));
436
}
437
443
static
bool
is_little_endian
();
444
448
static
void
close_handler
(
handler_t
& socket_)
449
{
450
#if defined (WIN32)
451
closesocket (socket_);
452
#else
453
::close
(socket_);
454
#endif
455
disable_handler
(socket_);
456
}
457
460
static
string
decode_fcntl_flags
(
long
mask_);
461
462
/*------------------------------------------------------------------
463
* Protected Members
464
*------------------------------------------------------------------
465
*/
466
protected
:
470
int
set_option
(
int
level_,
int
optname_,
int
val_);
471
475
int
set_fd_options
(
long
flags_);
476
480
int
clear_fd_options
(
long
flags_);
481
482
protected
:
485
handler_t
m_fd
;
// u_int, INVALID_SOCKET=(SOCKET)(~0)
486
488
int
m_type
;
489
490
#if defined (WIN32)
491
bool
m_nonblocking;
// We cannot retrieve the status of the
492
// socket. So, we remember what it was instead.
493
#endif
494
496
IOState
m_state
;
497
498
//------------------------------------------------------------------------------
499
// Inline functions
500
//------------------------------------------------------------------------------
501
502
private
:
509
Socket
(
const
Socket
&);
510
Socket
&
operator=
(
const
Socket
&);
511
};
512
513
//------------------------------------------------------------------------------
514
// Inline functions
515
//------------------------------------------------------------------------------
516
517
inline