summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2019-03-17 01:59:46 +0100
committerMichael Lutz <michi@icosahedron.de>2019-04-06 11:27:39 +0200
commit05bc2ed7cbe07cb4cd535932f10778b35f72e944 (patch)
tree0faaf12fd1bafb0786236ffc82052e8b83dfca60 /src/network
parent05f4e7360886e36b221ef5c3af4426625a3de686 (diff)
downloadopenttd-05bc2ed7cbe07cb4cd535932f10778b35f72e944.tar.xz
Codechange: Replace custom thread code with C++11 thread objects.
We assume a conforming C++11 compiler environment that has a valid <thread>-header. Failure to run a real thread is handled gracefully.
Diffstat (limited to 'src/network')
-rw-r--r--src/network/core/tcp.h3
-rw-r--r--src/network/core/tcp_connect.cpp8
-rw-r--r--src/network/network_server.h1
-rw-r--r--src/network/network_udp.cpp61
4 files changed, 20 insertions, 53 deletions
diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h
index 243ec042d..f9e1e00cb 100644
--- a/src/network/core/tcp.h
+++ b/src/network/core/tcp.h
@@ -63,7 +63,6 @@ public:
*/
class TCPConnecter {
private:
- class ThreadObject *thread; ///< Thread used to create the TCP connection
bool connected; ///< Whether we succeeded in making the connection
bool aborted; ///< Whether we bailed out (i.e. connection making failed)
bool killed; ///< Whether we got killed
@@ -71,7 +70,7 @@ private:
void Connect();
- static void ThreadEntry(void *param);
+ static void ThreadEntry(TCPConnecter *param);
protected:
/** Address we're connecting to */
diff --git a/src/network/core/tcp_connect.cpp b/src/network/core/tcp_connect.cpp
index d76042821..d923688b1 100644
--- a/src/network/core/tcp_connect.cpp
+++ b/src/network/core/tcp_connect.cpp
@@ -12,7 +12,7 @@
*/
#include "../../stdafx.h"
-#include "../../thread/thread.h"
+#include "../../thread.h"
#include "tcp.h"
@@ -33,7 +33,7 @@ TCPConnecter::TCPConnecter(const NetworkAddress &address) :
address(address)
{
_tcp_connecters.push_back(this);
- if (!ThreadObject::New(TCPConnecter::ThreadEntry, this, &this->thread, "ottd:tcp")) {
+ if (!StartNewThread(NULL, "ottd:tcp", &TCPConnecter::ThreadEntry, this)) {
this->Connect();
}
}
@@ -53,9 +53,9 @@ void TCPConnecter::Connect()
* Entry point for the new threads.
* @param param the TCPConnecter instance to call Connect on.
*/
-/* static */ void TCPConnecter::ThreadEntry(void *param)
+/* static */ void TCPConnecter::ThreadEntry(TCPConnecter *param)
{
- static_cast<TCPConnecter*>(param)->Connect();
+ param->Connect();
}
/**
diff --git a/src/network/network_server.h b/src/network/network_server.h
index f35192a11..f13691fcd 100644
--- a/src/network/network_server.h
+++ b/src/network/network_server.h
@@ -14,7 +14,6 @@
#include "network_internal.h"
#include "core/tcp_listen.h"
-#include "../thread/thread.h"
class ServerNetworkGameSocketHandler;
/** Make the code look slightly nicer/simpler. */
diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp
index df8c59aac..7674036e7 100644
--- a/src/network/network_udp.cpp
+++ b/src/network/network_udp.cpp
@@ -24,7 +24,7 @@
#include "network.h"
#include "../core/endian_func.hpp"
#include "../company_base.h"
-#include "../thread/thread.h"
+#include "../thread.h"
#include "../rev.h"
#include "../newgrf_text.h"
#include "../strings_func.h"
@@ -49,35 +49,19 @@ NetworkUDPSocketHandler *_udp_client_socket = NULL; ///< udp client socket
NetworkUDPSocketHandler *_udp_server_socket = NULL; ///< udp server socket
NetworkUDPSocketHandler *_udp_master_socket = NULL; ///< udp master socket
-/** Simpler wrapper struct for NetworkUDPQueryServerThread */
-struct NetworkUDPQueryServerInfo : NetworkAddress {
- bool manually; ///< Did we connect manually or not?
-
- /**
- * Create the structure.
- * @param address The address of the server to query.
- * @param manually Whether the address was entered manually.
- */
- NetworkUDPQueryServerInfo(const NetworkAddress &address, bool manually) :
- NetworkAddress(address),
- manually(manually)
- {
- }
-};
-
/**
* Helper function doing the actual work for querying the server.
* @param address The address of the server.
* @param needs_mutex Whether we need to acquire locks when sending the packet or not.
* @param manually Whether the address was entered manually.
*/
-static void NetworkUDPQueryServer(NetworkAddress *address, bool needs_mutex, bool manually)
+static void DoNetworkUDPQueryServer(NetworkAddress &address, bool needs_mutex, bool manually)
{
/* Clear item in gamelist */
NetworkGameList *item = CallocT<NetworkGameList>(1);
- address->GetAddressAsString(item->info.server_name, lastof(item->info.server_name));
- strecpy(item->info.hostname, address->GetHostname(), lastof(item->info.hostname));
- item->address = *address;
+ address.GetAddressAsString(item->info.server_name, lastof(item->info.server_name));
+ strecpy(item->info.hostname, address.GetHostname(), lastof(item->info.hostname));
+ item->address = address;
item->manually = manually;
NetworkGameListAddItemDelayed(item);
@@ -85,19 +69,7 @@ static void NetworkUDPQueryServer(NetworkAddress *address, bool needs_mutex, boo
if (needs_mutex) lock.lock();
/* Init the packet */
Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
- if (_udp_client_socket != NULL) _udp_client_socket->SendPacket(&p, address);
-}
-
-/**
- * Threaded part for resolving the IP of a server and querying it.
- * @param pntr the NetworkUDPQueryServerInfo.
- */
-static void NetworkUDPQueryServerThread(void *pntr)
-{
- NetworkUDPQueryServerInfo *info = (NetworkUDPQueryServerInfo*)pntr;
- NetworkUDPQueryServer(info, true, info->manually);
-
- delete info;
+ if (_udp_client_socket != NULL) _udp_client_socket->SendPacket(&p, &address);
}
/**
@@ -107,9 +79,8 @@ static void NetworkUDPQueryServerThread(void *pntr)
*/
void NetworkUDPQueryServer(NetworkAddress address, bool manually)
{
- NetworkUDPQueryServerInfo *info = new NetworkUDPQueryServerInfo(address, manually);
- if (address.IsResolved() || !ThreadObject::New(NetworkUDPQueryServerThread, info, NULL, "ottd:udp-query")) {
- NetworkUDPQueryServerThread(info);
+ if (address.IsResolved() || !StartNewThread(NULL, "ottd:udp-query", &DoNetworkUDPQueryServer, std::move(address), true, std::move(manually))) {
+ DoNetworkUDPQueryServer(address, true, manually);
}
}
@@ -429,7 +400,7 @@ void ClientNetworkUDPSocketHandler::Receive_MASTER_RESPONSE_LIST(Packet *p, Netw
/* Somehow we reached the end of the packet */
if (this->HasClientQuit()) return;
- NetworkUDPQueryServer(&addr, false, false);
+ DoNetworkUDPQueryServer(addr, false, false);
}
}
}
@@ -535,9 +506,8 @@ void NetworkUDPSearchGame()
/**
* Thread entry point for de-advertising.
- * @param pntr unused.
*/
-static void NetworkUDPRemoveAdvertiseThread(void *pntr)
+static void NetworkUDPRemoveAdvertiseThread()
{
DEBUG(net, 1, "[udp] removing advertise from master server");
@@ -563,16 +533,15 @@ void NetworkUDPRemoveAdvertise(bool blocking)
/* Check if we are advertising */
if (!_networking || !_network_server || !_network_udp_server) return;
- if (blocking || !ThreadObject::New(NetworkUDPRemoveAdvertiseThread, NULL, NULL, "ottd:udp-advert")) {
- NetworkUDPRemoveAdvertiseThread(NULL);
+ if (blocking || !StartNewThread(NULL, "ottd:udp-advert", &NetworkUDPRemoveAdvertiseThread)) {
+ NetworkUDPRemoveAdvertiseThread();
}
}
/**
* Thread entry point for advertising.
- * @param pntr unused.
*/
-static void NetworkUDPAdvertiseThread(void *pntr)
+static void NetworkUDPAdvertiseThread()
{
/* Find somewhere to send */
NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
@@ -645,8 +614,8 @@ void NetworkUDPAdvertise()
if (_next_advertisement < _last_advertisement) _next_advertisement = UINT32_MAX;
if (_next_retry < _last_advertisement) _next_retry = UINT32_MAX;
- if (!ThreadObject::New(NetworkUDPAdvertiseThread, NULL, NULL, "ottd:udp-advert")) {
- NetworkUDPAdvertiseThread(NULL);
+ if (!StartNewThread(NULL, "ottd:udp-advert", &NetworkUDPAdvertiseThread)) {
+ NetworkUDPAdvertiseThread();
}
}