ovr_sdk
diff LibOVR/Src/Net/OVR_Unix_Socket.cpp @ 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.cpp Wed Jan 14 06:51:16 2015 +0200 1.3 @@ -0,0 +1,600 @@ 1.4 +/************************************************************************************ 1.5 + 1.6 +Filename : OVR_Unix_Socket.cpp 1.7 +Content : Berkley sockets networking implementation 1.8 +Created : July 1, 2014 1.9 +Authors : Kevin Jenkins 1.10 + 1.11 +Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. 1.12 + 1.13 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 1.14 +you may not use the Oculus VR Rift SDK except in compliance with the License, 1.15 +which is provided at the time of installation or download, or which 1.16 +otherwise accompanies this software in either electronic or hard copy form. 1.17 + 1.18 +You may obtain a copy of the License at 1.19 + 1.20 +http://www.oculusvr.com/licenses/LICENSE-3.2 1.21 + 1.22 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 1.23 +distributed under the License is distributed on an "AS IS" BASIS, 1.24 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.25 +See the License for the specific language governing permissions and 1.26 +limitations under the License. 1.27 + 1.28 +************************************************************************************/ 1.29 + 1.30 +#include "OVR_Unix_Socket.h" 1.31 +#include "../Kernel/OVR_Std.h" 1.32 +#include "../Kernel/OVR_Allocator.h" 1.33 +#include "../Kernel/OVR_Threads.h" // Thread::MSleep 1.34 +#include "../Kernel/OVR_Log.h" 1.35 + 1.36 +#include <errno.h> 1.37 + 1.38 +namespace OVR { namespace Net { 1.39 + 1.40 +//----------------------------------------------------------------------------- 1.41 +// BerkleySocket 1.42 + 1.43 +void BerkleySocket::Close() 1.44 +{ 1.45 + if (TheSocket != INVALID_SOCKET) 1.46 + { 1.47 + close(TheSocket); 1.48 + TheSocket = INVALID_SOCKET; 1.49 + } 1.50 +} 1.51 + 1.52 +SInt32 BerkleySocket::GetSockname(SockAddr *pSockAddrOut) 1.53 +{ 1.54 + struct sockaddr_in6 sa; 1.55 + memset(&sa,0,sizeof(sa)); 1.56 + socklen_t size = sizeof(sa); 1.57 + SInt32 i = getsockname(TheSocket, (sockaddr*)&sa, &size); 1.58 + if (i>=0) 1.59 + { 1.60 + pSockAddrOut->Set(&sa); 1.61 + } 1.62 + return i; 1.63 +} 1.64 + 1.65 + 1.66 +//----------------------------------------------------------------------------- 1.67 +// BitStream overloads for SockAddr 1.68 + 1.69 +BitStream& operator<<(BitStream& out, SockAddr& in) 1.70 +{ 1.71 + out.WriteBits((const unsigned char*) &in.Addr6, sizeof(in.Addr6)*8, true); 1.72 + return out; 1.73 +} 1.74 + 1.75 +BitStream& operator>>(BitStream& in, SockAddr& out) 1.76 +{ 1.77 + bool success = in.ReadBits((unsigned char*) &out.Addr6, sizeof(out.Addr6)*8, true); 1.78 + OVR_ASSERT(success); 1.79 + OVR_UNUSED(success); 1.80 + return in; 1.81 +} 1.82 + 1.83 + 1.84 +//----------------------------------------------------------------------------- 1.85 +// SockAddr 1.86 + 1.87 +SockAddr::SockAddr() 1.88 +{ 1.89 +} 1.90 + 1.91 +SockAddr::SockAddr(SockAddr* address) 1.92 +{ 1.93 + Set(&address->Addr6); 1.94 +} 1.95 + 1.96 +SockAddr::SockAddr(sockaddr_storage* storage) 1.97 +{ 1.98 + Set(storage); 1.99 +} 1.100 + 1.101 +SockAddr::SockAddr(sockaddr_in6* address) 1.102 +{ 1.103 + Set(address); 1.104 +} 1.105 + 1.106 +SockAddr::SockAddr(const char* hostAddress, UInt16 port, int sockType) 1.107 +{ 1.108 + Set(hostAddress, port, sockType); 1.109 +} 1.110 + 1.111 +void SockAddr::Set(const sockaddr_storage* storage) 1.112 +{ 1.113 + memcpy(&Addr6, storage, sizeof(Addr6)); 1.114 +} 1.115 + 1.116 +void SockAddr::Set(const sockaddr_in6* address) 1.117 +{ 1.118 + memcpy(&Addr6, address, sizeof(Addr6)); 1.119 +} 1.120 + 1.121 +void SockAddr::Set(const char* hostAddress, UInt16 port, int sockType) 1.122 +{ 1.123 + memset(&Addr6, 0, sizeof(Addr6)); 1.124 + 1.125 + struct addrinfo hints; 1.126 + 1.127 + // make sure the struct is empty 1.128 + memset(&hints, 0, sizeof (addrinfo)); 1.129 + 1.130 + hints.ai_socktype = sockType; // SOCK_DGRAM or SOCK_STREAM 1.131 + hints.ai_flags = AI_PASSIVE; // fill in my IP for me 1.132 + hints.ai_family = AF_UNSPEC ; 1.133 + 1.134 + if (SOCK_DGRAM == sockType) 1.135 + { 1.136 + hints.ai_protocol = IPPROTO_UDP; 1.137 + } 1.138 + else if (SOCK_STREAM == sockType) 1.139 + { 1.140 + hints.ai_protocol = IPPROTO_TCP; 1.141 + } 1.142 + 1.143 + struct addrinfo* servinfo = NULL; // will point to the results 1.144 + 1.145 + char portStr[32]; 1.146 + OVR_itoa(port, portStr, sizeof(portStr), 10); 1.147 + int errcode = getaddrinfo(hostAddress, portStr, &hints, &servinfo); 1.148 + 1.149 + if (0 != errcode) 1.150 + { 1.151 + OVR::LogError("getaddrinfo error: %s", gai_strerror(errcode)); 1.152 + } 1.153 + 1.154 + OVR_ASSERT(servinfo); 1.155 + 1.156 + if (servinfo) 1.157 + { 1.158 + memcpy(&Addr6, servinfo->ai_addr, sizeof(Addr6)); 1.159 + 1.160 + freeaddrinfo(servinfo); 1.161 + } 1.162 +} 1.163 + 1.164 +UInt16 SockAddr::GetPort() 1.165 +{ 1.166 + return htons(Addr6.sin6_port); 1.167 +} 1.168 + 1.169 +String SockAddr::ToString(bool writePort, char portDelineator) const 1.170 +{ 1.171 + char dest[INET6_ADDRSTRLEN + 1]; 1.172 + 1.173 + int ret = getnameinfo((struct sockaddr*)&Addr6, 1.174 + sizeof(struct sockaddr_in6), 1.175 + dest, 1.176 + INET6_ADDRSTRLEN, 1.177 + NULL, 1.178 + 0, 1.179 + NI_NUMERICHOST); 1.180 + if (ret != 0) 1.181 + { 1.182 + dest[0] = '\0'; 1.183 + } 1.184 + 1.185 + if (writePort) 1.186 + { 1.187 + unsigned char ch[2]; 1.188 + ch[0]=portDelineator; 1.189 + ch[1]=0; 1.190 + OVR_strcat(dest, 16, (const char*) ch); 1.191 + OVR_itoa(ntohs(Addr6.sin6_port), dest+strlen(dest), 16, 10); 1.192 + } 1.193 + 1.194 + return String(dest); 1.195 +} 1.196 +bool SockAddr::IsLocalhost() const 1.197 +{ 1.198 + static const unsigned char localhost_bytes[] = 1.199 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 1.200 + 1.201 + return memcmp(Addr6.sin6_addr.s6_addr, localhost_bytes, 16) == 0; 1.202 +} 1.203 +bool SockAddr::operator==( const SockAddr& right ) const 1.204 +{ 1.205 + return memcmp(&Addr6, &right.Addr6, sizeof(Addr6)) == 0; 1.206 +} 1.207 + 1.208 +bool SockAddr::operator!=( const SockAddr& right ) const 1.209 +{ 1.210 + return !(*this == right); 1.211 +} 1.212 + 1.213 +bool SockAddr::operator>( const SockAddr& right ) const 1.214 +{ 1.215 + return memcmp(&Addr6, &right.Addr6, sizeof(Addr6)) > 0; 1.216 +} 1.217 + 1.218 +bool SockAddr::operator<( const SockAddr& right ) const 1.219 +{ 1.220 + return memcmp(&Addr6, &right.Addr6, sizeof(Addr6)) < 0; 1.221 +} 1.222 + 1.223 + 1.224 +// Returns true on success 1.225 +static bool SetSocketOptions(SocketHandle sock) 1.226 +{ 1.227 + bool failed = false; 1.228 + int sock_opt; 1.229 + int sockError = 0; 1.230 + 1.231 + // This doubles the max throughput rate 1.232 + sock_opt=1024*256; 1.233 + sockError = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); 1.234 + if (sockError != 0) 1.235 + { 1.236 + int errsv = errno; 1.237 + OVR::LogError("[Socket] Failed SO_RCVBUF setsockopt, errno: %d", errsv); 1.238 + failed = true; 1.239 + } 1.240 + 1.241 + // This doesn't make much difference: 10% maybe 1.242 + // Not supported on console 2 1.243 + sock_opt=1024*16; 1.244 + sockError = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); 1.245 + if (sockError != 0) 1.246 + { 1.247 + int errsv = errno; 1.248 + OVR::LogError("[Socket] Failed SO_SNDBUF setsockopt, errno: %d", errsv); 1.249 + failed = true; 1.250 + } 1.251 + 1.252 + // NOTE: This should be OVR_OS_BSD, not Mac. 1.253 +#ifdef OVR_OS_MAC 1.254 + int value = 1; 1.255 + sockError = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value)); 1.256 + if (sockError != 0) 1.257 + { 1.258 + int errsv = errno; 1.259 + OVR::LogError("[Socket] Failed SO_NOSIGPIPE setsockopt, errno: %d", errsv); 1.260 + failed = true; 1.261 + } 1.262 +#endif 1.263 + 1.264 + // Reuse address is only needed for posix platforms, as it is the default 1.265 + // on Windows platforms. 1.266 + int optval = 1; 1.267 + sockError = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int)); 1.268 + if (sockError != 0) 1.269 + { 1.270 + int errsv = errno; 1.271 + OVR::LogError("[Socket] Failed SO_REUSEADDR setsockopt, errno: %d", errsv); 1.272 + failed = true; 1.273 + } 1.274 + 1.275 + return !failed; 1.276 +} 1.277 + 1.278 +void _Ioctlsocket(SocketHandle sock, unsigned long nonblocking) 1.279 +{ 1.280 + int flags = fcntl(sock, F_GETFL, 0); 1.281 + if (flags < 0) return; // return false 1.282 + if (nonblocking == 0) { flags &= ~O_NONBLOCK; } 1.283 + else { flags |= O_NONBLOCK; } 1.284 + fcntl(sock, F_SETFL, flags); 1.285 +} 1.286 + 1.287 +static SocketHandle BindShared(int ai_family, int ai_socktype, BerkleyBindParameters *pBindParameters) 1.288 +{ 1.289 + SocketHandle sock; 1.290 + 1.291 + struct addrinfo hints; 1.292 + memset(&hints, 0, sizeof (addrinfo)); // make sure the struct is empty 1.293 + hints.ai_family = ai_family; 1.294 + hints.ai_socktype = ai_socktype; 1.295 + hints.ai_flags = AI_PASSIVE; // fill in my IP for me 1.296 + struct addrinfo *servinfo=0, *aip; // will point to the results 1.297 + char portStr[32]; 1.298 + OVR_itoa(pBindParameters->Port, portStr, sizeof(portStr), 10); 1.299 + 1.300 + int errcode = 0; 1.301 + if (!pBindParameters->Address.IsEmpty()) 1.302 + errcode = getaddrinfo(pBindParameters->Address.ToCStr(), portStr, &hints, &servinfo); 1.303 + else 1.304 + errcode = getaddrinfo(0, portStr, &hints, &servinfo); 1.305 + 1.306 + if (0 != errcode) 1.307 + { 1.308 + OVR::LogError("getaddrinfo error: %s", gai_strerror(errcode)); 1.309 + } 1.310 + 1.311 + for (aip = servinfo; aip != NULL; aip = aip->ai_next) 1.312 + { 1.313 + // Open socket. The address type depends on what 1.314 + // getaddrinfo() gave us. 1.315 + sock = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol); 1.316 + if (sock != 0) 1.317 + { 1.318 + SetSocketOptions(sock); 1.319 + int ret = bind( sock, aip->ai_addr, (int) aip->ai_addrlen ); 1.320 + if (ret>=0) 1.321 + { 1.322 + // The actual socket is always non-blocking 1.323 + // I control blocking or not using WSAEventSelect 1.324 + _Ioctlsocket(sock, 1); 1.325 + freeaddrinfo(servinfo); 1.326 + return sock; 1.327 + } 1.328 + else 1.329 + { 1.330 + close(sock); 1.331 + } 1.332 + } 1.333 + } 1.334 + 1.335 + if (servinfo) { freeaddrinfo(servinfo); } 1.336 + return INVALID_SOCKET; 1.337 +} 1.338 + 1.339 + 1.340 +//----------------------------------------------------------------------------- 1.341 +// UDPSocket 1.342 + 1.343 +UDPSocket::UDPSocket() 1.344 +{ 1.345 + RecvBuf = new UByte[RecvBufSize]; 1.346 +} 1.347 + 1.348 +UDPSocket::~UDPSocket() 1.349 +{ 1.350 + delete[] RecvBuf; 1.351 +} 1.352 + 1.353 +SocketHandle UDPSocket::Bind(BerkleyBindParameters *pBindParameters) 1.354 +{ 1.355 + SocketHandle s = BindShared(AF_INET6, SOCK_DGRAM, pBindParameters); 1.356 + if (s < 0) 1.357 + return s; 1.358 + 1.359 + Close(); 1.360 + TheSocket = s; 1.361 + 1.362 + return TheSocket; 1.363 +} 1.364 + 1.365 +void UDPSocket::OnRecv(SocketEvent_UDP* eventHandler, UByte* pData, int bytesRead, SockAddr* address) 1.366 +{ 1.367 + eventHandler->UDP_OnRecv(this, pData, bytesRead, address); 1.368 +} 1.369 + 1.370 +int UDPSocket::Send(const void* pData, int bytes, SockAddr* address) 1.371 +{ 1.372 + // NOTE: This should be OVR_OS_BSD 1.373 +#ifdef OVR_OS_MAC 1.374 + int flags = 0; 1.375 +#else 1.376 + int flags = MSG_NOSIGNAL; 1.377 +#endif 1.378 + 1.379 + return (int)sendto(TheSocket, (const char*)pData, bytes, flags, (const sockaddr*)&address->Addr6, sizeof(address->Addr6)); 1.380 +} 1.381 + 1.382 +void UDPSocket::Poll(SocketEvent_UDP *eventHandler) 1.383 +{ 1.384 + struct sockaddr_storage win32_addr; 1.385 + socklen_t fromlen; 1.386 + int bytesRead; 1.387 + 1.388 + // FIXME: Implement blocking poll wait for UDP 1.389 + 1.390 + // While some bytes are read, 1.391 + while (fromlen = sizeof(win32_addr), // Must set fromlen each time 1.392 + bytesRead = (int)recvfrom(TheSocket, (char*)RecvBuf, RecvBufSize, 0, (sockaddr*)&win32_addr, &fromlen), 1.393 + bytesRead > 0) 1.394 + { 1.395 + SockAddr address(&win32_addr); // Wrap address 1.396 + 1.397 + OnRecv(eventHandler, RecvBuf, bytesRead, &address); 1.398 + } 1.399 +} 1.400 + 1.401 + 1.402 +//----------------------------------------------------------------------------- 1.403 +// TCPSocket 1.404 + 1.405 +TCPSocket::TCPSocket() 1.406 +{ 1.407 + IsConnecting = false; 1.408 + IsListenSocket = false; 1.409 +} 1.410 +TCPSocket::TCPSocket(SocketHandle boundHandle, bool isListenSocket) 1.411 +{ 1.412 + TheSocket = boundHandle; 1.413 + IsListenSocket = isListenSocket; 1.414 + IsConnecting = false; 1.415 + SetSocketOptions(TheSocket); 1.416 + 1.417 + // The actual socket is always non-blocking 1.418 + _Ioctlsocket(TheSocket, 1); 1.419 +} 1.420 + 1.421 +TCPSocket::~TCPSocket() 1.422 +{ 1.423 +} 1.424 + 1.425 +void TCPSocket::OnRecv(SocketEvent_TCP* eventHandler, UByte* pData, int bytesRead) 1.426 +{ 1.427 + eventHandler->TCP_OnRecv(this, pData, bytesRead); 1.428 +} 1.429 + 1.430 +SocketHandle TCPSocket::Bind(BerkleyBindParameters* pBindParameters) 1.431 +{ 1.432 + SocketHandle s = BindShared(AF_INET6, SOCK_STREAM, pBindParameters); 1.433 + if (s < 0) 1.434 + return s; 1.435 + 1.436 + Close(); 1.437 + 1.438 + SetBlockingTimeout(pBindParameters->blockingTimeout); 1.439 + TheSocket = s; 1.440 + 1.441 + return TheSocket; 1.442 +} 1.443 + 1.444 +int TCPSocket::Listen() 1.445 +{ 1.446 + if (IsListenSocket) 1.447 + { 1.448 + return 0; 1.449 + } 1.450 + 1.451 + int i = listen(TheSocket, SOMAXCONN); 1.452 + if (i >= 0) 1.453 + { 1.454 + IsListenSocket = true; 1.455 + } 1.456 + 1.457 + return i; 1.458 +} 1.459 + 1.460 +int TCPSocket::Connect(SockAddr* address) 1.461 +{ 1.462 + int retval; 1.463 + 1.464 + retval = connect(TheSocket, (struct sockaddr *) &address->Addr6, sizeof(address->Addr6)); 1.465 + if (retval < 0) 1.466 + { 1.467 + int errsv = errno; 1.468 + // EINPROGRESS should not be checked on windows but should 1.469 + // be checked on POSIX platforms. 1.470 + if (errsv == EWOULDBLOCK || errsv == EINPROGRESS) 1.471 + { 1.472 + IsConnecting = true; 1.473 + return 0; 1.474 + } 1.475 + 1.476 + OVR::LogText( "TCPSocket::Connect failed:Error code - %d\n", errsv ); 1.477 + } 1.478 + 1.479 + return retval; 1.480 +} 1.481 + 1.482 +int TCPSocket::Send(const void* pData, int bytes) 1.483 +{ 1.484 + if (bytes <= 0) 1.485 + { 1.486 + return 0; 1.487 + } 1.488 + else 1.489 + { 1.490 + return (int)send(TheSocket, (const char*)pData, bytes, 0); 1.491 + } 1.492 +} 1.493 + 1.494 + 1.495 +//// TCPSocketPollState 1.496 + 1.497 +TCPSocketPollState::TCPSocketPollState() 1.498 +{ 1.499 + FD_ZERO(&readFD); 1.500 + FD_ZERO(&exceptionFD); 1.501 + FD_ZERO(&writeFD); 1.502 + largestDescriptor = INVALID_SOCKET; 1.503 +} 1.504 + 1.505 +bool TCPSocketPollState::IsValid() const 1.506 +{ 1.507 + return largestDescriptor != INVALID_SOCKET; 1.508 +} 1.509 + 1.510 +void TCPSocketPollState::Add(TCPSocket* tcpSocket) 1.511 +{ 1.512 + if (!tcpSocket) 1.513 + { 1.514 + return; 1.515 + } 1.516 + 1.517 + SocketHandle handle = tcpSocket->GetSocketHandle(); 1.518 + 1.519 + if (handle == INVALID_SOCKET) 1.520 + { 1.521 + return; 1.522 + } 1.523 + 1.524 + if (largestDescriptor == INVALID_SOCKET || 1.525 + largestDescriptor < handle) 1.526 + { 1.527 + largestDescriptor = handle; 1.528 + } 1.529 + 1.530 + FD_SET(handle, &readFD); 1.531 + FD_SET(handle, &exceptionFD); 1.532 + 1.533 + if (tcpSocket->IsConnecting) 1.534 + { 1.535 + FD_SET(handle, &writeFD); 1.536 + } 1.537 +} 1.538 + 1.539 +bool TCPSocketPollState::Poll(long usec, long seconds) 1.540 +{ 1.541 + timeval tv; 1.542 + tv.tv_sec = seconds; 1.543 + tv.tv_usec = (int)usec; 1.544 + 1.545 + return select(largestDescriptor + 1, &readFD, &writeFD, &exceptionFD, &tv) > 0; 1.546 +} 1.547 + 1.548 +void TCPSocketPollState::HandleEvent(TCPSocket* tcpSocket, SocketEvent_TCP* eventHandler) 1.549 +{ 1.550 + if (!tcpSocket || !eventHandler) 1.551 + { 1.552 + return; 1.553 + } 1.554 + 1.555 + SocketHandle handle = tcpSocket->GetSocketHandle(); 1.556 + 1.557 + if (tcpSocket->IsConnecting && FD_ISSET(handle, &writeFD)) 1.558 + { 1.559 + tcpSocket->IsConnecting = false; 1.560 + eventHandler->TCP_OnConnected(tcpSocket); 1.561 + } 1.562 + 1.563 + if (FD_ISSET(handle, &readFD)) 1.564 + { 1.565 + if (!tcpSocket->IsListenSocket) 1.566 + { 1.567 + static const int BUFF_SIZE = 8096; 1.568 + char data[BUFF_SIZE]; 1.569 + 1.570 + int bytesRead = (int)recv(handle, data, BUFF_SIZE, 0); 1.571 + if (bytesRead > 0) 1.572 + { 1.573 + tcpSocket->OnRecv(eventHandler, (UByte*)data, bytesRead); 1.574 + } 1.575 + else // Disconnection event: 1.576 + { 1.577 + tcpSocket->IsConnecting = false; 1.578 + eventHandler->TCP_OnClosed(tcpSocket); 1.579 + } 1.580 + } 1.581 + else 1.582 + { 1.583 + struct sockaddr_storage sockAddr; 1.584 + socklen_t sockAddrSize = sizeof(sockAddr); 1.585 + 1.586 + SocketHandle newSock = accept(handle, (sockaddr*)&sockAddr, (socklen_t*)&sockAddrSize); 1.587 + if (newSock > 0) 1.588 + { 1.589 + SockAddr sa(&sockAddr); 1.590 + eventHandler->TCP_OnAccept(tcpSocket, &sa, newSock); 1.591 + } 1.592 + } 1.593 + } 1.594 + 1.595 + if (FD_ISSET(handle, &exceptionFD)) 1.596 + { 1.597 + tcpSocket->IsConnecting = false; 1.598 + eventHandler->TCP_OnClosed(tcpSocket); 1.599 + } 1.600 +} 1.601 + 1.602 + 1.603 +}} // namespace OVR::Net