| 
									
										
										
										
											2016-10-10 19:50:51 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  networked_multiplayer_enet.h                                         */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2016-10-10 19:50:51 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2017-01-01 22:01:57 +01:00
										 |  |  | /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							| 
									
										
										
										
											2017-04-08 00:11:42 +02:00
										 |  |  | /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md)    */ | 
					
						
							| 
									
										
										
										
											2016-10-10 19:50:51 +02:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | #ifndef NETWORKED_MULTIPLAYER_ENET_H
 | 
					
						
							|  |  |  | #define NETWORKED_MULTIPLAYER_ENET_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | #include "io/compression.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "io/networked_multiplayer_peer.h"
 | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 19:12:22 +01:00
										 |  |  | #include <enet/enet.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	GDCLASS(NetworkedMultiplayerENet, NetworkedMultiplayerPeer) | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | public: | 
					
						
							|  |  |  | 	enum CompressionMode { | 
					
						
							|  |  |  | 		COMPRESS_NONE, | 
					
						
							|  |  |  | 		COMPRESS_RANGE_CODER, | 
					
						
							|  |  |  | 		COMPRESS_FASTLZ, | 
					
						
							| 
									
										
										
										
											2017-06-08 20:43:56 -05:00
										 |  |  | 		COMPRESS_ZLIB, | 
					
						
							|  |  |  | 		COMPRESS_ZSTD | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2016-08-19 16:48:08 -03:00
										 |  |  | 	enum { | 
					
						
							|  |  |  | 		SYSMSG_ADD_PEER, | 
					
						
							|  |  |  | 		SYSMSG_REMOVE_PEER | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 01:23:54 +02:00
										 |  |  | 	enum { | 
					
						
							|  |  |  | 		SYSCH_CONFIG, | 
					
						
							|  |  |  | 		SYSCH_RELIABLE, | 
					
						
							|  |  |  | 		SYSCH_UNRELIABLE, | 
					
						
							|  |  |  | 		SYSCH_MAX | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 	bool active; | 
					
						
							|  |  |  | 	bool server; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 16:48:08 -03:00
										 |  |  | 	uint32_t unique_id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int target_peer; | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 	TransferMode transfer_mode; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ENetEvent event; | 
					
						
							|  |  |  | 	ENetPeer *peer; | 
					
						
							|  |  |  | 	ENetHost *host; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 16:48:08 -03:00
										 |  |  | 	bool refuse_connections; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 14:06:51 -03:00
										 |  |  | 	ConnectionStatus connection_status; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Map<int, ENetPeer *> peer_map; | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	struct Packet { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ENetPacket *packet; | 
					
						
							| 
									
										
										
										
											2016-08-19 16:48:08 -03:00
										 |  |  | 		int from; | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	CompressionMode compression_mode; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 	mutable List<Packet> incoming_packets; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutable Packet current_packet; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 16:48:08 -03:00
										 |  |  | 	uint32_t _gen_unique_id() const; | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 	void _pop_current_packet() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	Vector<uint8_t> src_compressor_mem; | 
					
						
							|  |  |  | 	Vector<uint8_t> dst_compressor_mem; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ENetCompressor enet_compressor; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	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); | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	void _setup_compressor(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 06:00:59 +01:00
										 |  |  | 	IP_Address bind_ip; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | protected: | 
					
						
							|  |  |  | 	static void _bind_methods(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 	virtual void set_transfer_mode(TransferMode p_mode); | 
					
						
							| 
									
										
										
										
											2016-08-19 16:48:08 -03:00
										 |  |  | 	virtual void set_target_peer(int p_peer); | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 16:48:08 -03:00
										 |  |  | 	virtual int get_packet_peer() const; | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-12 12:52:50 -04:00
										 |  |  | 	Error create_server(int p_port, int p_max_clients = 32, int p_in_bandwidth = 0, int p_out_bandwidth = 0); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Error create_client(const IP_Address &p_ip, int p_port, int p_in_bandwidth = 0, int p_out_bandwidth = 0); | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 18:49:50 -03:00
										 |  |  | 	void close_connection(); | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	virtual void poll(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 18:49:50 -03:00
										 |  |  | 	virtual bool is_server() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 	virtual int get_available_packet_count() const; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	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); | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	virtual int get_max_packet_size() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 14:06:51 -03:00
										 |  |  | 	virtual ConnectionStatus get_connection_status() const; | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 16:48:08 -03:00
										 |  |  | 	virtual void set_refuse_new_connections(bool p_enable); | 
					
						
							|  |  |  | 	virtual bool is_refusing_new_connections() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	virtual int get_unique_id() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	void set_compression_mode(CompressionMode p_mode); | 
					
						
							|  |  |  | 	CompressionMode get_compression_mode() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | 	NetworkedMultiplayerENet(); | 
					
						
							|  |  |  | 	~NetworkedMultiplayerENet(); | 
					
						
							| 
									
										
										
										
											2016-08-27 13:41:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	void set_bind_ip(const IP_Address &p_ip); | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | VARIANT_ENUM_CAST(NetworkedMultiplayerENet::CompressionMode); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 13:29:25 -03:00
										 |  |  | #endif // NETWORKED_MULTIPLAYER_ENET_H
 |