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  virtual bool isLocalConnection() const = 0;
67 protected:
68  bool myIsValid;
69 };
70 
71 class NET_API NET_BufferStream : public NET_WebStream
72 {
73 public:
74  NET_BufferStream() = delete;
75  explicit NET_BufferStream(UT_WorkBuffer &in_buffer,
76  UT_WorkBuffer &out_buffer);
77 
78  bool sendFile(UT_IFStream& fstream, exint count, exint &offset) override;
79  int getFD() const override { return -1; }
80  // Peek at len bytes in the buffer
81  int peek(void *data, int len, int timeout_ms = -1) override;
82  // Read from a buffer
83  int read(UT_WorkBuffer &wbuf,
84  unsigned len,
85  int timeout = -1) override;
86  bool readPacket(UT_WorkBuffer &wbuf, int timeout = -1) override;
87  // Write to a buffer
88  bool write(const UT_WorkBuffer &data) override;
89  bool write(const UT_StringHolder& data) override;
90  int write(const UT_StringView &data, int& nwritten) override;
91  // Write packet to buffer
92  int writePacket(const UT_WorkBuffer &wbuf) override;
93  int writePacket(const char *data, exint length) override;
94 
95  int dataAvailable(int timeout = 0) override;
96 
97  bool isLocalConnection() const override { return true; }
98 private:
99  UT_WorkBuffer &myInBuffer;
100  UT_WorkBuffer &myOutBuffer;
101 };
102 
103 // A network stream using UT_NetSocket
104 class NET_API NET_NetworkStream : public NET_WebStream
105 {
106 public:
107  NET_NetworkStream() = delete;
109 
110  NET_NetworkStream(const NET_NetworkStream &) = delete;
111  NET_NetworkStream &operator=(const NET_NetworkStream &) = delete;
113  : mySocket(std::move(stream.mySocket))
114  {
115  }
117  {
118  mySocket = std::move(stream.mySocket);
119  return *this;
120  }
121 
122  bool sendFile(UT_IFStream& fstream, exint count, exint &offset) override;
123  int getFD() const override;
124  int port() const override
125  {
126  if (!mySocket)
127  return -1;
128  return mySocket->getPort();
129  }
130 
131  // Peek at len bytes in the socket
132  int peek(void *data, int len, int timeout_ms = -1) override;
133  // Read from a socket
134  int read(UT_WorkBuffer &wbuf,
135  unsigned len,
136  int timeout = -1) override;
137 
138  bool readPacket(UT_WorkBuffer &wbuf, int timeout = -1) override;
139 
140  // Write to the underlying stream
141  bool write(const UT_WorkBuffer &data) override;
142  bool write(const UT_StringHolder& data) override;
143  int write(const UT_StringView &data, int& nwritten) override;
144  // Write packet to buffer
145  int writePacket(const UT_WorkBuffer &data) override;
146  int writePacket(const char *data, exint length) override;
147 
148  int dataAvailable(int timeout = 0) override;
149 
150  bool isLocalConnection() const override
151  {
152  if (!mySocket)
153  return false;
154  return mySocket->isLocalConnection();
155  }
156 private:
157  bool writeAll_(UT_StringView data);
159 };
160 
162 {
163 public:
164  NET_SecureNetworkStream() = delete;
167 
169  NET_SecureNetworkStream &operator=(const NET_SecureNetworkStream &) = delete;
170 
172  : mySocket(std::move(stream.mySocket))
173  {
174  }
176  {
177  mySocket = std::move(stream.mySocket);
178  return *this;
179  }
180 
181  bool sendFile(UT_IFStream& fstream, exint count, exint &offset) override;
182  int getFD() const override;
183  int port() const override
184  {
185  if (!mySocket)
186  return -1;
187  return mySocket->port();
188  }
189 
190  // Peek at len bytes in the socket
191  int peek(void *data, int len, int timeout_ms = -1) override;
192  // Read from a socket
193  int read(UT_WorkBuffer &wbuf,
194  unsigned len,
195  int timeout = -1) override;
196  bool readPacket(UT_WorkBuffer &wbuf, int timeout = -1) override;
197  // Write to the underlying stream
198  bool write(const UT_WorkBuffer &data) override;
199  bool write(const UT_StringHolder& data) override;
200  int write(const UT_StringView &data, int& nwritten) override;
201  int writePacket(const UT_WorkBuffer &data) override;
202  int writePacket(const char *data, exint length) override;
203 
204  int dataAvailable(int timeout = 0) override;
205 
206  bool isLocalConnection() const override
207  {
208  if (!mySocket)
209  return false;
210  return mySocket->isLocalConnection();
211  }
212 private:
214 };
215 
217 
218 #endif // __NET_WEBSTREAM_H__
219 
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
bool isLocalConnection() const override
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:79
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 isLocalConnection() const override
Definition: NET_WebStream.h:97
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
bool isLocalConnection() const override
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