home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 400_01 / socketpp-1.5 / sockstream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-06  |  6.0 KB  |  222 lines

  1. // sockstream.h -*- C++ -*- socket library
  2. // Copyright (C) 1992,1993 Gnanasekaran Swaminathan <gs4t@virginia.edu>
  3. // 
  4. // Permission is granted to use at your own risk and distribute this software
  5. // in source and binary forms provided the above copyright
  6. // notice and this paragraph are preserved on all copies.
  7. // This software is provided "as is" with no express or implied warranty.
  8. //
  9. // Version: 31Jan93 1.3
  10.  
  11. #ifndef _SOCKSTREAM_H
  12. #define    _SOCKSTREAM_H
  13.  
  14. #include <iostream.h>
  15. #include <stddef.h>
  16. #include <sys/types.h>
  17. #include <sys/uio.h>
  18.  
  19. #ifdef _G_LIB_VERSION
  20. // make socket++ work under g++-2.5.x and libg++-2.5.x
  21. #define _S_USER_BUF        _IO_USER_BUF
  22. #define _S_UNBUFFERED        _IO_UNBUFFERED
  23. #define _S_NO_READS        _IO_NO_READS
  24. #define _S_NO_WRITES        _IO_NO_WRITES
  25. #define _S_EOF_SEEN        _IO_EOF_SEEN
  26. #define _S_ERR_SEEN        _IO_ERR_SEEN
  27. #define _S_DELETE_DONT_CLOSE    _IO_DELETE_DONT_CLOSE
  28. #define _S_LINKED        _IO_LINKED
  29. #define _S_LINE_BUF        _IO_LINE_BUF
  30. #endif
  31.  
  32. class sockAddr {
  33. public:
  34.     virtual        ~sockAddr() {}
  35.     
  36.     virtual        operator void*    () const =0;
  37.     virtual int        size         () const =0;
  38.     virtual int        family        () const =0;
  39.  
  40.     void        error (const char* errmsg) const;
  41. };
  42.  
  43. struct msghdr {
  44.     caddr_t        msg_name;
  45.     int            msg_namelen;
  46.     iovec*        msg_iov;
  47.     int            msg_iovlen;
  48.     caddr_t        msg_accrights;
  49.     int            msg_accrightslen;
  50. };
  51.  
  52. class sockbuf: public streambuf {
  53. public:
  54.     enum type {
  55.     sock_stream    = 0x01,
  56.     sock_dgram    = 0x02,
  57.     sock_raw    = 0x03,
  58.     sock_rdm    = 0x04,
  59.     sock_seqpacket  = 0x05,
  60.     };
  61.     enum option {
  62.     so_debug    = 0x0001,
  63.     so_acceptconn    = 0x0002,
  64.     so_reuseaddr    = 0x0004,
  65.     so_keepalive    = 0x0008,
  66.     so_dontroute    = 0x0010,
  67.     so_broadcast    = 0x0020,
  68.     so_useloopback    = 0x0040,
  69.     so_linger    = 0x0080,
  70.     so_oobinline    = 0x0100,
  71.     so_sndbuf    = 0x1001,
  72.     so_rcvbuf    = 0x1002,
  73.     so_sndlowat    = 0x1003,
  74.     so_rcvlowat    = 0x1004,
  75.     so_sndtimeo    = 0x1005,
  76.     so_rcvtimeo    = 0x1006,
  77.     so_error    = 0x1007,
  78.     so_type        = 0x1008,
  79.     };    
  80.     enum level { sol_socket = 0xffff };
  81.     enum msgflag {
  82.     msg_oob        = 0x01,
  83.     msg_peek    = 0x02,
  84.     msg_dontroute    = 0x04,
  85.     
  86.     msg_maxiovlen    = 16,
  87.     };
  88.     enum shuthow {
  89.     shut_read,
  90.     shut_write,
  91.     shut_readwrite,
  92.     };
  93.     enum { somaxconn    = 5 };
  94.     struct socklinger {
  95.     int    l_onoff;    // option on/off
  96.     int    l_linger;    // linger time
  97.     };
  98.     
  99. protected:
  100.     struct sockcnt {
  101.     int    sock;
  102.     int    cnt;
  103.  
  104.     sockcnt(int s, int c): sock(s), cnt(c) {}
  105.     };
  106.     
  107.     sockcnt*    rep;  // counts the # refs to sock
  108.     int        stmo; // -1==block, 0==poll, >0 == waiting time in secs
  109.     int        rtmo; // -1==block, 0==poll, >0 == waiting time in secs
  110.     
  111.     virtual int        underflow ();
  112.     virtual int        overflow (int c = EOF);
  113.     virtual int        doallocate ();
  114.     int            flush_output ();
  115.  
  116. public:
  117.                 sockbuf (int soc = -1);
  118.                 sockbuf (int, type, int proto=0);
  119.                 sockbuf (const sockbuf&);
  120.     sockbuf&        operator = (sockbuf&);
  121.     virtual         ~sockbuf ();
  122.                 operator int () const { return rep->sock; }
  123.     
  124.     
  125.     virtual sockbuf*    open    (type, int proto=0);
  126.     virtual sockbuf*    close    ();
  127.     virtual int        sync    ();
  128.     virtual _G_ssize_t    sys_read (char* buf, _G_ssize_t len);
  129.     virtual _G_ssize_t    sys_write (const void* buf, long len);
  130.     virtual int        xsputn (const char* s, int n);
  131.     int            is_open () {return rep->sock >= 0; }
  132.     int            is_eof ()  {return xflags() & _S_EOF_SEEN; }
  133.     
  134.     
  135.     virtual void    bind    (sockAddr&);
  136.     virtual void    connect    (sockAddr&);
  137.     
  138.     void        listen    (int num=somaxconn);
  139.     virtual sockbuf    accept    ();
  140.     virtual sockbuf    accept    (sockAddr& sa);
  141.     
  142.     int            read    (void* buf, int len);
  143.     int            recv    (void* buf, int len, int msgf=0);
  144.     int            recvfrom(sockAddr& sa,
  145.                  void* buf, int len, int msgf=0);
  146.     int            recvmsg (msghdr* msg, int msgf=0);
  147.     
  148.     int            write    (const void* buf, int len);
  149.     int            send    (const void* buf, int len, int msgf=0);
  150.     int            sendto    (sockAddr& sa,
  151.                  const void* buf, int len, int msgf=0);
  152.     int            sendmsg    (const msghdr* msg, int msgf=0);
  153.     
  154.     int            sendtimeout (int wp=-1);
  155.     int            recvtimeout (int wp=-1);
  156.     int            is_readready (int wp_sec, int wp_usec=0) const;
  157.     int            is_writeready (int wp_sec, int wp_usec=0) const;
  158.     int            is_exceptionpending (int wp_sec, int wp_usec=0) const;
  159.     
  160.     void        shutdown (shuthow sh);
  161.     
  162.     int            getopt(option op, void* buf,int len,
  163.                    level l=sol_socket) const;
  164.     void        setopt(option op, void* buf,int len,
  165.                    level l=sol_socket) const;
  166.     
  167.     type        gettype () const;
  168.     int            clearerror () const;
  169.     int            debug      (int opt= -1) const;
  170.     int            reuseaddr (int opt= -1) const;
  171.     int            keepalive (int opt= -1) const;
  172.     int            dontroute (int opt= -1) const;
  173.     int            broadcast (int opt= -1) const;
  174.     int            oobinline (int opt= -1) const;
  175.     int            linger    (int tim= -1) const;
  176.     int            sendbufsz (int sz=-1)   const;
  177.     int            recvbufsz (int sz=-1)   const;
  178.     
  179.     void        error (const char* errmsg) const;
  180. };
  181.  
  182. class isockstream: public istream {
  183. public:
  184.                 isockstream(sockbuf& sb, ostream* tied=0)
  185.                 : ios (&sb,tied) { ios::setf(ios::dont_close); }
  186.                 isockstream(sockbuf* sb=0, ostream* tied=0)
  187.             : ios (sb, tied) { ios::setf(ios::dont_close); }
  188.  
  189.     sockbuf*        rdbuf () { return (sockbuf*)ios::rdbuf(); }
  190.     sockbuf*        operator -> () { return rdbuf(); }
  191.  
  192.     void        error (const char* errmsg) const;
  193. };
  194.  
  195. class osockstream: public ostream {
  196. public:
  197.                 osockstream(sockbuf& sb, ostream* tied=0)
  198.             : ios (&sb,tied) { ios::setf(ios::dont_close); }
  199.                 osockstream(sockbuf* sb=0, ostream* tied=0)
  200.             : ios (sb, tied) { ios::setf(ios::dont_close); }
  201.  
  202.     sockbuf*        rdbuf () { return (sockbuf*)ios::rdbuf(); }
  203.     sockbuf*        operator -> () { return rdbuf(); }
  204.  
  205.     void        error (const char* errmsg) const;
  206. };
  207.  
  208. class iosockstream: public iostream {
  209. public:
  210.                 iosockstream(sockbuf& sb, ostream* tied=0)
  211.             : ios (&sb,tied) { ios::setf(ios::dont_close); }
  212.                 iosockstream(sockbuf* sb=0, ostream* tied=0)
  213.             : ios (sb,tied) { ios::setf(ios::dont_close); }
  214.  
  215.     sockbuf*        rdbuf () { return (sockbuf*)ios::rdbuf(); }
  216.     sockbuf*        operator -> () { return rdbuf(); }
  217.  
  218.     void        error (const char* errmsg) const;
  219. };
  220.  
  221. #endif    _SOCKSTREAM_H
  222.