ovr_sdk

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/LibOVR/Src/Net/OVR_Socket.h	Wed Jan 14 06:51:16 2015 +0200
     1.3 @@ -0,0 +1,242 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +PublicHeader:   n/a
     1.7 +Filename    :   OVR_Socket.h
     1.8 +Content     :   Socket common data shared between all platforms.
     1.9 +Created     :   June 10, 2014
    1.10 +Authors     :   Kevin Jenkins, Chris Taylor
    1.11 +
    1.12 +Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.
    1.13 +
    1.14 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
    1.15 +you may not use the Oculus VR Rift SDK except in compliance with the License, 
    1.16 +which is provided at the time of installation or download, or which 
    1.17 +otherwise accompanies this software in either electronic or hard copy form.
    1.18 +
    1.19 +You may obtain a copy of the License at
    1.20 +
    1.21 +http://www.oculusvr.com/licenses/LICENSE-3.2 
    1.22 +
    1.23 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
    1.24 +distributed under the License is distributed on an "AS IS" BASIS,
    1.25 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.26 +See the License for the specific language governing permissions and
    1.27 +limitations under the License.
    1.28 +
    1.29 +************************************************************************************/
    1.30 +
    1.31 +#ifndef OVR_Socket_h
    1.32 +#define OVR_Socket_h
    1.33 +
    1.34 +#include "../Kernel/OVR_Types.h"
    1.35 +#include "../Kernel/OVR_Timer.h"
    1.36 +#include "../Kernel/OVR_Allocator.h"
    1.37 +#include "../Kernel/OVR_RefCount.h"
    1.38 +#include "../Kernel/OVR_String.h"
    1.39 +
    1.40 +// OS-specific socket headers
    1.41 +#if defined(OVR_OS_WIN32)
    1.42 +#include <WinSock2.h>
    1.43 +#include <WS2tcpip.h>
    1.44 +#define WIN32_LEAN_AND_MEAN
    1.45 +#include <windows.h>
    1.46 +#else
    1.47 +# include <unistd.h>
    1.48 +# include <sys/types.h>
    1.49 +# include <netinet/in.h>
    1.50 +#ifdef OVR_OS_ANDROID
    1.51 +#include <sys/socket.h>
    1.52 +#endif
    1.53 +#endif
    1.54 +
    1.55 +namespace OVR { namespace Net {
    1.56 +
    1.57 +class SockAddr;
    1.58 +class UDPSocket;
    1.59 +class TCPSocket;
    1.60 +
    1.61 +
    1.62 +//-----------------------------------------------------------------------------
    1.63 +// Portable numeric Socket handle
    1.64 +#if defined(OVR_OS_WIN32)
    1.65 +typedef SOCKET SocketHandle;
    1.66 +#else
    1.67 +typedef int SocketHandle;
    1.68 +static const SocketHandle INVALID_SOCKET = -1;
    1.69 +static const int SOCKET_ERROR = -1;
    1.70 +#endif
    1.71 +
    1.72 +
    1.73 +//-----------------------------------------------------------------------------
    1.74 +// Types of network transport
    1.75 +enum TransportType
    1.76 +{
    1.77 +	TransportType_None,          // No transport (useful placeholder for invalid states)
    1.78 +	TransportType_Loopback,      // Loopback transport: Class talks to itself
    1.79 +	TransportType_TCP,           // TCP/IPv4/v6
    1.80 +	TransportType_UDP,           // UDP/IPv4/v6
    1.81 +	TransportType_PacketizedTCP  // Packetized TCP: Message framing is automatic
    1.82 +};
    1.83 +
    1.84 +
    1.85 +//-----------------------------------------------------------------------------
    1.86 +// Abstraction for a network socket. Inheritance hierarchy
    1.87 +// modeled after RakNet so that future support can be added
    1.88 +// for Linux, Windows RT, consoles, etc.
    1.89 +class Socket : public RefCountBase<Socket>
    1.90 +{
    1.91 +public:
    1.92 +	Socket();
    1.93 +	virtual void Close() = 0;
    1.94 +
    1.95 +public:
    1.96 +	TransportType Transport; // Type of transport
    1.97 +};
    1.98 +
    1.99 +
   1.100 +//-----------------------------------------------------------------------------
   1.101 +// Bind parameters for Berkley sockets
   1.102 +struct BerkleyBindParameters
   1.103 +{
   1.104 +public:
   1.105 +	BerkleyBindParameters();
   1.106 +
   1.107 +public:
   1.108 +	uint16_t Port;     // Port
   1.109 +	String Address;
   1.110 +    uint32_t blockingTimeout;
   1.111 +};
   1.112 +
   1.113 +
   1.114 +//-----------------------------------------------------------------------------
   1.115 +// Berkley socket
   1.116 +class BerkleySocket : public Socket
   1.117 +{
   1.118 +public:
   1.119 +	BerkleySocket();
   1.120 +	virtual ~BerkleySocket();
   1.121 +
   1.122 +	virtual void   Close();
   1.123 +	virtual int32_t GetSockname(SockAddr* pSockAddrOut);
   1.124 +	virtual void   SetBlockingTimeout(int timeoutMs) // milliseconds
   1.125 +	{
   1.126 +        TimeoutSec = timeoutMs / 1000;
   1.127 +        TimeoutUsec = (timeoutMs % 1000) * 1000;
   1.128 +	}
   1.129 +    int            GetBlockingTimeoutUsec() const
   1.130 +    {
   1.131 +        return TimeoutUsec;
   1.132 +    }
   1.133 +    int            GetBlockingTimeoutSec() const
   1.134 +    {
   1.135 +        return TimeoutSec;
   1.136 +    }
   1.137 +    SocketHandle   GetSocketHandle() const
   1.138 +    {
   1.139 +        return TheSocket;
   1.140 +    }
   1.141 +
   1.142 +protected:
   1.143 +	SocketHandle TheSocket;           // Socket handle
   1.144 +    int TimeoutUsec, TimeoutSec;
   1.145 +};
   1.146 +
   1.147 +
   1.148 +//-----------------------------------------------------------------------------
   1.149 +// UDP socket events
   1.150 +class SocketEvent_UDP
   1.151 +{
   1.152 +public:
   1.153 +	virtual ~SocketEvent_UDP(){}
   1.154 +
   1.155 +	virtual void UDP_OnRecv(Socket* pSocket, uint8_t* pData,
   1.156 +							uint32_t bytesRead, SockAddr* pSockAddr)
   1.157 +	{
   1.158 +		OVR_UNUSED4(pSocket, pData, bytesRead, pSockAddr);
   1.159 +	}
   1.160 +};
   1.161 +
   1.162 +
   1.163 +//-----------------------------------------------------------------------------
   1.164 +// TCP socket events
   1.165 +class SocketEvent_TCP
   1.166 +{
   1.167 +public:
   1.168 +	virtual ~SocketEvent_TCP(){}
   1.169 +
   1.170 +	virtual void TCP_OnRecv     (Socket* pSocket,
   1.171 +                                 uint8_t* pData,
   1.172 +                                 int bytesRead)
   1.173 +	{
   1.174 +		OVR_UNUSED3(pSocket, pData, bytesRead);
   1.175 +	}
   1.176 +	virtual void TCP_OnClosed   (TCPSocket* pSocket)
   1.177 +	{
   1.178 +		OVR_UNUSED(pSocket);
   1.179 +	}
   1.180 +	virtual void TCP_OnAccept   (TCPSocket* pListener,
   1.181 +                                 SockAddr* pSockAddr,
   1.182 +								 SocketHandle newSock)
   1.183 +	{
   1.184 +		OVR_UNUSED3(pListener, pSockAddr, newSock);
   1.185 +	}
   1.186 +	virtual void TCP_OnConnected(TCPSocket* pSocket)
   1.187 +	{
   1.188 +		OVR_UNUSED(pSocket);
   1.189 +	}
   1.190 +};
   1.191 +
   1.192 +
   1.193 +//-----------------------------------------------------------------------------
   1.194 +// UDP Berkley socket
   1.195 +
   1.196 +// Base class for UDP sockets, code shared between platforms
   1.197 +class UDPSocketBase : public BerkleySocket
   1.198 +{
   1.199 +public:
   1.200 +	UDPSocketBase();
   1.201 +
   1.202 +public:
   1.203 +	virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters) = 0;
   1.204 +	virtual int          Send(const void* pData,
   1.205 +                              int bytes,
   1.206 +                              SockAddr* pSockAddr) = 0;
   1.207 +	virtual void         Poll(SocketEvent_UDP* eventHandler) = 0;
   1.208 +
   1.209 +protected:
   1.210 +	virtual void         OnRecv(SocketEvent_UDP* eventHandler,
   1.211 +                                uint8_t* pData,
   1.212 +								int bytesRead,
   1.213 +                                SockAddr* address) = 0;
   1.214 +};
   1.215 +
   1.216 +
   1.217 +//-----------------------------------------------------------------------------
   1.218 +// TCP Berkley socket
   1.219 +
   1.220 +// Base class for TCP sockets, code shared between platforms
   1.221 +class TCPSocketBase : public BerkleySocket
   1.222 +{
   1.223 +public:
   1.224 +	TCPSocketBase();
   1.225 +	TCPSocketBase(SocketHandle handle);
   1.226 +
   1.227 +public:
   1.228 +	virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters) = 0;
   1.229 +	virtual int          Listen() = 0;
   1.230 +	virtual int          Connect(SockAddr* pSockAddr) = 0;
   1.231 +	virtual int          Send(const void* pData,
   1.232 +                              int bytes) = 0;
   1.233 +protected:
   1.234 +	virtual void         OnRecv(SocketEvent_TCP* eventHandler,
   1.235 +                                uint8_t* pData,
   1.236 +                                int bytesRead) = 0;
   1.237 +
   1.238 +protected:
   1.239 +	bool IsListenSocket; // Is the socket listening (acting as a server)?
   1.240 +};
   1.241 +
   1.242 +
   1.243 +}} // OVR::Net
   1.244 +
   1.245 +#endif