ovr_sdk

view LibOVR/Src/Net/OVR_Socket.h @ 0:1b39a1b46319

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