ovr_sdk

view 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 source
1 /************************************************************************************
3 PublicHeader: n/a
4 Filename : OVR_Unix_Socket.h
5 Content : Berkley sockets networking implementation
6 Created : July 1, 2014
7 Authors : Kevin Jenkins
9 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
11 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
12 you may not use the Oculus VR Rift SDK except in compliance with the License,
13 which is provided at the time of installation or download, or which
14 otherwise accompanies this software in either electronic or hard copy form.
16 You may obtain a copy of the License at
18 http://www.oculusvr.com/licenses/LICENSE-3.2
20 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
21 distributed under the License is distributed on an "AS IS" BASIS,
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 See the License for the specific language governing permissions and
24 limitations under the License.
26 ************************************************************************************/
28 #ifndef OVR_Unix_Socket_h
29 #define OVR_Unix_Socket_h
31 #include "OVR_Socket.h"
32 #include "OVR_BitStream.h"
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <netdb.h>
39 #include <fcntl.h>
41 namespace OVR { namespace Net {
43 //-----------------------------------------------------------------------------
44 // SockAddr
46 // Abstraction for IPV6 socket address, with various convenience functions
47 class SockAddr
48 {
49 public:
50 SockAddr();
51 SockAddr(SockAddr* sa);
52 SockAddr(sockaddr_storage* sa);
53 SockAddr(sockaddr_in6* sa);
54 SockAddr(const char* hostAddress, UInt16 port, int sockType);
56 public:
57 void Set(const sockaddr_storage* sa);
58 void Set(const sockaddr_in6* sa);
59 void Set(const char* hostAddress, UInt16 port, int sockType); // SOCK_DGRAM or SOCK_STREAM
61 UInt16 GetPort();
63 String ToString(bool writePort, char portDelineator) const;
65 bool IsLocalhost() const;
67 void Serialize(BitStream* bs);
68 bool Deserialize(BitStream);
70 bool operator==( const SockAddr& right ) const;
71 bool operator!=( const SockAddr& right ) const;
72 bool operator >( const SockAddr& right ) const;
73 bool operator <( const SockAddr& right ) const;
75 public:
76 sockaddr_in6 Addr6;
77 };
80 //-----------------------------------------------------------------------------
81 // UDP Socket
83 // Windows version of TCP socket
84 class UDPSocket : public UDPSocketBase
85 {
86 public:
87 UDPSocket();
88 virtual ~UDPSocket();
90 public:
91 virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters);
92 virtual int Send(const void* pData, int bytes, SockAddr* address);
93 virtual void Poll(SocketEvent_UDP* eventHandler);
95 protected:
96 static const int RecvBufSize = 1048576;
97 UByte* RecvBuf;
99 virtual void OnRecv(SocketEvent_UDP* eventHandler, UByte* pData,
100 int bytesRead, SockAddr* address);
101 };
104 //-----------------------------------------------------------------------------
105 // TCP Socket
107 // Windows version of TCP socket
108 class TCPSocket : public TCPSocketBase
109 {
110 friend class TCPSocketPollState;
112 public:
113 TCPSocket();
114 TCPSocket(SocketHandle boundHandle, bool isListenSocket);
115 virtual ~TCPSocket();
117 public:
118 virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters);
119 virtual int Listen();
120 virtual int Connect(SockAddr* address);
121 virtual int Send(const void* pData, int bytes);
123 protected:
124 virtual void OnRecv(SocketEvent_TCP* eventHandler, UByte* pData,
125 int bytesRead);
127 public:
128 bool IsConnecting; // Is in the process of connecting?
129 };
132 //-----------------------------------------------------------------------------
133 // TCPSocketPollState
135 // Polls multiple blocking TCP sockets at once
136 class TCPSocketPollState
137 {
138 fd_set readFD, exceptionFD, writeFD;
139 SocketHandle largestDescriptor;
141 public:
142 TCPSocketPollState();
143 bool IsValid() const;
144 void Add(TCPSocket* tcpSocket);
145 bool Poll(long usec = 30000, long seconds = 0);
146 void HandleEvent(TCPSocket* tcpSocket, SocketEvent_TCP* eventHandler);
147 };
150 }} // OVR::Net
152 #endif