summaryrefslogtreecommitdiff
path: root/modules/enet/networked_multiplayer_enet.h
blob: 59863c1f780f34fdb0243d5e46d4fdb7b809c45a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef NETWORKED_MULTIPLAYER_ENET_H
#define NETWORKED_MULTIPLAYER_ENET_H

#include "io/networked_multiplayer_peer.h"
#include "enet/enet.h"
#include "io/compression.h"

class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer {

	OBJ_TYPE(NetworkedMultiplayerENet,NetworkedMultiplayerPeer)
public:
	enum CompressionMode {
		COMPRESS_NONE,
		COMPRESS_RANGE_CODER,
		COMPRESS_FASTLZ,
		COMPRESS_ZLIB
	};
private:


	enum {
		SYSMSG_ADD_PEER,
		SYSMSG_REMOVE_PEER
	};

	bool active;
	bool server;

	uint32_t unique_id;

	int target_peer;
	TransferMode transfer_mode;

	ENetEvent event;
	ENetPeer *peer;
	ENetHost *host;

	bool refuse_connections;

	ConnectionStatus connection_status;

	Map<int,ENetPeer*> peer_map;

	struct Packet {

		ENetPacket *packet;
		int from;
	};

	CompressionMode compression_mode;

	mutable List<Packet> incoming_packets;

	mutable Packet current_packet;

	uint32_t _gen_unique_id() const;
	void _pop_current_packet() const;

	Vector<uint8_t> src_compressor_mem;
	Vector<uint8_t> dst_compressor_mem;

	ENetCompressor enet_compressor;
	static size_t enet_compress(void * context, const ENetBuffer * inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 * outData, size_t outLimit);
	static size_t  enet_decompress (void * context, const enet_uint8 * inData, size_t inLimit, enet_uint8 * outData, size_t outLimit);
	static void enet_compressor_destroy(void * context);
	void _setup_compressor();

	enet_uint32 bind_ip;
protected:
	static void _bind_methods();
public:

	virtual void set_transfer_mode(TransferMode p_mode);
	virtual void set_target_peer(int p_peer);


	virtual int get_packet_peer() const;


	Error create_server(int p_port, int p_max_peers=32, int p_in_bandwidth=0, int p_out_bandwidth=0);
	Error create_client(const IP_Address& p_ip, int p_port, int p_in_bandwidth=0, int p_out_bandwidth=0);

	void close_connection();

	virtual void poll();

	virtual bool is_server() const;

	virtual int get_available_packet_count() const;
	virtual Error get_packet(const uint8_t **r_buffer,int &r_buffer_size) const; ///< buffer is GONE after next get_packet
	virtual Error put_packet(const uint8_t *p_buffer,int p_buffer_size);

	virtual int get_max_packet_size() const;

	virtual ConnectionStatus get_connection_status() const;

	virtual void set_refuse_new_connections(bool p_enable);
	virtual bool is_refusing_new_connections() const;

	virtual int get_unique_id() const;

	void set_compression_mode(CompressionMode p_mode);
	CompressionMode get_compression_mode() const;

	NetworkedMultiplayerENet();
	~NetworkedMultiplayerENet();

	void set_bind_ip(const IP_Address& p_ip);
};

VARIANT_ENUM_CAST(NetworkedMultiplayerENet::CompressionMode);


#endif // NETWORKED_MULTIPLAYER_ENET_H