home *** CD-ROM | disk | FTP | other *** search
- // sockstream.h -*- C++ -*- socket library
- // Copyright (C) 1992,1993 Gnanasekaran Swaminathan <gs4t@virginia.edu>
- //
- // Permission is granted to use at your own risk and distribute this software
- // in source and binary forms provided the above copyright
- // notice and this paragraph are preserved on all copies.
- // This software is provided "as is" with no express or implied warranty.
- //
- // Version: 31Jan93 1.3
-
- #ifndef _SOCKSTREAM_H
- #define _SOCKSTREAM_H
-
- #include <iostream.h>
- #include <stddef.h>
- #include <sys/types.h>
- #include <sys/uio.h>
-
- #ifdef _G_LIB_VERSION
- // make socket++ work under g++-2.5.x and libg++-2.5.x
- #define _S_USER_BUF _IO_USER_BUF
- #define _S_UNBUFFERED _IO_UNBUFFERED
- #define _S_NO_READS _IO_NO_READS
- #define _S_NO_WRITES _IO_NO_WRITES
- #define _S_EOF_SEEN _IO_EOF_SEEN
- #define _S_ERR_SEEN _IO_ERR_SEEN
- #define _S_DELETE_DONT_CLOSE _IO_DELETE_DONT_CLOSE
- #define _S_LINKED _IO_LINKED
- #define _S_LINE_BUF _IO_LINE_BUF
- #endif
-
- class sockAddr {
- public:
- virtual ~sockAddr() {}
-
- virtual operator void* () const =0;
- virtual int size () const =0;
- virtual int family () const =0;
-
- void error (const char* errmsg) const;
- };
-
- struct msghdr {
- caddr_t msg_name;
- int msg_namelen;
- iovec* msg_iov;
- int msg_iovlen;
- caddr_t msg_accrights;
- int msg_accrightslen;
- };
-
- class sockbuf: public streambuf {
- public:
- enum type {
- sock_stream = 0x01,
- sock_dgram = 0x02,
- sock_raw = 0x03,
- sock_rdm = 0x04,
- sock_seqpacket = 0x05,
- };
- enum option {
- so_debug = 0x0001,
- so_acceptconn = 0x0002,
- so_reuseaddr = 0x0004,
- so_keepalive = 0x0008,
- so_dontroute = 0x0010,
- so_broadcast = 0x0020,
- so_useloopback = 0x0040,
- so_linger = 0x0080,
- so_oobinline = 0x0100,
- so_sndbuf = 0x1001,
- so_rcvbuf = 0x1002,
- so_sndlowat = 0x1003,
- so_rcvlowat = 0x1004,
- so_sndtimeo = 0x1005,
- so_rcvtimeo = 0x1006,
- so_error = 0x1007,
- so_type = 0x1008,
- };
- enum level { sol_socket = 0xffff };
- enum msgflag {
- msg_oob = 0x01,
- msg_peek = 0x02,
- msg_dontroute = 0x04,
-
- msg_maxiovlen = 16,
- };
- enum shuthow {
- shut_read,
- shut_write,
- shut_readwrite,
- };
- enum { somaxconn = 5 };
- struct socklinger {
- int l_onoff; // option on/off
- int l_linger; // linger time
- };
-
- protected:
- struct sockcnt {
- int sock;
- int cnt;
-
- sockcnt(int s, int c): sock(s), cnt(c) {}
- };
-
- sockcnt* rep; // counts the # refs to sock
- int stmo; // -1==block, 0==poll, >0 == waiting time in secs
- int rtmo; // -1==block, 0==poll, >0 == waiting time in secs
-
- virtual int underflow ();
- virtual int overflow (int c = EOF);
- virtual int doallocate ();
- int flush_output ();
-
- public:
- sockbuf (int soc = -1);
- sockbuf (int, type, int proto=0);
- sockbuf (const sockbuf&);
- sockbuf& operator = (sockbuf&);
- virtual ~sockbuf ();
- operator int () const { return rep->sock; }
-
-
- virtual sockbuf* open (type, int proto=0);
- virtual sockbuf* close ();
- virtual int sync ();
- virtual _G_ssize_t sys_read (char* buf, _G_ssize_t len);
- virtual _G_ssize_t sys_write (const void* buf, long len);
- virtual int xsputn (const char* s, int n);
- int is_open () {return rep->sock >= 0; }
- int is_eof () {return xflags() & _S_EOF_SEEN; }
-
-
- virtual void bind (sockAddr&);
- virtual void connect (sockAddr&);
-
- void listen (int num=somaxconn);
- virtual sockbuf accept ();
- virtual sockbuf accept (sockAddr& sa);
-
- int read (void* buf, int len);
- int recv (void* buf, int len, int msgf=0);
- int recvfrom(sockAddr& sa,
- void* buf, int len, int msgf=0);
- int recvmsg (msghdr* msg, int msgf=0);
-
- int write (const void* buf, int len);
- int send (const void* buf, int len, int msgf=0);
- int sendto (sockAddr& sa,
- const void* buf, int len, int msgf=0);
- int sendmsg (const msghdr* msg, int msgf=0);
-
- int sendtimeout (int wp=-1);
- int recvtimeout (int wp=-1);
- int is_readready (int wp_sec, int wp_usec=0) const;
- int is_writeready (int wp_sec, int wp_usec=0) const;
- int is_exceptionpending (int wp_sec, int wp_usec=0) const;
-
- void shutdown (shuthow sh);
-
- int getopt(option op, void* buf,int len,
- level l=sol_socket) const;
- void setopt(option op, void* buf,int len,
- level l=sol_socket) const;
-
- type gettype () const;
- int clearerror () const;
- int debug (int opt= -1) const;
- int reuseaddr (int opt= -1) const;
- int keepalive (int opt= -1) const;
- int dontroute (int opt= -1) const;
- int broadcast (int opt= -1) const;
- int oobinline (int opt= -1) const;
- int linger (int tim= -1) const;
- int sendbufsz (int sz=-1) const;
- int recvbufsz (int sz=-1) const;
-
- void error (const char* errmsg) const;
- };
-
- class isockstream: public istream {
- public:
- isockstream(sockbuf& sb, ostream* tied=0)
- : ios (&sb,tied) { ios::setf(ios::dont_close); }
- isockstream(sockbuf* sb=0, ostream* tied=0)
- : ios (sb, tied) { ios::setf(ios::dont_close); }
-
- sockbuf* rdbuf () { return (sockbuf*)ios::rdbuf(); }
- sockbuf* operator -> () { return rdbuf(); }
-
- void error (const char* errmsg) const;
- };
-
- class osockstream: public ostream {
- public:
- osockstream(sockbuf& sb, ostream* tied=0)
- : ios (&sb,tied) { ios::setf(ios::dont_close); }
- osockstream(sockbuf* sb=0, ostream* tied=0)
- : ios (sb, tied) { ios::setf(ios::dont_close); }
-
- sockbuf* rdbuf () { return (sockbuf*)ios::rdbuf(); }
- sockbuf* operator -> () { return rdbuf(); }
-
- void error (const char* errmsg) const;
- };
-
- class iosockstream: public iostream {
- public:
- iosockstream(sockbuf& sb, ostream* tied=0)
- : ios (&sb,tied) { ios::setf(ios::dont_close); }
- iosockstream(sockbuf* sb=0, ostream* tied=0)
- : ios (sb,tied) { ios::setf(ios::dont_close); }
-
- sockbuf* rdbuf () { return (sockbuf*)ios::rdbuf(); }
- sockbuf* operator -> () { return rdbuf(); }
-
- void error (const char* errmsg) const;
- };
-
- #endif _SOCKSTREAM_H
-