src/network/core/udp.cpp
author rubidium
Mon, 03 Dec 2007 23:39:38 +0000
branchNewGRF_ports
changeset 6871 5a9dc001e1ad
parent 6743 cabfaa4a0295
child 6872 1c4a4a609f85
permissions -rw-r--r--
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
     1
/* $Id$ */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
     2
6447
3b71e57fd22b (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 6226
diff changeset
     3
/**
3b71e57fd22b (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 6226
diff changeset
     4
 * @file core/udp.cpp Basic functions to receive and send UDP packets.
3b71e57fd22b (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 6226
diff changeset
     5
 */
3b71e57fd22b (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 6226
diff changeset
     6
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
     7
#ifdef ENABLE_NETWORK
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
     8
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
     9
#include "../../stdafx.h"
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    10
#include "../../debug.h"
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    11
#include "../../macros.h"
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    12
#include "../../helpers.hpp"
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    13
#include "packet.h"
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    14
#include "udp.h"
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    15
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    16
/**
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    17
 * Start listening on the given host and port.
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    18
 * @param host      the host (ip) to listen on
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    19
 * @param port      the port to listen on
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    20
 * @param broadcast whether to allow broadcast sending/receiving
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    21
 * @return true if the listening succeeded
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    22
 */
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
    23
bool NetworkUDPSocketHandler::Listen(const uint32 host, const uint16 port, const bool broadcast)
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    24
{
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    25
	struct sockaddr_in sin;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    26
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    27
	/* Make sure socket is closed */
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
    28
	this->Close();
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    29
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    30
	this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    31
	if (!this->IsConnected()) {
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    32
		DEBUG(net, 0, "[udp] failed to start UDP listener");
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    33
		return false;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    34
	}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    35
6226
993f5aa4c6cb (svn r8673) -Codechange: use SetNonBlocking instead of implementing yet another version.
rubidium
parents: 6169
diff changeset
    36
	SetNonBlocking(this->sock);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    37
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    38
	sin.sin_family = AF_INET;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    39
	/* Listen on all IPs */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    40
	sin.sin_addr.s_addr = host;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    41
	sin.sin_port = htons(port);
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    42
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    43
	if (bind(this->sock, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    44
		DEBUG(net, 0, "[udp] bind failed on %s:%i", inet_ntoa(*(struct in_addr *)&host), port);
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    45
		return false;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    46
	}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    47
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    48
	if (broadcast) {
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    49
		/* Enable broadcast */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    50
		unsigned long val = 1;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    51
#ifndef BEOS_NET_SERVER // will work around this, some day; maybe.
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    52
		setsockopt(this->sock, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val));
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    53
#endif
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    54
	}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    55
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    56
	DEBUG(net, 1, "[udp] listening on port %s:%d", inet_ntoa(*(struct in_addr *)&host), port);
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    57
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    58
	return true;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    59
}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    60
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    61
/**
5772
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    62
 * Close the given UDP socket
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    63
 */
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
    64
void NetworkUDPSocketHandler::Close()
5772
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    65
{
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    66
	if (!this->IsConnected()) return;
5772
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    67
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    68
	closesocket(this->sock);
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    69
	this->sock = INVALID_SOCKET;
5772
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    70
}
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    71
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    72
NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection()
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    73
{
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    74
	this->has_quit = true;
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    75
	return NETWORK_RECV_STATUS_OKAY;
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    76
}
5772
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    77
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    78
/**
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    79
 * Send a packet over UDP
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    80
 * @param p    the packet to send
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    81
 * @param recv the receiver (target) of the packet
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    82
 */
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
    83
void NetworkUDPSocketHandler::SendPacket(Packet *p, const struct sockaddr_in *recv)
5772
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    84
{
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    85
	int res;
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    86
6149
44aedfd50791 (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 6121
diff changeset
    87
	p->PrepareToSend();
5772
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    88
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    89
	/* Send the buffer */
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
    90
	res = sendto(this->sock, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv));
5772
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    91
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    92
	/* Check for any errors, but ignore it otherwise */
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    93
	if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR());
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    94
}
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    95
fbb42af982b4 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5770
diff changeset
    96
/**
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    97
 * Receive a packet at UDP level
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
    98
 */
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
    99
void NetworkUDPSocketHandler::ReceivePackets()
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   100
{
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   101
	struct sockaddr_in client_addr;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   102
	socklen_t client_len;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   103
	int nbytes;
6149
44aedfd50791 (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 6121
diff changeset
   104
	Packet p(this);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   105
	int packet_len;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   106
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
   107
	if (!this->IsConnected()) return;
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   108
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   109
	packet_len = sizeof(p.buffer);
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   110
	client_len = sizeof(client_addr);
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   111
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   112
	/* Try to receive anything */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6447
diff changeset
   113
	SetNonBlocking(this->sock); // Some OSes seem to loose the non-blocking status of the socket
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
   114
	nbytes = recvfrom(this->sock, (char*)p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   115
5823
7cd91e6e5c71 (svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
rubidium
parents: 5803
diff changeset
   116
	/* We got some bytes for the base header of the packet. */
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   117
	if (nbytes > 2) {
6149
44aedfd50791 (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 6121
diff changeset
   118
		p.PrepareToRead();
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   119
5823
7cd91e6e5c71 (svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
rubidium
parents: 5803
diff changeset
   120
		/* If the size does not match the packet must be corrupted.
7cd91e6e5c71 (svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
rubidium
parents: 5803
diff changeset
   121
		 * Otherwise it will be marked as corrupted later on. */
7cd91e6e5c71 (svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
rubidium
parents: 5803
diff changeset
   122
		if (nbytes != p.size) {
7cd91e6e5c71 (svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
rubidium
parents: 5803
diff changeset
   123
			DEBUG(net, 1, "received a packet with mismatching size from %s:%d",
7cd91e6e5c71 (svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
rubidium
parents: 5803
diff changeset
   124
					inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
7cd91e6e5c71 (svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
rubidium
parents: 5803
diff changeset
   125
7cd91e6e5c71 (svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
rubidium
parents: 5803
diff changeset
   126
			return;
7cd91e6e5c71 (svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
rubidium
parents: 5803
diff changeset
   127
		}
7cd91e6e5c71 (svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
rubidium
parents: 5803
diff changeset
   128
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   129
		/* Handle the packet */
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   130
		this->HandleUDPPacket(&p, &client_addr);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   131
	}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   132
}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   133
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   134
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   135
/**
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   136
 * Serializes the NetworkGameInfo struct to the packet
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   137
 * @param p    the packet to write the data to
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   138
 * @param info the NetworkGameInfo struct to serialize
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   139
 */
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   140
void NetworkUDPSocketHandler::Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info)
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   141
{
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   142
	p->Send_uint8 (NETWORK_GAME_INFO_VERSION);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   143
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   144
	/*
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   145
	 *              Please observe the order.
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   146
	 * The parts must be read in the same order as they are sent!
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   147
	 */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   148
5796
61f3b59a00e3 (svn r7888) -Change: add documentation about the 'on-the-wire' structure of the network game information request and reply packets.
rubidium
parents: 5782
diff changeset
   149
	/* Update the documentation in udp.h on changes
61f3b59a00e3 (svn r7888) -Change: add documentation about the 'on-the-wire' structure of the network game information request and reply packets.
rubidium
parents: 5782
diff changeset
   150
	 * to the NetworkGameInfo wire-protocol! */
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   151
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   152
	/* NETWORK_GAME_INFO_VERSION = 4 */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   153
	{
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   154
		/* Only send the GRF Identification (GRF_ID and MD5 checksum) of
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   155
		 * the GRFs that are needed, i.e. the ones that the server has
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   156
		 * selected in the NewGRF GUI and not the ones that are used due
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   157
		 * to the fact that they are in [newgrf-static] in openttd.cfg */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   158
		const GRFConfig *c;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   159
		uint count = 0;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   160
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   161
		/* Count number of GRFs to send information about */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   162
		for (c = info->grfconfig; c != NULL; c = c->next) {
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   163
			if (!HasBit(c->flags, GCF_STATIC)) count++;
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   164
		}
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   165
		p->Send_uint8 (count); // Send number of GRFs
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   166
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   167
		/* Send actual GRF Identifications */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   168
		for (c = info->grfconfig; c != NULL; c = c->next) {
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   169
			if (!HasBit(c->flags, GCF_STATIC)) this->Send_GRFIdentifier(p, c);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   170
		}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   171
	}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   172
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   173
	/* NETWORK_GAME_INFO_VERSION = 3 */
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   174
	p->Send_uint32(info->game_date);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   175
	p->Send_uint32(info->start_date);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   176
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   177
	/* NETWORK_GAME_INFO_VERSION = 2 */
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   178
	p->Send_uint8 (info->companies_max);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   179
	p->Send_uint8 (info->companies_on);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   180
	p->Send_uint8 (info->spectators_max);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   181
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   182
	/* NETWORK_GAME_INFO_VERSION = 1 */
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   183
	p->Send_string(info->server_name);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   184
	p->Send_string(info->server_revision);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   185
	p->Send_uint8 (info->server_lang);
6169
31cdbb4845f1 (svn r8546) -Codechange: add a seperate (wrapper) functions to send/receive booleans.
rubidium
parents: 6151
diff changeset
   186
	p->Send_bool  (info->use_password);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   187
	p->Send_uint8 (info->clients_max);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   188
	p->Send_uint8 (info->clients_on);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   189
	p->Send_uint8 (info->spectators_on);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   190
	p->Send_string(info->map_name);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   191
	p->Send_uint16(info->map_width);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   192
	p->Send_uint16(info->map_height);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   193
	p->Send_uint8 (info->map_set);
6169
31cdbb4845f1 (svn r8546) -Codechange: add a seperate (wrapper) functions to send/receive booleans.
rubidium
parents: 6151
diff changeset
   194
	p->Send_bool  (info->dedicated);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   195
}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   196
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   197
/**
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   198
 * Deserializes the NetworkGameInfo struct from the packet
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   199
 * @param p    the packet to read the data from
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   200
 * @param info the NetworkGameInfo to deserialize into
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   201
 */
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   202
void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *info)
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   203
{
6050
77bd608748c8 (svn r8361) -Codechange: make sure the range of the dates coming from the network are valid in OpenTTD
rubidium
parents: 6016
diff changeset
   204
	static const Date MAX_DATE = ConvertYMDToDate(MAX_YEAR, 11, 31); // December is month 11
77bd608748c8 (svn r8361) -Codechange: make sure the range of the dates coming from the network are valid in OpenTTD
rubidium
parents: 6016
diff changeset
   205
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   206
	info->game_info_version = p->Recv_uint8();
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   207
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   208
	/*
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   209
	 *              Please observe the order.
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   210
	 * The parts must be read in the same order as they are sent!
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   211
	 */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   212
5796
61f3b59a00e3 (svn r7888) -Change: add documentation about the 'on-the-wire' structure of the network game information request and reply packets.
rubidium
parents: 5782
diff changeset
   213
	/* Update the documentation in udp.h on changes
61f3b59a00e3 (svn r7888) -Change: add documentation about the 'on-the-wire' structure of the network game information request and reply packets.
rubidium
parents: 5782
diff changeset
   214
	 * to the NetworkGameInfo wire-protocol! */
61f3b59a00e3 (svn r7888) -Change: add documentation about the 'on-the-wire' structure of the network game information request and reply packets.
rubidium
parents: 5782
diff changeset
   215
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   216
	switch (info->game_info_version) {
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   217
		case 4: {
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
   218
			GRFConfig **dst = &info->grfconfig;
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   219
			uint i;
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   220
			uint num_grfs = p->Recv_uint8();
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   221
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   222
			for (i = 0; i < num_grfs; i++) {
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
   223
				GRFConfig *c = CallocT<GRFConfig>(1);
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   224
				this->Recv_GRFIdentifier(p, c);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   225
				this->HandleIncomingNetworkGameInfoGRFConfig(c);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   226
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   227
				/* Append GRFConfig to the list */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   228
				*dst = c;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   229
				dst = &c->next;
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   230
			}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   231
		} /* Fallthrough */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   232
		case 3:
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   233
			info->game_date      = Clamp(p->Recv_uint32(), 0, MAX_DATE);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   234
			info->start_date     = Clamp(p->Recv_uint32(), 0, MAX_DATE);
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   235
			/* Fallthrough */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   236
		case 2:
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   237
			info->companies_max  = p->Recv_uint8 ();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   238
			info->companies_on   = p->Recv_uint8 ();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   239
			info->spectators_max = p->Recv_uint8 ();
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   240
			/* Fallthrough */
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   241
		case 1:
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   242
			p->Recv_string(info->server_name,     sizeof(info->server_name));
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   243
			p->Recv_string(info->server_revision, sizeof(info->server_revision));
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   244
			info->server_lang    = p->Recv_uint8 ();
6169
31cdbb4845f1 (svn r8546) -Codechange: add a seperate (wrapper) functions to send/receive booleans.
rubidium
parents: 6151
diff changeset
   245
			info->use_password   = p->Recv_bool  ();
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   246
			info->clients_max    = p->Recv_uint8 ();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   247
			info->clients_on     = p->Recv_uint8 ();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   248
			info->spectators_on  = p->Recv_uint8 ();
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   249
			if (info->game_info_version < 3) { // 16 bits dates got scrapped and are read earlier
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   250
				info->game_date    = p->Recv_uint16() + DAYS_TILL_ORIGINAL_BASE_YEAR;
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   251
				info->start_date   = p->Recv_uint16() + DAYS_TILL_ORIGINAL_BASE_YEAR;
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   252
			}
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   253
			p->Recv_string(info->map_name, sizeof(info->map_name));
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   254
			info->map_width      = p->Recv_uint16();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   255
			info->map_height     = p->Recv_uint16();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   256
			info->map_set        = p->Recv_uint8 ();
6169
31cdbb4845f1 (svn r8546) -Codechange: add a seperate (wrapper) functions to send/receive booleans.
rubidium
parents: 6151
diff changeset
   257
			info->dedicated      = p->Recv_bool  ();
6050
77bd608748c8 (svn r8361) -Codechange: make sure the range of the dates coming from the network are valid in OpenTTD
rubidium
parents: 6016
diff changeset
   258
6061
e1e35dd62309 (svn r8372) -Fix (8361): NUM_LANDSCAPE comes (via some detour) from openttd.h, which does not exist in the masterserver/updater.
rubidium
parents: 6050
diff changeset
   259
			if (info->server_lang >= NETWORK_NUM_LANGUAGES)  info->server_lang = 0;
e1e35dd62309 (svn r8372) -Fix (8361): NUM_LANDSCAPE comes (via some detour) from openttd.h, which does not exist in the masterserver/updater.
rubidium
parents: 6050
diff changeset
   260
			if (info->map_set     >= NETWORK_NUM_LANDSCAPES) info->map_set     = 0;
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   261
	}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   262
}
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   263
6115
c5bde03ec914 (svn r8445) -Cleanup: remove some @params from comments as the parameters did not exist anymore and add comments to several variables/functions.
rubidium
parents: 6061
diff changeset
   264
/**
c5bde03ec914 (svn r8445) -Cleanup: remove some @params from comments as the parameters did not exist anymore and add comments to several variables/functions.
rubidium
parents: 6061
diff changeset
   265
 * Defines a simple (switch) case for each network packet
c5bde03ec914 (svn r8445) -Cleanup: remove some @params from comments as the parameters did not exist anymore and add comments to several variables/functions.
rubidium
parents: 6061
diff changeset
   266
 * @param type the packet type to create the case for
c5bde03ec914 (svn r8445) -Cleanup: remove some @params from comments as the parameters did not exist anymore and add comments to several variables/functions.
rubidium
parents: 6061
diff changeset
   267
 */
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   268
#define UDP_COMMAND(type) case type: this->NetworkPacketReceive_ ## type ## _command(p, client_addr); break;
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   269
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   270
/**
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   271
 * Handle an incoming packets by sending it to the correct function.
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   272
 * @param p the received packet
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   273
 * @param client_addr the sender of the packet
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   274
 */
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   275
void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_in *client_addr)
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   276
{
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   277
	PacketUDPType type;
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   278
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
   279
	/* New packet == new client, which has not quit yet */
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
   280
	this->has_quit = false;
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   281
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   282
	type = (PacketUDPType)p->Recv_uint8();
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   283
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
   284
	switch (this->HasClientQuit() ? PACKET_UDP_END : type) {
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   285
		UDP_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   286
		UDP_COMMAND(PACKET_UDP_SERVER_RESPONSE);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   287
		UDP_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   288
		UDP_COMMAND(PACKET_UDP_SERVER_DETAIL_INFO);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   289
		UDP_COMMAND(PACKET_UDP_SERVER_REGISTER);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   290
		UDP_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   291
		UDP_COMMAND(PACKET_UDP_CLIENT_GET_LIST);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   292
		UDP_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   293
		UDP_COMMAND(PACKET_UDP_SERVER_UNREGISTER);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   294
		UDP_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   295
		UDP_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   296
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   297
		default:
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
   298
			if (this->HasClientQuit()) {
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   299
				DEBUG(net, 0, "[udp] received invalid packet type %d from %s:%d", type,  inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   300
			} else {
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   301
				DEBUG(net, 0, "[udp] received illegal packet from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   302
			}
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   303
			break;
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   304
	}
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   305
}
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   306
6115
c5bde03ec914 (svn r8445) -Cleanup: remove some @params from comments as the parameters did not exist anymore and add comments to several variables/functions.
rubidium
parents: 6061
diff changeset
   307
/**
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   308
 * Create stub implementations for all receive commands that only
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   309
 * show a warning that the given command is not available for the
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   310
 * socket where the packet came from.
6115
c5bde03ec914 (svn r8445) -Cleanup: remove some @params from comments as the parameters did not exist anymore and add comments to several variables/functions.
rubidium
parents: 6061
diff changeset
   311
 * @param type the packet type to create the stub for
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   312
 */
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   313
#define DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(type) \
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   314
void NetworkUDPSocketHandler::NetworkPacketReceive_## type ##_command(\
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   315
		Packet *p, const struct sockaddr_in *client_addr) { \
5875
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
   316
	DEBUG(net, 0, "[udp] received packet type %d on wrong port from %s:%d", \
4a1391019791 (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5870
diff changeset
   317
			type, inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); \
5870
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   318
}
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   319
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   320
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   321
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   322
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   323
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_DETAIL_INFO);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   324
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_REGISTER);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   325
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   326
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_LIST);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   327
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   328
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_UNREGISTER);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   329
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   330
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
86e55167c9e3 (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5860
diff changeset
   331
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
diff changeset
   332
#endif /* ENABLE_NETWORK */