2009-01-20 12:28:18 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-20 12:28:18 +01:00
|
|
|
/**
|
|
|
|
* @file tcp_connect.cpp Basic functions to create connections without blocking.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../../stdafx.h"
|
2009-09-01 12:07:22 +02:00
|
|
|
#include "../../thread/thread.h"
|
2009-01-20 12:28:18 +01:00
|
|
|
|
|
|
|
#include "tcp.h"
|
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2009-01-20 12:28:18 +01:00
|
|
|
/** List of connections that are currently being created */
|
|
|
|
static SmallVector<TCPConnecter *, 1> _tcp_connecters;
|
|
|
|
|
2011-01-22 10:53:15 +01:00
|
|
|
/**
|
|
|
|
* Create a new connecter for the given address
|
|
|
|
* @param address the (un)resolved address to connect to
|
|
|
|
*/
|
2009-01-20 12:28:18 +01:00
|
|
|
TCPConnecter::TCPConnecter(const NetworkAddress &address) :
|
|
|
|
connected(false),
|
|
|
|
aborted(false),
|
|
|
|
killed(false),
|
|
|
|
sock(INVALID_SOCKET),
|
|
|
|
address(address)
|
|
|
|
{
|
|
|
|
*_tcp_connecters.Append() = this;
|
2016-10-30 18:29:33 +01:00
|
|
|
if (!ThreadObject::New(TCPConnecter::ThreadEntry, this, &this->thread, "ottd:tcp")) {
|
2009-01-20 12:28:18 +01:00
|
|
|
this->Connect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-22 10:53:15 +01:00
|
|
|
/** The actual connection function */
|
2009-01-20 12:28:18 +01:00
|
|
|
void TCPConnecter::Connect()
|
|
|
|
{
|
2009-04-03 01:59:43 +02:00
|
|
|
this->sock = this->address.Connect();
|
2009-01-20 12:28:18 +01:00
|
|
|
if (this->sock == INVALID_SOCKET) {
|
|
|
|
this->aborted = true;
|
2009-04-03 01:59:43 +02:00
|
|
|
} else {
|
|
|
|
this->connected = true;
|
2009-01-20 12:28:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-22 10:53:15 +01:00
|
|
|
/**
|
|
|
|
* Entry point for the new threads.
|
|
|
|
* @param param the TCPConnecter instance to call Connect on.
|
|
|
|
*/
|
2009-01-20 12:28:18 +01:00
|
|
|
/* static */ void TCPConnecter::ThreadEntry(void *param)
|
|
|
|
{
|
|
|
|
static_cast<TCPConnecter*>(param)->Connect();
|
|
|
|
}
|
|
|
|
|
2011-01-22 10:53:15 +01:00
|
|
|
/**
|
|
|
|
* Check whether we need to call the callback, i.e. whether we
|
|
|
|
* have connected or aborted and call the appropriate callback
|
|
|
|
* for that. It's done this way to ease on the locking that
|
|
|
|
* would otherwise be needed everywhere.
|
|
|
|
*/
|
2009-01-20 12:28:18 +01:00
|
|
|
/* static */ void TCPConnecter::CheckCallbacks()
|
|
|
|
{
|
|
|
|
for (TCPConnecter **iter = _tcp_connecters.Begin(); iter < _tcp_connecters.End(); /* nothing */) {
|
|
|
|
TCPConnecter *cur = *iter;
|
|
|
|
if ((cur->connected || cur->aborted) && cur->killed) {
|
|
|
|
_tcp_connecters.Erase(iter);
|
|
|
|
if (cur->sock != INVALID_SOCKET) closesocket(cur->sock);
|
|
|
|
delete cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cur->connected) {
|
|
|
|
_tcp_connecters.Erase(iter);
|
|
|
|
cur->OnConnect(cur->sock);
|
|
|
|
delete cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cur->aborted) {
|
|
|
|
_tcp_connecters.Erase(iter);
|
|
|
|
cur->OnFailure();
|
|
|
|
delete cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-22 10:53:15 +01:00
|
|
|
/** Kill all connection attempts. */
|
2009-01-20 12:28:18 +01:00
|
|
|
/* static */ void TCPConnecter::KillAll()
|
|
|
|
{
|
|
|
|
for (TCPConnecter **iter = _tcp_connecters.Begin(); iter != _tcp_connecters.End(); iter++) (*iter)->killed = true;
|
|
|
|
}
|