HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_WebStream.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: NET_WebStream.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_WEBSTREAM_H__
13 #define __NET_WEBSTREAM_H__
14 
15 #include "NET_API.h"
16 
17 #include "NET_SecureSocket.h"
18 
19 #include <UT/UT_NetFDSet.h>
20 #include <UT/UT_NetSocket.h>
21 #include <UT/UT_StringHolder.h>
22 #include <UT/UT_StringView.h>
23 #include <UT/UT_UniquePtr.h>
24 #include <UT/UT_WorkBuffer.h>
25 
26 class UT_IFStream;
27 
28 // This class defines methods to write to a "stream". For instance, io to a
29 // socket, buffer, file. This will allow us to write unit tests for network
30 // code without specifically creating network connections.
31 class NET_API NET_WebStream
32 {
33 public:
34  NET_WebStream() = default;
35  virtual ~NET_WebStream() {}
36 
37  // True if the stream is a socket type. In some cases we need to know if
38  // we can use system calls that need a socket fd.
39  virtual bool sendFile(UT_IFStream& fstream, exint count, exint &offset) = 0;
40 
41  virtual int getFD() const = 0;
42  virtual int port() const { return -1; }
43 
44  // Peek at len bytes in stream
45  virtual int peek(void *data, int len, int timeout_ms = -1) = 0;
46  // Read from the underlying stream
47  // NOTE: this function appends to the passed in work buffer.
48  virtual int read(UT_WorkBuffer &wbuf, unsigned len, int timeout = -1) = 0;
49 
50  // This tries to read the entire packet and does not append to the passed
51  // in buffer.
52  virtual bool readPacket(UT_WorkBuffer &wbuf, int timeout = -1) = 0;
53 
54  // Write to the underlying stream
55  virtual bool write(const UT_WorkBuffer &data) = 0;
56  virtual bool write(const UT_StringHolder& data) = 0;
57  virtual int write(const UT_StringView& data, int& nwritten) = 0;
58  virtual int writePacket(const char *data, exint length) = 0;
59  // This writes the packet into the stream
60  virtual int writePacket(const UT_WorkBuffer &wbuf) = 0;
61 
62  inline bool isValid() const { return myIsValid; }
63 
64  virtual int dataAvailable(int timeout = 0) = 0;
65 
66 protected:
67  bool myIsValid;
68 };
69 
70 class NET_API NET_BufferStream : public NET_WebStream
71 {
72 public:
73  NET_BufferStream() = delete;
74  explicit NET_BufferStream(UT_WorkBuffer &in_buffer,
75  UT_WorkBuffer &out_buffer);
76 
77  bool sendFile(UT_IFStream& fstream, exint count, exint &offset) override;
78  int getFD() const override { return -1; }
79  // Peek at len bytes in the buffer
80  int peek(void *data, int len, int timeout_ms = -1) override;
81  // Read from a buffer
82  int read(UT_WorkBuffer &wbuf,
83  unsigned len,
84  int timeout = -1) override;
85  bool readPacket(UT_WorkBuffer &wbuf, int timeout = -1) override;
86  // Write to a buffer
87  bool write(const UT_WorkBuffer &data) override;
88  bool write(const UT_StringHolder& data) override;
89  int write(const UT_StringView &data, int& nwritten) override;
90  // Write packet to buffer
91  int writePacket(const UT_WorkBuffer &wbuf) override;
92  int writePacket(const char *data, exint length) override;
93 
94  int dataAvailable(int timeout = 0) override;
95 
96 private:
97  UT_WorkBuffer &myInBuffer;
98  UT_WorkBuffer &myOutBuffer;
99 };
100 
101 // A network stream using UT_NetSocket
102 class NET_API NET_NetworkStream : public NET_WebStream
103 {
104 public:
105  NET_NetworkStream() = delete;
107 
108  NET_NetworkStream(const NET_NetworkStream &) = delete;
109  NET_NetworkStream &operator=(const NET_NetworkStream &) = delete;
111  : mySocket(std::move(stream.mySocket))
112  {
113  }
115  {
116  mySocket = std::move(stream.mySocket);
117  return *this;
118  }
119 
120  bool sendFile(UT_IFStream& fstream, exint count, exint &offset) override;
121  int getFD() const override;
122  int port() const override
123  {
124  if (!mySocket)
125  return -1;
126  return mySocket->getPort();
127  }
128 
129  // Peek at len bytes in the socket
130  int peek(void *data, int len, int timeout_ms = -1) override;
131  // Read from a socket
132  int read(UT_WorkBuffer &wbuf,
133  unsigned len,
134  int timeout = -1) override;
135 
136  bool readPacket(UT_WorkBuffer &wbuf, int timeout = -1) override;
137 
138  // Write to the underlying stream
139  bool write(const UT_WorkBuffer &data) override;
140  bool write(const UT_StringHolder& data) override;
141  int write(const UT_StringView &data, int& nwritten) override;
142  // Write packet to buffer
143  int writePacket(const UT_WorkBuffer &data) override;
144  int writePacket(const char *data, exint length) override;
145 
146  int dataAvailable(int timeout = 0) override;
147 
148 private:
149  bool writeAll_(UT_StringView data);
151 };
152 
154 {
155 public:
156  NET_SecureNetworkStream() = delete;
159 
161  NET_SecureNetworkStream &operator=(const NET_SecureNetworkStream &) = delete;
162 
164  : mySocket(std::move(stream.mySocket))
165  {
166  }
168  {
169  mySocket = std::move(stream.mySocket);
170  return *this;
171  }
172 
173  bool sendFile(UT_IFStream& fstream, exint count, exint &offset) override;
174  int getFD() const override;
175  int port() const override
176  {
177  if (!mySocket)
178  return -1;
179  return mySocket->port();
180  }
181 
182  // Peek at len bytes in the socket
183  int peek(void *data, int len, int timeout_ms = -1) override;
184  // Read from a socket
185  int read(UT_WorkBuffer &wbuf,
186  unsigned len,
187  int timeout = -1) override;
188  bool readPacket(UT_WorkBuffer &wbuf, int timeout = -1) override;
189  // Write to the underlying stream
190  bool write(const UT_WorkBuffer &data) override;
191  bool write(const UT_StringHolder& data) override;
192  int write(const UT_StringView &data, int& nwritten) override;
193  int writePacket(const UT_WorkBuffer &data) override;
194  int writePacket(const char *data, exint length) override;
195 
196  int dataAvailable(int timeout = 0) override;
197 
198 private:
200 };
201 
203 
204 #endif // __NET_WEBSTREAM_H__
205 
GLuint GLuint stream
Definition: glew.h:7265
virtual int getFD() const =0
int64 exint
Definition: SYS_Types.h:125
NET_SecureNetworkStream(NET_SecureNetworkStream &&stream)
virtual bool sendFile(UT_IFStream &fstream, exint count, exint &offset)=0
void read(T &in, bool &v)
Definition: ImfXdr.h:611
virtual int read(UT_WorkBuffer &wbuf, unsigned len, int timeout=-1)=0
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
A utility class to do read-only operations on a subset of an existing string.
Definition: UT_StringView.h:40
virtual int writePacket(const char *data, exint length)=0
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
NET_SecureNetworkStream & operator=(NET_SecureNetworkStream &&stream)
GLuint GLsizei GLsizei * length
Definition: glew.h:1825
UT_UniquePtr< NET_WebStream > NET_WebStreamPtr
virtual int port() const
Definition: NET_WebStream.h:42
int getFD() const override
Definition: NET_WebStream.h:78
NET_NetworkStream & operator=(NET_NetworkStream &&stream)
int port() const override
virtual int dataAvailable(int timeout=0)=0
GLuint GLuint GLsizei count
Definition: glew.h:1253
virtual int peek(void *data, int len, int timeout_ms=-1)=0
virtual bool write(const UT_WorkBuffer &data)=0
bool isValid() const
Definition: NET_WebStream.h:62
GLbitfield GLuint64 timeout
Definition: glew.h:6605
void write(T &out, bool v)
Definition: ImfXdr.h:332
NET_NetworkStream(NET_NetworkStream &&stream)
virtual ~NET_WebStream()
Definition: NET_WebStream.h:35
virtual bool readPacket(UT_WorkBuffer &wbuf, int timeout=-1)=0
int port() const override
GLenum GLsizei len
Definition: glew.h:7752
GLintptr offset
Definition: glew.h:1682