nuclear@0: /************************************************************************************ nuclear@0: nuclear@0: PublicHeader: n/a nuclear@0: Filename : OVR_Unix_Socket.h nuclear@0: Content : Berkley sockets networking implementation nuclear@0: Created : July 1, 2014 nuclear@0: Authors : Kevin Jenkins 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_Unix_Socket_h nuclear@0: #define OVR_Unix_Socket_h nuclear@0: nuclear@0: #include "OVR_Socket.h" nuclear@0: #include "OVR_BitStream.h" nuclear@0: nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: nuclear@0: namespace OVR { namespace Net { nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // SockAddr nuclear@0: nuclear@0: // Abstraction for IPV6 socket address, with various convenience functions nuclear@0: class SockAddr nuclear@0: { nuclear@0: public: nuclear@0: SockAddr(); nuclear@0: SockAddr(SockAddr* sa); nuclear@0: SockAddr(sockaddr_storage* sa); nuclear@0: SockAddr(sockaddr_in6* sa); nuclear@0: SockAddr(const char* hostAddress, UInt16 port, int sockType); nuclear@0: nuclear@0: public: nuclear@0: void Set(const sockaddr_storage* sa); nuclear@0: void Set(const sockaddr_in6* sa); nuclear@0: void Set(const char* hostAddress, UInt16 port, int sockType); // SOCK_DGRAM or SOCK_STREAM nuclear@0: nuclear@0: UInt16 GetPort(); nuclear@0: nuclear@0: String ToString(bool writePort, char portDelineator) const; nuclear@0: nuclear@0: bool IsLocalhost() const; nuclear@0: nuclear@0: void Serialize(BitStream* bs); nuclear@0: bool Deserialize(BitStream); nuclear@0: nuclear@0: bool operator==( const SockAddr& right ) const; nuclear@0: bool operator!=( const SockAddr& right ) const; nuclear@0: bool operator >( const SockAddr& right ) const; nuclear@0: bool operator <( const SockAddr& right ) const; nuclear@0: nuclear@0: public: nuclear@0: sockaddr_in6 Addr6; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // UDP Socket nuclear@0: nuclear@0: // Windows version of TCP socket nuclear@0: class UDPSocket : public UDPSocketBase nuclear@0: { nuclear@0: public: nuclear@0: UDPSocket(); nuclear@0: virtual ~UDPSocket(); nuclear@0: nuclear@0: public: nuclear@0: virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters); nuclear@0: virtual int Send(const void* pData, int bytes, SockAddr* address); nuclear@0: virtual void Poll(SocketEvent_UDP* eventHandler); nuclear@0: nuclear@0: protected: nuclear@0: static const int RecvBufSize = 1048576; nuclear@0: UByte* RecvBuf; nuclear@0: nuclear@0: virtual void OnRecv(SocketEvent_UDP* eventHandler, UByte* pData, nuclear@0: int bytesRead, SockAddr* address); nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // TCP Socket nuclear@0: nuclear@0: // Windows version of TCP socket nuclear@0: class TCPSocket : public TCPSocketBase nuclear@0: { nuclear@0: friend class TCPSocketPollState; nuclear@0: nuclear@0: public: nuclear@0: TCPSocket(); nuclear@0: TCPSocket(SocketHandle boundHandle, bool isListenSocket); nuclear@0: virtual ~TCPSocket(); nuclear@0: nuclear@0: public: nuclear@0: virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters); nuclear@0: virtual int Listen(); nuclear@0: virtual int Connect(SockAddr* address); nuclear@0: virtual int Send(const void* pData, int bytes); nuclear@0: nuclear@0: protected: nuclear@0: virtual void OnRecv(SocketEvent_TCP* eventHandler, UByte* pData, nuclear@0: int bytesRead); nuclear@0: nuclear@0: public: nuclear@0: bool IsConnecting; // Is in the process of connecting? nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // TCPSocketPollState nuclear@0: nuclear@0: // Polls multiple blocking TCP sockets at once nuclear@0: class TCPSocketPollState nuclear@0: { nuclear@0: fd_set readFD, exceptionFD, writeFD; nuclear@0: SocketHandle largestDescriptor; nuclear@0: nuclear@0: public: nuclear@0: TCPSocketPollState(); nuclear@0: bool IsValid() const; nuclear@0: void Add(TCPSocket* tcpSocket); nuclear@0: bool Poll(long usec = 30000, long seconds = 0); nuclear@0: void HandleEvent(TCPSocket* tcpSocket, SocketEvent_TCP* eventHandler); nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: }} // OVR::Net nuclear@0: nuclear@0: #endif