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