HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_IPC.h
Go to the documentation of this file.
1 #ifndef __UT_IPC__
2 #define __UT_IPC__
3 
4 #include "UT_API.h"
5 // This file contains the following class declarations:
6 // UT_InetAddress
7 // UT_UnixAddress
8 // UT_Transport
9 // UT_Socket
10 // UT_UnixSocket
11 // UT_InetSocket
12 // UT_UnixStream
13 // UT_InetStream
14 // UT_InetDatagram
15 
16 #include <sys/types.h>
17 
18 #ifdef WIN32
19  #include <windows.h>
20  #include <winsock.h>
21  struct sockaddr_un {
22  short sun_family;
23  char sun_path[108];
24  };
25 #else
26  #include <sys/socket.h>
27  #include <sys/un.h>
28  #include <netinet/in.h>
29  #include <arpa/inet.h>
30 #endif
31 
32 #include <iosfwd>
33 
35 public:
36  UT_IPCPacket(char *msg, int len = 0);
37  ~UT_IPCPacket();
38 
39  int len; // Size of the packet
40  char *buf; // Message data
41 };
42 
43 typedef struct sockaddr *SOCKADDR_PTR;
44 typedef struct iovec *IOVEC_PTR;
45 typedef const struct sockaddr *CONST_SOCKADDR_PTR;
46 
47 //===========================================================================
49 {
50  friend class UT_InetSocket;
51  friend std::ostream &operator<<(std::ostream &os, const UT_InetAddress &ia);
52 
53 public:
54  static const char *UT_Any; // any address
55  static const char *UT_Broadcast; // broadcast address
56  enum UT_Status {
60  UT_HostFound
61  };
62 
64  explicit UT_InetAddress(int port);
65  UT_InetAddress(const char *service, const char *protocol);
66  UT_InetAddress(const char *hostname, int port);
67  UT_InetAddress(const char *hostname, const char *service, const char *proto);
68 
69  const char * hostname() const;
70  const char *numeric_hostname() const;
71 
72  int port() const;
73  int ok() const { return state==UT_HostFound; }
74  UT_Status status() const { return state; }
75  int size() const { return sizeof(address); }
76  int isany() const { return any_flag; }
77  int isbroadcast() const { return broadcast_flag; }
78 
79  operator SOCKADDR_PTR() { return (SOCKADDR_PTR)&address; }
80  operator CONST_SOCKADDR_PTR() const { return (CONST_SOCKADDR_PTR)&address; }
81 
82 protected:
83 
84  int lookup(const char *hostname, int port);
85  int lookup(const char *hostname,
86  const char *service,
87  const char *proto);
88 
89  int lookup(int aport) { return lookup(UT_Any,aport); }
90  int lookup(const char *service, const char *protocol)
91  { return lookup(UT_Any, service, protocol); }
92 
93  struct sockaddr_in address;
94  int addrlen;
96  int any_flag;
98 };
99 
100 //===========================================================================
102 {
103  friend class UT_UnixSocket;
104  friend std::ostream &operator<<(std::ostream &os, const UT_UnixAddress &ia);
105 public:
106  static char *Temp; // any address
107 
108  UT_UnixAddress(); // Not sure what this one does.
109  UT_UnixAddress(const char *pathname);
110 
111  const char *pathname() const;
112 
113  operator SOCKADDR_PTR() { return (SOCKADDR_PTR) &address; }
114  operator CONST_SOCKADDR_PTR() const { return (CONST_SOCKADDR_PTR)&address;}
115 
116 protected:
117  void set(const char *pathname);
118  int size() const { return sizeof(address); }
119 
120  struct sockaddr_un address;
121  int addrlen;
122 };
123 
124 
125 //===========================================================================
127 {
128 public:
129  UT_Transport();
130  virtual ~UT_Transport();
131 
132  virtual void abstract_class() = 0;
133  virtual int close();
134 
135  virtual int read( char *buffer, int len);
136  virtual int write(const char *buffer, int len);
137  virtual UT_IPCPacket *getPacket(); // Used for non-binary data
138 
139  virtual int readv(const IOVEC_PTR, int);
140  virtual int writev(const IOVEC_PTR, int);
141 
142  virtual int ok();
143  virtual int inuse();
144 
145  const char *getError() const { return errorMsg; }
146  int getDescriptor() const { return td; }
147 
149 
150  static void daemon_start(int ignore_sigcld);
151 
152 protected:
153  void setError(const char *module, const char *func,
154  const char *mess);
155  int readPartial(char *buffer, int len);
156  int writePartial(char *buffer, int len);
157 
159  UT_ShutDownReceive = 0,
160  UT_ShutDownSend = 1,
161  UT_ShutDownBoth = 2
162  };
163  virtual void shutdown(UT_ShutDownType) = 0;
164 
165  int status; // connection status
166  int td; // the transport file descriptor
167  char errorMsg[256];
168 };
169 
170 //===========================================================================
172 {
173  void abstract_class();
174 
175 public:
179  UT_Server
180  };
181  enum UT_AddressFamily { // just work with these three for now
182  UT_AfUnix = AF_UNIX,
183  UT_AfInet = AF_INET
184  };
186  UT_Stream = SOCK_STREAM,
187  UT_Datagram = SOCK_DGRAM,
188  UT_Raw = SOCK_RAW,
189  UT_RDM = SOCK_RDM,
190  UT_SeqPacket = SOCK_SEQPACKET
191  };
192 
194  UT_MsgSendNoFlags = 0,
195  UT_MsgSendDontRoute = MSG_DONTROUTE,
196  UT_MsgSendOob = MSG_OOB,
197  UT_MsgSendDontRouteOob = MSG_OOB|MSG_DONTROUTE
198  };
200  UT_MsgReceiveNoFlags = 0,
201  UT_MsgReceivePeek = MSG_PEEK,
202  UT_MsgReceiveOob = MSG_OOB,
203  UT_MsgReceivePeekOob = MSG_OOB|MSG_PEEK
204  };
205 
207  UT_Off = 0,
208  UT_On = 1
209  };
211  UT_Broadcast = SO_BROADCAST,
212  UT_Debug = SO_DEBUG,
213  UT_DontRoute = SO_DONTROUTE,
214  UT_KeepAlive = SO_KEEPALIVE,
215  UT_OOBInline = SO_OOBINLINE,
216  UT_ReuseAddr = SO_REUSEADDR,
217 #if !defined(LINUX) && !defined(MBSD)
218  UT_UseLoopBack = SO_USELOOPBACK
219 #endif
220  };
221  UT_Socket(UT_AddressFamily af, UT_SocketType st);
222  virtual ~UT_Socket();
223 
224  UT_Socket(UT_AddressFamily af);
225  UT_Socket(UT_SocketType st);
226 
227  virtual int close();
228 
229  int listen(int);
230  int recv (char *, int, UT_MsgReceiveFlags = UT_MsgReceiveNoFlags);
231  int recvfrom(char *, int, struct sockaddr *,
232  int &, UT_MsgReceiveFlags = UT_MsgReceiveNoFlags);
233 
234  int send (const char *, int, UT_MsgSendFlags = UT_MsgSendNoFlags);
235  int sendto(const char *, int, const struct sockaddr *,
236  int, UT_MsgSendFlags = UT_MsgSendNoFlags);
237 
238  int recvmsg(struct msghdr *, UT_MsgReceiveFlags = UT_MsgReceiveNoFlags);
239  int sendmsg(struct msghdr *, UT_MsgSendFlags = UT_MsgSendNoFlags);
240 
241  void linger();
242  int setOption(UT_SocketLevelOption slo, UT_FlagOption fo);
243  int getOption(UT_SocketLevelOption slo, UT_FlagOption &fo);
244 
245  UT_AddressFamily address_family() { return family; }
247  UT_SocketSex socket_sex() { return sex; }
248  int socket_protocol() { return protocol; }
249  int isclient() { return sex==UT_Client; }
250  int isserver() { return sex==UT_Server; }
251  virtual int socket();
252 
253 protected:
254  virtual void shutdown(UT_ShutDownType);
255 
256  int accept(struct sockaddr *sa, int &len);
257  int bind(const struct sockaddr *,int);
258  int connect(const struct sockaddr *,int);
259  int getpeername(struct sockaddr *, int &len);
260  int getsockname(struct sockaddr *, int &len);
261  int getsockopt(int, int, char *, int *);
262  int setsockopt(int, int, const char *, int); //
263 
264  int check_syscall(int state, const char *mess);
265  int syscall(int state, const char *mess);
266 
267  UT_SocketType type; // Socket Type
268  UT_AddressFamily family; // Address Family
269  int protocol; // Protocol (currently 0)
270  UT_SocketSex sex; // Client or Server?
271 };
272 
273 
274 
275 //===========================================================================
277 {
278 public:
280  ~UT_UnixSocket();
281 
282  int open(const UT_UnixAddress &ua, UT_SocketSex sex);
283  int accept(UT_UnixSocket &newsocket);
284  int close();
285 
286  const UT_UnixAddress &local_address() { return local; }
287  const UT_UnixAddress &remote_address() { return remote; }
288 
289 private:
290  UT_UnixAddress local;
291  UT_UnixAddress remote;
292  int unlink_socket;
293 };
294 
295 //===========================================================================
297 {
298 public:
299  UT_InetSocket();
301  ~UT_InetSocket();
302 
303  int accept(UT_InetSocket &newsocket);
304  int bind(const UT_InetAddress &ia);
305  int connect(const UT_InetAddress &ia);
306  int getpeername(UT_InetAddress &ia);
307  int getsockname(UT_InetAddress &ia);
308 
309  int recvfrom(char *buffer, int max, UT_InetAddress &is);
310  int sendto(char *buffer, int len, const UT_InetAddress &is);
311 };
312 
313 //===========================================================================
315 {
316 public:
317  UT_UnixStream() : UT_UnixSocket(UT_Stream) {};
318  ~UT_UnixStream();
319 
320  int open(const char *path, UT_SocketSex asex)
321  { return UT_UnixSocket::open(path,asex); }
322  int accept(UT_UnixStream &newstream)
323  { return UT_UnixSocket::accept(newstream); }
324  int close();
325 };
326 
327 //===========================================================================
329 {
330 public:
331  UT_InetStream();
332  ~UT_InetStream();
333 
334  int accept(UT_InetStream &newstream)
335  { return UT_InetSocket::accept(newstream); }
336 
337  int socketServer(const UT_InetAddress &ia, int minPort = -1,
338  int maxPort = -1 );
339  int socketClient(const UT_InetAddress &ia, int minPort = -1,
340  int maxPort = -1 );
341  void setNoDelay(int on_or_off);
342 };
343 
344 
345 //===========================================================================
347 {
348 public:
349  UT_InetDatagram();
350  ~UT_InetDatagram();
351 
352  int accept(UT_InetDatagram &newstream);
353 
354  int socketClient();
355  int socketClient(const UT_InetAddress &ia);
356  int socketServer(const UT_InetAddress &ia);
357 
358  virtual int socket();
359 };
360 
361 #endif /* __UT_IPC__ */
struct iovec * IOVEC_PTR
Definition: UT_IPC.h:44
UT_AddressFamily address_family()
Definition: UT_IPC.h:245
const UT_UnixAddress & remote_address()
Definition: UT_IPC.h:287
int accept(UT_InetStream &newstream)
Definition: UT_IPC.h:334
int lookup(int aport)
Definition: UT_IPC.h:89
virtual int close()
UT_AddressFamily family
Definition: UT_IPC.h:268
int any_flag
Definition: UT_IPC.h:96
int open(const UT_UnixAddress &ua, UT_SocketSex sex)
UT_AddressFamily
Definition: UT_IPC.h:181
int broadcast_flag
Definition: UT_IPC.h:97
UT_SocketType socket_type()
Definition: UT_IPC.h:246
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
const UT_UnixAddress & local_address()
Definition: UT_IPC.h:286
int accept(struct sockaddr *sa, int &len)
int isclient()
Definition: UT_IPC.h:249
int accept(UT_UnixSocket &newsocket)
UT_SocketSex socket_sex()
Definition: UT_IPC.h:247
#define UT_API
Definition: UT_API.h:13
UT_SocketType type
Definition: UT_IPC.h:267
int size() const
Definition: UT_IPC.h:118
int bind(const struct sockaddr *, int)
UT_SocketSex sex
Definition: UT_IPC.h:270
int isany() const
Definition: UT_IPC.h:76
static const char * UT_Broadcast
Definition: UT_IPC.h:55
void read(T &in, bool &v)
Definition: ImfXdr.h:611
GLuint buffer
Definition: glcorearb.h:659
UT_Status status() const
Definition: UT_IPC.h:74
int return_on_error
Definition: UT_IPC.h:148
int isserver()
Definition: UT_IPC.h:250
UT_UnixStream()
Definition: UT_IPC.h:317
std::ostream & operator<<(std::ostream &ostr, const DataType &a)
Definition: DataType.h:133
int recvfrom(char *, int, struct sockaddr *, int &, UT_MsgReceiveFlags=UT_MsgReceiveNoFlags)
int sendto(const char *, int, const struct sockaddr *, int, UT_MsgSendFlags=UT_MsgSendNoFlags)
const char * getError() const
Definition: UT_IPC.h:145
int connect(const struct sockaddr *, int)
virtual void abstract_class()=0
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:133
char * buf
Definition: UT_IPC.h:40
struct sockaddr * SOCKADDR_PTR
Definition: UT_IPC.h:43
int status
Definition: UT_IPC.h:165
static const char * UT_Any
Definition: UT_IPC.h:54
int accept(UT_InetSocket &newsocket)
int accept(UT_UnixStream &newstream)
Definition: UT_IPC.h:322
virtual int open(float queuesize)
int size() const
Definition: UT_IPC.h:75
UT_SocketType
Definition: UT_IPC.h:185
UT_SocketSex
Definition: UT_IPC.h:176
virtual void shutdown(UT_ShutDownType)=0
int socket_protocol()
Definition: UT_IPC.h:248
UT_MsgSendFlags
Definition: UT_IPC.h:193
virtual int socket()
UT_MsgReceiveFlags
Definition: UT_IPC.h:199
int getsockname(struct sockaddr *, int &len)
virtual int close()
GLenum func
Definition: glcorearb.h:782
int ok() const
Definition: UT_IPC.h:73
virtual void close()
int protocol
Definition: UT_IPC.h:269
UT_Status state
Definition: UT_IPC.h:95
int addrlen
Definition: UT_IPC.h:94
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
int getpeername(struct sockaddr *, int &len)
int lookup(const char *service, const char *protocol)
Definition: UT_IPC.h:90
UT_SocketLevelOption
Definition: UT_IPC.h:210
const struct sockaddr * CONST_SOCKADDR_PTR
Definition: UT_IPC.h:45
void write(T &out, bool v)
Definition: ImfXdr.h:332
int open(const char *path, UT_SocketSex asex)
Definition: UT_IPC.h:320
int getDescriptor() const
Definition: UT_IPC.h:146
int len
Definition: UT_IPC.h:39
int isbroadcast() const
Definition: UT_IPC.h:77
UT_FlagOption
Definition: UT_IPC.h:206
static char * Temp
Definition: UT_IPC.h:106