nuclear@0: /************************************************************************************ nuclear@0: nuclear@0: PublicHeader: n/a nuclear@0: Filename : OVR_Socket.h nuclear@0: Content : Socket common data shared between all platforms. nuclear@0: Created : June 10, 2014 nuclear@0: Authors : Kevin Jenkins, Chris Taylor nuclear@0: nuclear@0: Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. nuclear@0: nuclear@0: Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); nuclear@0: you may not use the Oculus VR Rift SDK except in compliance with the License, nuclear@0: which is provided at the time of installation or download, or which nuclear@0: otherwise accompanies this software in either electronic or hard copy form. nuclear@0: nuclear@0: You may obtain a copy of the License at nuclear@0: nuclear@0: http://www.oculusvr.com/licenses/LICENSE-3.2 nuclear@0: nuclear@0: Unless required by applicable law or agreed to in writing, the Oculus VR SDK nuclear@0: distributed under the License is distributed on an "AS IS" BASIS, nuclear@0: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. nuclear@0: See the License for the specific language governing permissions and nuclear@0: limitations under the License. nuclear@0: nuclear@0: ************************************************************************************/ nuclear@0: nuclear@0: #ifndef OVR_Socket_h nuclear@0: #define OVR_Socket_h nuclear@0: nuclear@0: #include "../Kernel/OVR_Types.h" nuclear@0: #include "../Kernel/OVR_Timer.h" nuclear@0: #include "../Kernel/OVR_Allocator.h" nuclear@0: #include "../Kernel/OVR_RefCount.h" nuclear@0: #include "../Kernel/OVR_String.h" nuclear@0: nuclear@0: // OS-specific socket headers nuclear@0: #if defined(OVR_OS_WIN32) nuclear@0: #include nuclear@0: #include nuclear@0: #define WIN32_LEAN_AND_MEAN nuclear@0: #include nuclear@0: #else nuclear@0: # include nuclear@0: # include nuclear@0: # include nuclear@0: #ifdef OVR_OS_ANDROID nuclear@0: #include nuclear@0: #endif nuclear@0: #endif nuclear@0: nuclear@0: namespace OVR { namespace Net { nuclear@0: nuclear@0: class SockAddr; nuclear@0: class UDPSocket; nuclear@0: class TCPSocket; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // Portable numeric Socket handle nuclear@0: #if defined(OVR_OS_WIN32) nuclear@0: typedef SOCKET SocketHandle; nuclear@0: #else nuclear@0: typedef int SocketHandle; nuclear@0: static const SocketHandle INVALID_SOCKET = -1; nuclear@0: static const int SOCKET_ERROR = -1; nuclear@0: #endif nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // Types of network transport nuclear@0: enum TransportType nuclear@0: { nuclear@0: TransportType_None, // No transport (useful placeholder for invalid states) nuclear@0: TransportType_Loopback, // Loopback transport: Class talks to itself nuclear@0: TransportType_TCP, // TCP/IPv4/v6 nuclear@0: TransportType_UDP, // UDP/IPv4/v6 nuclear@0: TransportType_PacketizedTCP // Packetized TCP: Message framing is automatic nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // Abstraction for a network socket. Inheritance hierarchy nuclear@0: // modeled after RakNet so that future support can be added nuclear@0: // for Linux, Windows RT, consoles, etc. nuclear@0: class Socket : public RefCountBase nuclear@0: { nuclear@0: public: nuclear@0: Socket(); nuclear@0: virtual void Close() = 0; nuclear@0: nuclear@0: public: nuclear@0: TransportType Transport; // Type of transport nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // Bind parameters for Berkley sockets nuclear@0: struct BerkleyBindParameters nuclear@0: { nuclear@0: public: nuclear@0: BerkleyBindParameters(); nuclear@0: nuclear@0: public: nuclear@0: uint16_t Port; // Port nuclear@0: String Address; nuclear@0: uint32_t blockingTimeout; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // Berkley socket nuclear@0: class BerkleySocket : public Socket nuclear@0: { nuclear@0: public: nuclear@0: BerkleySocket(); nuclear@0: virtual ~BerkleySocket(); nuclear@0: nuclear@0: virtual void Close(); nuclear@0: virtual int32_t GetSockname(SockAddr* pSockAddrOut); nuclear@0: virtual void SetBlockingTimeout(int timeoutMs) // milliseconds nuclear@0: { nuclear@0: TimeoutSec = timeoutMs / 1000; nuclear@0: TimeoutUsec = (timeoutMs % 1000) * 1000; nuclear@0: } nuclear@0: int GetBlockingTimeoutUsec() const nuclear@0: { nuclear@0: return TimeoutUsec; nuclear@0: } nuclear@0: int GetBlockingTimeoutSec() const nuclear@0: { nuclear@0: return TimeoutSec; nuclear@0: } nuclear@0: SocketHandle GetSocketHandle() const nuclear@0: { nuclear@0: return TheSocket; nuclear@0: } nuclear@0: nuclear@0: protected: nuclear@0: SocketHandle TheSocket; // Socket handle nuclear@0: int TimeoutUsec, TimeoutSec; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // UDP socket events nuclear@0: class SocketEvent_UDP nuclear@0: { nuclear@0: public: nuclear@0: virtual ~SocketEvent_UDP(){} nuclear@0: nuclear@0: virtual void UDP_OnRecv(Socket* pSocket, uint8_t* pData, nuclear@0: uint32_t bytesRead, SockAddr* pSockAddr) nuclear@0: { nuclear@0: OVR_UNUSED4(pSocket, pData, bytesRead, pSockAddr); nuclear@0: } nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // TCP socket events nuclear@0: class SocketEvent_TCP nuclear@0: { nuclear@0: public: nuclear@0: virtual ~SocketEvent_TCP(){} nuclear@0: nuclear@0: virtual void TCP_OnRecv (Socket* pSocket, nuclear@0: uint8_t* pData, nuclear@0: int bytesRead) nuclear@0: { nuclear@0: OVR_UNUSED3(pSocket, pData, bytesRead); nuclear@0: } nuclear@0: virtual void TCP_OnClosed (TCPSocket* pSocket) nuclear@0: { nuclear@0: OVR_UNUSED(pSocket); nuclear@0: } nuclear@0: virtual void TCP_OnAccept (TCPSocket* pListener, nuclear@0: SockAddr* pSockAddr, nuclear@0: SocketHandle newSock) nuclear@0: { nuclear@0: OVR_UNUSED3(pListener, pSockAddr, newSock); nuclear@0: } nuclear@0: virtual void TCP_OnConnected(TCPSocket* pSocket) nuclear@0: { nuclear@0: OVR_UNUSED(pSocket); nuclear@0: } nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // UDP Berkley socket nuclear@0: nuclear@0: // Base class for UDP sockets, code shared between platforms nuclear@0: class UDPSocketBase : public BerkleySocket nuclear@0: { nuclear@0: public: nuclear@0: UDPSocketBase(); nuclear@0: nuclear@0: public: nuclear@0: virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters) = 0; nuclear@0: virtual int Send(const void* pData, nuclear@0: int bytes, nuclear@0: SockAddr* pSockAddr) = 0; nuclear@0: virtual void Poll(SocketEvent_UDP* eventHandler) = 0; nuclear@0: nuclear@0: protected: nuclear@0: virtual void OnRecv(SocketEvent_UDP* eventHandler, nuclear@0: uint8_t* pData, nuclear@0: int bytesRead, nuclear@0: SockAddr* address) = 0; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // TCP Berkley socket nuclear@0: nuclear@0: // Base class for TCP sockets, code shared between platforms nuclear@0: class TCPSocketBase : public BerkleySocket nuclear@0: { nuclear@0: public: nuclear@0: TCPSocketBase(); nuclear@0: TCPSocketBase(SocketHandle handle); nuclear@0: nuclear@0: public: nuclear@0: virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters) = 0; nuclear@0: virtual int Listen() = 0; nuclear@0: virtual int Connect(SockAddr* pSockAddr) = 0; nuclear@0: virtual int Send(const void* pData, nuclear@0: int bytes) = 0; nuclear@0: protected: nuclear@0: virtual void OnRecv(SocketEvent_TCP* eventHandler, nuclear@0: uint8_t* pData, nuclear@0: int bytesRead) = 0; nuclear@0: nuclear@0: protected: nuclear@0: bool IsListenSocket; // Is the socket listening (acting as a server)? nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: }} // OVR::Net nuclear@0: nuclear@0: #endif