aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/Net/OVR_Socket.h
blob: c1ea75c99040b62d8784586868609610c8c140f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/************************************************************************************

PublicHeader:   n/a
Filename    :   OVR_Socket.h
Content     :   Socket common data shared between all platforms.
Created     :   June 10, 2014
Authors     :   Kevin Jenkins, Chris Taylor

Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
you may not use the Oculus VR Rift SDK except in compliance with the License, 
which is provided at the time of installation or download, or which 
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.2 

Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#ifndef OVR_Socket_h
#define OVR_Socket_h

#include "Kernel/OVR_Types.h"
#include "Kernel/OVR_Timer.h"
#include "Kernel/OVR_Allocator.h"
#include "Kernel/OVR_RefCount.h"
#include "Kernel/OVR_String.h"

// OS-specific socket headers
#if defined(OVR_OS_WIN32)
#if defined(_WINSOCKAPI_) && !defined(OVR_CC_MSVC) && !defined(_WINSOCK2API_)
/** Silent warning using mingw64, _WINSOCKAPI_ is disabled in ./LibOVRKernel/Src/Kernel/OVR_Win32_IncludeWindows.h */
#undef _WINSOCKAPI_
#endif /* defined(_WINSOCKAPI_) && !defined(OVR_CC_MSVC) && !defined(_WINSOCK2API_) */
#include <WinSock2.h>
#include <WS2tcpip.h>
#include "Kernel/OVR_Win32_IncludeWindows.h"
#else
# include <unistd.h>
# include <sys/types.h>
# include <netinet/in.h>
#ifdef OVR_OS_ANDROID
#include <sys/socket.h>
#endif
#endif

namespace OVR { namespace Net {

class SockAddr;
class UDPSocket;
class TCPSocket;


//-----------------------------------------------------------------------------
// Portable numeric Socket handle
#if defined(OVR_OS_WIN32)
typedef SOCKET SocketHandle;
#else
typedef int SocketHandle;
static const SocketHandle INVALID_SOCKET = -1;
static const int SOCKET_ERROR = -1;
#endif


//-----------------------------------------------------------------------------
// Types of network transport
enum TransportType
{
	TransportType_None,          // No transport (useful placeholder for invalid states)
	TransportType_TCP,           // TCP/IPv4/v6
	TransportType_UDP,           // UDP/IPv4/v6
	TransportType_PacketizedTCP  // Packetized TCP: Message framing is automatic
};


//-----------------------------------------------------------------------------
// Abstraction for a network socket. Inheritance hierarchy
// modeled after RakNet so that future support can be added
// for Linux, Windows RT, consoles, etc.
class Socket : public RefCountBase<Socket>
{
public:
	Socket();
	virtual void Close() = 0;

public:
	TransportType Transport; // Type of transport
};


//-----------------------------------------------------------------------------
// Bind parameters for Berkley sockets
struct BerkleyBindParameters
{
public:
	BerkleyBindParameters();

public:
	uint16_t Port;     // Port
	String Address;
    uint32_t blockingTimeout;
};


//-----------------------------------------------------------------------------
// Berkley socket
class BerkleySocket : public Socket
{
public:
	BerkleySocket();
	virtual ~BerkleySocket();

	virtual void   Close();
	virtual int32_t GetSockname(SockAddr* pSockAddrOut);
	virtual void   SetBlockingTimeout(int timeoutMs) // milliseconds
	{
        TimeoutSec = timeoutMs / 1000;
        TimeoutUsec = (timeoutMs % 1000) * 1000;
	}
    int            GetBlockingTimeoutUsec() const
    {
        return TimeoutUsec;
    }
    int            GetBlockingTimeoutSec() const
    {
        return TimeoutSec;
    }
    SocketHandle   GetSocketHandle() const
    {
        return TheSocket;
    }

protected:
	SocketHandle TheSocket;           // Socket handle
    int TimeoutUsec, TimeoutSec;
};


//-----------------------------------------------------------------------------
// UDP socket events
class SocketEvent_UDP
{
public:
	virtual ~SocketEvent_UDP(){}

	virtual void UDP_OnRecv(Socket* pSocket, uint8_t* pData,
							uint32_t bytesRead, SockAddr* pSockAddr)
	{
		OVR_UNUSED4(pSocket, pData, bytesRead, pSockAddr);
	}
};


//-----------------------------------------------------------------------------
// TCP socket events
class SocketEvent_TCP
{
public:
	virtual ~SocketEvent_TCP(){}

	virtual void TCP_OnRecv     (Socket* pSocket,
                                 uint8_t* pData,
                                 int bytesRead)
	{
		OVR_UNUSED3(pSocket, pData, bytesRead);
	}
	virtual void TCP_OnClosed   (TCPSocket* pSocket)
	{
		OVR_UNUSED(pSocket);
	}
	virtual void TCP_OnAccept   (TCPSocket* pListener,
                                 SockAddr* pSockAddr,
								 SocketHandle newSock)
	{
		OVR_UNUSED3(pListener, pSockAddr, newSock);
	}
	virtual void TCP_OnConnected(TCPSocket* pSocket)
	{
		OVR_UNUSED(pSocket);
	}
};


//-----------------------------------------------------------------------------
// UDP Berkley socket

// Base class for UDP sockets, code shared between platforms
class UDPSocketBase : public BerkleySocket
{
public:
	UDPSocketBase();

public:
	virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters) = 0;
	virtual int          Send(const void* pData,
                              int bytes,
                              SockAddr* pSockAddr) = 0;
	virtual void         Poll(SocketEvent_UDP* eventHandler) = 0;

protected:
	virtual void         OnRecv(SocketEvent_UDP* eventHandler,
                                uint8_t* pData,
								int bytesRead,
                                SockAddr* address) = 0;
};


//-----------------------------------------------------------------------------
// TCP Berkley socket

// Base class for TCP sockets, code shared between platforms
class TCPSocketBase : public BerkleySocket
{
public:
	TCPSocketBase();
	TCPSocketBase(SocketHandle handle);

public:
	virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters) = 0;
	virtual int          Listen() = 0;
	virtual int          Connect(SockAddr* pSockAddr) = 0;
	virtual int          Send(const void* pData,
                              int bytes) = 0;
protected:
	virtual void         OnRecv(SocketEvent_TCP* eventHandler,
                                uint8_t* pData,
                                int bytesRead) = 0;

protected:
	bool IsListenSocket; // Is the socket listening (acting as a server)?
};


}} // OVR::Net

#endif