blob: dd2944b918ca260dfd6ec1adf0201049d207f083 [file] [log] [blame]
Nicolas Capens68a82382018-10-02 13:16:55 -04001// 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 Clayton595d9112019-12-17 20:37:57 +000018# include <ws2tcpip.h>
Nicolas Capens68a82382018-10-02 13:16:55 -040019#else
Ben Clayton595d9112019-12-17 20:37:57 +000020# include <unistd.h>
21# include <netdb.h>
22# include <netinet/in.h>
23# include <sys/select.h>
Nicolas Capens68a82382018-10-02 13:16:55 -040024#endif
25
Nicolas Capens157ba262019-12-10 17:49:14 -050026namespace sw {
27
Ben Clayton595d9112019-12-17 20:37:57 +000028Socket::Socket(SOCKET socket)
29 : socket(socket)
Nicolas Capens68a82382018-10-02 13:16:55 -040030{
Nicolas Capens157ba262019-12-10 17:49:14 -050031}
32
33Socket::Socket(const char *address, const char *port)
34{
Ben Clayton595d9112019-12-17 20:37:57 +000035#if defined(_WIN32)
36 socket = INVALID_SOCKET;
37#else
38 socket = -1;
39#endif
Nicolas Capens157ba262019-12-10 17:49:14 -050040
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 Capens68a82382018-10-02 13:16:55 -040051 {
Nicolas Capens157ba262019-12-10 17:49:14 -050052 socket = ::socket(info->ai_family, info->ai_socktype, info->ai_protocol);
53 bind(socket, info->ai_addr, (int)info->ai_addrlen);
Nicolas Capens68a82382018-10-02 13:16:55 -040054 }
55}
Nicolas Capens157ba262019-12-10 17:49:14 -050056
57Socket::~Socket()
58{
Ben Clayton595d9112019-12-17 20:37:57 +000059#if defined(_WIN32)
60 closesocket(socket);
61#else
62 close(socket);
63#endif
Nicolas Capens157ba262019-12-10 17:49:14 -050064}
65
66void Socket::listen(int backlog)
67{
68 ::listen(socket, backlog);
69}
70
71bool Socket::select(int us)
72{
73 fd_set sockets;
74 FD_ZERO(&sockets);
75 FD_SET(socket, &sockets);
76
Ben Clayton595d9112019-12-17 20:37:57 +000077 timeval timeout = { us / 1000000, us % 1000000 };
Nicolas Capens157ba262019-12-10 17:49:14 -050078
79 return ::select(FD_SETSIZE, &sockets, 0, 0, &timeout) >= 1;
80}
81
82Socket *Socket::accept()
83{
84 return new Socket(::accept(socket, 0, 0));
85}
86
87int Socket::receive(char *buffer, int length)
88{
89 return recv(socket, buffer, length, 0);
90}
91
92void Socket::send(const char *buffer, int length)
93{
94 ::send(socket, buffer, length, 0);
95}
96
97void Socket::startup()
98{
Ben Clayton595d9112019-12-17 20:37:57 +000099#if defined(_WIN32)
100 WSADATA winsockData;
101 WSAStartup(MAKEWORD(2, 2), &winsockData);
102#endif
Nicolas Capens157ba262019-12-10 17:49:14 -0500103}
104
105void Socket::cleanup()
106{
Ben Clayton595d9112019-12-17 20:37:57 +0000107#if defined(_WIN32)
108 WSACleanup();
109#endif
Nicolas Capens157ba262019-12-10 17:49:14 -0500110}
111
112} // namespace sw