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 "UT_NonCopyable.h"
24 #include <SYS/SYS_Types.h>
25 #include <SYS/SYS_Deprecated.h>
26 
27 #include <string.h>
28 
29 class UT_NetSocket;
30 class UT_IpAddressV4;
31 class UT_StringHolder;
32 class UT_WorkBuffer;
33 
34 /// Use NET_PacketSocket instead of UT_NetPacket. UT_NetPacket has a lot of
35 /// short comings when something unexpected occurs. UT_NetPacket also
36 /// doesn't support async very well. Additionally, its very difficult to get
37 /// proper error information when something goes wrong.
39 {
40 public:
41  // By default, UT_NetPacket uses buffered writes and unbuffered reads.
42  // To change this behavior, adjust the bufsize parameters passed to
43  // the constructor. If you use a non-zero read buffer size, make sure
44  // that your algorithm only uses a single UT_NetPacket for the socket
45  // being read.
46  explicit UT_NetPacket(unsigned write_bufsize=4096,
47  unsigned read_bufsize=0);
48  ~UT_NetPacket();
49 
51 
52  int64 getMemoryUsage(bool inclusive) const;
53 
54  // The acceptClient convenience function can be used by a server to look
55  // for a client. If there's no client, the server will wait for a few ms.
56  // before returning.
57  // - The server should be created using: UT_NetSocket(server_port)
58  // - If no client connects, the server socket should not be re-created
59  // For example:
60  // while (1) {
61  // UT_NetSocket server(port, 0);
62  // if (!server.isValid()) error("Invalid server port");
63  // do {
64  // extraProcessing();
65  // client = UT_NetPacket::acceptClient(server, 1);
66  // } while (!client);
67  // Timeout is specified in ms.
68  //
69  static UT_NetSocket *acceptClient(UT_NetSocket &server,
70  int blocking = 1,
71  int timeout=10);
72 
73  // Convenience method to get "host" string.
74  static const char *getHost();
75 
76  // Convenience method to get "user@host" string.
77  static const char *getUserHost();
78 
79  // This will return 1 if the IP address matches anything in the mask.
80  static int matchIPMask(const char *mask,
81  const char *address,
82  UT_NetSocket *socket);
83 
84  // Returns true if server ip matches the client's under given mask pattern.
85  static bool matchServerClientIPs(
86  const UT_IpAddressV4& server,
87  const UT_IpAddressV4& client,
88  const char *mask_pattern);
89 
90  // The connectToServer convenience function can be used to connect to a
91  // server on a remote host. If no connection is made within the specified
92  // timeout (in seconds), then a null will be returned.
93  // If no remote host is specified, then we will connect to the localhost.
94  // This will construct a PipeSocket if NOSOCKETS is set, unless the
95  // force socket is chosen.
96  static UT_NetSocket *connectToServer(const char *host = 0,
97  int port = -1,
98  int blocking=1,
99  int timeout=5);
100 
101  static int getDefaultServerPort();
102 
103  // Disconnect and delete the socket.
104  static void disconnectFromServer(UT_NetSocket *socket);
105 
106  enum {
107  PacketCompress = 0x01,
108  PacketCrypt = 0x02 // Currently not enabled
109  };
110 
111  // Caution: If compression is enabled, writing data may grow the internal
112  // packet buffer
113  // This returns the length of the buffer written (compressed length if
114  // turned on).
115  int writeData(UT_NetSocket &sock, const void *data, unsigned len);
116  int writeData(UT_NetSocket &sock, const char *data)
117  {
118  return writeData(sock, data, strlen(data)+1);
119  }
120  // Write out just the data found in the passed UT_WorkBuffer.
121  bool write(UT_NetSocket& sock, const UT_WorkBuffer& data);
122 
123  // If the flush fails, then 0 is returned
124  int flush(UT_NetSocket &sock);
125 
126  // Read the uncompressed size of the next packet (0 indicates error or
127  // timeout).
128  // If timeout_ms is NULL or *timout_ms < 0, a blocking socket
129  // will block until data is available; a nonblocking will either read
130  // available data or not and will return immediatelly.
131  // If *timeout_ms >= 0, both blocking and nonblocking sockets will wait for
132  // the data until timeout in miliseconds elapses or data shows up on the
133  // socket. The timeout_ms is updated to reflect the remaining time
134  // the call would block untill the timeout would expire.
135  int readPacketLength(UT_NetSocket &sock, int *timeout_ms = NULL);
136  int readPacketLength(UT_NetSocket &sock, int *timeout_ms,
137  int &status);
138 
139  // Read the next packets data. See above for timeout description.
140  int readPacketData(UT_NetSocket &sock, void *buf, unsigned len,
141  int *timeout_ms = NULL);
142  int readPacketData(UT_NetSocket &sock, UT_WorkBuffer &buf,
143  unsigned int len, int *timeout_ms = NULL);
144  int readPacketData(UT_NetSocket &sock, void *buf, unsigned len,
145  int *timeout_ms, int &status);
146  int readPacketData(UT_NetSocket &sock, UT_WorkBuffer &buf,
147  unsigned int len, int *timeout_ms,
148  int &status);
149 
150  // A convenience method to use my internal buffer size. See above for
151  // timeout description.
152  void *readPacket(UT_NetSocket &sock, unsigned &len,
153  int timeout_ms, int &status);
154  void *readPacket(UT_NetSocket &sock, unsigned &len,
155  int timeout_ms = -1);
156 
157  // Read a packet into the buffer pointed to by data. The size of the
158  // buffer is specified. If the packet is too big for the buffer or if
159  // there is an error reading the packet, 0 is returned. See above
160  // for timeout description.
161  unsigned readPacket(UT_NetSocket &sock, void *data, unsigned size,
162  int timeout_ms, int &status);
163  unsigned readPacket(UT_NetSocket &sock, void *data, unsigned size,
164  int timeout_ms = -1);
165 
166  // Append chunks of socket data into a UT_WorkBuffer
167  bool read(UT_NetSocket &sock, UT_WorkBuffer& wbuf,
168  unsigned len, int timeout = -1);
169  bool read(UT_NetSocket &sock, UT_WorkBuffer& wbuf,
170  unsigned len, int timeout, int &status);
171  // Reading a packet into a workbuffer assumes that the packet
172  // data is a string. That is, no null characters are
173  // contained within the packet data.
174  unsigned readPacket(UT_NetSocket &sock, UT_WorkBuffer &wbuf,
175  int timeout_ms = -1);
176  unsigned readPacket(UT_NetSocket &sock, UT_WorkBuffer &wbuf,
177  int timeout_ms, int &status);
178 
179  void setFlag(unsigned flag) { myFlag |= flag; }
180  void clearFlag(unsigned flag) { myFlag &= ~flag; }
181  int getFlag(unsigned flag) const { return myFlag & flag; }
182 
183  unsigned getDataSize() const { return myDataSize; }
184  void limitDataSize(unsigned maxsize=4096)
185  {
186  if (maxsize > myDataSize)
187  growBuffer(maxsize, true);
188  }
189  void *steal()
190  {
191  void *rcode = myData;
192  myData = 0;
193  myDataSize = 0;
194  return rcode;
195  }
196 
197 private:
198  // Ensure the buffer is big enough
199  void growBuffer(unsigned len, bool doshrink=false);
200 
201  int bufferWrite(UT_NetSocket &sock, const void *data, unsigned l);
202  int bufferRead(UT_NetSocket &sock, void *data, unsigned l,
203  int *timeout_ms, int &status);
204 
205  void *myData;
206  char *myWriteBuf, *myWriteCurr, *myWriteEnd;
207  char *myReadBuf, *myReadCurr, *myReadLast, *myReadEnd;
208  unsigned myDataSize;
209  unsigned myFlag;
210 };
211 
212 inline
213 void*
215  int timeout_ms, int &status)
216 {
217  void* rcode = 0;
218  // consume the first 4 bytes of the packet.
219  len = readPacketLength(sock, &timeout_ms, status);
220  if (len > 0)
221  {
222  growBuffer(len);
223  if (readPacketData(sock, myData, len, &timeout_ms, status))
224  rcode = myData;
225  }
226  return rcode;
227 }
228 
229 inline
230 void*
231 UT_NetPacket::readPacket(UT_NetSocket& sock, unsigned& len, int timeout_ms)
232 {
233  int status;
234  return readPacket(sock, len, timeout_ms, status);
235 }
236 
237 inline
238 unsigned
240  int timeout_ms, int& status)
241 {
242  unsigned len = readPacketLength(sock, &timeout_ms, status);
243  if (len > 0 && len <= size)
244  {
245  if (!readPacketData(sock, data, len, &timeout_ms, status))
246  len = 0;
247  }
248  else len = 0;
249  return len;
250 }
251 
252 inline
253 unsigned
255  int timeout_ms)
256 {
257  int status;
258  return readPacket(sock, data, size, timeout_ms, status);
259 }
260 
261 #endif
262 
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
int writeData(UT_NetSocket &sock, const char *data)
Definition: UT_NetPacket.h:116
void writeData(std::ostream &os, const T *data, Index count, uint32_t compression)
Definition: Compression.h:363
This represents a Ipv4 address.
Definition: UT_IpAddress.h:34
void * steal()
Definition: UT_NetPacket.h:189
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:214
#define UT_API
Definition: UT_API.h:14
void clearFlag(unsigned flag)
Definition: UT_NetPacket.h:180
void read(T &in, bool &v)
Definition: ImfXdr.h:502
int getFlag(unsigned flag) const
Definition: UT_NetPacket.h:181
unsigned getDataSize() const
Definition: UT_NetPacket.h:183
GLbitfield GLuint64 timeout
Definition: glcorearb.h:1599
void limitDataSize(unsigned maxsize=4096)
Definition: UT_NetPacket.h:184
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
GLint GLuint mask
Definition: glcorearb.h:124
long long int64
Definition: SYS_Types.h:116
int readPacketData(UT_NetSocket &sock, void *buf, unsigned len, int *timeout_ms=NULL)
GLsizeiptr size
Definition: glcorearb.h:664
void setFlag(unsigned flag)
Definition: UT_NetPacket.h:179
void write(T &out, bool v)
Definition: ImfXdr.h:287
Definition: format.h:895