Mercurial > hg > openttd
comparison src/network/core/tcp_connect.cpp @ 17148:848eb1ffb17d draft
(svn r21886) -Codechange: move documentation towards the code to make it more likely to be updated [n].
author | rubidium <rubidium@openttd.org> |
---|---|
date | Sat, 22 Jan 2011 09:53:15 +0000 |
parents | afdfdda87cd4 |
children |
comparison
equal
deleted
inserted
replaced
17147:5ffc4ae6944d | 17148:848eb1ffb17d |
---|---|
19 #include "tcp.h" | 19 #include "tcp.h" |
20 | 20 |
21 /** List of connections that are currently being created */ | 21 /** List of connections that are currently being created */ |
22 static SmallVector<TCPConnecter *, 1> _tcp_connecters; | 22 static SmallVector<TCPConnecter *, 1> _tcp_connecters; |
23 | 23 |
24 /** | |
25 * Create a new connecter for the given address | |
26 * @param address the (un)resolved address to connect to | |
27 */ | |
24 TCPConnecter::TCPConnecter(const NetworkAddress &address) : | 28 TCPConnecter::TCPConnecter(const NetworkAddress &address) : |
25 connected(false), | 29 connected(false), |
26 aborted(false), | 30 aborted(false), |
27 killed(false), | 31 killed(false), |
28 sock(INVALID_SOCKET), | 32 sock(INVALID_SOCKET), |
32 if (!ThreadObject::New(TCPConnecter::ThreadEntry, this, &this->thread)) { | 36 if (!ThreadObject::New(TCPConnecter::ThreadEntry, this, &this->thread)) { |
33 this->Connect(); | 37 this->Connect(); |
34 } | 38 } |
35 } | 39 } |
36 | 40 |
41 /** The actual connection function */ | |
37 void TCPConnecter::Connect() | 42 void TCPConnecter::Connect() |
38 { | 43 { |
39 this->sock = this->address.Connect(); | 44 this->sock = this->address.Connect(); |
40 if (this->sock == INVALID_SOCKET) { | 45 if (this->sock == INVALID_SOCKET) { |
41 this->aborted = true; | 46 this->aborted = true; |
42 } else { | 47 } else { |
43 this->connected = true; | 48 this->connected = true; |
44 } | 49 } |
45 } | 50 } |
46 | 51 |
47 | 52 /** |
53 * Entry point for the new threads. | |
54 * @param param the TCPConnecter instance to call Connect on. | |
55 */ | |
48 /* static */ void TCPConnecter::ThreadEntry(void *param) | 56 /* static */ void TCPConnecter::ThreadEntry(void *param) |
49 { | 57 { |
50 static_cast<TCPConnecter*>(param)->Connect(); | 58 static_cast<TCPConnecter*>(param)->Connect(); |
51 } | 59 } |
52 | 60 |
61 /** | |
62 * Check whether we need to call the callback, i.e. whether we | |
63 * have connected or aborted and call the appropriate callback | |
64 * for that. It's done this way to ease on the locking that | |
65 * would otherwise be needed everywhere. | |
66 */ | |
53 /* static */ void TCPConnecter::CheckCallbacks() | 67 /* static */ void TCPConnecter::CheckCallbacks() |
54 { | 68 { |
55 for (TCPConnecter **iter = _tcp_connecters.Begin(); iter < _tcp_connecters.End(); /* nothing */) { | 69 for (TCPConnecter **iter = _tcp_connecters.Begin(); iter < _tcp_connecters.End(); /* nothing */) { |
56 TCPConnecter *cur = *iter; | 70 TCPConnecter *cur = *iter; |
57 if ((cur->connected || cur->aborted) && cur->killed) { | 71 if ((cur->connected || cur->aborted) && cur->killed) { |
74 } | 88 } |
75 iter++; | 89 iter++; |
76 } | 90 } |
77 } | 91 } |
78 | 92 |
93 /** Kill all connection attempts. */ | |
79 /* static */ void TCPConnecter::KillAll() | 94 /* static */ void TCPConnecter::KillAll() |
80 { | 95 { |
81 for (TCPConnecter **iter = _tcp_connecters.Begin(); iter != _tcp_connecters.End(); iter++) (*iter)->killed = true; | 96 for (TCPConnecter **iter = _tcp_connecters.Begin(); iter != _tcp_connecters.End(); iter++) (*iter)->killed = true; |
82 } | 97 } |
83 | 98 |