rev |
line source |
nuclear@0
|
1 /************************************************************************************
|
nuclear@0
|
2
|
nuclear@0
|
3 PublicHeader: n/a
|
nuclear@0
|
4 Filename : OVR_Socket.h
|
nuclear@0
|
5 Content : Socket common data shared between all platforms.
|
nuclear@0
|
6 Created : June 10, 2014
|
nuclear@0
|
7 Authors : Kevin Jenkins, Chris Taylor
|
nuclear@0
|
8
|
nuclear@0
|
9 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
|
nuclear@0
|
10
|
nuclear@0
|
11 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
|
nuclear@0
|
12 you may not use the Oculus VR Rift SDK except in compliance with the License,
|
nuclear@0
|
13 which is provided at the time of installation or download, or which
|
nuclear@0
|
14 otherwise accompanies this software in either electronic or hard copy form.
|
nuclear@0
|
15
|
nuclear@0
|
16 You may obtain a copy of the License at
|
nuclear@0
|
17
|
nuclear@0
|
18 http://www.oculusvr.com/licenses/LICENSE-3.2
|
nuclear@0
|
19
|
nuclear@0
|
20 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
|
nuclear@0
|
21 distributed under the License is distributed on an "AS IS" BASIS,
|
nuclear@0
|
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
nuclear@0
|
23 See the License for the specific language governing permissions and
|
nuclear@0
|
24 limitations under the License.
|
nuclear@0
|
25
|
nuclear@0
|
26 ************************************************************************************/
|
nuclear@0
|
27
|
nuclear@0
|
28 #ifndef OVR_Socket_h
|
nuclear@0
|
29 #define OVR_Socket_h
|
nuclear@0
|
30
|
nuclear@0
|
31 #include "../Kernel/OVR_Types.h"
|
nuclear@0
|
32 #include "../Kernel/OVR_Timer.h"
|
nuclear@0
|
33 #include "../Kernel/OVR_Allocator.h"
|
nuclear@0
|
34 #include "../Kernel/OVR_RefCount.h"
|
nuclear@0
|
35 #include "../Kernel/OVR_String.h"
|
nuclear@0
|
36
|
nuclear@0
|
37 // OS-specific socket headers
|
nuclear@0
|
38 #if defined(OVR_OS_WIN32)
|
nuclear@0
|
39 #include <WinSock2.h>
|
nuclear@0
|
40 #include <WS2tcpip.h>
|
nuclear@0
|
41 #define WIN32_LEAN_AND_MEAN
|
nuclear@0
|
42 #include <windows.h>
|
nuclear@0
|
43 #else
|
nuclear@0
|
44 # include <unistd.h>
|
nuclear@0
|
45 # include <sys/types.h>
|
nuclear@0
|
46 # include <netinet/in.h>
|
nuclear@0
|
47 #ifdef OVR_OS_ANDROID
|
nuclear@0
|
48 #include <sys/socket.h>
|
nuclear@0
|
49 #endif
|
nuclear@0
|
50 #endif
|
nuclear@0
|
51
|
nuclear@0
|
52 namespace OVR { namespace Net {
|
nuclear@0
|
53
|
nuclear@0
|
54 class SockAddr;
|
nuclear@0
|
55 class UDPSocket;
|
nuclear@0
|
56 class TCPSocket;
|
nuclear@0
|
57
|
nuclear@0
|
58
|
nuclear@0
|
59 //-----------------------------------------------------------------------------
|
nuclear@0
|
60 // Portable numeric Socket handle
|
nuclear@0
|
61 #if defined(OVR_OS_WIN32)
|
nuclear@0
|
62 typedef SOCKET SocketHandle;
|
nuclear@0
|
63 #else
|
nuclear@0
|
64 typedef int SocketHandle;
|
nuclear@0
|
65 static const SocketHandle INVALID_SOCKET = -1;
|
nuclear@0
|
66 static const int SOCKET_ERROR = -1;
|
nuclear@0
|
67 #endif
|
nuclear@0
|
68
|
nuclear@0
|
69
|
nuclear@0
|
70 //-----------------------------------------------------------------------------
|
nuclear@0
|
71 // Types of network transport
|
nuclear@0
|
72 enum TransportType
|
nuclear@0
|
73 {
|
nuclear@0
|
74 TransportType_None, // No transport (useful placeholder for invalid states)
|
nuclear@0
|
75 TransportType_Loopback, // Loopback transport: Class talks to itself
|
nuclear@0
|
76 TransportType_TCP, // TCP/IPv4/v6
|
nuclear@0
|
77 TransportType_UDP, // UDP/IPv4/v6
|
nuclear@0
|
78 TransportType_PacketizedTCP // Packetized TCP: Message framing is automatic
|
nuclear@0
|
79 };
|
nuclear@0
|
80
|
nuclear@0
|
81
|
nuclear@0
|
82 //-----------------------------------------------------------------------------
|
nuclear@0
|
83 // Abstraction for a network socket. Inheritance hierarchy
|
nuclear@0
|
84 // modeled after RakNet so that future support can be added
|
nuclear@0
|
85 // for Linux, Windows RT, consoles, etc.
|
nuclear@0
|
86 class Socket : public RefCountBase<Socket>
|
nuclear@0
|
87 {
|
nuclear@0
|
88 public:
|
nuclear@0
|
89 Socket();
|
nuclear@0
|
90 virtual void Close() = 0;
|
nuclear@0
|
91
|
nuclear@0
|
92 public:
|
nuclear@0
|
93 TransportType Transport; // Type of transport
|
nuclear@0
|
94 };
|
nuclear@0
|
95
|
nuclear@0
|
96
|
nuclear@0
|
97 //-----------------------------------------------------------------------------
|
nuclear@0
|
98 // Bind parameters for Berkley sockets
|
nuclear@0
|
99 struct BerkleyBindParameters
|
nuclear@0
|
100 {
|
nuclear@0
|
101 public:
|
nuclear@0
|
102 BerkleyBindParameters();
|
nuclear@0
|
103
|
nuclear@0
|
104 public:
|
nuclear@0
|
105 uint16_t Port; // Port
|
nuclear@0
|
106 String Address;
|
nuclear@0
|
107 uint32_t blockingTimeout;
|
nuclear@0
|
108 };
|
nuclear@0
|
109
|
nuclear@0
|
110
|
nuclear@0
|
111 //-----------------------------------------------------------------------------
|
nuclear@0
|
112 // Berkley socket
|
nuclear@0
|
113 class BerkleySocket : public Socket
|
nuclear@0
|
114 {
|
nuclear@0
|
115 public:
|
nuclear@0
|
116 BerkleySocket();
|
nuclear@0
|
117 virtual ~BerkleySocket();
|
nuclear@0
|
118
|
nuclear@0
|
119 virtual void Close();
|
nuclear@0
|
120 virtual int32_t GetSockname(SockAddr* pSockAddrOut);
|
nuclear@0
|
121 virtual void SetBlockingTimeout(int timeoutMs) // milliseconds
|
nuclear@0
|
122 {
|
nuclear@0
|
123 TimeoutSec = timeoutMs / 1000;
|
nuclear@0
|
124 TimeoutUsec = (timeoutMs % 1000) * 1000;
|
nuclear@0
|
125 }
|
nuclear@0
|
126 int GetBlockingTimeoutUsec() const
|
nuclear@0
|
127 {
|
nuclear@0
|
128 return TimeoutUsec;
|
nuclear@0
|
129 }
|
nuclear@0
|
130 int GetBlockingTimeoutSec() const
|
nuclear@0
|
131 {
|
nuclear@0
|
132 return TimeoutSec;
|
nuclear@0
|
133 }
|
nuclear@0
|
134 SocketHandle GetSocketHandle() const
|
nuclear@0
|
135 {
|
nuclear@0
|
136 return TheSocket;
|
nuclear@0
|
137 }
|
nuclear@0
|
138
|
nuclear@0
|
139 protected:
|
nuclear@0
|
140 SocketHandle TheSocket; // Socket handle
|
nuclear@0
|
141 int TimeoutUsec, TimeoutSec;
|
nuclear@0
|
142 };
|
nuclear@0
|
143
|
nuclear@0
|
144
|
nuclear@0
|
145 //-----------------------------------------------------------------------------
|
nuclear@0
|
146 // UDP socket events
|
nuclear@0
|
147 class SocketEvent_UDP
|
nuclear@0
|
148 {
|
nuclear@0
|
149 public:
|
nuclear@0
|
150 virtual ~SocketEvent_UDP(){}
|
nuclear@0
|
151
|
nuclear@0
|
152 virtual void UDP_OnRecv(Socket* pSocket, uint8_t* pData,
|
nuclear@0
|
153 uint32_t bytesRead, SockAddr* pSockAddr)
|
nuclear@0
|
154 {
|
nuclear@0
|
155 OVR_UNUSED4(pSocket, pData, bytesRead, pSockAddr);
|
nuclear@0
|
156 }
|
nuclear@0
|
157 };
|
nuclear@0
|
158
|
nuclear@0
|
159
|
nuclear@0
|
160 //-----------------------------------------------------------------------------
|
nuclear@0
|
161 // TCP socket events
|
nuclear@0
|
162 class SocketEvent_TCP
|
nuclear@0
|
163 {
|
nuclear@0
|
164 public:
|
nuclear@0
|
165 virtual ~SocketEvent_TCP(){}
|
nuclear@0
|
166
|
nuclear@0
|
167 virtual void TCP_OnRecv (Socket* pSocket,
|
nuclear@0
|
168 uint8_t* pData,
|
nuclear@0
|
169 int bytesRead)
|
nuclear@0
|
170 {
|
nuclear@0
|
171 OVR_UNUSED3(pSocket, pData, bytesRead);
|
nuclear@0
|
172 }
|
nuclear@0
|
173 virtual void TCP_OnClosed (TCPSocket* pSocket)
|
nuclear@0
|
174 {
|
nuclear@0
|
175 OVR_UNUSED(pSocket);
|
nuclear@0
|
176 }
|
nuclear@0
|
177 virtual void TCP_OnAccept (TCPSocket* pListener,
|
nuclear@0
|
178 SockAddr* pSockAddr,
|
nuclear@0
|
179 SocketHandle newSock)
|
nuclear@0
|
180 {
|
nuclear@0
|
181 OVR_UNUSED3(pListener, pSockAddr, newSock);
|
nuclear@0
|
182 }
|
nuclear@0
|
183 virtual void TCP_OnConnected(TCPSocket* pSocket)
|
nuclear@0
|
184 {
|
nuclear@0
|
185 OVR_UNUSED(pSocket);
|
nuclear@0
|
186 }
|
nuclear@0
|
187 };
|
nuclear@0
|
188
|
nuclear@0
|
189
|
nuclear@0
|
190 //-----------------------------------------------------------------------------
|
nuclear@0
|
191 // UDP Berkley socket
|
nuclear@0
|
192
|
nuclear@0
|
193 // Base class for UDP sockets, code shared between platforms
|
nuclear@0
|
194 class UDPSocketBase : public BerkleySocket
|
nuclear@0
|
195 {
|
nuclear@0
|
196 public:
|
nuclear@0
|
197 UDPSocketBase();
|
nuclear@0
|
198
|
nuclear@0
|
199 public:
|
nuclear@0
|
200 virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters) = 0;
|
nuclear@0
|
201 virtual int Send(const void* pData,
|
nuclear@0
|
202 int bytes,
|
nuclear@0
|
203 SockAddr* pSockAddr) = 0;
|
nuclear@0
|
204 virtual void Poll(SocketEvent_UDP* eventHandler) = 0;
|
nuclear@0
|
205
|
nuclear@0
|
206 protected:
|
nuclear@0
|
207 virtual void OnRecv(SocketEvent_UDP* eventHandler,
|
nuclear@0
|
208 uint8_t* pData,
|
nuclear@0
|
209 int bytesRead,
|
nuclear@0
|
210 SockAddr* address) = 0;
|
nuclear@0
|
211 };
|
nuclear@0
|
212
|
nuclear@0
|
213
|
nuclear@0
|
214 //-----------------------------------------------------------------------------
|
nuclear@0
|
215 // TCP Berkley socket
|
nuclear@0
|
216
|
nuclear@0
|
217 // Base class for TCP sockets, code shared between platforms
|
nuclear@0
|
218 class TCPSocketBase : public BerkleySocket
|
nuclear@0
|
219 {
|
nuclear@0
|
220 public:
|
nuclear@0
|
221 TCPSocketBase();
|
nuclear@0
|
222 TCPSocketBase(SocketHandle handle);
|
nuclear@0
|
223
|
nuclear@0
|
224 public:
|
nuclear@0
|
225 virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters) = 0;
|
nuclear@0
|
226 virtual int Listen() = 0;
|
nuclear@0
|
227 virtual int Connect(SockAddr* pSockAddr) = 0;
|
nuclear@0
|
228 virtual int Send(const void* pData,
|
nuclear@0
|
229 int bytes) = 0;
|
nuclear@0
|
230 protected:
|
nuclear@0
|
231 virtual void OnRecv(SocketEvent_TCP* eventHandler,
|
nuclear@0
|
232 uint8_t* pData,
|
nuclear@0
|
233 int bytesRead) = 0;
|
nuclear@0
|
234
|
nuclear@0
|
235 protected:
|
nuclear@0
|
236 bool IsListenSocket; // Is the socket listening (acting as a server)?
|
nuclear@0
|
237 };
|
nuclear@0
|
238
|
nuclear@0
|
239
|
nuclear@0
|
240 }} // OVR::Net
|
nuclear@0
|
241
|
nuclear@0
|
242 #endif
|