celestar@5642: /* $Id$ */ celestar@5642: celestar@5642: #ifndef NETWORK_DATA_H celestar@5642: #define NETWORK_DATA_H celestar@5642: celestar@5642: // Is the network enabled? celestar@5642: #ifdef ENABLE_NETWORK celestar@5642: celestar@5642: #include "../openttd.h" celestar@5642: #include "network.h" celestar@5642: #include "core/os_abstraction.h" celestar@5642: #include "core/config.h" celestar@5642: #include "core/packet.h" celestar@5642: celestar@5642: #define MAX_TEXT_MSG_LEN 1024 /* long long long long sentences :-) */ celestar@5642: celestar@5642: // The client-info-server-index is always 1 celestar@5642: #define NETWORK_SERVER_INDEX 1 celestar@5642: #define NETWORK_EMPTY_INDEX 0 celestar@5642: celestar@5642: typedef struct CommandPacket { celestar@5642: struct CommandPacket *next; celestar@5650: PlayerByte player; /// player that is executing the command celestar@5642: uint32 cmd; /// command being executed celestar@5642: uint32 p1; /// parameter p1 celestar@5642: uint32 p2; /// parameter p2 celestar@5642: TileIndex tile; /// tile command being executed on celestar@5642: char text[80]; celestar@5642: uint32 frame; /// the frame in which this packet is executed celestar@5642: byte callback; /// any callback function executed upon successful completion of the command celestar@5642: } CommandPacket; celestar@5642: celestar@5642: typedef enum { celestar@5642: STATUS_INACTIVE, celestar@5642: STATUS_AUTH, // This means that the client is authorized celestar@5642: STATUS_MAP_WAIT, // This means that the client is put on hold because someone else is getting the map celestar@5642: STATUS_MAP, celestar@5642: STATUS_DONE_MAP, celestar@5642: STATUS_PRE_ACTIVE, celestar@5642: STATUS_ACTIVE, celestar@5642: } ClientStatus; celestar@5642: celestar@5642: typedef enum { celestar@5642: MAP_PACKET_START, celestar@5642: MAP_PACKET_NORMAL, celestar@5642: MAP_PACKET_END, celestar@5642: } MapPacket; celestar@5642: celestar@5642: typedef enum { celestar@5642: NETWORK_RECV_STATUS_OKAY, celestar@5642: NETWORK_RECV_STATUS_DESYNC, celestar@5642: NETWORK_RECV_STATUS_SAVEGAME, celestar@5642: NETWORK_RECV_STATUS_CONN_LOST, celestar@5642: NETWORK_RECV_STATUS_MALFORMED_PACKET, celestar@5642: NETWORK_RECV_STATUS_SERVER_ERROR, // The server told us we made an error celestar@5642: NETWORK_RECV_STATUS_SERVER_FULL, celestar@5642: NETWORK_RECV_STATUS_SERVER_BANNED, celestar@5642: NETWORK_RECV_STATUS_CLOSE_QUERY, // Done quering the server celestar@5642: } NetworkRecvStatus; celestar@5642: celestar@5642: typedef enum { celestar@5642: NETWORK_ERROR_GENERAL, // Try to use thisone like never celestar@5642: celestar@5642: // Signals from clients celestar@5642: NETWORK_ERROR_DESYNC, celestar@5642: NETWORK_ERROR_SAVEGAME_FAILED, celestar@5642: NETWORK_ERROR_CONNECTION_LOST, celestar@5642: NETWORK_ERROR_ILLEGAL_PACKET, celestar@5642: celestar@5642: // Signals from servers celestar@5642: NETWORK_ERROR_NOT_AUTHORIZED, celestar@5642: NETWORK_ERROR_NOT_EXPECTED, celestar@5642: NETWORK_ERROR_WRONG_REVISION, celestar@5642: NETWORK_ERROR_NAME_IN_USE, celestar@5642: NETWORK_ERROR_WRONG_PASSWORD, celestar@5642: NETWORK_ERROR_PLAYER_MISMATCH, // Happens in CLIENT_COMMAND celestar@5642: NETWORK_ERROR_KICKED, celestar@5642: NETWORK_ERROR_CHEATER, celestar@5642: NETWORK_ERROR_FULL, celestar@5642: } NetworkErrorCode; celestar@5642: celestar@5642: // Actions that can be used for NetworkTextMessage celestar@5642: typedef enum { celestar@5642: NETWORK_ACTION_JOIN, celestar@5642: NETWORK_ACTION_LEAVE, celestar@5642: NETWORK_ACTION_SERVER_MESSAGE, celestar@5642: NETWORK_ACTION_CHAT, celestar@5642: NETWORK_ACTION_CHAT_COMPANY, celestar@5642: NETWORK_ACTION_CHAT_CLIENT, celestar@5642: NETWORK_ACTION_GIVE_MONEY, celestar@5642: NETWORK_ACTION_NAME_CHANGE, celestar@5642: } NetworkAction; celestar@5642: celestar@5642: typedef enum { celestar@5642: NETWORK_GAME_PASSWORD, celestar@5642: NETWORK_COMPANY_PASSWORD, celestar@5642: } NetworkPasswordType; celestar@5642: celestar@5642: // To keep the clients all together celestar@5642: struct NetworkClientState { // Typedeffed in network_core/packet.h celestar@5642: SOCKET socket; celestar@5642: uint16 index; celestar@5642: uint32 last_frame; celestar@5642: uint32 last_frame_server; celestar@5642: byte lag_test; // This byte is used for lag-testing the client celestar@5642: celestar@5642: ClientStatus status; celestar@5642: bool writable; // is client ready to write to? celestar@5642: bool has_quit; celestar@5642: celestar@5642: Packet *packet_queue; // Packets that are awaiting delivery celestar@5642: Packet *packet_recv; // Partially received packet celestar@5642: celestar@5642: CommandPacket *command_queue; // The command-queue awaiting delivery celestar@5642: }; celestar@5642: celestar@5642: typedef enum { celestar@5642: DESTTYPE_BROADCAST, ///< Send message/notice to all players (All) celestar@5642: DESTTYPE_TEAM, ///< Send message/notice to everyone playing the same company (Team) celestar@5642: DESTTYPE_CLIENT, ///< Send message/notice to only a certain player (Private) celestar@5642: } DestType; celestar@5642: celestar@5650: // following externs are instantiated at network.cpp celestar@5650: extern CommandPacket *_local_command_queue; celestar@5642: celestar@5650: extern SOCKET _udp_client_socket; // udp client socket celestar@5650: extern SOCKET _udp_server_socket; // udp server socket celestar@5650: extern SOCKET _udp_master_socket; // udp master socket celestar@5642: celestar@5642: // Here we keep track of the clients celestar@5642: // (and the client uses [0] for his own communication) celestar@5650: extern NetworkClientState _clients[MAX_CLIENTS]; celestar@5650: celestar@5642: #define DEREF_CLIENT(i) (&_clients[i]) celestar@5642: // This returns the NetworkClientInfo from a NetworkClientState celestar@5642: #define DEREF_CLIENT_INFO(cs) (&_network_client_info[cs - _clients]) celestar@5642: celestar@5642: // Macros to make life a bit more easier celestar@5642: #define DEF_CLIENT_RECEIVE_COMMAND(type) NetworkRecvStatus NetworkPacketReceive_ ## type ## _command(Packet *p) celestar@5642: #define DEF_CLIENT_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command(void) celestar@5642: #define DEF_CLIENT_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command celestar@5642: #define DEF_SERVER_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(NetworkClientState *cs, Packet *p) celestar@5642: #define DEF_SERVER_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command(NetworkClientState *cs) celestar@5642: #define DEF_SERVER_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command celestar@5642: celestar@5642: #define SEND_COMMAND(type) NetworkPacketSend_ ## type ## _command celestar@5642: #define RECEIVE_COMMAND(type) NetworkPacketReceive_ ## type ## _command celestar@5642: celestar@5642: #define FOR_ALL_CLIENTS(cs) for (cs = _clients; cs != endof(_clients) && cs->socket != INVALID_SOCKET; cs++) celestar@5642: #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) celestar@5642: celestar@5642: void NetworkExecuteCommand(CommandPacket *cp); celestar@5642: void NetworkAddCommandQueue(NetworkClientState *cs, CommandPacket *cp); celestar@5642: celestar@5642: // from network.c celestar@5642: void NetworkCloseClient(NetworkClientState *cs); celestar@5642: void CDECL NetworkTextMessage(NetworkAction action, uint16 color, bool self_send, const char *name, const char *str, ...); celestar@5642: void NetworkGetClientName(char *clientname, size_t size, const NetworkClientState *cs); celestar@5642: uint NetworkCalculateLag(const NetworkClientState *cs); celestar@5642: byte NetworkGetCurrentLanguageIndex(void); celestar@5642: NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index); celestar@5642: NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip); celestar@5642: NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index); celestar@5642: unsigned long NetworkResolveHost(const char *hostname); celestar@5642: char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last); celestar@5642: celestar@5642: #endif /* ENABLE_NETWORK */ celestar@5642: celestar@5642: #endif /* NETWORK_DATA_H */