src/network/core/udp.cpp
author rubidium
Tue, 22 Jan 2008 21:00:30 +0000
branchNewGRF_ports
changeset 6872 1c4a4a609f85
parent 6871 5a9dc001e1ad
child 10142 56ee7da4ad56
permissions -rw-r--r--
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
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"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    11
#include "../../core/bitmath_func.hpp"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    12
#include "../../core/math_func.hpp"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    13
#include "../../core/alloc_func.hpp"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    14
#include "../../date_func.h"
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
    15
#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
    16
#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
    17
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
/**
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
 * 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
    20
 * @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
    21
 * @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
    22
 * @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
    23
 * @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
    24
 */
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
    25
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
    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
	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
    28
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
	/* 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
    30
	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
    31
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
    32
	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
    33
	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
    34
		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
    35
		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
    36
	}
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
6226
993f5aa4c6cb (svn r8673) -Codechange: use SetNonBlocking instead of implementing yet another version.
rubidium
parents: 6169
diff changeset
    38
	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
    39
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_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
    41
	/* 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
    42
	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
    43
	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
    44
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
    45
	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
    46
		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
    47
		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
    48
	}
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
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
	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
    51
		/* 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
    52
		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
    53
#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
    54
		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
    55
#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
    56
	}
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
	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
    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
	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
    61
}
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
    62
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
    63
/**
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
    64
 * 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
    65
 */
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
    66
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
    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
	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
    69
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
    70
	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
    71
	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
    72
}
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
    73
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
    74
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
    75
{
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
	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
    77
	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
    78
}
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
    79
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
/**
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
 * 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
    82
 * @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
    83
 * @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
    84
 */
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
    85
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
    86
{
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
    87
	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
    88
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
    89
	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
    90
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
	/* 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
    92
	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
    93
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
	/* 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
    95
	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
    96
}
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
    97
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
    98
/**
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
    99
 * 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
   100
 */
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
   101
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
   102
{
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
	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
   104
	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
   105
	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
   106
	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
   107
	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
   108
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
   109
	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
   110
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
   111
	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
   112
	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
   113
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
   114
	/* Try to receive anything */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6447
diff changeset
   115
	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
   116
	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
   117
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
   118
	/* 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
   119
	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
   120
		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
   121
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
   122
		/* 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
   123
		 * 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
   124
		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
   125
			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
   126
					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
   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
			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
   129
		}
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
   130
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
		/* 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
   132
		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
   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
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
/**
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
 * 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
   139
 * @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
   140
 * @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
   141
 */
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
   142
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
   143
{
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   144
	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
   145
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
	/*
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
	 *              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
   148
	 * 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
   149
	 */
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
   150
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
   151
	/* 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
   152
	 * 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
   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
	/* 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
   155
	{
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
		/* 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
   157
		 * 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
   158
		 * 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
   159
		 * 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
   160
		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
   161
		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
   162
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
   163
		/* 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
   164
		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
   165
			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
   166
		}
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   167
		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
   168
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
   169
		/* 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
   170
		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
   171
			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
   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
	}
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
   174
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
   175
	/* 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
   176
	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
   177
	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
   178
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
   179
	/* 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
   180
	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
   181
	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
   182
	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
   183
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
   184
	/* 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
   185
	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
   186
	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
   187
	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
   188
	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
   189
	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
   190
	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
   191
	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
   192
	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
   193
	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
   194
	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
   195
	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
   196
	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
   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
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
/**
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
 * 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
   201
 * @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
   202
 * @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
   203
 */
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
   204
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
   205
{
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
   206
	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
   207
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   208
	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
   209
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
	/*
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
	 *              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
   212
	 * 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
   213
	 */
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
   214
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
   215
	/* 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
   216
	 * 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
   217
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
   218
	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
   219
		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
   220
			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
   221
			uint i;
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   222
			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
   223
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
   224
			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
   225
				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
   226
				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
   227
				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
   228
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
				/* 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
   230
				*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
   231
				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
   232
			}
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
   233
		} /* 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
   234
		case 3:
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   235
			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
   236
			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
   237
			/* 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
   238
		case 2:
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   239
			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
   240
			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
   241
			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
   242
			/* 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
   243
		case 1:
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   244
			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
   245
			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
   246
			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
   247
			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
   248
			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
   249
			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
   250
			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
   251
			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
   252
				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
   253
				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
   254
			}
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   255
			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
   256
			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
   257
			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
   258
			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
   259
			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
   260
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
   261
			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
   262
			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
   263
	}
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
   264
}
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
   265
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
   266
/**
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
 * 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
   268
 * @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
   269
 */
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
   270
#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
   271
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
/**
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
 * 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
   274
 * @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
   275
 * @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
   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
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
   278
{
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
   279
	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
   280
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
   281
	/* 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
   282
	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
   283
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   284
	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
   285
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
   286
	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
   287
		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
   288
		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
   289
		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
   290
		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
   291
		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
   292
		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
   293
		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
   294
		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
   295
		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
   296
		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
   297
		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
   298
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
		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
   300
			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
   301
				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
   302
			} 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
   303
				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
   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
			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
   306
	}
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
   307
}
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
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
   309
/**
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
   310
 * 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
   311
 * 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
   312
 * 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
   313
 * @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
   314
 */
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
#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
   316
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
   317
		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
   318
	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
   319
			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
   320
}
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
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_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
   323
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
   324
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
   325
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
   326
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
   327
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
   328
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
   329
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
   330
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
   331
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
   332
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
   333
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
   334
#endif /* ENABLE_NETWORK */