GNU CommonC++
unix.h
Go to the documentation of this file.
1// Copyright (C) 1999-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
44#ifndef CCXX_UNIX_H_
45#define CCXX_UNIX_H_
46
47#ifndef CCXX_MISSING_H_
48#include <cc++/missing.h>
49#endif
50
51#ifndef CCXX_SOCKET_H_
52#include <cc++/socket.h>
53#endif
54
55#ifdef CCXX_NAMESPACES
56namespace ost {
57#endif
58
59#ifndef WIN32
60
74 class UnixSocket : protected Socket {
75 protected:
76 friend class UnixStream;
77 friend class SocketPort;
78 friend class unixstream;
79
80 void close(void);
81 char *path;
82
83 public:
92 UnixSocket(const char* pathname, int backlog = 5);
93
98 {return Socket::isPending(pendingInput, timeout);}
99
103 virtual ~UnixSocket();
104 };
105
119 class UnixStream : public Socket, public std::streambuf, public std::iostream {
120 private:
121 int doallocate();
122
123 protected:
126 char *gbuf, *pbuf;
127
132 UnixStream(bool throwflag = true);
133
140 void allocate(int size);
141
146 void endStream(void);
147
154 virtual int underflow(void);
155
164 int uflow(void);
165
173 int overflow(int ch);
174
181 void connect(const char* pathname, int size);
182
190 std::iostream *unixstr(void)
191 {return ((std::iostream *)this);};
192
193 public:
204 UnixStream(UnixSocket &server, int size = 512, bool throwflag = true, timeout_t timeout = 0);
205
214 UnixStream(const char* pathname, int size = 512, bool throwflag = true, timeout_t to = 0);
215
221 inline void setTimeout(timeout_t to)
222 {timeout = to;};
223
230 UnixStream(const UnixStream &source);
231
236 virtual ~UnixStream();
237
244 int sync(void);
245
254
260 int getBufferSize(void) const
261 {return bufsize;};
262 };
263
272 class unixstream : public UnixStream {
273 public:
278
286 unixstream(const char *pathname, int buffer = 512);
287
295 unixstream(UnixSocket &unixsock, int buffer = 512);
296
304 void open(const char *pathname, int buffer = 512)
305 {UnixStream::connect( pathname, buffer );}
306
313 void open(UnixSocket &unixsock, int buffer = 512);
314
318 void close(void);
319
323 bool operator!() const;
324 };
325
336 class __EXPORT UnixSession : public Thread, public UnixStream {
337 protected:
351
358 void initial(void);
359
360 public:
370 UnixSession(const char* pathname, int size = 512, int pri = 0, int stack = 0);
371
381 UnixSession(UnixSocket &server, int size = 512,
382 int pri = 0, int stack = 0);
383
387 virtual ~UnixSession();
388 };
389
390#endif // ndef WIN32
391
392#ifdef CCXX_NAMESPACES
393}
394#endif
395
396#endif
397
398
399
The Socket is used as the base for all Internet protocol services under Common C++.
Definition socket.h:120
Pending
Definition socket.h:174
@ pendingInput
Definition socket.h:175
virtual bool isPending(Pending pend, timeout_t timeout=TIMEOUT_INF)
Get the status of pending operations.
The socket port is an internal class which is attached to and then serviced by a specific SocketServi...
Definition socketport.h:82
Every thread of execution in an application is created by instantiating an object of a class derived ...
Definition thread.h:1094
The Unix domain session is used to primarily to represent a client connection that can be managed on ...
Definition unix.h:336
UnixSession(const char *pathname, int size=512, int pri=0, int stack=0)
Create a Unix domain socket that will be connected to a local server server and that will execute und...
void initial(void)
The initial method is used to esablish a connection when delayed completion is used.
int waitConnection(timeout_t timeout=TIMEOUT_INF)
Normally called during the thread Initial() method by default, this will wait for the socket connecti...
virtual ~UnixSession()
Virtual destructor.
UnixSession(UnixSocket &server, int size=512, int pri=0, int stack=0)
Create a Unix domain socket from a bound Unix domain server by accepting a pending connection from th...
Unix domain sockets are used for stream based connected sessions between processes on the same machin...
Definition unix.h:74
virtual ~UnixSocket()
Use base socket handler for ending this socket.
void close(void)
bool isPendingConnection(timeout_t timeout=TIMEOUT_INF)
Used to wait for pending connection requests.
Definition unix.h:97
UnixSocket(const char *pathname, int backlog=5)
A Unix domain "server" is created as a Unix domain socket that is bound to a pathname and that has a ...
char * path
Definition unix.h:81
Unix streams are used to represent Unix domain client connections to a local server for accepting cli...
Definition unix.h:119
int uflow(void)
This streambuf method is used for doing unbuffered reads through the established unix domain socket c...
std::iostream * unixstr(void)
Used in derived classes to refer to the current object via it's iostream.
Definition unix.h:190
void connect(const char *pathname, int size)
Create a Unix domain stream by connecting to a Unix domain socket.
void setTimeout(timeout_t to)
Set the I/O operation timeout for socket I/O operations.
Definition unix.h:221
UnixStream(UnixSocket &server, int size=512, bool throwflag=true, timeout_t timeout=0)
Create a Unix domain stream by accepting a connection from a bound Unix domain socket acting as a ser...
void allocate(int size)
Used to allocate the buffer space needed for iostream operations.
int getBufferSize(void) const
Return the size of the current stream buffering used.
Definition unix.h:260
UnixStream(const UnixStream &source)
A copy constructor creates a new stream buffer.
void endStream(void)
Used to terminate the buffer space and cleanup the socket connection.
char * pbuf
Definition unix.h:126
char * gbuf
Definition unix.h:126
UnixStream(const char *pathname, int size=512, bool throwflag=true, timeout_t to=0)
Create a Unix domain stream by connecting to a Unix domain socket.
bool isPending(Pending pend, timeout_t timeout=TIMEOUT_INF)
Get the status of pending stream data.
virtual ~UnixStream()
Flush and empty all buffers, and then remove the allocated buffers.
UnixStream(bool throwflag=true)
The constructor required for "unixstream", a more C++ style version of the TCPStream class.
timeout_t timeout
Definition unix.h:124
int overflow(int ch)
This streambuf method is used to write the output buffer through the established unix domain connecti...
virtual int underflow(void)
This streambuf method is used to load the input buffer through the established unix domain socket con...
int bufsize
Definition unix.h:125
int sync(void)
Flushes the stream input and output buffers, writes pending output.
A more natural C++ "unixstream" class for use by non-threaded applications.
Definition unix.h:272
unixstream(UnixSocket &unixsock, int buffer=512)
Construct and "accept" (connect) the tcp stream through a server.
unixstream()
Construct an unopened "tcpstream" object.
void open(UnixSocket &unixsock, int buffer=512)
Open a tcp stream connection by accepting a tcp socket.
void open(const char *pathname, int buffer=512)
Open a tcp stream connection.
Definition unix.h:304
bool operator!() const
Test to see if stream is open.
void close(void)
Close the active tcp stream connection.
unixstream(const char *pathname, int buffer=512)
Construct and "open" (connect) the tcp stream to a remote socket.
#define __EXPORT
Definition config.h:1045
substitute functions which may be missing in target platform libc.
Definition address.h:64
Network addresses and sockets related classes.
#define TIMEOUT_INF
Definition thread.h:115
unsigned long timeout_t
Definition thread.h:74