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

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

tcp_connect.cpp
Go to the documentation of this file.
1 /* $Id: tcp_connect.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
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
24 static SmallVector<TCPConnecter *, 1> _tcp_connecters;
25
30 TCPConnecter::TCPConnecter(const NetworkAddress &address) :
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)) {
39 this->Connect();
40 }
41 }
42
44 void TCPConnecter::Connect()
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 */