OpenTTD Source: src/network/core/tcp_connect.cpp Source File

OpenTTD Source 1.11.0-RC1

tcp_connect.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
12 #include "../../stdafx.h"
13 #include "../../thread.h"
14
15 #include "tcp.h"
16
17 #include "../../safeguards.h"
18
20 static std::vector<TCPConnecter *> _tcp_connecters;
21
26 TCPConnecter::TCPConnecter(const NetworkAddress &address) :
27 connected(false),
28 aborted(false),
29 killed(false),
30 sock(INVALID_SOCKET),
31 address(address)
32 {
33 _tcp_connecters.push_back(this);
34 if (!StartNewThread(nullptr, "ottd:tcp", &TCPConnecter::ThreadEntry, this)) {
35 this->Connect();
36 }
37 }
38
40 void TCPConnecter::Connect()
41 {
42 this->sock = this->address.Connect();
43 if (this->sock == INVALID_SOCKET) {
44 this->aborted = true;
45 } else {
46 this->connected = true;
47 }
48 }
49
54 /* static */ void TCPConnecter::ThreadEntry(TCPConnecter *param)
55 {
56 param->Connect();
57 }
58
65 /* static */ void TCPConnecter::CheckCallbacks()
66 {
67 for (auto iter = _tcp_connecters.begin(); iter < _tcp_connecters.end(); /* nothing */) {
68 TCPConnecter *cur = *iter;
69 const bool connected = cur->connected.load();
70 const bool aborted = cur->aborted.load();
71 if ((connected || aborted) && cur->killed) {
72 iter = _tcp_connecters.erase(iter);
73 if (cur->sock != INVALID_SOCKET) closesocket(cur->sock);
74 delete cur;
75 continue;
76 }
77 if (connected) {
78 iter = _tcp_connecters.erase(iter);
79 cur->OnConnect(cur->sock);
80 delete cur;
81 continue;
82 }
83 if (aborted) {
84 iter = _tcp_connecters.erase(iter);
85 cur->OnFailure();
86 delete cur;
87 continue;
88 }
89 iter++;
90 }
91 }
92
94 /* static */ void TCPConnecter::KillAll()
95 {
96 for (TCPConnecter *conn : _tcp_connecters) conn->killed = true;
97 }
TCPConnecter::CheckCallbacks
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
Definition: tcp_connect.cpp:65
TCPConnecter::TCPConnecter
TCPConnecter(const NetworkAddress &address)
Create a new connecter for the given address.
Definition: tcp_connect.cpp:26
TCPConnecter::OnConnect
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Definition: tcp.h:88
NetworkAddress::Connect
SOCKET Connect()
Connect to the given address.
Definition: address.cpp:340
TCPConnecter::aborted
std::atomic< bool > aborted
Whether we bailed out (i.e. connection making failed)
Definition: tcp.h:67
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:64
_tcp_connecters
static std::vector< TCPConnecter * > _tcp_connecters
List of connections that are currently being created.
Definition: tcp_connect.cpp:20
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:45
TCPConnecter::killed
bool killed
Whether we got killed.
Definition: tcp.h:68
TCPConnecter::KillAll
static void KillAll()
Kill all connection attempts.
Definition: tcp_connect.cpp:94
TCPConnecter::address
NetworkAddress address
Address we're connecting to.
Definition: tcp.h:77
TCPConnecter::sock
SOCKET sock
The socket we're connecting with.
Definition: tcp.h:69
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:29
TCPConnecter::connected
std::atomic< bool > connected
Whether we succeeded in making the connection.
Definition: tcp.h:66
TCPConnecter::ThreadEntry
static void ThreadEntry(TCPConnecter *param)
Entry point for the new threads.
Definition: tcp_connect.cpp:54
tcp.h
TCPConnecter::OnFailure
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: tcp.h:93
TCPConnecter::Connect
void Connect()
The actual connection function.
Definition: tcp_connect.cpp:40