2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2006-10-12 14:13:39 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "stdafx.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2005-02-06 13:41:02 +00:00
|
|
|
#include "string.h"
|
2005-02-06 08:18:00 +00:00
|
|
|
#include "strings.h"
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "network_data.h"
|
2006-08-14 14:21:15 +00:00
|
|
|
#include "date.h"
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "table/strings.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "network_client.h"
|
|
|
|
#include "network_gamelist.h"
|
2006-09-01 13:35:43 +00:00
|
|
|
#include "network_gui.h"
|
2005-07-22 06:31:31 +00:00
|
|
|
#include "saveload.h"
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "command.h"
|
|
|
|
#include "window.h"
|
2005-01-15 20:09:16 +00:00
|
|
|
#include "console.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "variables.h"
|
2005-11-22 15:55:38 +00:00
|
|
|
#include "ai/ai.h"
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
// This file handles all the client-commands
|
|
|
|
|
|
|
|
|
|
|
|
// So we don't make too much typos ;)
|
|
|
|
#define MY_CLIENT DEREF_CLIENT(0)
|
|
|
|
|
|
|
|
static uint32 last_ack_frame;
|
|
|
|
|
|
|
|
// **********
|
|
|
|
// Sending functions
|
|
|
|
// DEF_CLIENT_SEND_COMMAND has no parameters
|
|
|
|
// **********
|
|
|
|
|
|
|
|
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: CLIENT_COMPANY_INFO
|
|
|
|
// Function: Request company-info (in detail)
|
|
|
|
// Data:
|
|
|
|
// <none>
|
|
|
|
//
|
|
|
|
Packet *p;
|
|
|
|
_network_join_status = NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO;
|
|
|
|
InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
|
|
|
p = NetworkSend_Init(PACKET_CLIENT_COMPANY_INFO);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_JOIN)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: CLIENT_JOIN
|
|
|
|
// Function: Try to join the server
|
|
|
|
// Data:
|
|
|
|
// String: OpenTTD Revision (norev000 if no revision)
|
|
|
|
// String: Player Name (max NETWORK_NAME_LENGTH)
|
|
|
|
// uint8: Play as Player id (1..MAX_PLAYERS)
|
|
|
|
// uint8: Language ID
|
2004-12-12 16:04:32 +00:00
|
|
|
// String: Unique id to find the player back in server-listing
|
2004-12-04 17:54:56 +00:00
|
|
|
//
|
|
|
|
|
2006-10-13 15:52:22 +00:00
|
|
|
extern const char _openttd_revision[];
|
2004-12-04 17:54:56 +00:00
|
|
|
Packet *p;
|
|
|
|
_network_join_status = NETWORK_JOIN_STATUS_AUTHORIZING;
|
|
|
|
InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
|
|
|
p = NetworkSend_Init(PACKET_CLIENT_JOIN);
|
|
|
|
NetworkSend_string(p, _openttd_revision);
|
|
|
|
NetworkSend_string(p, _network_player_name); // Player name
|
2004-12-15 15:19:51 +00:00
|
|
|
NetworkSend_uint8(p, _network_playas); // PlayAs
|
2004-12-04 17:54:56 +00:00
|
|
|
NetworkSend_uint8(p, NETLANG_ANY); // Language
|
2004-12-12 16:04:32 +00:00
|
|
|
NetworkSend_string(p, _network_unique_id);
|
2004-12-04 17:54:56 +00:00
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_PASSWORD)(NetworkPasswordType type, const char *password)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: CLIENT_PASSWORD
|
|
|
|
// Function: Send a password to the server to authorize
|
|
|
|
// Data:
|
|
|
|
// uint8: NetworkPasswordType
|
|
|
|
// String: Password
|
|
|
|
//
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_PASSWORD);
|
|
|
|
NetworkSend_uint8(p, type);
|
|
|
|
NetworkSend_string(p, password);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_GETMAP)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: CLIENT_GETMAP
|
|
|
|
// Function: Request the map from the server
|
|
|
|
// Data:
|
|
|
|
// <none>
|
|
|
|
//
|
|
|
|
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_GETMAP);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_MAP_OK)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: CLIENT_MAP_OK
|
|
|
|
// Function: Tell the server that we are done receiving/loading the map
|
|
|
|
// Data:
|
|
|
|
// <none>
|
|
|
|
//
|
|
|
|
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_MAP_OK);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_ACK)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: CLIENT_ACK
|
|
|
|
// Function: Tell the server we are done with this frame
|
|
|
|
// Data:
|
|
|
|
// uint32: current FrameCounter of the client
|
|
|
|
//
|
|
|
|
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_ACK);
|
|
|
|
|
|
|
|
NetworkSend_uint32(p, _frame_counter);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a command packet to the server
|
|
|
|
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_COMMAND)(CommandPacket *cp)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: CLIENT_COMMAND
|
|
|
|
// Function: Send a DoCommand to the Server
|
|
|
|
// Data:
|
|
|
|
// uint8: PlayerID (0..MAX_PLAYERS-1)
|
|
|
|
// uint32: CommandID (see command.h)
|
|
|
|
// uint32: P1 (free variables used in DoCommand)
|
|
|
|
// uint32: P2
|
|
|
|
// uint32: Tile
|
2005-05-15 18:50:55 +00:00
|
|
|
// string: text
|
2004-12-04 17:54:56 +00:00
|
|
|
// uint8: CallBackID (see callback_table.c)
|
|
|
|
//
|
|
|
|
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_COMMAND);
|
|
|
|
|
|
|
|
NetworkSend_uint8(p, cp->player);
|
|
|
|
NetworkSend_uint32(p, cp->cmd);
|
|
|
|
NetworkSend_uint32(p, cp->p1);
|
|
|
|
NetworkSend_uint32(p, cp->p2);
|
|
|
|
NetworkSend_uint32(p, (uint32)cp->tile);
|
2005-05-15 18:50:55 +00:00
|
|
|
NetworkSend_string(p, cp->text);
|
2004-12-04 17:54:56 +00:00
|
|
|
NetworkSend_uint8(p, cp->callback);
|
|
|
|
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a chat-packet over the network
|
|
|
|
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_CHAT)(NetworkAction action, DestType desttype, int dest, const char *msg)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: CLIENT_CHAT
|
|
|
|
// Function: Send a chat-packet to the serve
|
|
|
|
// Data:
|
|
|
|
// uint8: ActionID (see network_data.h, NetworkAction)
|
|
|
|
// uint8: Destination Type (see network_data.h, DestType);
|
|
|
|
// uint8: Destination Player (1..MAX_PLAYERS)
|
|
|
|
// String: Message (max MAX_TEXT_MSG_LEN)
|
|
|
|
//
|
|
|
|
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_CHAT);
|
|
|
|
|
|
|
|
NetworkSend_uint8(p, action);
|
|
|
|
NetworkSend_uint8(p, desttype);
|
|
|
|
NetworkSend_uint8(p, dest);
|
|
|
|
NetworkSend_string(p, msg);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an error-packet over the network
|
|
|
|
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_ERROR)(NetworkErrorCode errorno)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: CLIENT_ERROR
|
|
|
|
// Function: The client made an error and is quiting the game
|
|
|
|
// Data:
|
|
|
|
// uint8: ErrorID (see network_data.h, NetworkErrorCode)
|
|
|
|
//
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_ERROR);
|
|
|
|
|
|
|
|
NetworkSend_uint8(p, errorno);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_PASSWORD)(const char *password)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: PACKET_CLIENT_SET_PASSWORD
|
|
|
|
// Function: Set the password for the clients current company
|
|
|
|
// Data:
|
|
|
|
// String: Password
|
|
|
|
//
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_SET_PASSWORD);
|
|
|
|
|
|
|
|
NetworkSend_string(p, password);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_NAME)(const char *name)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: PACKET_CLIENT_SET_NAME
|
|
|
|
// Function: Gives the player a new name
|
|
|
|
// Data:
|
|
|
|
// String: Name
|
|
|
|
//
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_SET_NAME);
|
|
|
|
|
|
|
|
NetworkSend_string(p, name);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an quit-packet over the network
|
|
|
|
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_QUIT)(const char *leavemsg)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Packet: CLIENT_QUIT
|
|
|
|
// Function: The client is quiting the game
|
|
|
|
// Data:
|
|
|
|
// String: leave-message
|
|
|
|
//
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_QUIT);
|
|
|
|
|
|
|
|
NetworkSend_string(p, leavemsg);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
2005-01-15 20:09:16 +00:00
|
|
|
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_RCON)(const char *pass, const char *command)
|
|
|
|
{
|
|
|
|
Packet *p = NetworkSend_Init(PACKET_CLIENT_RCON);
|
|
|
|
NetworkSend_string(p, pass);
|
|
|
|
NetworkSend_string(p, command);
|
|
|
|
NetworkSend_Packet(p, MY_CLIENT);
|
|
|
|
}
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
// **********
|
|
|
|
// Receiving functions
|
|
|
|
// DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
|
|
|
|
// **********
|
|
|
|
|
|
|
|
extern bool SafeSaveOrLoad(const char *filename, int mode, int newgm);
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_FULL)
|
|
|
|
{
|
|
|
|
// We try to join a server which is full
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_FULL;
|
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_SERVER_FULL;
|
|
|
|
}
|
|
|
|
|
2005-01-02 12:03:43 +00:00
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_BANNED)
|
|
|
|
{
|
|
|
|
// We try to join a server where we are banned
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_BANNED;
|
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_SERVER_BANNED;
|
|
|
|
}
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO)
|
|
|
|
{
|
|
|
|
byte company_info_version;
|
|
|
|
int i;
|
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
company_info_version = NetworkRecv_uint8(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2005-01-14 21:59:31 +00:00
|
|
|
if (!MY_CLIENT->quited && company_info_version == NETWORK_COMPANY_INFO_VERSION) {
|
2004-12-04 17:54:56 +00:00
|
|
|
byte total;
|
|
|
|
byte current;
|
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
total = NetworkRecv_uint8(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
// There is no data at all..
|
|
|
|
if (total == 0)
|
|
|
|
return NETWORK_RECV_STATUS_CLOSE_QUERY;
|
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
current = NetworkRecv_uint8(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
if (current >= MAX_PLAYERS)
|
|
|
|
return NETWORK_RECV_STATUS_CLOSE_QUERY;
|
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
NetworkRecv_string(MY_CLIENT, p, _network_player_info[current].company_name, sizeof(_network_player_info[current].company_name));
|
2006-08-20 18:40:57 +00:00
|
|
|
_network_player_info[current].inaugurated_year = NetworkRecv_uint32(MY_CLIENT, p);
|
2005-01-05 14:39:48 +00:00
|
|
|
_network_player_info[current].company_value = NetworkRecv_uint64(MY_CLIENT, p);
|
|
|
|
_network_player_info[current].money = NetworkRecv_uint64(MY_CLIENT, p);
|
|
|
|
_network_player_info[current].income = NetworkRecv_uint64(MY_CLIENT, p);
|
|
|
|
_network_player_info[current].performance = NetworkRecv_uint16(MY_CLIENT, p);
|
2005-01-14 21:47:35 +00:00
|
|
|
_network_player_info[current].use_password = NetworkRecv_uint8(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
for (i = 0; i < NETWORK_VEHICLE_TYPES; i++)
|
2005-01-05 14:39:48 +00:00
|
|
|
_network_player_info[current].num_vehicle[i] = NetworkRecv_uint16(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
for (i = 0; i < NETWORK_STATION_TYPES; i++)
|
2005-01-05 14:39:48 +00:00
|
|
|
_network_player_info[current].num_station[i] = NetworkRecv_uint16(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
NetworkRecv_string(MY_CLIENT, p, _network_player_info[current].players, sizeof(_network_player_info[current].players));
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
InvalidateWindow(WC_NETWORK_WINDOW, 0);
|
|
|
|
|
2004-12-20 16:02:01 +00:00
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_CLOSE_QUERY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This packet contains info about the client (playas and name)
|
|
|
|
// as client we save this in NetworkClientInfo, linked via 'index'
|
|
|
|
// which is always an unique number on a server.
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO)
|
|
|
|
{
|
|
|
|
NetworkClientInfo *ci;
|
2005-01-05 14:39:48 +00:00
|
|
|
uint16 index = NetworkRecv_uint16(MY_CLIENT, p);
|
2006-04-29 14:31:16 +00:00
|
|
|
PlayerID playas = NetworkRecv_uint8(MY_CLIENT, p);
|
2004-12-15 15:19:51 +00:00
|
|
|
char name[NETWORK_NAME_LENGTH];
|
|
|
|
char unique_id[NETWORK_NAME_LENGTH];
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
NetworkRecv_string(MY_CLIENT, p, name, sizeof(name));
|
|
|
|
NetworkRecv_string(MY_CLIENT, p, unique_id, sizeof(unique_id));
|
|
|
|
|
|
|
|
if (MY_CLIENT->quited)
|
|
|
|
return NETWORK_RECV_STATUS_CONN_LOST;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2004-12-15 15:19:51 +00:00
|
|
|
/* Do we receive a change of data? Most likely we changed playas */
|
2005-11-22 15:55:38 +00:00
|
|
|
if (index == _network_own_client_index) {
|
2004-12-15 15:19:51 +00:00
|
|
|
_network_playas = playas;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2005-11-22 15:55:38 +00:00
|
|
|
}
|
|
|
|
|
2004-12-15 15:19:51 +00:00
|
|
|
ci = NetworkFindClientInfoFromIndex(index);
|
|
|
|
if (ci != NULL) {
|
2004-12-04 17:54:56 +00:00
|
|
|
if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
|
|
|
|
// Client name changed, display the change
|
2005-08-28 12:24:57 +00:00
|
|
|
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, 1, false, ci->client_name, "%s", name);
|
2004-12-04 17:54:56 +00:00
|
|
|
} else if (playas != ci->client_playas) {
|
|
|
|
// The player changed from client-player..
|
|
|
|
// Do not display that for now
|
|
|
|
}
|
|
|
|
|
|
|
|
ci->client_playas = playas;
|
|
|
|
ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name));
|
2004-12-15 15:19:51 +00:00
|
|
|
|
2004-12-19 15:14:55 +00:00
|
|
|
InvalidateWindow(WC_CLIENT_LIST, 0);
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have this index yet, find an empty index, and put the data there
|
|
|
|
ci = NetworkFindClientInfoFromIndex(NETWORK_EMPTY_INDEX);
|
|
|
|
if (ci != NULL) {
|
|
|
|
ci->client_index = index;
|
2004-12-15 15:19:51 +00:00
|
|
|
ci->client_playas = playas;
|
|
|
|
|
|
|
|
ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name));
|
|
|
|
ttd_strlcpy(ci->unique_id, unique_id, sizeof(ci->unique_id));
|
|
|
|
|
2004-12-19 15:14:55 +00:00
|
|
|
InvalidateWindow(WC_CLIENT_LIST, 0);
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here the program should never ever come.....
|
|
|
|
return NETWORK_RECV_STATUS_MALFORMED_PACKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR)
|
|
|
|
{
|
2005-01-05 14:39:48 +00:00
|
|
|
NetworkErrorCode error = NetworkRecv_uint8(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2006-01-25 18:11:06 +00:00
|
|
|
switch (error) {
|
|
|
|
/* We made an error in the protocol, and our connection is closed.... */
|
|
|
|
case NETWORK_ERROR_NOT_AUTHORIZED:
|
|
|
|
case NETWORK_ERROR_NOT_EXPECTED:
|
|
|
|
case NETWORK_ERROR_PLAYER_MISMATCH:
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_ERROR;
|
|
|
|
break;
|
|
|
|
case NETWORK_ERROR_FULL:
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_FULL;
|
|
|
|
break;
|
|
|
|
case NETWORK_ERROR_WRONG_REVISION:
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_WRONG_REVISION;
|
|
|
|
break;
|
|
|
|
case NETWORK_ERROR_WRONG_PASSWORD:
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_WRONG_PASSWORD;
|
|
|
|
break;
|
|
|
|
case NETWORK_ERROR_KICKED:
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_KICKED;
|
|
|
|
break;
|
|
|
|
case NETWORK_ERROR_CHEATER:
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_CHEATER;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_LOSTCONNECTION;
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD)
|
|
|
|
{
|
2006-09-01 13:35:43 +00:00
|
|
|
NetworkPasswordType type = NetworkRecv_uint8(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2006-09-01 13:35:43 +00:00
|
|
|
switch (type) {
|
|
|
|
case NETWORK_GAME_PASSWORD:
|
|
|
|
case NETWORK_COMPANY_PASSWORD:
|
|
|
|
ShowNetworkNeedPassword(type);
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2006-09-01 13:35:43 +00:00
|
|
|
default: return NETWORK_RECV_STATUS_MALFORMED_PACKET;
|
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WELCOME)
|
|
|
|
{
|
2005-01-05 14:39:48 +00:00
|
|
|
_network_own_client_index = NetworkRecv_uint16(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
// Start receiving the map
|
|
|
|
SEND_COMMAND(PACKET_CLIENT_GETMAP)();
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WAIT)
|
|
|
|
{
|
|
|
|
_network_join_status = NETWORK_JOIN_STATUS_WAITING;
|
2005-01-05 14:39:48 +00:00
|
|
|
_network_join_waiting = NetworkRecv_uint8(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
|
|
|
// We are put on hold for receiving the map.. we need GUI for this ;)
|
|
|
|
DEBUG(net, 1)("[NET] The server is currently busy sending the map to someone else.. please hold..." );
|
|
|
|
DEBUG(net, 1)("[NET] There are %d clients in front of you", _network_join_waiting);
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP)
|
|
|
|
{
|
|
|
|
static char filename[256];
|
|
|
|
static FILE *file_pointer;
|
|
|
|
|
|
|
|
byte maptype;
|
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
maptype = NetworkRecv_uint8(MY_CLIENT, p);
|
|
|
|
|
|
|
|
if (MY_CLIENT->quited)
|
|
|
|
return NETWORK_RECV_STATUS_CONN_LOST;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
// First packet, init some stuff
|
|
|
|
if (maptype == MAP_PACKET_START) {
|
|
|
|
// The name for the temp-map
|
|
|
|
sprintf(filename, "%s%snetwork_client.tmp", _path.autosave_dir, PATHSEP);
|
|
|
|
|
|
|
|
file_pointer = fopen(filename, "wb");
|
|
|
|
if (file_pointer == NULL) {
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_SAVEGAMEERROR;
|
|
|
|
return NETWORK_RECV_STATUS_SAVEGAME;
|
|
|
|
}
|
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
_frame_counter = _frame_counter_server = _frame_counter_max = NetworkRecv_uint32(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
_network_join_status = NETWORK_JOIN_STATUS_DOWNLOADING;
|
|
|
|
_network_join_kbytes = 0;
|
2005-01-05 14:39:48 +00:00
|
|
|
_network_join_kbytes_total = NetworkRecv_uint32(MY_CLIENT, p) / 1024;
|
2004-12-04 17:54:56 +00:00
|
|
|
InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
|
|
|
// The first packet does not contain any more data
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maptype == MAP_PACKET_NORMAL) {
|
|
|
|
// We are still receiving data, put it to the file
|
|
|
|
fwrite(p->buffer + p->pos, 1, p->size - p->pos, file_pointer);
|
|
|
|
|
|
|
|
_network_join_kbytes = ftell(file_pointer) / 1024;
|
|
|
|
InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this was the last packet
|
|
|
|
if (maptype == MAP_PACKET_END) {
|
|
|
|
fclose(file_pointer);
|
|
|
|
|
|
|
|
_network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
|
|
|
|
InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
|
|
|
// The map is done downloading, load it
|
|
|
|
// Load the map
|
|
|
|
if (!SafeSaveOrLoad(filename, SL_LOAD, GM_NORMAL)) {
|
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_SAVEGAMEERROR;
|
|
|
|
return NETWORK_RECV_STATUS_SAVEGAME;
|
|
|
|
}
|
2005-03-12 21:21:47 +00:00
|
|
|
|
|
|
|
_opt_ptr = &_opt; // during a network game you are always in-game
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
// Say we received the map and loaded it correctly!
|
|
|
|
SEND_COMMAND(PACKET_CLIENT_MAP_OK)();
|
|
|
|
|
2006-10-15 23:48:34 +00:00
|
|
|
// new company/spectator (invalid player) or company we want to join is not active
|
|
|
|
if (_network_playas == PLAYER_NEW_COMPANY || !IsValidPlayer(_network_playas - 1) ||
|
2005-06-21 16:28:17 +00:00
|
|
|
!GetPlayer(_network_playas - 1)->is_active) {
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2006-10-14 15:49:43 +00:00
|
|
|
if (_network_playas == PLAYER_SPECTATOR) {
|
2004-12-04 17:54:56 +00:00
|
|
|
// The client wants to be a spectator..
|
2006-10-14 15:49:43 +00:00
|
|
|
_local_player = PLAYER_SPECTATOR;
|
2004-12-15 21:13:52 +00:00
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
2004-12-04 17:54:56 +00:00
|
|
|
} else {
|
2005-05-12 23:47:45 +00:00
|
|
|
/* We have arrived and ready to start playing; send a command to make a new player;
|
|
|
|
* the server will give us a client-id and let us in */
|
2006-10-14 15:49:43 +00:00
|
|
|
_local_player = PLAYER_SPECTATOR;
|
2006-10-15 23:42:18 +00:00
|
|
|
NetworkSend_Command(0, 0, 0, CMD_PLAYER_CTRL, NULL);
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// take control over an existing company
|
|
|
|
_local_player = _network_playas - 1;
|
2005-08-06 16:07:22 +00:00
|
|
|
_patches.autorenew = GetPlayer(_local_player)->engine_renew;
|
|
|
|
_patches.autorenew_months = GetPlayer(_local_player)->engine_renew_months;
|
|
|
|
_patches.autorenew_money = GetPlayer(_local_player)->engine_renew_money;
|
2004-12-16 11:09:00 +00:00
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_FRAME)
|
|
|
|
{
|
2005-01-05 14:39:48 +00:00
|
|
|
_frame_counter_server = NetworkRecv_uint32(MY_CLIENT, p);
|
|
|
|
_frame_counter_max = NetworkRecv_uint32(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
|
|
|
|
// Test if the server supports this option
|
|
|
|
// and if we are at the frame the server is
|
|
|
|
if (p->pos < p->size) {
|
|
|
|
_sync_frame = _frame_counter_server;
|
2005-01-05 14:39:48 +00:00
|
|
|
_sync_seed_1 = NetworkRecv_uint32(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef NETWORK_SEND_DOUBLE_SEED
|
2005-01-05 14:39:48 +00:00
|
|
|
_sync_seed_2 = NetworkRecv_uint32(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
DEBUG(net, 7)("[NET] Received FRAME %d",_frame_counter_server);
|
|
|
|
|
|
|
|
// Let the server know that we received this frame correctly
|
|
|
|
// We do this only once per day, to save some bandwidth ;)
|
|
|
|
if (!_network_first_time && last_ack_frame < _frame_counter) {
|
|
|
|
last_ack_frame = _frame_counter + DAY_TICKS;
|
|
|
|
DEBUG(net,6)("[NET] Sent ACK at %d", _frame_counter);
|
|
|
|
SEND_COMMAND(PACKET_CLIENT_ACK)();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_SYNC)
|
|
|
|
{
|
2005-01-05 14:39:48 +00:00
|
|
|
_sync_frame = NetworkRecv_uint32(MY_CLIENT, p);
|
|
|
|
_sync_seed_1 = NetworkRecv_uint32(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef NETWORK_SEND_DOUBLE_SEED
|
2005-01-05 14:39:48 +00:00
|
|
|
_sync_seed_2 = NetworkRecv_uint32(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMMAND)
|
|
|
|
{
|
|
|
|
CommandPacket *cp = malloc(sizeof(CommandPacket));
|
2005-01-05 14:39:48 +00:00
|
|
|
cp->player = NetworkRecv_uint8(MY_CLIENT, p);
|
|
|
|
cp->cmd = NetworkRecv_uint32(MY_CLIENT, p);
|
|
|
|
cp->p1 = NetworkRecv_uint32(MY_CLIENT, p);
|
|
|
|
cp->p2 = NetworkRecv_uint32(MY_CLIENT, p);
|
|
|
|
cp->tile = NetworkRecv_uint32(MY_CLIENT, p);
|
2005-05-15 18:50:55 +00:00
|
|
|
NetworkRecv_string(MY_CLIENT, p, cp->text, sizeof(cp->text));
|
2005-01-05 14:39:48 +00:00
|
|
|
cp->callback = NetworkRecv_uint8(MY_CLIENT, p);
|
|
|
|
cp->frame = NetworkRecv_uint32(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
cp->next = NULL;
|
|
|
|
|
|
|
|
// The server did send us this command..
|
|
|
|
// queue it in our own queue, so we can handle it in the upcoming frame!
|
|
|
|
|
|
|
|
if (_local_command_queue == NULL) {
|
|
|
|
_local_command_queue = cp;
|
|
|
|
} else {
|
|
|
|
// Find last packet
|
|
|
|
CommandPacket *c = _local_command_queue;
|
|
|
|
while (c->next != NULL) c = c->next;
|
|
|
|
c->next = cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT)
|
|
|
|
{
|
2005-01-05 14:39:48 +00:00
|
|
|
NetworkAction action = NetworkRecv_uint8(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
char msg[MAX_TEXT_MSG_LEN];
|
2004-12-23 00:24:44 +00:00
|
|
|
NetworkClientInfo *ci = NULL, *ci_to;
|
2004-12-04 17:54:56 +00:00
|
|
|
uint16 index;
|
|
|
|
char name[NETWORK_NAME_LENGTH];
|
2004-12-19 15:14:55 +00:00
|
|
|
bool self_send;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
index = NetworkRecv_uint16(MY_CLIENT, p);
|
|
|
|
self_send = NetworkRecv_uint8(MY_CLIENT, p);
|
|
|
|
NetworkRecv_string(MY_CLIENT, p, msg, MAX_TEXT_MSG_LEN);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
ci_to = NetworkFindClientInfoFromIndex(index);
|
|
|
|
if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
|
2004-12-19 15:14:55 +00:00
|
|
|
/* Do we display the action locally? */
|
|
|
|
if (self_send) {
|
|
|
|
switch (action) {
|
|
|
|
case NETWORK_ACTION_CHAT_CLIENT:
|
|
|
|
/* For speak to client we need the client-name */
|
|
|
|
snprintf(name, sizeof(name), "%s", ci_to->client_name);
|
|
|
|
ci = NetworkFindClientInfoFromIndex(_network_own_client_index);
|
|
|
|
break;
|
|
|
|
case NETWORK_ACTION_CHAT_PLAYER:
|
|
|
|
case NETWORK_ACTION_GIVE_MONEY:
|
|
|
|
/* For speak to player or give money, we need the player-name */
|
2004-12-23 13:53:05 +00:00
|
|
|
if (ci_to->client_playas > MAX_PLAYERS)
|
|
|
|
return NETWORK_RECV_STATUS_OKAY; // This should never happen
|
2005-06-21 16:28:17 +00:00
|
|
|
GetString(name, GetPlayer(ci_to->client_playas-1)->name_1);
|
2004-12-19 15:14:55 +00:00
|
|
|
ci = NetworkFindClientInfoFromIndex(_network_own_client_index);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* This should never happen */
|
|
|
|
NOT_REACHED();
|
|
|
|
break;
|
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
} else {
|
2004-12-19 15:14:55 +00:00
|
|
|
/* Display message from somebody else */
|
2004-12-12 16:04:32 +00:00
|
|
|
snprintf(name, sizeof(name), "%s", ci_to->client_name);
|
2004-12-04 17:54:56 +00:00
|
|
|
ci = ci_to;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ci != NULL)
|
2004-12-19 15:14:55 +00:00
|
|
|
NetworkTextMessage(action, GetDrawStringPlayerColor(ci->client_playas-1), self_send, name, "%s", msg);
|
2004-12-04 17:54:56 +00:00
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT)
|
|
|
|
{
|
2004-12-19 15:14:55 +00:00
|
|
|
char str[100];
|
2004-12-04 17:54:56 +00:00
|
|
|
uint16 index;
|
|
|
|
NetworkClientInfo *ci;
|
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
index = NetworkRecv_uint16(MY_CLIENT, p);
|
2006-04-03 18:31:01 +00:00
|
|
|
GetNetworkErrorMsg(str, NetworkRecv_uint8(MY_CLIENT, p));
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
ci = NetworkFindClientInfoFromIndex(index);
|
|
|
|
if (ci != NULL) {
|
2005-08-28 12:24:57 +00:00
|
|
|
NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, ci->client_name, "%s", str);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
// The client is gone, give the NetworkClientInfo free
|
|
|
|
ci->client_index = NETWORK_EMPTY_INDEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateWindow(WC_CLIENT_LIST, 0);
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_QUIT)
|
|
|
|
{
|
2004-12-19 15:14:55 +00:00
|
|
|
char str[100];
|
2004-12-04 17:54:56 +00:00
|
|
|
uint16 index;
|
|
|
|
NetworkClientInfo *ci;
|
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
index = NetworkRecv_uint16(MY_CLIENT, p);
|
2005-08-28 12:24:57 +00:00
|
|
|
NetworkRecv_string(MY_CLIENT, p, str, lengthof(str));
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
ci = NetworkFindClientInfoFromIndex(index);
|
|
|
|
if (ci != NULL) {
|
2005-08-28 12:24:57 +00:00
|
|
|
NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, ci->client_name, "%s", str);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
// The client is gone, give the NetworkClientInfo free
|
|
|
|
ci->client_index = NETWORK_EMPTY_INDEX;
|
|
|
|
} else {
|
|
|
|
DEBUG(net, 0)("[NET] Error - unknown client (%d) is leaving the game", index);
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateWindow(WC_CLIENT_LIST, 0);
|
|
|
|
|
|
|
|
// If we come here it means we could not locate the client.. strange :s
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_JOIN)
|
|
|
|
{
|
|
|
|
uint16 index;
|
|
|
|
NetworkClientInfo *ci;
|
|
|
|
|
2005-01-05 14:39:48 +00:00
|
|
|
index = NetworkRecv_uint16(MY_CLIENT, p);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
ci = NetworkFindClientInfoFromIndex(index);
|
2004-12-19 15:14:55 +00:00
|
|
|
if (ci != NULL)
|
|
|
|
NetworkTextMessage(NETWORK_ACTION_JOIN, 1, false, ci->client_name, "");
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
InvalidateWindow(WC_CLIENT_LIST, 0);
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN)
|
|
|
|
{
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_SERVER_SHUTDOWN;
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEWGAME)
|
|
|
|
{
|
|
|
|
// To trottle the reconnects a bit, every clients waits
|
|
|
|
// his _local_player value before reconnecting
|
2006-10-14 15:49:43 +00:00
|
|
|
// PLAYER_SPECTATOR is currently 255, so to avoid long wait periods
|
2004-12-04 17:54:56 +00:00
|
|
|
// set the max to 10.
|
|
|
|
_network_reconnect = min(_local_player + 1, 10);
|
|
|
|
_switch_mode_errorstr = STR_NETWORK_SERVER_REBOOT;
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
2005-01-15 20:09:16 +00:00
|
|
|
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_RCON)
|
|
|
|
{
|
|
|
|
char rcon_out[NETWORK_RCONCOMMAND_LENGTH];
|
|
|
|
uint16 color_code;
|
|
|
|
|
|
|
|
color_code = NetworkRecv_uint16(MY_CLIENT, p);
|
|
|
|
NetworkRecv_string(MY_CLIENT, p, rcon_out, sizeof(rcon_out));
|
|
|
|
|
|
|
|
IConsolePrint(color_code, rcon_out);
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The layout for the receive-functions by the client
|
|
|
|
typedef NetworkRecvStatus NetworkClientPacket(Packet *p);
|
|
|
|
|
|
|
|
// This array matches PacketType. At an incoming
|
|
|
|
// packet it is matches against this array
|
|
|
|
// and that way the right function to handle that
|
|
|
|
// packet is found.
|
|
|
|
static NetworkClientPacket* const _network_client_packet[] = {
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_FULL),
|
2005-01-02 12:03:43 +00:00
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_BANNED),
|
2004-12-04 17:54:56 +00:00
|
|
|
NULL, /*PACKET_CLIENT_JOIN,*/
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_ERROR),
|
|
|
|
NULL, /*PACKET_CLIENT_COMPANY_INFO,*/
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO),
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO),
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD),
|
|
|
|
NULL, /*PACKET_CLIENT_PASSWORD,*/
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_WELCOME),
|
|
|
|
NULL, /*PACKET_CLIENT_GETMAP,*/
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_WAIT),
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_MAP),
|
|
|
|
NULL, /*PACKET_CLIENT_MAP_OK,*/
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_JOIN),
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_FRAME),
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_SYNC),
|
|
|
|
NULL, /*PACKET_CLIENT_ACK,*/
|
|
|
|
NULL, /*PACKET_CLIENT_COMMAND,*/
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_COMMAND),
|
|
|
|
NULL, /*PACKET_CLIENT_CHAT,*/
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_CHAT),
|
|
|
|
NULL, /*PACKET_CLIENT_SET_PASSWORD,*/
|
|
|
|
NULL, /*PACKET_CLIENT_SET_NAME,*/
|
|
|
|
NULL, /*PACKET_CLIENT_QUIT,*/
|
|
|
|
NULL, /*PACKET_CLIENT_ERROR,*/
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_QUIT),
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT),
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN),
|
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_NEWGAME),
|
2005-01-15 20:09:16 +00:00
|
|
|
RECEIVE_COMMAND(PACKET_SERVER_RCON),
|
|
|
|
NULL, /*PACKET_CLIENT_RCON,*/
|
2004-12-04 17:54:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// If this fails, check the array above with network_data.h
|
|
|
|
assert_compile(lengthof(_network_client_packet) == PACKET_END);
|
|
|
|
|
|
|
|
// Is called after a client is connected to the server
|
|
|
|
void NetworkClient_Connected(void)
|
|
|
|
{
|
|
|
|
// Set the frame-counter to 0 so nothing happens till we are ready
|
|
|
|
_frame_counter = 0;
|
|
|
|
_frame_counter_server = 0;
|
|
|
|
last_ack_frame = 0;
|
|
|
|
// Request the game-info
|
|
|
|
SEND_COMMAND(PACKET_CLIENT_JOIN)();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads the packets from the socket-stream, if available
|
2004-12-19 10:17:26 +00:00
|
|
|
NetworkRecvStatus NetworkClient_ReadPackets(NetworkClientState *cs)
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
|
|
|
Packet *p;
|
|
|
|
NetworkRecvStatus res = NETWORK_RECV_STATUS_OKAY;
|
|
|
|
|
|
|
|
while (res == NETWORK_RECV_STATUS_OKAY && (p = NetworkRecv_Packet(cs, &res)) != NULL) {
|
2005-01-05 14:39:48 +00:00
|
|
|
byte type = NetworkRecv_uint8(MY_CLIENT, p);
|
|
|
|
if (type < PACKET_END && _network_client_packet[type] != NULL && !MY_CLIENT->quited) {
|
2004-12-04 17:54:56 +00:00
|
|
|
res = _network_client_packet[type](p);
|
2005-07-08 22:25:24 +00:00
|
|
|
} else {
|
2004-12-04 17:54:56 +00:00
|
|
|
res = NETWORK_RECV_STATUS_MALFORMED_PACKET;
|
|
|
|
DEBUG(net, 0)("[NET][client] Received invalid packet type %d", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ENABLE_NETWORK */
|