ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/c++/socket++-1.10/sockstream.h
Revision: 1.1
Committed: Mon Feb 26 15:01:39 2001 UTC (23 years, 6 months ago) by ab11
Content type: text/plain
Branch: MAIN
CVS Tags: PROJECT_COMPLETION
Log Message:
Networking class. Assumed to be bug free.

File Contents

# User Rev Content
1 ab11 1.1 // sockstream.h -*- C++ -*- socket library
2     // Copyright (C) 1992,1993,1994 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: 17Oct95 1.10
10    
11     #ifndef _SOCKSTREAM_H
12     #define _SOCKSTREAM_H
13    
14     #include <iostream.h>
15     #include <stddef.h>
16     #include <stdio.h>
17     #include <sys/types.h>
18     #include <sys/uio.h>
19     #include <sys/socket.h>
20    
21     #ifndef _G_config_h
22     // non libg++ or g++ compilation
23     # define _S_NOLIBGXX
24    
25     # define _S_USER_BUF 0x0001
26     # define _S_UNBUFFERED 0x0002
27     # define _S_NO_READS 0x0004
28     # define _S_NO_WRITES 0x0008
29     # define _S_EOF_SEEN 0x0010
30     # define _S_ERR_SEEN 0x0020
31     # define _S_DELETE_DONT_CLOSE 0x0040
32     # define _S_LINKED 0x0080
33     # define _S_LINE_BUF 0x0200
34    
35     # define _G_ssize_t size_t
36    
37     #else
38     # ifndef _S_USER_BUF
39     // libg++ 2.5.x
40     # define _S_USER_BUF _IO_USER_BUF
41     # define _S_UNBUFFERED _IO_UNBUFFERED
42     # define _S_NO_READS _IO_NO_READS
43     # define _S_NO_WRITES _IO_NO_WRITES
44     # define _S_EOF_SEEN _IO_EOF_SEEN
45     # define _S_ERR_SEEN _IO_ERR_SEEN
46     # define _S_DELETE_DONT_CLOSE _IO_DELETE_DONT_CLOSE
47     # define _S_LINKED _IO_LINKED
48     # define _S_LINE_BUF _IO_LINE_BUF
49     # endif // !_S_USER_BUF
50    
51     #endif // !_G_config_h
52    
53     #ifdef __linux__
54     // linux leaves these things out. We define it for compatitbility
55     // not for their use.
56     # define SO_ACCEPTCONN 0x0002
57     # define SO_USELOOPBACK 0x0040
58     # define SO_SNDLOWAT 0x1003
59     # define SO_RCVLOWAT 0x1004
60     # define SO_SNDTIMEO 0x1005
61     # define SO_RCVTIMEO 0x1006
62    
63     # define MSG_MAXIOVLEN 16
64     # define SOMAXCONN 5
65     #endif // __linux__
66    
67     extern void sock_error (const char* classname, const char* error_message);
68    
69     typedef int sockexcept;
70    
71     struct sockaddr;
72    
73     class sockAddr {
74     public:
75     virtual ~sockAddr() {}
76    
77     virtual operator void* () const =0;
78     operator sockaddr* () const { return addr (); }
79     virtual int size () const =0;
80     virtual int family () const =0;
81     virtual sockaddr* addr () const =0;
82    
83     void error (const char* errmsg) const;
84     };
85    
86     struct msghdr;
87    
88     class sockbuf: public streambuf {
89     public:
90     enum type {
91     sock_stream = SOCK_STREAM,
92     sock_dgram = SOCK_DGRAM,
93     sock_raw = SOCK_RAW,
94     sock_rdm = SOCK_RDM,
95     sock_seqpacket = SOCK_SEQPACKET
96     };
97     enum option {
98     so_debug = SO_DEBUG,
99     so_acceptconn = SO_ACCEPTCONN,
100     so_reuseaddr = SO_REUSEADDR,
101     so_keepalive = SO_KEEPALIVE,
102     so_dontroute = SO_DONTROUTE,
103     so_broadcast = SO_BROADCAST,
104     so_useloopback = SO_USELOOPBACK,
105     so_linger = SO_LINGER,
106     so_oobinline = SO_OOBINLINE,
107     so_sndbuf = SO_SNDBUF,
108     so_rcvbuf = SO_RCVBUF,
109     so_sndlowat = SO_SNDLOWAT,
110     so_rcvlowat = SO_RCVLOWAT,
111     so_sndtimeo = SO_SNDTIMEO,
112     so_rcvtimeo = SO_RCVTIMEO,
113     so_error = SO_ERROR,
114     so_type = SO_TYPE
115     };
116     enum level { sol_socket = SOL_SOCKET };
117     enum msgflag {
118     msg_oob = MSG_OOB,
119     msg_peek = MSG_PEEK,
120     msg_dontroute = MSG_DONTROUTE,
121    
122     msg_maxiovlen = MSG_MAXIOVLEN
123     };
124     enum shuthow {
125     shut_read,
126     shut_write,
127     shut_readwrite
128     };
129     enum { somaxconn = SOMAXCONN };
130     struct socklinger {
131     int l_onoff; // option on/off
132     int l_linger; // linger time
133    
134     socklinger (int a, int b): l_onoff (a), l_linger (b) {}
135     };
136    
137     protected:
138     struct sockcnt {
139     int sock;
140     int cnt;
141    
142     sockcnt(int s, int c): sock(s), cnt(c) {}
143     };
144    
145     sockcnt* rep; // counts the # refs to sock
146     int stmo; // -1==block, 0==poll, >0 == waiting time in secs
147     int rtmo; // -1==block, 0==poll, >0 == waiting time in secs
148    
149     virtual int underflow ();
150     virtual int overflow (int c = EOF);
151     virtual int doallocate ();
152     int flush_output ();
153    
154     #ifdef _S_NOLIBGXX
155     int x_flags; // port to USL iostream
156     int xflags () const { return x_flags; }
157     int xsetflags (int f) { return x_flags |= f; }
158     int xflags (int f)
159     { int ret = x_flags; x_flags = f; return ret; }
160     void xput_char (char c) { *pptr() = c; pbump (1); }
161     int linebuffered () const { return x_flags & _S_LINE_BUF; }
162     #endif // _S_NOLIBGXX
163    
164     public:
165     sockbuf (int soc = -1);
166     sockbuf (int, type, int proto=0);
167     sockbuf (const sockbuf&);
168     sockbuf& operator = (const sockbuf&);
169     virtual ~sockbuf ();
170     operator int () const { return rep->sock; }
171    
172    
173     virtual sockbuf* open (type, int proto=0);
174     virtual sockbuf* close ();
175     virtual int sync ();
176     virtual _G_ssize_t sys_read (char* buf, _G_ssize_t len);
177     virtual _G_ssize_t sys_write (const void* buf, long len);
178     virtual int xsputn (const char* s, int n);
179     int is_open () const { return rep->sock >= 0; }
180     int is_eof () { return xflags() & _S_EOF_SEEN; }
181    
182    
183     virtual int bind (sockAddr&);
184     virtual int connect (sockAddr&);
185    
186     void listen (int num=somaxconn);
187     virtual sockbuf accept ();
188     virtual sockbuf accept (sockAddr& sa);
189    
190     int read (void* buf, int len);
191     int recv (void* buf, int len, int msgf=0);
192     int recvfrom(sockAddr& sa,
193     void* buf, int len, int msgf=0);
194    
195     #ifndef __linux__
196     int recvmsg (msghdr* msg, int msgf=0);
197     int sendmsg (msghdr* msg, int msgf=0);
198     #endif
199    
200     int write (const void* buf, int len);
201     int send (const void* buf, int len, int msgf=0);
202     int sendto (sockAddr& sa,
203     const void* buf, int len, int msgf=0);
204    
205     int sendtimeout (int wp=-1);
206     int recvtimeout (int wp=-1);
207     int is_readready (int wp_sec, int wp_usec=0) const;
208     int is_writeready (int wp_sec, int wp_usec=0) const;
209     int is_exceptionpending (int wp_sec, int wp_usec=0) const;
210    
211     void shutdown (shuthow sh);
212    
213     int getopt(option op, void* buf,int len,
214     level l=sol_socket) const;
215     void setopt(option op, void* buf,int len,
216     level l=sol_socket) const;
217    
218     type gettype () const;
219     int clearerror () const;
220     int debug (int opt= -1) const;
221     int reuseaddr (int opt= -1) const;
222     int keepalive (int opt= -1) const;
223     int dontroute (int opt= -1) const;
224     int broadcast (int opt= -1) const;
225     int oobinline (int opt= -1) const;
226     int linger (int tim= -1) const;
227     int sendbufsz (int sz=-1) const;
228     int recvbufsz (int sz=-1) const;
229    
230     void error (const char* errmsg) const;
231     };
232    
233     class isockstream: public istream {
234     protected:
235     isockstream (): ios (0) {}
236     public:
237     isockstream(sockbuf& sb)
238     : ios (new sockbuf(sb)) {}
239     ~isockstream ();
240    
241    
242     sockbuf* rdbuf () { return (sockbuf*)ios::rdbuf(); }
243     sockbuf* operator -> () { return rdbuf(); }
244    
245     void error (const char* errmsg) const;
246     };
247    
248     class osockstream: public ostream {
249     protected:
250     osockstream (): ios (0) {}
251     public:
252     osockstream(sockbuf& sb)
253     : ios (new sockbuf(sb)) {}
254     ~osockstream ();
255    
256     sockbuf* rdbuf () { return (sockbuf*)ios::rdbuf(); }
257     sockbuf* operator -> () { return rdbuf(); }
258    
259     void error (const char* errmsg) const;
260     };
261    
262     class iosockstream: public iostream {
263     protected:
264     iosockstream (): ios (0) {}
265     public:
266     iosockstream(sockbuf& sb)
267     : ios (new sockbuf(sb)) {}
268     ~iosockstream ();
269    
270     sockbuf* rdbuf () { return (sockbuf*)ios::rdbuf(); }
271     sockbuf* operator -> () { return rdbuf(); }
272    
273     void error (const char* errmsg) const;
274     };
275    
276     #endif // _SOCKSTREAM_H