ovr_sdk

diff LibOVR/Src/Net/OVR_Unix_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_Unix_Socket.h	Wed Jan 14 06:51:16 2015 +0200
     1.3 @@ -0,0 +1,152 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +PublicHeader:   n/a
     1.7 +Filename    :   OVR_Unix_Socket.h
     1.8 +Content     :   Berkley sockets networking implementation
     1.9 +Created     :   July 1, 2014
    1.10 +Authors     :   Kevin Jenkins
    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_Unix_Socket_h
    1.32 +#define OVR_Unix_Socket_h
    1.33 +
    1.34 +#include "OVR_Socket.h"
    1.35 +#include "OVR_BitStream.h"
    1.36 +
    1.37 +#include <sys/types.h>
    1.38 +#include <sys/socket.h>
    1.39 +#include <netinet/in.h>
    1.40 +#include <arpa/inet.h>
    1.41 +#include <netdb.h>
    1.42 +#include <fcntl.h>
    1.43 +
    1.44 +namespace OVR { namespace Net { 
    1.45 +
    1.46 +//-----------------------------------------------------------------------------
    1.47 +// SockAddr
    1.48 +
    1.49 +// Abstraction for IPV6 socket address, with various convenience functions
    1.50 +class SockAddr
    1.51 +{
    1.52 +public:
    1.53 +	SockAddr();
    1.54 +	SockAddr(SockAddr* sa);
    1.55 +	SockAddr(sockaddr_storage* sa);
    1.56 +	SockAddr(sockaddr_in6* sa);
    1.57 +	SockAddr(const char* hostAddress, UInt16 port, int sockType);
    1.58 +
    1.59 +public:
    1.60 +	void   Set(const sockaddr_storage* sa);
    1.61 +	void   Set(const sockaddr_in6* sa);
    1.62 +	void   Set(const char* hostAddress, UInt16 port, int sockType); // SOCK_DGRAM or SOCK_STREAM
    1.63 +
    1.64 +	UInt16 GetPort();
    1.65 +
    1.66 +    String ToString(bool writePort, char portDelineator) const;
    1.67 +
    1.68 +    bool   IsLocalhost() const;
    1.69 +
    1.70 +	void   Serialize(BitStream* bs);
    1.71 +	bool   Deserialize(BitStream);
    1.72 +
    1.73 +	bool   operator==( const SockAddr& right ) const;
    1.74 +	bool   operator!=( const SockAddr& right ) const;
    1.75 +	bool   operator >( const SockAddr& right ) const;
    1.76 +	bool   operator <( const SockAddr& right ) const;
    1.77 +
    1.78 +public:
    1.79 +	sockaddr_in6 Addr6;
    1.80 +};
    1.81 +
    1.82 +
    1.83 +//-----------------------------------------------------------------------------
    1.84 +// UDP Socket
    1.85 +
    1.86 +// Windows version of TCP socket
    1.87 +class UDPSocket : public UDPSocketBase
    1.88 +{
    1.89 +public:
    1.90 +	UDPSocket();
    1.91 +	virtual ~UDPSocket();
    1.92 +
    1.93 +public:
    1.94 +	virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters);
    1.95 +	virtual int          Send(const void* pData, int bytes, SockAddr* address);
    1.96 +	virtual void         Poll(SocketEvent_UDP* eventHandler);
    1.97 +
    1.98 +protected:
    1.99 +	static const int RecvBufSize = 1048576;
   1.100 +	UByte* RecvBuf;
   1.101 +
   1.102 +	virtual void         OnRecv(SocketEvent_UDP* eventHandler, UByte* pData,
   1.103 +								int bytesRead, SockAddr* address);
   1.104 +};
   1.105 +
   1.106 +
   1.107 +//-----------------------------------------------------------------------------
   1.108 +// TCP Socket
   1.109 +
   1.110 +// Windows version of TCP socket
   1.111 +class TCPSocket : public TCPSocketBase
   1.112 +{
   1.113 +    friend class TCPSocketPollState;
   1.114 +
   1.115 +public:
   1.116 +	TCPSocket();
   1.117 +	TCPSocket(SocketHandle boundHandle, bool isListenSocket);
   1.118 +	virtual ~TCPSocket();
   1.119 +
   1.120 +public:
   1.121 +	virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters);
   1.122 +	virtual int          Listen();
   1.123 +	virtual int          Connect(SockAddr* address);
   1.124 +	virtual int          Send(const void* pData, int bytes);
   1.125 +
   1.126 +protected:
   1.127 +	virtual void         OnRecv(SocketEvent_TCP* eventHandler, UByte* pData,
   1.128 +								int bytesRead);
   1.129 +
   1.130 +public:
   1.131 +	bool IsConnecting; // Is in the process of connecting?
   1.132 +};
   1.133 +
   1.134 +
   1.135 +//-----------------------------------------------------------------------------
   1.136 +// TCPSocketPollState
   1.137 +
   1.138 +// Polls multiple blocking TCP sockets at once
   1.139 +class TCPSocketPollState
   1.140 +{
   1.141 +    fd_set readFD, exceptionFD, writeFD;
   1.142 +    SocketHandle largestDescriptor;
   1.143 +
   1.144 +public:
   1.145 +    TCPSocketPollState();
   1.146 +    bool IsValid() const;
   1.147 +    void Add(TCPSocket* tcpSocket);
   1.148 +    bool Poll(long usec = 30000, long seconds = 0);
   1.149 +    void HandleEvent(TCPSocket* tcpSocket, SocketEvent_TCP* eventHandler);
   1.150 +};
   1.151 +
   1.152 +
   1.153 +}} // OVR::Net
   1.154 +
   1.155 +#endif