# network_data.h

    00001 /* $Id: network_data.h 11840 2008-01-13 21:51:53Z rubidium $ */
    00002
    00003 #ifndef NETWORK_DATA_H
    00004 #define NETWORK_DATA_H
    00005
    00006 #include "../openttd.h"
    00007 #include "network.h"
    00008 #include "network_internal.h"
    00009
    00010 // Is the network enabled?
    00011 #ifdef ENABLE_NETWORK
    00012
    00013 #include "core/os_abstraction.h"
    00014 #include "core/core.h"
    00015 #include "core/config.h"
    00016 #include "core/packet.h"
    00017 #include "core/tcp.h"
    00018
    00019 #define MAX_TEXT_MSG_LEN 1024 /* long long long long sentences :-) */
    00020
    00021 // The client-info-server-index is always 1
    00022 #define NETWORK_SERVER_INDEX 1
    00023 #define NETWORK_EMPTY_INDEX 0
    00024
    00025 enum MapPacket {
    00026   MAP_PACKET_START,
    00027   MAP_PACKET_NORMAL,
    00028   MAP_PACKET_END,
    00029 };
    00030
    00031 enum NetworkErrorCode {
    00032   NETWORK_ERROR_GENERAL, // Try to use thisone like never
    00033
    00034   // Signals from clients
    00035   NETWORK_ERROR_DESYNC,
    00036   NETWORK_ERROR_SAVEGAME_FAILED,
    00037   NETWORK_ERROR_CONNECTION_LOST,
    00038   NETWORK_ERROR_ILLEGAL_PACKET,
    00039   NETWORK_ERROR_NEWGRF_MISMATCH,
    00040
    00041   // Signals from servers
    00042   NETWORK_ERROR_NOT_AUTHORIZED,
    00043   NETWORK_ERROR_NOT_EXPECTED,
    00044   NETWORK_ERROR_WRONG_REVISION,
    00045   NETWORK_ERROR_NAME_IN_USE,
    00046   NETWORK_ERROR_WRONG_PASSWORD,
    00047   NETWORK_ERROR_PLAYER_MISMATCH, // Happens in CLIENT_COMMAND
    00048   NETWORK_ERROR_KICKED,
    00049   NETWORK_ERROR_CHEATER,
    00050   NETWORK_ERROR_FULL,
    00051 };
    00052
    00053 // Actions that can be used for NetworkTextMessage
    00054 enum NetworkAction {
    00055   NETWORK_ACTION_JOIN,
    00056   NETWORK_ACTION_LEAVE,
    00057   NETWORK_ACTION_SERVER_MESSAGE,
    00058   NETWORK_ACTION_CHAT,
    00059   NETWORK_ACTION_CHAT_COMPANY,
    00060   NETWORK_ACTION_CHAT_CLIENT,
    00061   NETWORK_ACTION_GIVE_MONEY,
    00062   NETWORK_ACTION_NAME_CHANGE,
    00063 };
    00064
    00065 enum NetworkPasswordType {
    00066   NETWORK_GAME_PASSWORD,
    00067   NETWORK_COMPANY_PASSWORD,
    00068 };
    00069
    00070 enum DestType {
    00071   DESTTYPE_BROADCAST,
    00072   DESTTYPE_TEAM,
    00073   DESTTYPE_CLIENT,
    00074 };
    00075
    00076 // following externs are instantiated at network.cpp
    00077 extern CommandPacket *_local_command_queue;
    00078
    00079 // Here we keep track of the clients
    00080 //  (and the client uses [0] for his own communication)
    00081 extern NetworkTCPSocketHandler _clients[MAX_CLIENTS];
    00082
    00083 #define DEREF_CLIENT(i) (&_clients[i])
    00084 // This returns the NetworkClientInfo from a NetworkClientState
    00085 #define DEREF_CLIENT_INFO(cs) (&_network_client_info[cs - _clients])
    00086
    00087 // Macros to make life a bit more easier
    00088 #define DEF_CLIENT_RECEIVE_COMMAND(type) NetworkRecvStatus NetworkPacketReceive_ ## type ## _command(Packet *p)
    00089 #define DEF_CLIENT_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command()
    00090 #define DEF_CLIENT_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command
    00091 #define DEF_SERVER_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(NetworkTCPSocketHandler *cs, Packet *p)
    00092 #define DEF_SERVER_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command(NetworkTCPSocketHandler *cs)
    00093 #define DEF_SERVER_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command
    00094
    00095 #define SEND_COMMAND(type) NetworkPacketSend_ ## type ## _command
    00096 #define RECEIVE_COMMAND(type) NetworkPacketReceive_ ## type ## _command
    00097
    00098 #define FOR_ALL_CLIENTS(cs) for (cs = _clients; cs != endof(_clients) && cs->IsConnected(); cs++)
    00099 #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)
    00100
    00101 void NetworkExecuteCommand(CommandPacket *cp);
    00102 void NetworkAddCommandQueue(NetworkTCPSocketHandler *cs, CommandPacket *cp);
    00103
    00104 // from network.c
    00105 void NetworkCloseClient(NetworkTCPSocketHandler *cs);
    00106 void CDECL NetworkTextMessage(NetworkAction action, uint16 color, bool self_send, const char *name, const char *str, ...);
    00107 void NetworkGetClientName(char *clientname, size_t size, const NetworkTCPSocketHandler *cs);
    00108 uint NetworkCalculateLag(const NetworkTCPSocketHandler *cs);
    00109 byte NetworkGetCurrentLanguageIndex();
    00110 NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index);
    00111 NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip);
    00112 NetworkTCPSocketHandler *NetworkFindClientStateFromIndex(uint16 client_index);
    00113 unsigned long NetworkResolveHost(const char *hostname);
    00114 char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last);
    00115
    00116 #endif /* ENABLE_NETWORK */
    00117
    00118 #endif /* NETWORK_DATA_H */