Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "Socket.hpp" |
| 16 | |
| 17 | #if defined(_WIN32) |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 18 | # include <ws2tcpip.h> |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 19 | #else |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 20 | # include <unistd.h> |
| 21 | # include <netdb.h> |
| 22 | # include <netinet/in.h> |
| 23 | # include <sys/select.h> |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 24 | #endif |
| 25 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 26 | namespace sw { |
| 27 | |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 28 | Socket::Socket(SOCKET socket) |
| 29 | : socket(socket) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 30 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | Socket::Socket(const char *address, const char *port) |
| 34 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 35 | #if defined(_WIN32) |
| 36 | socket = INVALID_SOCKET; |
| 37 | #else |
| 38 | socket = -1; |
| 39 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 40 | |
| 41 | addrinfo hints = {}; |
| 42 | hints.ai_family = AF_INET; |
| 43 | hints.ai_socktype = SOCK_STREAM; |
| 44 | hints.ai_protocol = IPPROTO_TCP; |
| 45 | hints.ai_flags = AI_PASSIVE; |
| 46 | |
| 47 | addrinfo *info = 0; |
| 48 | getaddrinfo(address, port, &hints, &info); |
| 49 | |
| 50 | if(info) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 51 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 52 | socket = ::socket(info->ai_family, info->ai_socktype, info->ai_protocol); |
| 53 | bind(socket, info->ai_addr, (int)info->ai_addrlen); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 54 | } |
| 55 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 56 | |
| 57 | Socket::~Socket() |
| 58 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 59 | #if defined(_WIN32) |
| 60 | closesocket(socket); |
| 61 | #else |
| 62 | close(socket); |
| 63 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void Socket::listen(int backlog) |
| 67 | { |
| 68 | ::listen(socket, backlog); |
| 69 | } |
| 70 | |
| 71 | bool Socket::select(int us) |
| 72 | { |
| 73 | fd_set sockets; |
| 74 | FD_ZERO(&sockets); |
| 75 | FD_SET(socket, &sockets); |
| 76 | |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 77 | timeval timeout = { us / 1000000, us % 1000000 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 78 | |
| 79 | return ::select(FD_SETSIZE, &sockets, 0, 0, &timeout) >= 1; |
| 80 | } |
| 81 | |
| 82 | Socket *Socket::accept() |
| 83 | { |
| 84 | return new Socket(::accept(socket, 0, 0)); |
| 85 | } |
| 86 | |
| 87 | int Socket::receive(char *buffer, int length) |
| 88 | { |
| 89 | return recv(socket, buffer, length, 0); |
| 90 | } |
| 91 | |
| 92 | void Socket::send(const char *buffer, int length) |
| 93 | { |
| 94 | ::send(socket, buffer, length, 0); |
| 95 | } |
| 96 | |
| 97 | void Socket::startup() |
| 98 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 99 | #if defined(_WIN32) |
| 100 | WSADATA winsockData; |
| 101 | WSAStartup(MAKEWORD(2, 2), &winsockData); |
| 102 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void Socket::cleanup() |
| 106 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 107 | #if defined(_WIN32) |
| 108 | WSACleanup(); |
| 109 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace sw |