1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
29 | |
30 | #ifndef PSFOUNDATION_PSSOCKET_H |
31 | #define PSFOUNDATION_PSSOCKET_H |
32 | |
33 | #include "PsUserAllocated.h" |
34 | |
35 | namespace physx |
36 | { |
37 | namespace shdfnd |
38 | { |
39 | /** |
40 | Socket abstraction API |
41 | */ |
42 | |
43 | class PX_FOUNDATION_API Socket : public UserAllocated |
44 | { |
45 | public: |
46 | static const uint32_t DEFAULT_BUFFER_SIZE; |
47 | |
48 | Socket(bool inEnableBuffering = true, bool blocking = true); |
49 | |
50 | virtual ~Socket(); |
51 | |
52 | /*! |
53 | Opens a network socket for input and/or output |
54 | |
55 | \param host |
56 | Name of the host to connect to. This can be an IP, URL, etc |
57 | |
58 | \param port |
59 | The port to connect to on the remote host |
60 | |
61 | \param timeout |
62 | Timeout in ms until the connection must be established. |
63 | |
64 | \return |
65 | True if the connection was successful, false otherwise |
66 | */ |
67 | bool connect(const char* host, uint16_t port, uint32_t timeout = 1000); |
68 | |
69 | /*! |
70 | Opens a network socket for input and/or output as a server. Put the connection in listening mode |
71 | |
72 | \param port |
73 | The port on which the socket listens |
74 | */ |
75 | bool listen(uint16_t port); |
76 | |
77 | /*! |
78 | Accept a connection on a socket that is in listening mode |
79 | |
80 | \note |
81 | This method only supports a single connection client. Additional clients |
82 | that connect to the listening port will overwrite the existing socket handle. |
83 | |
84 | \param block |
85 | whether or not the call should block |
86 | |
87 | \return whether a connection was established |
88 | */ |
89 | bool accept(bool block); |
90 | |
91 | /*! |
92 | Disconnects an open socket |
93 | */ |
94 | void disconnect(); |
95 | |
96 | /*! |
97 | Returns whether the socket is currently open (connected) or not. |
98 | |
99 | \return |
100 | True if the socket is connected, false otherwise |
101 | */ |
102 | bool isConnected() const; |
103 | |
104 | /*! |
105 | Returns the name of the connected host. This is the same as the string |
106 | that was supplied to the connect call. |
107 | |
108 | \return |
109 | The name of the connected host |
110 | */ |
111 | const char* getHost() const; |
112 | |
113 | /*! |
114 | Returns the port of the connected host. This is the same as the port |
115 | that was supplied to the connect call. |
116 | |
117 | \return |
118 | The port of the connected host |
119 | */ |
120 | uint16_t getPort() const; |
121 | |
122 | /*! |
123 | Flushes the output stream. Until the stream is flushed, there is no |
124 | guarantee that the written data has actually reached the destination |
125 | storage. Flush forces all buffered data to be sent to the output. |
126 | |
127 | \note flush always blocks. If the socket is in non-blocking mode, this will result |
128 | the thread spinning. |
129 | |
130 | \return |
131 | True if the flush was successful, false otherwise |
132 | */ |
133 | bool flush(); |
134 | |
135 | /*! |
136 | Writes data to the output stream. |
137 | |
138 | \param data |
139 | Pointer to a block of data to write to the stream |
140 | |
141 | \param length |
142 | Amount of data to write, in bytes |
143 | |
144 | \return |
145 | Number of bytes actually written. This could be lower than length if the socket is non-blocking. |
146 | */ |
147 | |
148 | uint32_t write(const uint8_t* data, uint32_t length); |
149 | |
150 | /*! |
151 | Reads data from the output stream. |
152 | |
153 | \param data |
154 | Pointer to a buffer where the read data will be stored. |
155 | |
156 | \param length |
157 | Amount of data to read, in bytes. |
158 | |
159 | \return |
160 | Number of bytes actually read. This could be lower than length if the stream end is |
161 | encountered or the socket is non-blocking. |
162 | */ |
163 | uint32_t read(uint8_t* data, uint32_t length); |
164 | |
165 | /*! |
166 | Sets blocking mode of the socket. |
167 | Socket must be connected, otherwise calling this method won't take any effect. |
168 | */ |
169 | void setBlocking(bool blocking); |
170 | |
171 | /*! |
172 | Returns whether read/write/flush calls to the socket are blocking. |
173 | |
174 | \return |
175 | True if the socket is blocking. |
176 | */ |
177 | bool isBlocking() const; |
178 | |
179 | private: |
180 | class SocketImpl* mImpl; |
181 | }; |
182 | |
183 | } // namespace shdfnd |
184 | } // namespace physx |
185 | |
186 | #endif // PSFOUNDATION_PSSOCKET_H |
187 | |