OpenTTD
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 
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 
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  if ((cur->connected || cur->aborted) && cur->killed) {
70  iter = _tcp_connecters.erase(iter);
71  if (cur->sock != INVALID_SOCKET) closesocket(cur->sock);
72  delete cur;
73  continue;
74  }
75  if (cur->connected) {
76  iter = _tcp_connecters.erase(iter);
77  cur->OnConnect(cur->sock);
78  delete cur;
79  continue;
80  }
81  if (cur->aborted) {
82  iter = _tcp_connecters.erase(iter);
83  cur->OnFailure();
84  delete cur;
85  continue;
86  }
87  iter++;
88  }
89 }
90 
92 /* static */ void TCPConnecter::KillAll()
93 {
94  for (TCPConnecter *conn : _tcp_connecters) conn->killed = true;
95 }
NetworkAddress address
Address we&#39;re connecting to.
Definition: tcp.h:75
static void KillAll()
Kill all connection attempts.
Definition: tcp_connect.cpp:92
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:62
bool aborted
Whether we bailed out (i.e. connection making failed)
Definition: tcp.h:65
SOCKET sock
The socket we&#39;re connecting with.
Definition: tcp.h:67
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:27
TCPConnecter(const NetworkAddress &address)
Create a new connecter for the given address.
Definition: tcp_connect.cpp:26
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Definition: tcp.h:86
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: tcp.h:91
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:48
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
Definition: tcp_connect.cpp:65
static void ThreadEntry(TCPConnecter *param)
Entry point for the new threads.
Definition: tcp_connect.cpp:54
bool killed
Whether we got killed.
Definition: tcp.h:66
SOCKET Connect()
Connect to the given address.
Definition: address.cpp:320
bool connected
Whether we succeeded in making the connection.
Definition: tcp.h:64
static std::vector< TCPConnecter * > _tcp_connecters
List of connections that are currently being created.
Definition: tcp_connect.cpp:20
Basic functions to receive and send TCP packets.
void Connect()
The actual connection function.
Definition: tcp_connect.cpp:40