libassa  3.5.1
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  {
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  {
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 
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 
497 
498 //------------------------------------------------------------------------------
499 // Inline functions
500 //------------------------------------------------------------------------------
501 
502 private:
509  Socket (const Socket&);
511 };
512 
513 //------------------------------------------------------------------------------
514 // Inline functions
515 //------------------------------------------------------------------------------
516 
517 inline
519  :
520  m_fd (BAD_HANDLER),
521  m_type(0),
522 #if defined (WIN32)
523  m_nonblocking (false),
524 #endif
525  m_state(Socket::badbit)
526 {
527  trace_with_mask("Socket::Socket",SOCKTRACE);
528 }
529 
530 inline
532 {
533  trace_with_mask("Socket::~Socket",SOCKTRACE);
534 }
535 
536 inline bool
537 Socket::connect (const Address& /* address_ */)
538 {
539  trace_with_mask("Socket::connect",SOCKTRACE);
540  return false;
541 }
542 
543 inline int
544 Socket::write(const char* /*buf_*/, const u_int /*size_*/)
545 {
546  trace_with_mask("Socket::write",SOCKTRACE);
547  return -1;
548 }
549 
550 inline int
551 Socket::read(char* /*buf_*/, const u_int /*size_*/)
552 {
553  trace_with_mask("Socket::read()",SOCKTRACE);
554  return -1;
555 }
556 
557 inline
558 Socket::operator void*() const
559 {
560  return fail() ? (void *)0 : (void *)(-1);
561 }
562 
563 inline bool
565 {
566  return fail();
567 }
568 
569 
570 inline void
572 {
573  m_state = is_valid_handler (m_fd) ? state_ : state_ | Socket::badbit;
574 }
575 
576 inline void
578 {
579  m_state |= flag_;
580 }
581 
586 inline
588 {
589  os_.flush ();
590  return (os_);
591 }
592 
601 inline
603 {
604  char c = '\n';
605  os_.write (&c, 1);
606  os_.flush ();
607  return (os_);
608 }
609 
621 inline
623 {
624  char c = '\0';
625  os_.write (&c, 1);
626  return (os_);
627 }
628 
629 } // end namespace ASSA
630 
631 #include "assa/Streambuf.h"
632 
633 #endif // SOCKET_H
634 
635 
636 
Address is an abstraction for INET or UNIX-domain address data type.
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
#define BAD_HANDLER
Sort out WIN32/mingw oddities.
Definition: Logger_Impl.h:81
int handler_t
Definition: Logger_Impl.h:82
unsigned int u_int
Definition: Logger_Impl.h:40
Streambuf class is based on Standard C++ iostream streambuf class.
Socket & operator=(const Socket &)
bool fail() const
Indicates that earlier extraction opeartion has failed to match the required pattern of input.
Definition: Socket.h:328
Socket()
Constructor.
Definition: Socket.h:518
int clear_fd_options(long flags_)
Gateway method for clearing file descriptor options.
Definition: Socket.cpp:147
Socket & operator>>(char &c)
Input of built-in char type. The value will be XDR-decoded.
Definition: Socket.cpp:359
virtual const int getDomain() const =0
Get socket domain.
int m_type
Socket domain type.
Definition: Socket.h:488
virtual Streambuf * rdbuf()
Return a pointer to the Streambuf associated with the stream.
Definition: Socket.h:240
void dumpState() const
Write state bits of the socket to the log file.
Definition: Socket.cpp:653
virtual Streambuf * rdbuf(Streambuf *)
Virtual function that sets new socket buffer and returns the old one.
Definition: Socket.h:247
static size_t xdr_length(const std::string &s_)
Give the true length of the XDR-encoded STL string.
Definition: Socket.h:343
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Definition: Socket.h:577
static string decode_fcntl_flags(long mask_)
Decipher flags packed into mask_ used in fcntl() call.
Definition: Socket.cpp:713
virtual handler_t getHandler() const =0
Get file descriptor.
virtual bool connect(const Address &address_)
Make a connection.
Definition: Socket.h:537
static void close_handler(handler_t &socket_)
Close socket endpoint in a portable way.
Definition: Socket.h:448
virtual bool close()=0
Close socket.
Socket(const Socket &)
The copy constructor and assignment operator are private to prevent copying of Socket objects,...
iostate rdstate() const
Retrieve state of the socket.
Definition: Socket.h:302
int set_fd_options(long flags_)
Gateway method for setting file descriptor options.
Definition: Socket.cpp:104
bool turnOptionOff(opt_t opt_)
Disable socket option.
Definition: Socket.cpp:204
virtual int read(char *buf_, const u_int size_)
Read expected number of bytes from the socket.
Definition: Socket.h:551
int set_option(int level_, int optname_, int val_)
Gateway method of setting socket options.
Definition: Socket.cpp:86
bool turnOptionOn(opt_t opt_)
Enable socket option.
Definition: Socket.cpp:185
opt_t
Socket options.
Definition: Socket.h:99
@ reuseaddr
Allow local address reuse.
Definition: Socket.h:100
@ sndlowat
The send low-water mark si the amount of available space that must exist in the socket send buffer fo...
Definition: Socket.h:108
@ rcvlowat
The receiver low-water mark is the amount of data that must be in the socket receive buffer for selec...
Definition: Socket.h:102
@ nonblocking
Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK).
Definition: Socket.h:115
static bool is_little_endian()
Determine the endianess of the platform we are on.
Definition: Socket.cpp:700
bool eof() const
An earlier extraction operation has encountered the end of file of the input stream (peer closed its ...
Definition: Socket.h:321
handler_t m_fd
File descriptor.
Definition: Socket.h:485
bool bad() const
Socket fd == -1 or read/write error occured or some loss of integrity on assosiated stream buffer.
Definition: Socket.h:337
int getOption(opt_t opt_) const
Get current value of a socket option.
Definition: Socket.cpp:248
unsigned char IOState
Definition: Socket.h:93
virtual bool bind(const Address &my_address_)=0
Server binds listening socket to its local well-known port.
bool good() const
Indicates no error on the socket.
Definition: Socket.h:315
int iostate
Definition: Socket.h:92
int ignore(int n_=INT_MAX, int delim_=EOF)
Extracts bytes and discards them.
Definition: Socket.cpp:308
void clear(iostate state_=Socket::goodbit)
Clear the socket state. Closed socket remains in bad state.
Definition: Socket.h:571
Socket & operator<<(char c)
Output of built-in char type. The value will be XDR-encoded.
Definition: Socket.cpp:508
virtual ~Socket()
Destructor.
Definition: Socket.h:531
virtual bool open(const int domain_)=0
Open socket.
virtual Socket & flush()
This function simply calls the public "synchronizing" function rdbuf()->pubsync() (assuming the assoc...
Definition: Socket.cpp:74
io_state_t
State bits: goodbit, eofbit, failbit, badbit.
Definition: Socket.h:80
@ eofbit
indicates that an input operation reached the end of an input sequence
Definition: Socket.h:82
@ goodbit
indicates that socket is ready for use
Definition: Socket.h:81
@ badbit
indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error fro...
Definition: Socket.h:87
@ failbit
indicates that an input operation failed to read the expected characters, or that an output operation...
Definition: Socket.h:84
static const int PGSIZE
Size of bytes of a kernel page.
Definition: Socket.h:74
IOState m_state
Control state of the socket.
Definition: Socket.h:496
bool setOption(opt_t opt_, int arg_)
Set socket option to value required.
Definition: Socket.cpp:223
virtual int write(const char *buf_, const u_int size_)
Write specified number of bytes to the socket.
Definition: Socket.h:544
bool operator!() const
Alias to fail()
Definition: Socket.h:564
virtual int in_avail() const =0
This function returns the number of characters immediately available in the get area of the underly...
int getBytesAvail(void) const
Return number of bytes available in socket receive buffer.
Definition: Socket.cpp:48
Streambuf class.
Definition: Streambuf.h:91
Definition: Acceptor.h:40
Socket & flush(Socket &os_)
flush manipulator.
Definition: Socket.h:587
@ SOCKTRACE
Extended Socket & friends messages
Definition: LogMask.h:42
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition: Logger_Impl.h:100
Socket & endl(Socket &os_)
endl manipulator.
Definition: Socket.h:602
void disable_handler(handler_t &socket_)
Set socket descriptor to invalid value in a portable way.
Definition: Logger_Impl.h:108
Socket & ends(Socket &os_)
ends manipulator.
Definition: Socket.h:622