2016-10-10 17:50:51 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* networked_multiplayer_enet.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-10-10 17:50:51 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-10-10 17:50:51 +00: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-20 04:05:57 +00:00
|
|
|
#include "networked_multiplayer_enet.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "io/marshalls.h"
|
|
|
|
#include "os/os.h"
|
2016-08-14 16:29:25 +00:00
|
|
|
|
|
|
|
void NetworkedMultiplayerENet::set_transfer_mode(TransferMode p_mode) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
transfer_mode = p_mode;
|
2016-08-14 16:29:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void NetworkedMultiplayerENet::set_target_peer(int p_peer) {
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
target_peer = p_peer;
|
2016-08-14 16:29:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int NetworkedMultiplayerENet::get_packet_peer() const {
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!active, 1);
|
|
|
|
ERR_FAIL_COND_V(incoming_packets.size() == 0, 1);
|
2016-08-14 16:29:25 +00:00
|
|
|
|
|
|
|
return incoming_packets.front()->get().from;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int p_in_bandwidth, int p_out_bandwidth) {
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
|
2016-08-14 16:29:25 +00:00
|
|
|
|
|
|
|
ENetAddress address;
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-01-22 05:00:59 +00:00
|
|
|
#ifdef GODOT_ENET
|
|
|
|
if (bind_ip.is_wildcard()) {
|
|
|
|
address.wildcard = 1;
|
|
|
|
} else {
|
|
|
|
enet_address_set_ip(&address, bind_ip.get_ipv6(), 16);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (bind_ip.is_wildcard()) {
|
|
|
|
address.host = 0;
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_COND_V(!bind_ip.is_ipv4(), ERR_INVALID_PARAMETER);
|
|
|
|
address.host = *(uint32_t *)bind_ip.get_ipv4();
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-19 19:48:08 +00:00
|
|
|
address.port = p_port;
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
host = enet_host_create(&address /* the address to bind the server host to */,
|
|
|
|
p_max_clients /* allow up to 32 clients and/or outgoing connections */,
|
|
|
|
SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
|
|
|
|
p_in_bandwidth /* assume any amount of incoming bandwidth */,
|
|
|
|
p_out_bandwidth /* assume any amount of outgoing bandwidth */);
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!host, ERR_CANT_CREATE);
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2016-08-22 04:14:08 +00:00
|
|
|
_setup_compressor();
|
2017-03-05 15:44:50 +00:00
|
|
|
active = true;
|
|
|
|
server = true;
|
|
|
|
refuse_connections = false;
|
|
|
|
unique_id = 1;
|
|
|
|
connection_status = CONNECTION_CONNECTED;
|
2016-08-14 16:29:25 +00:00
|
|
|
return OK;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
Error NetworkedMultiplayerENet::create_client(const IP_Address &p_ip, int p_port, int p_in_bandwidth, int p_out_bandwidth) {
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
host = enet_host_create(NULL /* create a client host */,
|
|
|
|
1 /* only allow 1 outgoing connection */,
|
|
|
|
SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
|
|
|
|
p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */,
|
|
|
|
p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */);
|
2016-08-14 17:06:51 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!host, ERR_CANT_CREATE);
|
2016-08-14 17:06:51 +00:00
|
|
|
|
2016-08-22 04:14:08 +00:00
|
|
|
_setup_compressor();
|
|
|
|
|
2016-08-14 17:06:51 +00:00
|
|
|
ENetAddress address;
|
2017-01-22 05:00:59 +00:00
|
|
|
#ifdef GODOT_ENET
|
|
|
|
enet_address_set_ip(&address, p_ip.get_ipv6(), 16);
|
|
|
|
#else
|
|
|
|
ERR_FAIL_COND_V(!p_ip.is_ipv4(), ERR_INVALID_PARAMETER);
|
|
|
|
address.host = *(uint32_t *)p_ip.get_ipv4();
|
|
|
|
#endif
|
2017-03-05 15:44:50 +00:00
|
|
|
address.port = p_port;
|
2016-08-14 17:06:51 +00:00
|
|
|
|
2016-08-19 19:48:08 +00:00
|
|
|
//enet_address_set_host (& address, "localhost");
|
|
|
|
//address.port = p_port;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
unique_id = _gen_unique_id();
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2016-09-29 23:23:54 +00:00
|
|
|
/* Initiate the connection, allocating the enough channels */
|
2017-03-05 15:44:50 +00:00
|
|
|
ENetPeer *peer = enet_host_connect(host, &address, SYSCH_MAX, unique_id);
|
2016-08-14 17:06:51 +00:00
|
|
|
|
|
|
|
if (peer == NULL) {
|
|
|
|
enet_host_destroy(host);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!peer, ERR_CANT_CREATE);
|
2016-08-14 17:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//technically safe to ignore the peer or anything else.
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
connection_status = CONNECTION_CONNECTING;
|
|
|
|
active = true;
|
|
|
|
server = false;
|
|
|
|
refuse_connections = false;
|
2016-08-14 16:29:25 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void NetworkedMultiplayerENet::poll() {
|
2016-08-14 16:29:25 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!active);
|
|
|
|
|
|
|
|
_pop_current_packet();
|
|
|
|
|
|
|
|
ENetEvent event;
|
|
|
|
/* Wait up to 1000 milliseconds for an event. */
|
2016-08-19 19:48:08 +00:00
|
|
|
while (true) {
|
|
|
|
|
|
|
|
if (!host || !active) //might have been disconnected while emitting a notification
|
|
|
|
return;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ret = enet_host_service(host, &event, 1);
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ret < 0) {
|
2016-08-19 19:48:08 +00:00
|
|
|
//error, do something?
|
|
|
|
break;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (ret == 0) {
|
2016-08-19 19:48:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (event.type) {
|
2016-08-14 16:29:25 +00:00
|
|
|
case ENET_EVENT_TYPE_CONNECT: {
|
|
|
|
/* Store any relevant client information here. */
|
|
|
|
|
2016-08-19 19:48:08 +00:00
|
|
|
if (server && refuse_connections) {
|
|
|
|
enet_peer_reset(event.peer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int *new_id = memnew(int);
|
2016-08-19 19:48:08 +00:00
|
|
|
*new_id = event.data;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (*new_id == 0) { //data zero is sent by server (enet won't let you configure this). Server is always 1
|
|
|
|
*new_id = 1;
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
event.peer->data = new_id;
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
peer_map[*new_id] = event.peer;
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
connection_status = CONNECTION_CONNECTED; //if connecting, this means it connected t something!
|
2016-08-14 17:06:51 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
emit_signal("peer_connected", *new_id);
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2016-08-19 19:48:08 +00:00
|
|
|
if (server) {
|
|
|
|
//someone connected, let it know of all the peers available
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (E->key() == *new_id)
|
2016-08-19 19:48:08 +00:00
|
|
|
continue;
|
|
|
|
//send existing peers to new peer
|
2017-03-05 15:44:50 +00:00
|
|
|
ENetPacket *packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
encode_uint32(SYSMSG_ADD_PEER, &packet->data[0]);
|
|
|
|
encode_uint32(E->key(), &packet->data[4]);
|
|
|
|
enet_peer_send(event.peer, SYSCH_CONFIG, packet);
|
2016-08-19 19:48:08 +00:00
|
|
|
//send the new peer to existing peers
|
2017-03-05 15:44:50 +00:00
|
|
|
packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
encode_uint32(SYSMSG_ADD_PEER, &packet->data[0]);
|
|
|
|
encode_uint32(*new_id, &packet->data[4]);
|
|
|
|
enet_peer_send(E->get(), SYSCH_CONFIG, packet);
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
emit_signal("connection_succeeded");
|
|
|
|
}
|
|
|
|
|
2016-08-14 16:29:25 +00:00
|
|
|
} break;
|
|
|
|
case ENET_EVENT_TYPE_DISCONNECT: {
|
|
|
|
|
|
|
|
/* Reset the peer's client information. */
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int *id = (int *)event.peer->data;
|
2016-08-19 19:48:08 +00:00
|
|
|
|
|
|
|
if (!id) {
|
|
|
|
if (!server) {
|
|
|
|
emit_signal("connection_failed");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (server) {
|
|
|
|
//someone disconnected, let it know to everyone else
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (E->key() == *id)
|
2016-08-19 19:48:08 +00:00
|
|
|
continue;
|
|
|
|
//send the new peer to existing peers
|
2017-03-05 15:44:50 +00:00
|
|
|
ENetPacket *packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
encode_uint32(SYSMSG_REMOVE_PEER, &packet->data[0]);
|
|
|
|
encode_uint32(*id, &packet->data[4]);
|
|
|
|
enet_peer_send(E->get(), SYSCH_CONFIG, packet);
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
|
|
|
} else if (!server) {
|
|
|
|
emit_signal("server_disconnected");
|
|
|
|
close_connection();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
emit_signal("peer_disconnected", *id);
|
2016-08-19 19:48:08 +00:00
|
|
|
peer_map.erase(*id);
|
2017-03-05 15:44:50 +00:00
|
|
|
memdelete(id);
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
2016-08-14 16:29:25 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case ENET_EVENT_TYPE_RECEIVE: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (event.channelID == SYSCH_CONFIG) {
|
2016-08-19 19:48:08 +00:00
|
|
|
//some config message
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_CONTINUE(event.packet->dataLength < 8);
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2016-10-07 14:44:53 +00:00
|
|
|
// Only server can send config messages
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_CONTINUE(server);
|
2016-10-07 14:44:53 +00:00
|
|
|
|
2016-08-19 19:48:08 +00:00
|
|
|
int msg = decode_uint32(&event.packet->data[0]);
|
|
|
|
int id = decode_uint32(&event.packet->data[4]);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (msg) {
|
2016-08-19 19:48:08 +00:00
|
|
|
case SYSMSG_ADD_PEER: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
peer_map[id] = NULL;
|
|
|
|
emit_signal("peer_connected", id);
|
2016-08-19 19:48:08 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case SYSMSG_REMOVE_PEER: {
|
|
|
|
|
|
|
|
peer_map.erase(id);
|
2017-03-05 15:44:50 +00:00
|
|
|
emit_signal("peer_disconnected", id);
|
2016-08-19 19:48:08 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
enet_packet_destroy(event.packet);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (event.channelID < SYSCH_MAX) {
|
2016-08-19 19:48:08 +00:00
|
|
|
|
|
|
|
Packet packet;
|
|
|
|
packet.packet = event.packet;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t *id = (uint32_t *)event.peer->data;
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_CONTINUE(event.packet->dataLength < 12)
|
2016-08-19 19:48:08 +00:00
|
|
|
|
|
|
|
uint32_t source = decode_uint32(&event.packet->data[0]);
|
|
|
|
int target = decode_uint32(&event.packet->data[4]);
|
|
|
|
uint32_t flags = decode_uint32(&event.packet->data[8]);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
packet.from = source;
|
2016-08-19 19:48:08 +00:00
|
|
|
|
|
|
|
if (server) {
|
2016-10-07 14:44:53 +00:00
|
|
|
// Someone is cheating and trying to fake the source!
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_CONTINUE(source != *id);
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
packet.from = *id;
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (target == 0) {
|
2016-08-19 19:48:08 +00:00
|
|
|
//re-send the everyone but sender :|
|
|
|
|
|
|
|
|
incoming_packets.push_back(packet);
|
|
|
|
//and make copies for sending
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (uint32_t(E->key()) == source) //do not resend to self
|
2016-08-19 19:48:08 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ENetPacket *packet2 = enet_packet_create(packet.packet->data, packet.packet->dataLength, flags);
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
enet_peer_send(E->get(), event.channelID, packet2);
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (target < 0) {
|
2016-08-19 19:48:08 +00:00
|
|
|
//to all but one
|
|
|
|
|
|
|
|
//and make copies for sending
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (uint32_t(E->key()) == source || E->key() == -target) //do not resend to self, also do not send to excluded
|
2016-08-19 19:48:08 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ENetPacket *packet2 = enet_packet_create(packet.packet->data, packet.packet->dataLength, flags);
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
enet_peer_send(E->get(), event.channelID, packet2);
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (-target != 1) {
|
|
|
|
//server is not excluded
|
|
|
|
incoming_packets.push_back(packet);
|
|
|
|
} else {
|
|
|
|
//server is excluded, erase packet
|
|
|
|
enet_packet_destroy(packet.packet);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (target == 1) {
|
2016-08-19 19:48:08 +00:00
|
|
|
//to myself and only myself
|
|
|
|
incoming_packets.push_back(packet);
|
|
|
|
} else {
|
|
|
|
//to someone else, specifically
|
2016-08-19 21:44:09 +00:00
|
|
|
ERR_CONTINUE(!peer_map.has(target));
|
2017-03-05 15:44:50 +00:00
|
|
|
enet_peer_send(peer_map[target], event.channelID, packet.packet);
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
incoming_packets.push_back(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
//destroy packet later..
|
|
|
|
} else {
|
|
|
|
ERR_CONTINUE(true);
|
|
|
|
}
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} break;
|
2016-08-14 17:06:51 +00:00
|
|
|
case ENET_EVENT_TYPE_NONE: {
|
|
|
|
//do nothing
|
|
|
|
} break;
|
2016-08-14 16:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 21:49:50 +00:00
|
|
|
bool NetworkedMultiplayerENet::is_server() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!active, false);
|
2016-08-14 21:49:50 +00:00
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkedMultiplayerENet::close_connection() {
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2016-08-19 19:48:08 +00:00
|
|
|
if (!active)
|
|
|
|
return;
|
2016-08-14 16:29:25 +00:00
|
|
|
|
|
|
|
_pop_current_packet();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool peers_disconnected = false;
|
|
|
|
for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
|
2016-08-19 19:48:08 +00:00
|
|
|
if (E->get()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
enet_peer_disconnect_now(E->get(), unique_id);
|
|
|
|
peers_disconnected = true;
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peers_disconnected) {
|
|
|
|
enet_host_flush(host);
|
|
|
|
OS::get_singleton()->delay_usec(100); //wait 100ms for disconnection packets to send
|
|
|
|
}
|
|
|
|
|
2016-08-14 16:29:25 +00:00
|
|
|
enet_host_destroy(host);
|
2017-03-05 15:44:50 +00:00
|
|
|
active = false;
|
2016-08-14 16:29:25 +00:00
|
|
|
incoming_packets.clear();
|
2017-03-05 15:44:50 +00:00
|
|
|
unique_id = 1; //server is 1
|
|
|
|
connection_status = CONNECTION_DISCONNECTED;
|
2016-08-14 16:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int NetworkedMultiplayerENet::get_available_packet_count() const {
|
|
|
|
|
|
|
|
return incoming_packets.size();
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
Error NetworkedMultiplayerENet::get_packet(const uint8_t **r_buffer, int &r_buffer_size) const {
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(incoming_packets.size() == 0, ERR_UNAVAILABLE);
|
2016-08-14 16:29:25 +00:00
|
|
|
|
|
|
|
_pop_current_packet();
|
|
|
|
|
|
|
|
current_packet = incoming_packets.front()->get();
|
|
|
|
incoming_packets.pop_front();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_buffer = (const uint8_t *)(¤t_packet.packet->data[12]);
|
|
|
|
r_buffer_size = current_packet.packet->dataLength - 12;
|
2016-08-14 16:29:25 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!active, ERR_UNCONFIGURED);
|
|
|
|
ERR_FAIL_COND_V(connection_status != CONNECTION_CONNECTED, ERR_UNCONFIGURED);
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int packet_flags = 0;
|
|
|
|
int channel = SYSCH_RELIABLE;
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (transfer_mode) {
|
2016-08-14 16:29:25 +00:00
|
|
|
case TRANSFER_MODE_UNRELIABLE: {
|
2017-03-05 15:44:50 +00:00
|
|
|
packet_flags = ENET_PACKET_FLAG_UNSEQUENCED;
|
|
|
|
channel = SYSCH_UNRELIABLE;
|
2016-08-14 16:29:25 +00:00
|
|
|
} break;
|
2016-08-20 01:54:02 +00:00
|
|
|
case TRANSFER_MODE_UNRELIABLE_ORDERED: {
|
2017-03-05 15:44:50 +00:00
|
|
|
packet_flags = 0;
|
|
|
|
channel = SYSCH_UNRELIABLE;
|
2016-08-14 16:29:25 +00:00
|
|
|
} break;
|
2016-08-20 01:54:02 +00:00
|
|
|
case TRANSFER_MODE_RELIABLE: {
|
2017-03-05 15:44:50 +00:00
|
|
|
packet_flags = ENET_PACKET_FLAG_RELIABLE;
|
|
|
|
channel = SYSCH_RELIABLE;
|
2016-08-14 16:29:25 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<int, ENetPeer *>::Element *E = NULL;
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (target_peer != 0) {
|
2016-08-19 19:48:08 +00:00
|
|
|
|
|
|
|
E = peer_map.find(ABS(target_peer));
|
|
|
|
if (!E) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_EXPLAIN("Invalid Target Peer: " + itos(target_peer));
|
2016-08-19 19:48:08 +00:00
|
|
|
ERR_FAIL_V(ERR_INVALID_PARAMETER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ENetPacket *packet = enet_packet_create(NULL, p_buffer_size + 12, packet_flags);
|
|
|
|
encode_uint32(unique_id, &packet->data[0]); //source ID
|
|
|
|
encode_uint32(target_peer, &packet->data[4]); //dest ID
|
|
|
|
encode_uint32(packet_flags, &packet->data[8]); //dest ID
|
|
|
|
copymem(&packet->data[12], p_buffer, p_buffer_size);
|
2016-08-19 19:48:08 +00:00
|
|
|
|
|
|
|
if (server) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (target_peer == 0) {
|
|
|
|
enet_host_broadcast(host, channel, packet);
|
|
|
|
} else if (target_peer < 0) {
|
2016-08-19 19:48:08 +00:00
|
|
|
//send to all but one
|
|
|
|
//and make copies for sending
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int exclude = -target_peer;
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, ENetPeer *>::Element *F = peer_map.front(); F; F = F->next()) {
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (F->key() == exclude) // exclude packet
|
2016-08-19 19:48:08 +00:00
|
|
|
continue;
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ENetPacket *packet2 = enet_packet_create(packet->data, packet->dataLength, packet_flags);
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
enet_peer_send(F->get(), channel, packet2);
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enet_packet_destroy(packet); //original packet no longer needed
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
enet_peer_send(E->get(), channel, packet);
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
2016-08-14 16:29:25 +00:00
|
|
|
} else {
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!peer_map.has(1), ERR_BUG);
|
|
|
|
enet_peer_send(peer_map[1], channel, packet); //send to server for broadcast..
|
2016-08-14 16:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enet_host_flush(host);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NetworkedMultiplayerENet::get_max_packet_size() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return 1 << 24; //anything is good
|
2016-08-14 16:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkedMultiplayerENet::_pop_current_packet() const {
|
|
|
|
|
|
|
|
if (current_packet.packet) {
|
|
|
|
enet_packet_destroy(current_packet.packet);
|
2017-03-05 15:44:50 +00:00
|
|
|
current_packet.packet = NULL;
|
|
|
|
current_packet.from = 0;
|
2016-08-14 16:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 17:06:51 +00:00
|
|
|
NetworkedMultiplayerPeer::ConnectionStatus NetworkedMultiplayerENet::get_connection_status() const {
|
|
|
|
|
|
|
|
return connection_status;
|
|
|
|
}
|
|
|
|
|
2016-08-19 19:48:08 +00:00
|
|
|
uint32_t NetworkedMultiplayerENet::_gen_unique_id() const {
|
|
|
|
|
|
|
|
uint32_t hash = 0;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (hash == 0 || hash == 1) {
|
2016-08-19 19:48:08 +00:00
|
|
|
|
|
|
|
hash = hash_djb2_one_32(
|
2017-03-05 15:44:50 +00:00
|
|
|
(uint32_t)OS::get_singleton()->get_ticks_usec());
|
2016-08-19 19:48:08 +00:00
|
|
|
hash = hash_djb2_one_32(
|
2017-03-05 15:44:50 +00:00
|
|
|
(uint32_t)OS::get_singleton()->get_unix_time(), hash);
|
2016-08-19 19:48:08 +00:00
|
|
|
hash = hash_djb2_one_32(
|
2017-03-05 15:44:50 +00:00
|
|
|
(uint32_t)OS::get_singleton()->get_data_dir().hash64(), hash);
|
2017-01-14 11:26:56 +00:00
|
|
|
/*
|
|
|
|
hash = hash_djb2_one_32(
|
2017-08-07 10:17:31 +00:00
|
|
|
(uint32_t)OS::get_singleton()->get_unique_id().hash64(), hash );
|
2017-01-14 11:26:56 +00:00
|
|
|
*/
|
2016-08-19 19:48:08 +00:00
|
|
|
hash = hash_djb2_one_32(
|
2017-03-05 15:44:50 +00:00
|
|
|
(uint32_t)((uint64_t)this), hash); //rely on aslr heap
|
2016-08-19 19:48:08 +00:00
|
|
|
hash = hash_djb2_one_32(
|
2017-03-05 15:44:50 +00:00
|
|
|
(uint32_t)((uint64_t)&hash), hash); //rely on aslr stack
|
2016-08-19 19:48:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
hash = hash & 0x7FFFFFFF; // make it compatible with unsigned, since negatie id is used for exclusion
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NetworkedMultiplayerENet::get_unique_id() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!active, 0);
|
2016-08-19 19:48:08 +00:00
|
|
|
return unique_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkedMultiplayerENet::set_refuse_new_connections(bool p_enable) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
refuse_connections = p_enable;
|
2016-08-19 19:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NetworkedMultiplayerENet::is_refusing_new_connections() const {
|
|
|
|
|
|
|
|
return refuse_connections;
|
|
|
|
}
|
|
|
|
|
2016-08-22 04:14:08 +00:00
|
|
|
void NetworkedMultiplayerENet::set_compression_mode(CompressionMode p_mode) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
compression_mode = p_mode;
|
2016-08-22 04:14:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
NetworkedMultiplayerENet::CompressionMode NetworkedMultiplayerENet::get_compression_mode() const {
|
2016-08-22 04:14:08 +00:00
|
|
|
|
|
|
|
return compression_mode;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
size_t NetworkedMultiplayerENet::enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
|
2016-08-22 04:14:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
NetworkedMultiplayerENet *enet = (NetworkedMultiplayerENet *)(context);
|
2016-08-22 04:14:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (size_t(enet->src_compressor_mem.size()) < inLimit) {
|
|
|
|
enet->src_compressor_mem.resize(inLimit);
|
2016-08-22 04:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int total = inLimit;
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = 0;
|
|
|
|
while (total) {
|
|
|
|
for (size_t i = 0; i < inBufferCount; i++) {
|
|
|
|
int to_copy = MIN(total, int(inBuffers[i].dataLength));
|
|
|
|
copymem(&enet->src_compressor_mem[ofs], inBuffers[i].data, to_copy);
|
|
|
|
ofs += to_copy;
|
|
|
|
total -= to_copy;
|
2016-08-22 04:14:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Compression::Mode mode;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (enet->compression_mode) {
|
2016-08-22 04:14:08 +00:00
|
|
|
case COMPRESS_FASTLZ: {
|
2017-03-05 15:44:50 +00:00
|
|
|
mode = Compression::MODE_FASTLZ;
|
2016-08-22 04:14:08 +00:00
|
|
|
} break;
|
|
|
|
case COMPRESS_ZLIB: {
|
2017-03-05 15:44:50 +00:00
|
|
|
mode = Compression::MODE_DEFLATE;
|
2016-08-22 04:14:08 +00:00
|
|
|
} break;
|
2017-06-09 01:43:56 +00:00
|
|
|
case COMPRESS_ZSTD: {
|
|
|
|
mode = Compression::MODE_ZSTD;
|
|
|
|
} break;
|
2016-08-22 04:14:08 +00:00
|
|
|
default: { ERR_FAIL_V(0); }
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int req_size = Compression::get_max_compressed_buffer_size(ofs, mode);
|
|
|
|
if (enet->dst_compressor_mem.size() < req_size) {
|
2016-08-22 04:14:08 +00:00
|
|
|
enet->dst_compressor_mem.resize(req_size);
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int ret = Compression::compress(enet->dst_compressor_mem.ptr(), enet->src_compressor_mem.ptr(), ofs, mode);
|
2016-08-22 04:14:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ret < 0)
|
2016-08-22 04:14:08 +00:00
|
|
|
return 0;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ret > int(outLimit))
|
2016-08-22 04:14:08 +00:00
|
|
|
return 0; //do not bother
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
copymem(outData, enet->dst_compressor_mem.ptr(), ret);
|
2016-08-22 04:14:08 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
size_t NetworkedMultiplayerENet::enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
|
2016-08-22 04:14:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
NetworkedMultiplayerENet *enet = (NetworkedMultiplayerENet *)(context);
|
2016-08-22 04:14:08 +00:00
|
|
|
int ret = -1;
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (enet->compression_mode) {
|
2016-08-22 04:14:08 +00:00
|
|
|
case COMPRESS_FASTLZ: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_FASTLZ);
|
2016-08-22 04:14:08 +00:00
|
|
|
} break;
|
|
|
|
case COMPRESS_ZLIB: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_DEFLATE);
|
2016-08-22 04:14:08 +00:00
|
|
|
} break;
|
2017-06-09 01:43:56 +00:00
|
|
|
case COMPRESS_ZSTD: {
|
|
|
|
|
|
|
|
ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_ZSTD);
|
|
|
|
} break;
|
2016-08-22 04:14:08 +00:00
|
|
|
default: {}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ret < 0) {
|
2016-08-22 04:14:08 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkedMultiplayerENet::_setup_compressor() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (compression_mode) {
|
2016-08-22 04:14:08 +00:00
|
|
|
|
|
|
|
case COMPRESS_NONE: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
enet_host_compress(host, NULL);
|
2016-08-22 04:14:08 +00:00
|
|
|
} break;
|
|
|
|
case COMPRESS_RANGE_CODER: {
|
|
|
|
enet_host_compress_with_range_coder(host);
|
|
|
|
} break;
|
|
|
|
case COMPRESS_FASTLZ:
|
2017-06-09 01:43:56 +00:00
|
|
|
case COMPRESS_ZLIB:
|
|
|
|
case COMPRESS_ZSTD: {
|
2016-08-22 04:14:08 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
enet_host_compress(host, &enet_compressor);
|
2016-08-22 04:14:08 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void NetworkedMultiplayerENet::enet_compressor_destroy(void *context) {
|
2016-08-22 04:14:08 +00:00
|
|
|
|
|
|
|
//do none
|
|
|
|
}
|
|
|
|
|
2016-08-14 17:06:51 +00:00
|
|
|
void NetworkedMultiplayerENet::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("create_server", "port", "max_clients", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_server, DEFVAL(32), DEFVAL(0), DEFVAL(0));
|
|
|
|
ClassDB::bind_method(D_METHOD("create_client", "ip", "port", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_client, DEFVAL(0), DEFVAL(0));
|
|
|
|
ClassDB::bind_method(D_METHOD("close_connection"), &NetworkedMultiplayerENet::close_connection);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_compression_mode", "mode"), &NetworkedMultiplayerENet::set_compression_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_compression_mode"), &NetworkedMultiplayerENet::get_compression_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bind_ip", "ip"), &NetworkedMultiplayerENet::set_bind_ip);
|
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(COMPRESS_NONE);
|
|
|
|
BIND_ENUM_CONSTANT(COMPRESS_RANGE_CODER);
|
|
|
|
BIND_ENUM_CONSTANT(COMPRESS_FASTLZ);
|
|
|
|
BIND_ENUM_CONSTANT(COMPRESS_ZLIB);
|
|
|
|
BIND_ENUM_CONSTANT(COMPRESS_ZSTD);
|
2016-08-14 17:06:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
NetworkedMultiplayerENet::NetworkedMultiplayerENet() {
|
|
|
|
|
|
|
|
active = false;
|
|
|
|
server = false;
|
|
|
|
refuse_connections = false;
|
|
|
|
unique_id = 0;
|
|
|
|
target_peer = 0;
|
|
|
|
current_packet.packet = NULL;
|
|
|
|
transfer_mode = TRANSFER_MODE_RELIABLE;
|
|
|
|
connection_status = CONNECTION_DISCONNECTED;
|
|
|
|
compression_mode = COMPRESS_NONE;
|
|
|
|
enet_compressor.context = this;
|
|
|
|
enet_compressor.compress = enet_compress;
|
|
|
|
enet_compressor.decompress = enet_decompress;
|
|
|
|
enet_compressor.destroy = enet_compressor_destroy;
|
|
|
|
|
2017-01-22 05:00:59 +00:00
|
|
|
bind_ip = IP_Address("*");
|
2016-08-14 16:29:25 +00:00
|
|
|
}
|
2016-08-14 17:06:51 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
NetworkedMultiplayerENet::~NetworkedMultiplayerENet() {
|
2016-08-14 16:29:25 +00:00
|
|
|
|
2016-08-19 19:48:08 +00:00
|
|
|
close_connection();
|
2016-08-14 16:29:25 +00:00
|
|
|
}
|
2016-08-27 11:41:34 +00:00
|
|
|
|
|
|
|
// sets IP for ENet to bind when using create_server
|
|
|
|
// if no IP is set, then ENet bind to ENET_HOST_ANY
|
2017-03-05 15:44:50 +00:00
|
|
|
void NetworkedMultiplayerENet::set_bind_ip(const IP_Address &p_ip) {
|
2017-01-22 05:00:59 +00:00
|
|
|
ERR_FAIL_COND(!p_ip.is_valid() && !p_ip.is_wildcard());
|
|
|
|
|
|
|
|
bind_ip = p_ip;
|
2016-08-27 11:41:34 +00:00
|
|
|
}
|