HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_NetPacket.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_NetPacket.h ( UT Library, C++)
7  *
8  * COMMENTS: This class can be used to read/write packetized data over a
9  * UT_NetSocket. It's really just a convenience class. The
10  * read/write methods are a little "safer" than using vanilla
11  * sockets. There are methods for setting the sockets up, data
12  * compression is supported, etc.
13  *
14  * CAVEATS: - It is not possible to write a packet which has no data
15  * - The user is responsible for putting the packet data into
16  * network byte order.
17  */
18 
19 #ifndef __UT_NetPacket__
20 #define __UT_NetPacket__
21 
22 #include "UT_API.h"
23 #include <string.h>
24 #include <SYS/SYS_Types.h>
25 
26 class UT_NetSocket;
27 class UT_WorkBuffer;
28 
30 public:
31  // By default, UT_NetPacket uses buffered writes and unbuffered reads.
32  // To change this behavior, adjust the bufsize parameters passed to
33  // the constructor. If you use a non-zero read buffer size, make sure
34  // that your algorithm only uses a single UT_NetPacket for the socket
35  // being read.
36  explicit UT_NetPacket(unsigned write_bufsize=4096,
37  unsigned read_bufsize=0);
38  ~UT_NetPacket();
39 
40  int64 getMemoryUsage(bool inclusive) const;
41 
42  // The acceptClient convenience function can be used by a server to look
43  // for a client. If there's no client, the server will wait for a few ms.
44  // before returning.
45  // - The server should be created using: UT_NetSocket(server_port)
46  // - If no client connects, the server socket should not be re-created
47  // For example:
48  // while (1) {
49  // UT_NetSocket server(port, 0);
50  // if (!server.isValid()) error("Invalid server port");
51  // do {
52  // extraProcessing();
53  // client = UT_NetPacket::acceptClient(server, 1);
54  // } while (!client);
55  // Timeout is specified in ms.
56  //
57  static UT_NetSocket *acceptClient(UT_NetSocket &server,
58  int blocking = 1,
59  int timeout=10);
60 
61  // Convenience method to get "host" string.
62  static const char *getHost();
63 
64  // Convenience method to get "user@host" string.
65  static const char *getUserHost();
66 
67  // This will return 1 if the IP address matches anything in the mask.
68  static int matchIPMask(const char *mask,
69  const char *address,
70  UT_NetSocket *socket);
71 
72  // Returns true if server ip matches the client's under given mask pattern.
73  static bool matchServerClientIPs(
74  const unsigned short server[4],
75  const unsigned short client[4],
76  const char *mask_pattern);
77 
78  // The connectToServer convenience function can be used to connect to a
79  // server on a remote host. If no connection is made within the specified
80  // timeout (in seconds), then a null will be returned.
81  // If no remote host is specified, then we will connect to the localhost.
82  // This will construct a PipeSocket if NOSOCKETS is set, unless the
83  // force socket is chosen.
84  static UT_NetSocket *connectToServer(const char *host = 0,
85  int port = -1,
86  int blocking=1,
87  int timeout=5,
88  int forcesocket=0);
89 
90  static int getDefaultServerPort();
91 
92  // Disconnect and delete the socket.
93  static void disconnectFromServer(UT_NetSocket *socket);
94 
95  enum {
96  PacketCompress = 0x01,
97  PacketCrypt = 0x02 // Currently not enabled
98  };
99 
100  // Caution: If compression is enabled, writing data may grow the internal
101  // packet buffer
102  // This returns the length of the buffer written (compressed length if
103  // turned on).
104  int writeData(UT_NetSocket &sock, const void *data, unsigned len);
105  int writeData(UT_NetSocket &sock, const char *data)
106  {
107  return writeData(sock, data, strlen(data)+1);
108  }
109 
110  // If the flush fails, then 0 is returned
111  int flush(UT_NetSocket &sock);
112 
113  // Read the uncompressed size of the next packet (0 indicates error or
114  // timeout).
115  // If timeout_ms is NULL or *timout_ms < 0, a blocking socket
116  // will block until data is available; a nonblocking will either read
117  // available data or not and will return immediatelly.
118  // If *timeout_ms >= 0, both blocking and nonblocking sockets will wait for
119  // the data until timeout in miliseconds elapses or data shows up on the
120  // socket. The timeout_ms is updated to reflect the remaining time
121  // the call would block untill the timeout would expire.
122  int readPacketLength(UT_NetSocket &sock, int *timeout_ms = NULL);
123  int readPacketLength(UT_NetSocket &sock, int *timeout_ms,
124  int &status);
125 
126  // Read the next packets data. See above for timeout description.
127  int readPacketData(UT_NetSocket &sock, void *buf, unsigned len,
128  int *timeout_ms = NULL);
129  int readPacketData(UT_NetSocket &sock, UT_WorkBuffer &buf,
130  unsigned int len, int *timeout_ms = NULL);
131  int readPacketData(UT_NetSocket &sock, void *buf, unsigned len,
132  int *timeout_ms, int &status);
133  int readPacketData(UT_NetSocket &sock, UT_WorkBuffer &buf,
134  unsigned int len, int *timeout_ms,
135  int &status);
136 
137  // A convenience method to use my internal buffer size. See above for
138  // timeout description.
139  void *readPacket(UT_NetSocket &sock, unsigned &len,
140  int timeout_ms, int &status)
141  {
142  void *rcode = 0;
143  len = readPacketLength(sock, &timeout_ms, status);
144  if (len > 0)
145  {
146  growBuffer(len);
147  if (readPacketData(sock, myData, len, &timeout_ms,
148  status))
149  rcode = myData;
150  }
151  return rcode;
152  }
153  void *readPacket(UT_NetSocket &sock, unsigned &len,
154  int timeout_ms = -1)
155  {
156  int status;
157  return readPacket(sock, len, timeout_ms, status);
158  }
159 
160  // Read a packet into the buffer pointed to by data. The size of the
161  // buffer is specified. If the packet is too big for the buffer or if
162  // there is an error reading the packet, 0 is returned. See above
163  // for timeout description.
164  unsigned readPacket(UT_NetSocket &sock, void *data, unsigned size,
165  int timeout_ms, int &status)
166  {
167  unsigned len;
168  len = readPacketLength(sock, &timeout_ms, status);
169  if (len > 0 && len <= size)
170  {
171  if (!readPacketData(sock, data, len, &timeout_ms,
172  status))
173  len = 0;
174  }
175  else len = 0;
176  return len;
177  }
178  unsigned readPacket(UT_NetSocket &sock, void *data, unsigned size,
179  int timeout_ms = -1)
180  {
181  int status;
182  return readPacket(sock, data, size, timeout_ms, status);
183  }
184 
185  // Reading a packet into a workbuffer assumes that the packet
186  // data is a string. That is, no null characters are
187  // contained within the packet data.
188  unsigned readPacket(UT_NetSocket &sock, UT_WorkBuffer &wbuf,
189  int timeout_ms = -1);
190  unsigned readPacket(UT_NetSocket &sock, UT_WorkBuffer &wbuf,
191  int timeout_ms, int &status);
192 
193  void setFlag(unsigned flag) { myFlag |= flag; }
194  void clearFlag(unsigned flag) { myFlag &= ~flag; }
195  int getFlag(unsigned flag) const { return myFlag & flag; }
196 
197  unsigned getDataSize() const { return myDataSize; }
198  void limitDataSize(unsigned maxsize=4096)
199  {
200  if (maxsize > myDataSize)
201  growBuffer(maxsize, true);
202  }
203  void *steal()
204  {
205  void *rcode = myData;
206  myData = 0;
207  myDataSize = 0;
208  return rcode;
209  }
210 
211 private:
212  // Ensure the buffer is big enough
213  void growBuffer(unsigned len, bool doshrink=false);
214 
215  int bufferWrite(UT_NetSocket &sock, const void *data, unsigned l);
216  int bufferRead(UT_NetSocket &sock, void *data, unsigned l,
217  int *timeout_ms, int &status);
218 
219  void *myData;
220  char *myWriteBuf, *myWriteCurr, *myWriteEnd;
221  char *myReadBuf, *myReadCurr, *myReadLast, *myReadEnd;
222  unsigned myDataSize;
223  unsigned myFlag;
224 };
225 
226 #endif
227 
void * readPacket(UT_NetSocket &sock, unsigned &len, int timeout_ms=-1)
Definition: UT_NetPacket.h:153
int writeData(UT_NetSocket &sock, const char *data)
Definition: UT_NetPacket.h:105
void writeData(std::ostream &os, const T *data, Index count, uint32_t compression)
Definition: Compression.h:255
void * steal()
Definition: UT_NetPacket.h:203
GLint GLuint mask
Definition: glcorearb.h:123
#define UT_API
Definition: UT_API.h:12
void clearFlag(unsigned flag)
Definition: UT_NetPacket.h:194
void * readPacket(UT_NetSocket &sock, unsigned &len, int timeout_ms, int &status)
Definition: UT_NetPacket.h:139
GLsizeiptr size
Definition: glcorearb.h:663
int getFlag(unsigned flag) const
Definition: UT_NetPacket.h:195
long long int64
Definition: SYS_Types.h:106
unsigned getDataSize() const
Definition: UT_NetPacket.h:197
GLbitfield GLuint64 timeout
Definition: glcorearb.h:1598
void limitDataSize(unsigned maxsize=4096)
Definition: UT_NetPacket.h:198
unsigned readPacket(UT_NetSocket &sock, void *data, unsigned size, int timeout_ms, int &status)
Definition: UT_NetPacket.h:164
GLboolean * data
Definition: glcorearb.h:130
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
void setFlag(unsigned flag)
Definition: UT_NetPacket.h:193
unsigned readPacket(UT_NetSocket &sock, void *data, unsigned size, int timeout_ms=-1)
Definition: UT_NetPacket.h:178
png_infop png_uint_32 flag
Definition: png.h:2242