Files
OpenRCT2-Web/src/network/network.h
T

316 lines
9.1 KiB
C++
Raw Normal View History

2015-02-12 02:01:02 +00:00
/*****************************************************************************
* Copyright (c) 2014 Ted John
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* This file is part of OpenRCT2.
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
2015-10-20 20:16:30 +02:00
2015-02-12 02:01:02 +00:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2015-10-20 20:16:30 +02:00
2015-02-12 02:01:02 +00:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#ifndef _NETWORK_H_
#define _NETWORK_H_
2015-07-10 13:53:41 -06:00
enum {
NETWORK_MODE_NONE,
NETWORK_MODE_CLIENT,
NETWORK_MODE_SERVER
};
enum {
NETWORK_PLAYER_FLAG_ISSERVER = 1 << 0,
};
enum {
NETWORK_AUTH_NONE,
NETWORK_AUTH_REQUESTED,
NETWORK_AUTH_OK,
NETWORK_AUTH_BADVERSION,
NETWORK_AUTH_BADNAME,
NETWORK_AUTH_BADPASSWORD
};
enum {
NETWORK_STATUS_NONE,
NETWORK_STATUS_RESOLVING,
NETWORK_STATUS_CONNECTING,
NETWORK_STATUS_CONNECTED
};
#define NETWORK_DEFAULT_PORT 11753
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include "../common.h"
#include "../platform/platform.h"
#ifdef __cplusplus
}
#endif // __cplusplus
#ifndef DISABLE_NETWORK
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define LAST_SOCKET_ERROR() WSAGetLastError()
#undef EWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/fcntl.h>
typedef int SOCKET;
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#define LAST_SOCKET_ERROR() errno
#define closesocket close
#define ioctlsocket ioctl
2015-08-04 01:08:42 +02:00
#endif // _WIN32
2015-07-07 08:09:21 -06:00
#ifdef __cplusplus
#include <list>
#include <set>
2015-07-07 08:09:21 -06:00
#include <memory>
#include <vector>
2015-07-08 21:35:37 -06:00
#include <SDL.h>
2015-07-07 08:09:21 -06:00
2015-07-14 22:40:22 -06:00
template <std::size_t size>
struct ByteSwapT { };
template <>
2015-07-16 11:32:43 -06:00
struct ByteSwapT<1> { static uint8 SwapBE(uint8 value) { return value; } };
template <>
2015-07-14 22:40:22 -06:00
struct ByteSwapT<2> { static uint16 SwapBE(uint16 value) { return SDL_SwapBE16(value); } };
template <>
struct ByteSwapT<4> { static uint32 SwapBE(uint32 value) { return SDL_SwapBE32(value); } };
template <typename T>
T ByteSwapBE(const T& value) { return ByteSwapT<sizeof(T)>::SwapBE(value); }
2015-07-07 08:09:21 -06:00
class NetworkPacket
{
public:
NetworkPacket();
2015-07-18 17:57:42 -06:00
static std::unique_ptr<NetworkPacket> Allocate();
static std::unique_ptr<NetworkPacket> Duplicate(NetworkPacket& packet);
2015-07-07 08:09:21 -06:00
uint8* GetData();
2015-07-14 22:40:22 -06:00
template <typename T>
NetworkPacket& operator<<(T value) { T swapped = ByteSwapBE(value); uint8* bytes = (uint8*)&swapped; data->insert(data->end(), bytes, bytes + sizeof(value)); return *this; }
2015-07-15 00:23:04 -06:00
void Write(uint8* bytes, unsigned int size);
void WriteString(const char* string);
2015-07-14 22:40:22 -06:00
template <typename T>
2015-09-02 20:40:03 -06:00
NetworkPacket& operator>>(T& value) { if (read + sizeof(value) > size) { value = 0; } else { value = ByteSwapBE(*((T*)&GetData()[read])); read += sizeof(value); } return *this; }
2015-07-15 00:23:04 -06:00
const uint8* Read(unsigned int size);
const char* ReadString();
2015-07-08 20:19:12 -06:00
void Clear();
2015-07-07 08:09:21 -06:00
uint16 size;
std::shared_ptr<std::vector<uint8>> data;
2015-07-14 22:40:22 -06:00
int transferred;
2015-07-07 08:09:21 -06:00
int read;
};
2015-07-16 11:32:43 -06:00
class NetworkPlayer
{
public:
NetworkPlayer(const char* name);
uint8 id;
uint8 name[32 + 1];
uint16 ping;
uint32 flags;
};
2015-07-07 08:09:21 -06:00
class NetworkConnection
{
public:
2015-07-15 00:23:04 -06:00
NetworkConnection();
~NetworkConnection();
2015-07-07 08:09:21 -06:00
int ReadPacket();
void QueuePacket(std::unique_ptr<NetworkPacket> packet);
void SendQueuedPackets();
2015-09-02 20:40:03 -06:00
bool SetTCPNoDelay(bool on);
2015-09-24 18:39:15 +01:00
bool SetNonBlocking(bool on);
static bool SetNonBlocking(SOCKET socket, bool on);
2015-07-07 08:09:21 -06:00
SOCKET socket;
NetworkPacket inboundpacket;
2015-07-15 00:23:04 -06:00
int authstatus;
2015-07-16 11:32:43 -06:00
NetworkPlayer* player;
2015-07-16 18:24:39 -06:00
uint32 ping_time;
2015-07-07 08:09:21 -06:00
private:
2015-09-02 20:40:03 -06:00
bool SendPacket(NetworkPacket& packet);
2015-07-07 08:09:21 -06:00
std::list<std::unique_ptr<NetworkPacket>> outboundpackets;
};
class NetworkAddress
{
public:
NetworkAddress();
void Resolve(const char* host, unsigned short port, bool nonblocking = true);
int GetResolveStatus(void);
std::shared_ptr<sockaddr_storage> ss;
std::shared_ptr<int> ss_len;
enum {
RESOLVE_NONE,
RESOLVE_INPROGRESS,
RESOLVE_OK,
RESOLVE_FAILED
};
private:
static int ResolveFunc(void* pointer);
const char* host;
unsigned short port;
SDL_mutex* mutex;
SDL_cond* cond;
std::shared_ptr<int> status;
};
2015-07-10 13:53:41 -06:00
class Network
{
public:
Network();
~Network();
bool Init();
void Close();
bool BeginClient(const char* host, unsigned short port);
bool BeginServer(unsigned short port, const char* address = NULL);
2015-07-10 13:53:41 -06:00
int GetMode();
int GetStatus();
2015-07-16 11:32:43 -06:00
int GetAuthStatus();
2015-07-10 13:53:41 -06:00
uint32 GetServerTick();
2015-07-23 17:22:03 -06:00
uint8 GetPlayerID();
2015-07-10 13:53:41 -06:00
void Update();
2015-07-16 18:24:39 -06:00
NetworkPlayer* GetPlayerByID(int id);
2015-07-18 17:57:42 -06:00
const char* FormatChat(NetworkPlayer* fromplayer, const char* text);
void SendPacketToClients(NetworkPacket& packet);
2015-07-30 08:34:17 -06:00
bool CheckSRAND(uint32 tick, uint32 srand0);
void KickPlayer(int playerId);
2015-07-10 13:53:41 -06:00
2015-07-15 00:23:04 -06:00
void Client_Send_AUTH(const char* gameversion, const char* name, const char* password);
2015-07-31 08:14:44 -06:00
void Server_Send_MAP(NetworkConnection* connection = nullptr);
2015-07-15 00:23:04 -06:00
void Client_Send_CHAT(const char* text);
void Server_Send_CHAT(const char* text);
2015-07-23 17:22:03 -06:00
void Client_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 callback);
void Server_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 playerid, uint8 callback);
2015-07-15 00:23:04 -06:00
void Server_Send_TICK();
2015-07-16 11:32:43 -06:00
void Server_Send_PLAYERLIST();
2015-07-16 18:24:39 -06:00
void Client_Send_PING();
void Server_Send_PING();
void Server_Send_PINGLIST();
2015-07-16 11:32:43 -06:00
std::vector<std::unique_ptr<NetworkPlayer>> player_list;
2015-07-10 13:53:41 -06:00
private:
bool ProcessConnection(NetworkConnection& connection);
2015-07-15 00:23:04 -06:00
void ProcessPacket(NetworkConnection& connection, NetworkPacket& packet);
2015-07-10 13:53:41 -06:00
void ProcessGameCommandQueue();
void AddClient(SOCKET socket);
void RemoveClient(std::unique_ptr<NetworkConnection>& connection);
2015-07-16 18:24:39 -06:00
NetworkPlayer* AddPlayer(const char* name);
2015-07-10 13:53:41 -06:00
void PrintError();
struct GameCommand
{
2015-07-23 17:22:03 -06:00
GameCommand(uint32 t, uint32* args, uint8 p, uint8 cb) { tick = t, eax = args[0], ebx = args[1], ecx = args[2], edx = args[3], esi = args[4], edi = args[5], ebp = args[6]; playerid = p; callback = cb; };
2015-07-10 13:53:41 -06:00
uint32 tick;
uint32 eax, ebx, ecx, edx, esi, edi, ebp;
2015-07-23 17:22:03 -06:00
uint8 playerid;
uint8 callback;
2015-07-10 13:53:41 -06:00
bool operator<(const GameCommand& comp) const {
return tick < comp.tick;
}
};
int mode;
int status;
NetworkAddress server_address;
2015-07-10 13:53:41 -06:00
bool wsa_initialized;
SOCKET listening_socket;
NetworkConnection server_connection;
uint32 last_tick_sent_time;
2015-07-16 18:24:39 -06:00
uint32 last_ping_sent_time;
2015-07-10 13:53:41 -06:00
uint32 server_tick;
2015-07-30 08:34:17 -06:00
uint32 server_srand0;
uint32 server_srand0_tick;
2015-07-23 17:22:03 -06:00
uint8 player_id;
2015-07-10 13:53:41 -06:00
std::list<std::unique_ptr<NetworkConnection>> client_connection_list;
std::multiset<GameCommand> game_command_queue;
std::vector<uint8> chunk_buffer;
2015-07-15 00:23:04 -06:00
char password[33];
2015-09-05 18:46:13 +01:00
bool _desynchronised;
uint32 server_connect_time;
2015-07-14 22:40:22 -06:00
2015-08-15 23:19:15 +01:00
void UpdateServer();
void UpdateClient();
2015-07-14 22:40:22 -06:00
private:
2015-07-15 00:23:04 -06:00
std::vector<int (Network::*)(NetworkConnection& connection, NetworkPacket& packet)> client_command_handlers;
std::vector<int (Network::*)(NetworkConnection& connection, NetworkPacket& packet)> server_command_handlers;
int Client_Handle_AUTH(NetworkConnection& connection, NetworkPacket& packet);
int Server_Handle_AUTH(NetworkConnection& connection, NetworkPacket& packet);
int Client_Handle_MAP(NetworkConnection& connection, NetworkPacket& packet);
int Client_Handle_CHAT(NetworkConnection& connection, NetworkPacket& packet);
int Server_Handle_CHAT(NetworkConnection& connection, NetworkPacket& packet);
int Client_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket& packet);
int Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket& packet);
int Client_Handle_TICK(NetworkConnection& connection, NetworkPacket& packet);
2015-07-16 11:32:43 -06:00
int Client_Handle_PLAYERLIST(NetworkConnection& connection, NetworkPacket& packet);
2015-07-16 18:24:39 -06:00
int Client_Handle_PING(NetworkConnection& connection, NetworkPacket& packet);
int Server_Handle_PING(NetworkConnection& connection, NetworkPacket& packet);
int Client_Handle_PINGLIST(NetworkConnection& connection, NetworkPacket& packet);
2015-07-10 13:53:41 -06:00
};
#endif // __cplusplus
#endif /* DISABLE_NETWORK */
2015-07-07 08:09:21 -06:00
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
2015-02-12 02:01:02 +00:00
int network_init();
void network_close();
2015-02-12 11:30:57 +00:00
int network_begin_client(const char *host, int port);
2015-02-12 02:01:02 +00:00
int network_begin_server(int port);
int network_get_mode();
int network_get_status();
2015-02-12 11:30:57 +00:00
void network_update();
2015-07-16 11:32:43 -06:00
int network_get_authstatus();
2015-07-10 13:53:41 -06:00
uint32 network_get_server_tick();
uint8 network_get_current_player_id();
2015-07-16 11:32:43 -06:00
int network_get_num_players();
const char* network_get_player_name(unsigned int index);
2015-07-16 18:24:39 -06:00
uint32 network_get_player_flags(unsigned int index);
int network_get_player_ping(unsigned int index);
int network_get_player_id(unsigned int index);
2015-07-05 09:19:01 -06:00
void network_send_map();
2015-07-07 14:52:41 -06:00
void network_send_chat(const char* text);
2015-07-23 17:22:03 -06:00
void network_send_gamecmd(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 callback);
2015-02-12 11:30:57 +00:00
void network_kick_player(int playerId);
2015-02-12 11:30:57 +00:00
void network_print_error();
2015-02-12 02:01:02 +00:00
2015-07-07 08:09:21 -06:00
#ifdef __cplusplus
}
#endif // __cplusplus
2015-07-07 08:09:21 -06:00
#endif