HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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_StringHolder;
28 class UT_WorkBuffer;
29 
31 public:
32  // By default, UT_NetPacket uses buffered writes and unbuffered reads.
33  // To change this behavior, adjust the bufsize parameters passed to
34  // the constructor. If you use a non-zero read buffer size, make sure
35  // that your algorithm only uses a single UT_NetPacket for the socket
36  // being read.
37  explicit UT_NetPacket(unsigned write_bufsize=4096,
38  unsigned read_bufsize=0);
39  ~UT_NetPacket();
40 
41  int64 getMemoryUsage(bool inclusive) const;
42 
43  // The acceptClient convenience function can be used by a server to look
44  // for a client. If there's no client, the server will wait for a few ms.
45  // before returning.
46  // - The server should be created using: UT_NetSocket(server_port)
47  // - If no client connects, the server socket should not be re-created
48  // For example:
49  // while (1) {
50  // UT_NetSocket server(port, 0);
51  // if (!server.isValid()) error("Invalid server port");
52  // do {
53  // extraProcessing();
54  // client = UT_NetPacket::acceptClient(server, 1);
55  // } while (!client);
56  // Timeout is specified in ms.
57  //
58  static UT_NetSocket *acceptClient(UT_NetSocket &server,
59  int blocking = 1,
60  int timeout=10);
61 
62  // Convenience method to get "host" string.
63  static const char *getHost();
64 
65  // Convenience method to get "user@host" string.
66  static const char *getUserHost();
67 
68  // This will return 1 if the IP address matches anything in the mask.
69  static int matchIPMask(const char *mask,
70  const char *address,
71  UT_NetSocket *socket);
72 
73  // Returns true if server ip matches the client's under given mask pattern.
74  static bool matchServerClientIPs(
75  const unsigned short server[4],
76  const unsigned short client[4],
77  const char *mask_pattern);
78 
79  // The connectToServer convenience function can be used to connect to a
80  // server on a remote host. If no connection is made within the specified
81  // timeout (in seconds), then a null will be returned.
82  // If no remote host is specified, then we will connect to the localhost.
83  // This will construct a PipeSocket if NOSOCKETS is set, unless the
84  // force socket is chosen.
85  static UT_NetSocket *connectToServer(const char *host = 0,
86  int port = -1,
87  int blocking=1,
88  int timeout=5);
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  // Write out just the data found in the passed UT_WorkBuffer.
110  bool write(UT_NetSocket& sock, const UT_WorkBuffer& data);
111 
112  // If the flush fails, then 0 is returned
113  int flush(UT_NetSocket &sock);
114 
115  // Read the uncompressed size of the next packet (0 indicates error or
116  // timeout).
117  // If timeout_ms is NULL or *timout_ms < 0, a blocking socket
118  // will block until data is available; a nonblocking will either read
119  // available data or not and will return immediatelly.
120  // If *timeout_ms >= 0, both blocking and nonblocking sockets will wait for
121  // the data until timeout in miliseconds elapses or data shows up on the
122  // socket. The timeout_ms is updated to reflect the remaining time
123  // the call would block untill the timeout would expire.
124  int readPacketLength(UT_NetSocket &sock, int *timeout_ms = NULL);
125  int readPacketLength(UT_NetSocket &sock, int *timeout_ms,
126  int &status);
127 
128  // Read the next packets data. See above for timeout description.
129  int readPacketData(UT_NetSocket &sock, void *buf, unsigned len,
130  int *timeout_ms = NULL);
131  int readPacketData(UT_NetSocket &sock, UT_WorkBuffer &buf,
132  unsigned int len, int *timeout_ms = NULL);
133  int readPacketData(UT_NetSocket &sock, void *buf, unsigned len,
134  int *timeout_ms, int &status);
135  int readPacketData(UT_NetSocket &sock, UT_WorkBuffer &buf,
136  unsigned int len, int *timeout_ms,
137  int &status);
138 
139  // A convenience method to use my internal buffer size. See above for
140  // timeout description.
141  void *readPacket(UT_NetSocket &sock, unsigned &len,
142  int timeout_ms, int &status);
143  void *readPacket(UT_NetSocket &sock, unsigned &len,
144  int timeout_ms = -1);
145 
146  // Read a packet into the buffer pointed to by data. The size of the
147  // buffer is specified. If the packet is too big for the buffer or if
148  // there is an error reading the packet, 0 is returned. See above
149  // for timeout description.
150  unsigned readPacket(UT_NetSocket &sock, void *data, unsigned size,
151  int timeout_ms, int &status);
152  unsigned readPacket(UT_NetSocket &sock, void *data, unsigned size,
153  int timeout_ms = -1);
154 
155  // Append chunks of socket data into a UT_WorkBuffer
156  bool read(UT_NetSocket &sock, UT_WorkBuffer& wbuf,
157  unsigned len, int timeout = -1);
158  bool read(UT_NetSocket &sock, UT_WorkBuffer& wbuf,
159  unsigned len, int timeout, int &status);
160  // Reading a packet into a workbuffer assumes that the packet
161  // data is a string. That is, no null characters are
162  // contained within the packet data.
163  unsigned readPacket(UT_NetSocket &sock, UT_WorkBuffer &wbuf,
164  int timeout_ms = -1);
165  unsigned readPacket(UT_NetSocket &sock, UT_WorkBuffer &wbuf,
166  int timeout_ms, int &status);
167 
168  void setFlag(unsigned flag) { myFlag |= flag; }
169  void clearFlag(unsigned flag) { myFlag &= ~flag; }
170  int getFlag(unsigned flag) const { return myFlag & flag; }
171 
172  unsigned getDataSize() const { return myDataSize; }
173  void limitDataSize(unsigned maxsize=4096)
174  {
175  if (maxsize > myDataSize)
176  growBuffer(maxsize, true);
177  }
178  void *steal()
179  {
180  void *rcode = myData;
181  myData = 0;
182  myDataSize = 0;
183  return rcode;
184  }
185 
186 private:
187  // Ensure the buffer is big enough
188  void growBuffer(unsigned len, bool doshrink=false);
189 
190  int bufferWrite(UT_NetSocket &sock, const void *data, unsigned l);
191  int bufferRead(UT_NetSocket &sock, void *data, unsigned l,
192  int *timeout_ms, int &status);
193 
194  void *myData;
195  char *myWriteBuf, *myWriteCurr, *myWriteEnd;
196  char *myReadBuf, *myReadCurr, *myReadLast, *myReadEnd;
197  unsigned myDataSize;
198  unsigned myFlag;
199 };
200 
201 inline
202 void*
204  int timeout_ms, int &status)
205 {
206  void* rcode = 0;
207  // consume the first 4 bytes of the packet.
208  len = readPacketLength(sock, &timeout_ms, status);
209  if (len > 0)
210  {
211  growBuffer(len);
212  if (readPacketData(sock, myData, len, &timeout_ms, status))
213  rcode = myData;
214  }
215  return rcode;
216 }
217 
218 inline
219 void*
220 UT_NetPacket::readPacket(UT_NetSocket& sock, unsigned& len, int timeout_ms)
221 {
222  int status;
223  return readPacket(sock, len, timeout_ms, status);
224 }
225 
226 inline
227 unsigned
229  int timeout_ms, int& status)
230 {
231  unsigned len = readPacketLength(sock, &timeout_ms, status);
232  if (len > 0 && len <= size)
233  {
234  if (!readPacketData(sock, data, len, &timeout_ms, status))
235  len = 0;
236  }
237  else len = 0;
238  return len;
239 }
240 
241 inline
242 unsigned
244  int timeout_ms)
245 {
246  int status;
247  return readPacket(sock, data, size, timeout_ms, status);
248 }
249 
250 #endif
251 
GLsizeiptr size
Definition: glew.h:1681
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:363
void * steal()
Definition: UT_NetPacket.h:178
int readPacketLength(UT_NetSocket &sock, int *timeout_ms=NULL)
void * readPacket(UT_NetSocket &sock, unsigned &len, int timeout_ms, int &status)
Definition: UT_NetPacket.h:203
#define UT_API
Definition: UT_API.h:13
void clearFlag(unsigned flag)
Definition: UT_NetPacket.h:169
GLdouble l
Definition: glew.h:9122
GLenum GLint GLuint mask
Definition: glew.h:1845
void read(T &in, bool &v)
Definition: ImfXdr.h:611
GLuint GLuint64EXT address
Definition: glew.h:14600
int getFlag(unsigned flag) const
Definition: UT_NetPacket.h:170
unsigned getDataSize() const
Definition: UT_NetPacket.h:172
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
void limitDataSize(unsigned maxsize=4096)
Definition: UT_NetPacket.h:173
long long int64
Definition: SYS_Types.h:116
int readPacketData(UT_NetSocket &sock, void *buf, unsigned len, int *timeout_ms=NULL)
void setFlag(unsigned flag)
Definition: UT_NetPacket.h:168
GLbitfield GLuint64 timeout
Definition: glew.h:6605
void write(T &out, bool v)
Definition: ImfXdr.h:332
GLenum GLuint GLsizei const GLchar * buf
Definition: glew.h:2580
GLenum GLsizei len
Definition: glew.h:7752