OpenRCT2/src/network/network.h

129 lines
3.0 KiB
C
Raw Normal View History

2015-02-12 03:01:02 +01: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.
* 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.
* 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_
#ifndef DISABLE_NETWORK
#include "../common.h"
2015-07-07 16:09:21 +02:00
#ifdef __cplusplus
#include <list>
#include <memory>
#include <vector>
class NetworkPacket
{
public:
NetworkPacket();
2015-07-08 03:21:05 +02:00
static std::unique_ptr<NetworkPacket> AllocatePacket();
static std::unique_ptr<NetworkPacket> DuplicatePacket(NetworkPacket& packet);
2015-07-07 16:09:21 +02:00
uint8* GetData();
template <class T>
void Write(T value) { uint8* bytes = (uint8*)&value; data->insert(data->end(), bytes, bytes + sizeof(value)); }
void Write(uint8* bytes, unsigned int size) { data->insert(data->end(), bytes, bytes + size); }
2015-07-09 04:19:12 +02:00
void Clear();
2015-07-07 16:09:21 +02:00
uint16 size;
std::shared_ptr<std::vector<uint8>> data;
int read;
};
class NetworkConnection
{
public:
int ReadPacket();
void QueuePacket(std::unique_ptr<NetworkPacket> packet);
void SendQueuedPackets();
SOCKET socket;
NetworkPacket inboundpacket;
private:
int SendPacket(NetworkPacket& packet);
std::list<std::unique_ptr<NetworkPacket>> outboundpackets;
};
extern "C" {
#endif
2015-07-05 17:19:01 +02:00
typedef struct network_packet network_packet;
typedef struct network_packet {
2015-02-12 12:30:57 +01:00
uint16 size;
2015-07-05 17:19:01 +02:00
uint8* data;
int read;
network_packet* next;
2015-02-12 12:30:57 +01:00
} network_packet;
2015-02-12 03:01:02 +01:00
#define NETWORK_DEFAULT_PORT 11753
2015-02-12 12:30:57 +01:00
enum {
NETWORK_NONE,
NETWORK_CLIENT,
NETWORK_SERVER
};
enum {
NETWORK_SUCCESS,
NETWORK_NO_DATA,
2015-07-05 17:19:01 +02:00
NETWORK_MORE_DATA,
2015-02-12 12:30:57 +01:00
NETWORK_DISCONNECTED
};
2015-07-05 17:19:01 +02:00
enum {
NETWORK_COMMAND_AUTH,
NETWORK_COMMAND_MAP,
NETWORK_COMMAND_CHAT,
NETWORK_COMMAND_GAMECMD,
NETWORK_COMMAND_TICK
};
2015-02-12 12:30:57 +01:00
extern int gNetworkStart;
extern char gNetworkStartHost[128];
extern int gNetworkStartPort;
extern int gNetworkStatus;
2015-07-05 17:19:01 +02:00
extern uint32 gNetworkServerTick;
2015-02-12 12:30:57 +01:00
2015-02-12 03:01:02 +01:00
int network_init();
void network_close();
2015-02-12 12:30:57 +01:00
int network_begin_client(const char *host, int port);
void network_end_client();
2015-02-12 03:01:02 +01:00
int network_begin_server(int port);
void network_end_server();
2015-02-12 12:30:57 +01:00
void network_update();
2015-07-05 17:19:01 +02:00
void network_send_tick();
void network_send_map();
2015-07-07 22:52:41 +02:00
void network_send_chat(const char* text);
2015-07-09 04:19:12 +02:00
void network_send_gamecmd(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp);
2015-02-12 12:30:57 +01:00
void network_print_error();
2015-02-12 03:01:02 +01:00
2015-07-07 16:09:21 +02:00
#ifdef __cplusplus
}
#endif
2015-02-12 03:01:02 +01:00
#endif /* DISABLE_NETWORK */
#endif