OpenTTD
tcp_connect.cpp
Go to the documentation of this file.
1 /* $Id: tcp_connect.cpp 27670 2016-10-30 17:29:33Z frosch $ */
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 #ifdef ENABLE_NETWORK
15 
16 #include "../../stdafx.h"
17 #include "../../thread/thread.h"
18 
19 #include "tcp.h"
20 
21 #include "../../safeguards.h"
22 
25 
31  connected(false),
32  aborted(false),
33  killed(false),
34  sock(INVALID_SOCKET),
35  address(address)
36 {
37  *_tcp_connecters.Append() = this;
38  if (!ThreadObject::New(TCPConnecter::ThreadEntry, this, &this->thread, "ottd:tcp")) {
39  this->Connect();
40  }
41 }
42 
45 {
46  this->sock = this->address.Connect();
47  if (this->sock == INVALID_SOCKET) {
48  this->aborted = true;
49  } else {
50  this->connected = true;
51  }
52 }
53 
58 /* static */ void TCPConnecter::ThreadEntry(void *param)
59 {
60  static_cast<TCPConnecter*>(param)->Connect();
61 }
62 
69 /* static */ void TCPConnecter::CheckCallbacks()
70 {
71  for (TCPConnecter **iter = _tcp_connecters.Begin(); iter < _tcp_connecters.End(); /* nothing */) {
72  TCPConnecter *cur = *iter;
73  if ((cur->connected || cur->aborted) && cur->killed) {
74  _tcp_connecters.Erase(iter);
75  if (cur->sock != INVALID_SOCKET) closesocket(cur->sock);
76  delete cur;
77  continue;
78  }
79  if (cur->connected) {
80  _tcp_connecters.Erase(iter);
81  cur->OnConnect(cur->sock);
82  delete cur;
83  continue;
84  }
85  if (cur->aborted) {
86  _tcp_connecters.Erase(iter);
87  cur->OnFailure();
88  delete cur;
89  continue;
90  }
91  iter++;
92  }
93 }
94 
96 /* static */ void TCPConnecter::KillAll()
97 {
98  for (TCPConnecter **iter = _tcp_connecters.Begin(); iter != _tcp_connecters.End(); iter++) (*iter)->killed = true;
99 }
100 
101 #endif /* ENABLE_NETWORK */
NetworkAddress address
Address we&#39;re connecting to.
Definition: tcp.h:80
static void KillAll()
Kill all connection attempts.
Definition: tcp_connect.cpp:96
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:66
bool aborted
Whether we bailed out (i.e. connection making failed)
Definition: tcp.h:70
SOCKET sock
The socket we&#39;re connecting with.
Definition: tcp.h:72
static void ThreadEntry(void *param)
Entry point for the new threads.
Definition: tcp_connect.cpp:58
static SmallVector< TCPConnecter *, 1 > _tcp_connecters
List of connections that are currently being created.
Definition: tcp_connect.cpp:24
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:31
const T * Begin() const
Get the pointer to the first item (const)
class ThreadObject * thread
Thread used to create the TCP connection.
Definition: tcp.h:68
TCPConnecter(const NetworkAddress &address)
Create a new connecter for the given address.
Definition: tcp_connect.cpp:30
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Definition: tcp.h:91
Simple vector template class.
const T * End() const
Get the pointer behind the last valid item (const)
T * Append(uint to_add=1)
Append an item and return it.
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: tcp.h:96
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
Definition: tcp_connect.cpp:69
bool killed
Whether we got killed.
Definition: tcp.h:71
SOCKET Connect()
Connect to the given address.
Definition: address.cpp:325
bool connected
Whether we succeeded in making the connection.
Definition: tcp.h:69
void Erase(T *item)
Removes given item from this vector.
Basic functions to receive and send TCP packets.
void Connect()
The actual connection function.
Definition: tcp_connect.cpp:44
static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread=NULL, const char *name=NULL)
Create a thread; proc will be called as first function inside the thread, with optional params...