OpenTTD
tcp_connect.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
14 #include "../../stdafx.h"
15 #include "../../thread.h"
16 
17 #include "tcp.h"
18 
19 #include "../../safeguards.h"
20 
22 static std::vector<TCPConnecter *> _tcp_connecters;
23 
29  connected(false),
30  aborted(false),
31  killed(false),
32  sock(INVALID_SOCKET),
33  address(address)
34 {
35  _tcp_connecters.push_back(this);
36  if (!StartNewThread(nullptr, "ottd:tcp", &TCPConnecter::ThreadEntry, this)) {
37  this->Connect();
38  }
39 }
40 
43 {
44  this->sock = this->address.Connect();
45  if (this->sock == INVALID_SOCKET) {
46  this->aborted = true;
47  } else {
48  this->connected = true;
49  }
50 }
51 
56 /* static */ void TCPConnecter::ThreadEntry(TCPConnecter *param)
57 {
58  param->Connect();
59 }
60 
67 /* static */ void TCPConnecter::CheckCallbacks()
68 {
69  for (auto iter = _tcp_connecters.begin(); iter < _tcp_connecters.end(); /* nothing */) {
70  TCPConnecter *cur = *iter;
71  if ((cur->connected || cur->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 (cur->connected) {
78  iter = _tcp_connecters.erase(iter);
79  cur->OnConnect(cur->sock);
80  delete cur;
81  continue;
82  }
83  if (cur->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 }
NetworkAddress address
Address we&#39;re connecting to.
Definition: tcp.h:77
static void KillAll()
Kill all connection attempts.
Definition: tcp_connect.cpp:94
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:64
bool aborted
Whether we bailed out (i.e. connection making failed)
Definition: tcp.h:67
SOCKET sock
The socket we&#39;re connecting with.
Definition: tcp.h:69
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:29
TCPConnecter(const NetworkAddress &address)
Create a new connecter for the given address.
Definition: tcp_connect.cpp:28
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Definition: tcp.h:88
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: tcp.h:93
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:50
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
Definition: tcp_connect.cpp:67
static void ThreadEntry(TCPConnecter *param)
Entry point for the new threads.
Definition: tcp_connect.cpp:56
bool killed
Whether we got killed.
Definition: tcp.h:68
SOCKET Connect()
Connect to the given address.
Definition: address.cpp:322
bool connected
Whether we succeeded in making the connection.
Definition: tcp.h:66
static std::vector< TCPConnecter * > _tcp_connecters
List of connections that are currently being created.
Definition: tcp_connect.cpp:22
Basic functions to receive and send TCP packets.
void Connect()
The actual connection function.
Definition: tcp_connect.cpp:42