Socket.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 //                               Socket.h
00004 //------------------------------------------------------------------------------
00005 //  Copyright (C) 1997-2002,2005  Vladislav Grinchenko 
00006 //
00007 //  This library is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU Library General Public
00009 //  License as published by the Free Software Foundation; either
00010 //  version 2 of the License, or (at your option) any later version.
00011 //------------------------------------------------------------------------------
00012 //
00013 //  This class is a direct derivative from my Unix Network Programming
00014 //  class work on generalizing OO network interfaces.
00015 //
00016 //------------------------------------------------------------------------------
00017 //  Created: 03/22/1999
00018 //------------------------------------------------------------------------------
00019 
00020 #ifndef SOCKET_H
00021 #define SOCKET_H
00022 
00023 #include <sys/stat.h>
00024 #include <sys/time.h>
00025 #include <limits.h>     // for INT_MAX
00026 #include <stdio.h>      // for EOF
00027 #include <sys/types.h>
00028 #include <unistd.h>
00029 #include <fcntl.h>      // for fcntl(2)
00030 
00031 #ifdef linux
00032 #  include <sys/ioctl.h>    // ioctl(2)
00033 #endif
00034 #ifdef sun          // ioctl(2)
00035 #  include <unistd.h>
00036 #  include <stropts.h>
00037 #  include <sys/filio.h>
00038 #endif
00039 
00040 #include "assa/Address.h"
00041 
00051 #define BYTES_LEFT_IN_SOCKBUF(s) ((s).eof () ? -1 : (s).in_avail ())
00052 
00058 #define BYTES_LEFT_IN_SIN (cin.eof () ? -1 : cin.rdbuf ()->in_avail ())
00059 
00060 
00061 namespace ASSA {
00062 
00063 class Streambuf;        // Forward declaration
00064 
00070 class Socket {
00071 public:
00073     static const int PGSIZE;
00074 
00079     enum io_state_t { 
00080         goodbit = 0,  
00081         eofbit  = 1,  
00083         failbit = 2,  
00086         badbit  = 4   
00089     };
00090 
00091     typedef int iostate;
00092     typedef unsigned char IOState;
00093 
00097     enum opt_t { 
00098         reuseaddr,   
00099         rcvlowat,    
00104         sndlowat,    
00110         blocking,    
00120         nonblocking  
00134     };
00135 
00137     Socket();
00138 
00140     virtual ~Socket();
00141 
00143     virtual bool open(const int domain_) =0;
00144 
00146     virtual bool close() =0;
00147 
00152     virtual bool connect (const Address& address_);
00153 
00159     virtual bool bind (const Address& my_address_) =0;
00160 
00166     virtual int write (const char* buf_, const unsigned int size_); 
00167 
00170     int getBytesAvail (void) const;
00171 
00176     virtual int read (char* buf_, const unsigned int size_);
00177 
00215     int ignore (int n_ = INT_MAX, int delim_ = EOF);
00216 
00218     virtual const int getHandler() const = 0;
00219 
00221     virtual const int getDomain() const = 0;
00222 
00231     virtual Streambuf* rdbuf () { return 0; }
00232 
00238     virtual Streambuf* rdbuf (Streambuf* /*sb_*/) { return 0; }
00239 
00247     virtual int in_avail () const = 0;
00248 
00255     virtual Socket& flush ();
00256 
00261     bool turnOptionOn (opt_t opt_);
00262 
00267     bool turnOptionOff (opt_t opt_);
00268 
00275     bool setOption (opt_t opt_, int arg_);
00276 
00282     int  getOption (opt_t opt_) const;
00283 
00285     operator void* () const;
00286 
00288     bool operator! () const;
00289 
00293     iostate rdstate () const { return m_state; }
00294 
00296     void clear (iostate state_ = Socket::goodbit);
00297 
00301     void setstate (iostate flag_);
00302 
00306     bool good () const { return m_state == 0; }
00307 
00312     bool eof ()  const { return m_state & Socket::eofbit; }
00313 
00319     bool fail () const 
00320         { 
00321             return m_state & (Socket::failbit | Socket::badbit);
00322         }
00323 
00328     bool bad ()  const { return m_state & Socket::badbit; }
00329 
00331     void dumpState () const;
00332 
00334     static size_t xdr_length (const std::string& s_) 
00335     { 
00336         return (4 + s_.length () + s_.length () % 4);
00337     }
00338 
00340     Socket& operator>> (char& c);
00341 
00343     Socket& operator>> (unsigned char& c_) 
00344     { 
00345         return operator>>((char&) c_); 
00346     }
00347 
00349     Socket& operator>> (signed char& c_) 
00350     { 
00351         return operator>>((char&) c_); 
00352     }
00353 
00355     Socket& operator>> (std::string& s_);
00356 
00358     Socket& operator>> (short& n_);
00359 
00361     Socket& operator>> (unsigned short& n_);
00362 
00364     Socket& operator>> (int& n_);
00365 
00367     Socket& operator>> (unsigned int& n_);
00368 
00370     Socket& operator>> (long& n_);
00371 
00373     Socket& operator>> (unsigned long& n_);
00374 
00376     Socket& operator>> (float& n_);
00377 
00379     Socket& operator>> (double& n_);
00380 
00382     Socket& operator<< (char c);
00383 
00385     Socket& operator<< (unsigned char c_) 
00386     { 
00387         return (*this) << (char) c_; 
00388     }
00389 
00391     Socket& operator<< (signed char c_) 
00392     { 
00393         return (*this) << (char) c_; 
00394     }
00395 
00397     Socket& operator<< (const std::string& s_);
00398 
00400     Socket& operator<< (short n_);
00401 
00403     Socket& operator<< (unsigned short n_);
00404 
00406     Socket& operator<< (int n_);
00407 
00409     Socket& operator<< (unsigned int n_);
00410 
00412     Socket& operator<< (long n_);
00413 
00415     Socket& operator<< (unsigned long n_);
00416 
00418     Socket& operator<< (float n_);
00419 
00421     Socket& operator<< (double n_);
00422 
00424     Socket& operator<< (Socket& (*f) (Socket&))
00425     {
00426         return (f (*this));
00427     }
00428 
00433     static bool is_little_endian ();
00434 
00435 protected:
00439     int set_option (int level_, int optname_, int val_);
00440 
00444     int set_fd_options (int flags_);
00445 
00449     int clear_fd_options (int flags_);
00450 
00451 protected:
00453     int m_fd;
00454 
00456     int m_type;
00457 
00459     IOState m_state;
00460 
00461 //------------------------------------------------------------------------------
00462 // Inline functions
00463 //------------------------------------------------------------------------------
00464 
00465 private:
00472     Socket (const Socket&);
00473     Socket& operator= (const Socket&);
00474 };
00475 
00476 //------------------------------------------------------------------------------
00477 // Inline functions
00478 //------------------------------------------------------------------------------
00479 
00480 inline
00481 Socket::Socket() 
00482     : m_fd(-1), m_type(0), m_state(Socket::badbit)
00483 {
00484     trace_with_mask("Socket::Socket",SOCKTRACE);
00485 }
00486 
00487 inline
00488 Socket::~Socket ()
00489 { 
00490     trace_with_mask("Socket::~Socket",SOCKTRACE); 
00491 }
00492 
00493 inline bool
00494 Socket::connect (const Address& /* address_ */) 
00495 {
00496     trace_with_mask("Socket::connect",SOCKTRACE);
00497     return false; 
00498 }
00499 
00500 inline int 
00501 Socket::write(const char* /*buf_*/, const unsigned int /*size_*/) 
00502 { 
00503     trace_with_mask("Socket::write",SOCKTRACE); 
00504     return -1; 
00505 }
00506 
00507 inline int 
00508 Socket::read(char* /*buf_*/, const unsigned int /*size_*/)  
00509 { 
00510     trace_with_mask("Socket::read()",SOCKTRACE);
00511     return -1; 
00512 }
00513 
00514 inline
00515 Socket::operator void*() const 
00516 {
00517     return fail() ? (void *)0 : (void *)(-1); 
00518 }
00519 
00520 inline bool
00521 Socket::operator!() const 
00522 { 
00523     return fail(); 
00524 }
00525 
00526 
00527 inline void 
00528 Socket::clear(iostate state_)
00529 { 
00530     m_state = m_fd >=0 ? state_ : state_ | Socket::badbit; 
00531 }
00532 
00533 inline void 
00534 Socket::setstate(iostate flag_) 
00535 { 
00536     m_state |= flag_; 
00537 }
00538 
00543 inline
00544 Socket& flush (Socket& os_)
00545 {
00546     os_.flush ();
00547     return (os_);
00548 }
00549 
00558 inline 
00559 Socket& endl (Socket& os_)
00560 {
00561     char c = '\n';
00562     os_.write (&c, 1);
00563     os_.flush ();
00564     return (os_);
00565 }
00566 
00578 inline
00579 Socket& ends (Socket& os_)
00580 {
00581     char c = '\0';
00582     os_.write (&c, 1);
00583     return (os_);
00584 }
00585 
00586 } // end namespace ASSA
00587 
00588 #include "assa/Streambuf.h"
00589 
00590 #endif // SOCKET_H
00591 
00592 
00593 

Generated on Sat Dec 31 19:52:11 2005 for libassa by  doxygen 1.4.5