src/network/network_data.h
author maedhros
Sun, 21 Jan 2007 20:14:35 +0000
changeset 5776 d36a554d7ccd
parent 5624 6afe9d27430a
child 5872 63eafeb3f393
permissions -rw-r--r--
(svn r8328) -Codechange: Remove some magic numbers in the graph drawing code.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1977
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1977
diff changeset
     2
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     3
#ifndef NETWORK_DATA_H
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     4
#define NETWORK_DATA_H
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     5
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     6
// Is the network enabled?
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     7
#ifdef ENABLE_NETWORK
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     8
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5339
diff changeset
     9
#include "../openttd.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5339
diff changeset
    10
#include "network.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5339
diff changeset
    11
#include "core/os_abstraction.h"
5624
6afe9d27430a (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: 5619
diff changeset
    12
#include "core/core.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5339
diff changeset
    13
#include "core/config.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5339
diff changeset
    14
#include "core/packet.h"
5624
6afe9d27430a (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: 5619
diff changeset
    15
#include "core/tcp.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5339
diff changeset
    16
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    17
#define MAX_TEXT_MSG_LEN 1024 /* long long long long sentences :-) */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    18
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    19
// The client-info-server-index is always 1
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    20
#define NETWORK_SERVER_INDEX 1
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    21
#define NETWORK_EMPTY_INDEX 0
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    22
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    23
typedef enum {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    24
	MAP_PACKET_START,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    25
	MAP_PACKET_NORMAL,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    26
	MAP_PACKET_END,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    27
} MapPacket;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    28
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    29
typedef enum {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    30
	NETWORK_ERROR_GENERAL, // Try to use thisone like never
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    31
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    32
	// Signals from clients
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    33
	NETWORK_ERROR_DESYNC,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    34
	NETWORK_ERROR_SAVEGAME_FAILED,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    35
	NETWORK_ERROR_CONNECTION_LOST,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    36
	NETWORK_ERROR_ILLEGAL_PACKET,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    37
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    38
	// Signals from servers
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    39
	NETWORK_ERROR_NOT_AUTHORIZED,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    40
	NETWORK_ERROR_NOT_EXPECTED,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    41
	NETWORK_ERROR_WRONG_REVISION,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    42
	NETWORK_ERROR_NAME_IN_USE,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    43
	NETWORK_ERROR_WRONG_PASSWORD,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    44
	NETWORK_ERROR_PLAYER_MISMATCH, // Happens in CLIENT_COMMAND
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    45
	NETWORK_ERROR_KICKED,
748
fa61195ee8a8 (svn r1204) -Add: [Network] Added some cheaters-protection (money-cheat mostly)
truelight
parents: 722
diff changeset
    46
	NETWORK_ERROR_CHEATER,
2879
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2859
diff changeset
    47
	NETWORK_ERROR_FULL,
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    48
} NetworkErrorCode;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    49
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    50
// Actions that can be used for NetworkTextMessage
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    51
typedef enum {
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
    52
	NETWORK_ACTION_JOIN,
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
    53
	NETWORK_ACTION_LEAVE,
4944
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4912
diff changeset
    54
	NETWORK_ACTION_SERVER_MESSAGE,
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    55
	NETWORK_ACTION_CHAT,
4906
935e6280c093 (svn r6876) -Codechange: Change the naming of _PLAYER in DESTTYPE_PLAYER/NETWORK_ACTION_CHAT_PLAYER
Darkvater
parents: 4887
diff changeset
    56
	NETWORK_ACTION_CHAT_COMPANY,
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    57
	NETWORK_ACTION_CHAT_CLIENT,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    58
	NETWORK_ACTION_GIVE_MONEY,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    59
	NETWORK_ACTION_NAME_CHANGE,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    60
} NetworkAction;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    61
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    62
typedef enum {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    63
	NETWORK_GAME_PASSWORD,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    64
	NETWORK_COMPANY_PASSWORD,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    65
} NetworkPasswordType;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    66
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    67
typedef enum {
4887
71da70810c22 (svn r6824) -Feature: Change the functionality of the chat window. SHIFT+ENTER (SHIFT+T)
Darkvater
parents: 4883
diff changeset
    68
	DESTTYPE_BROADCAST, ///< Send message/notice to all players (All)
4906
935e6280c093 (svn r6876) -Codechange: Change the naming of _PLAYER in DESTTYPE_PLAYER/NETWORK_ACTION_CHAT_PLAYER
Darkvater
parents: 4887
diff changeset
    69
	DESTTYPE_TEAM,    ///< Send message/notice to everyone playing the same company (Team)
4887
71da70810c22 (svn r6824) -Feature: Change the functionality of the chat window. SHIFT+ENTER (SHIFT+T)
Darkvater
parents: 4883
diff changeset
    70
	DESTTYPE_CLIENT,    ///< Send message/notice to only a certain player (Private)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    71
} DestType;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    72
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5475
diff changeset
    73
// following externs are instantiated at network.cpp
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5475
diff changeset
    74
extern CommandPacket *_local_command_queue;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    75
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    76
// Here we keep track of the clients
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    77
//  (and the client uses [0] for his own communication)
5624
6afe9d27430a (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: 5619
diff changeset
    78
extern NetworkTCPSocketHandler _clients[MAX_CLIENTS];
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5475
diff changeset
    79
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    80
#define DEREF_CLIENT(i) (&_clients[i])
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 668
diff changeset
    81
// This returns the NetworkClientInfo from a NetworkClientState
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    82
#define DEREF_CLIENT_INFO(cs) (&_network_client_info[cs - _clients])
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    83
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    84
// Macros to make life a bit more easier
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    85
#define DEF_CLIENT_RECEIVE_COMMAND(type) NetworkRecvStatus NetworkPacketReceive_ ## type ## _command(Packet *p)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    86
#define DEF_CLIENT_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command(void)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    87
#define DEF_CLIENT_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command
5624
6afe9d27430a (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: 5619
diff changeset
    88
#define DEF_SERVER_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(NetworkTCPSocketHandler *cs, Packet *p)
6afe9d27430a (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: 5619
diff changeset
    89
#define DEF_SERVER_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command(NetworkTCPSocketHandler *cs)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    90
#define DEF_SERVER_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    91
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    92
#define SEND_COMMAND(type) NetworkPacketSend_ ## type ## _command
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    93
#define RECEIVE_COMMAND(type) NetworkPacketReceive_ ## type ## _command
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    94
5624
6afe9d27430a (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: 5619
diff changeset
    95
#define FOR_ALL_CLIENTS(cs) for (cs = _clients; cs != endof(_clients) && cs->IsConnected(); cs++)
4883
c9c7d15c0b0b (svn r6820) -Codechange: Some more const correctness, coding style.
Darkvater
parents: 4880
diff changeset
    96
#define FOR_ALL_ACTIVE_CLIENT_INFOS(ci) for (ci = _network_client_info; ci != endof(_network_client_info); ci++) if (ci->client_index != NETWORK_EMPTY_INDEX)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    97
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    98
void NetworkExecuteCommand(CommandPacket *cp);
5624
6afe9d27430a (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: 5619
diff changeset
    99
void NetworkAddCommandQueue(NetworkTCPSocketHandler *cs, CommandPacket *cp);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   100
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   101
// from network.c
5624
6afe9d27430a (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: 5619
diff changeset
   102
void NetworkCloseClient(NetworkTCPSocketHandler *cs);
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   103
void CDECL NetworkTextMessage(NetworkAction action, uint16 color, bool self_send, const char *name, const char *str, ...);
5624
6afe9d27430a (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: 5619
diff changeset
   104
void NetworkGetClientName(char *clientname, size_t size, const NetworkTCPSocketHandler *cs);
6afe9d27430a (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: 5619
diff changeset
   105
uint NetworkCalculateLag(const NetworkTCPSocketHandler *cs);
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1026
diff changeset
   106
byte NetworkGetCurrentLanguageIndex(void);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   107
NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index);
2859
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2498
diff changeset
   108
NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip);
5624
6afe9d27430a (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: 5619
diff changeset
   109
NetworkTCPSocketHandler *NetworkFindClientStateFromIndex(uint16 client_index);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   110
unsigned long NetworkResolveHost(const char *hostname);
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4906
diff changeset
   111
char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   112
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   113
#endif /* ENABLE_NETWORK */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   114
2436
7d5df545bd5d (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2186
diff changeset
   115
#endif /* NETWORK_DATA_H */