-
-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathtapplicationserverbase_win.cpp
117 lines (99 loc) · 3.5 KB
/
tapplicationserverbase_win.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Copyright (c) 2011-2019, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include "tapplicationserverbase.h"
#include <TSystemGlobal>
#include <winsock2.h>
void TApplicationServerBase::nativeSocketInit()
{
WSAData wsadata;
if (WSAStartup(MAKEWORD(2, 0), &wsadata) != 0) {
tSystemWarn("WinSock v2.0 initialization failed");
}
}
void TApplicationServerBase::nativeSocketCleanup()
{
WSACleanup();
}
/*!
Listen a port with SO_REUSEADDR option.
This function must be called in a tfserver process.
*/
int TApplicationServerBase::nativeListen(const QHostAddress &address, uint16_t port, OpenFlag)
{
int protocol = (address.protocol() == QAbstractSocket::IPv6Protocol) ? AF_INET6 : AF_INET;
SOCKET sock = ::WSASocket(protocol, SOCK_STREAM, 0, nullptr, 0, WSA_FLAG_OVERLAPPED);
if (sock == INVALID_SOCKET) {
tSystemError("WSASocket Error: {}", WSAGetLastError());
return -1;
}
// ReuseAddr
bool on = true;
if (::setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) != 0) {
tSystemError("setsockopt error: {}", WSAGetLastError());
goto error_socket;
}
if (address.protocol() == QAbstractSocket::IPv6Protocol) {
struct tf_in6_addr {
uint8_t tf_s6_addr[16];
};
struct tf_sockaddr_in6 {
short sin6_family; /* AF_INET6 */
uint16_t sin6_port; /* Transport level port number */
uint32_t sin6_flowinfo; /* IPv6 flow information */
struct tf_in6_addr sin6_addr; /* IPv6 address */
uint32_t sin6_scope_id; /* set of interfaces for a scope */
} sa6;
std::memset(&sa6, 0, sizeof(sa6));
sa6.sin6_family = AF_INET6;
WSAHtons(sock, port, &(sa6.sin6_port));
Q_IPV6ADDR ipv6 = address.toIPv6Address();
std::memcpy(&(sa6.sin6_addr.tf_s6_addr), &ipv6, sizeof(ipv6));
if (::bind(sock, (struct sockaddr *)&sa6, sizeof(sa6)) != 0) {
tSystemError("bind(v6) error: {}", WSAGetLastError());
goto error_socket;
}
} else if (address.protocol() == QAbstractSocket::IPv4Protocol
|| address.protocol() == QAbstractSocket::AnyIPProtocol) {
struct sockaddr_in sa;
std::memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
WSAHtons(sock, port, &(sa.sin_port));
WSAHtonl(sock, address.toIPv4Address(), &(sa.sin_addr.s_addr));
if (::bind(sock, (struct sockaddr *)&sa, sizeof(sa)) != 0) {
tSystemError("bind error: {}", WSAGetLastError());
goto error_socket;
}
} else { // UnknownNetworkLayerProtocol
goto error_socket;
}
if (::listen(sock, SOMAXCONN) != 0) {
tSystemError("listen error: {}", WSAGetLastError());
goto error_socket;
}
return sock;
error_socket:
nativeClose(sock);
return -1;
}
int TApplicationServerBase::nativeListen(const QString &, OpenFlag)
{
// must not reach here
Q_ASSERT(0);
return 0;
}
void TApplicationServerBase::nativeClose(int socket)
{
if (socket != (int)INVALID_SOCKET)
closesocket(socket);
}
int TApplicationServerBase::duplicateSocket(int socketDescriptor)
{
WSAPROTOCOL_INFO pi;
::WSADuplicateSocket(socketDescriptor, ::GetCurrentProcessId(), &pi);
SOCKET newsock = ::WSASocket(pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, 0);
return newsock;
}