Use 2 different ENet channels for reliable/unreliable packets

This avoids stalling other sequenced but unreliable packets
(i.e. UNRELIABLE_ORDERED) when sending RELIABLE packets.
This commit is contained in:
Fabio Alessandrelli 2016-09-30 01:23:54 +02:00
parent cb7661df94
commit 8b9e5453a5
2 changed files with 27 additions and 16 deletions

View file

@ -32,7 +32,7 @@ Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int
host = enet_host_create (& address /* the address to bind the server host to */, 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 */, p_max_clients /* allow up to 32 clients and/or outgoing connections */,
2 /* allow up to 2 channels to be used, 0 and 1 */, SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
p_in_bandwidth /* assume any amount of incoming bandwidth */, p_in_bandwidth /* assume any amount of incoming bandwidth */,
p_out_bandwidth /* assume any amount of outgoing bandwidth */); p_out_bandwidth /* assume any amount of outgoing bandwidth */);
@ -52,7 +52,7 @@ Error NetworkedMultiplayerENet::create_client(const IP_Address& p_ip, int p_port
host = enet_host_create (NULL /* create a client host */, host = enet_host_create (NULL /* create a client host */,
1 /* only allow 1 outgoing connection */, 1 /* only allow 1 outgoing connection */,
2 /* allow up 2 channels to be used, 0 and 1 */, SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */, p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */,
p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */); p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */);
@ -70,8 +70,8 @@ Error NetworkedMultiplayerENet::create_client(const IP_Address& p_ip, int p_port
unique_id=_gen_unique_id(); unique_id=_gen_unique_id();
/* Initiate the connection, allocating the two channels 0 and 1. */ /* Initiate the connection, allocating the enough channels */
ENetPeer *peer = enet_host_connect (host, & address, 2, unique_id); ENetPeer *peer = enet_host_connect (host, & address, SYSCH_MAX, unique_id);
if (peer == NULL) { if (peer == NULL) {
enet_host_destroy(host); enet_host_destroy(host);
@ -148,12 +148,12 @@ void NetworkedMultiplayerENet::poll(){
ENetPacket * packet = enet_packet_create (NULL,8,ENET_PACKET_FLAG_RELIABLE); ENetPacket * packet = enet_packet_create (NULL,8,ENET_PACKET_FLAG_RELIABLE);
encode_uint32(SYSMSG_ADD_PEER,&packet->data[0]); encode_uint32(SYSMSG_ADD_PEER,&packet->data[0]);
encode_uint32(E->key(),&packet->data[4]); encode_uint32(E->key(),&packet->data[4]);
enet_peer_send(event.peer,1,packet); enet_peer_send(event.peer,SYSCH_CONFIG,packet);
//send the new peer to existing peers //send the new peer to existing peers
packet = enet_packet_create (NULL,8,ENET_PACKET_FLAG_RELIABLE); packet = enet_packet_create (NULL,8,ENET_PACKET_FLAG_RELIABLE);
encode_uint32(SYSMSG_ADD_PEER,&packet->data[0]); encode_uint32(SYSMSG_ADD_PEER,&packet->data[0]);
encode_uint32(*new_id,&packet->data[4]); encode_uint32(*new_id,&packet->data[4]);
enet_peer_send(E->get(),1,packet); enet_peer_send(E->get(),SYSCH_CONFIG,packet);
} }
} else { } else {
@ -185,7 +185,7 @@ void NetworkedMultiplayerENet::poll(){
ENetPacket* packet = enet_packet_create (NULL,8,ENET_PACKET_FLAG_RELIABLE); ENetPacket* packet = enet_packet_create (NULL,8,ENET_PACKET_FLAG_RELIABLE);
encode_uint32(SYSMSG_REMOVE_PEER,&packet->data[0]); encode_uint32(SYSMSG_REMOVE_PEER,&packet->data[0]);
encode_uint32(*id,&packet->data[4]); encode_uint32(*id,&packet->data[4]);
enet_peer_send(E->get(),1,packet); enet_peer_send(E->get(),SYSCH_CONFIG,packet);
} }
} else if (!server) { } else if (!server) {
emit_signal("server_disconnected"); emit_signal("server_disconnected");
@ -204,7 +204,7 @@ void NetworkedMultiplayerENet::poll(){
case ENET_EVENT_TYPE_RECEIVE: { case ENET_EVENT_TYPE_RECEIVE: {
if (event.channelID==1) { if (event.channelID==SYSCH_CONFIG) {
//some config message //some config message
ERR_CONTINUE( event.packet->dataLength < 8); ERR_CONTINUE( event.packet->dataLength < 8);
@ -226,7 +226,7 @@ void NetworkedMultiplayerENet::poll(){
} }
enet_packet_destroy(event.packet); enet_packet_destroy(event.packet);
} else if (event.channelID==0){ } else if (event.channelID < SYSCH_MAX){
Packet packet; Packet packet;
packet.packet = event.packet; packet.packet = event.packet;
@ -258,7 +258,7 @@ void NetworkedMultiplayerENet::poll(){
ENetPacket* packet2 = enet_packet_create (packet.packet->data,packet.packet->dataLength,flags); ENetPacket* packet2 = enet_packet_create (packet.packet->data,packet.packet->dataLength,flags);
enet_peer_send(E->get(),0,packet2); enet_peer_send(E->get(),event.channelID,packet2);
} }
} else if (target<0) { } else if (target<0) {
@ -272,7 +272,7 @@ void NetworkedMultiplayerENet::poll(){
ENetPacket* packet2 = enet_packet_create (packet.packet->data,packet.packet->dataLength,flags); ENetPacket* packet2 = enet_packet_create (packet.packet->data,packet.packet->dataLength,flags);
enet_peer_send(E->get(),0,packet2); enet_peer_send(E->get(),event.channelID,packet2);
} }
if (-target != 1) { if (-target != 1) {
@ -289,7 +289,7 @@ void NetworkedMultiplayerENet::poll(){
} else { } else {
//to someone else, specifically //to someone else, specifically
ERR_CONTINUE(!peer_map.has(target)); ERR_CONTINUE(!peer_map.has(target));
enet_peer_send(peer_map[target],0,packet.packet); enet_peer_send(peer_map[target],event.channelID,packet.packet);
} }
} else { } else {
@ -369,16 +369,20 @@ Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer,int p_buffer_
ERR_FAIL_COND_V(connection_status!=CONNECTION_CONNECTED,ERR_UNCONFIGURED); ERR_FAIL_COND_V(connection_status!=CONNECTION_CONNECTED,ERR_UNCONFIGURED);
int packet_flags=0; int packet_flags=0;
int channel=SYSCH_RELIABLE;
switch(transfer_mode) { switch(transfer_mode) {
case TRANSFER_MODE_UNRELIABLE: { case TRANSFER_MODE_UNRELIABLE: {
packet_flags=ENET_PACKET_FLAG_UNSEQUENCED; packet_flags=ENET_PACKET_FLAG_UNSEQUENCED;
channel=SYSCH_UNRELIABLE;
} break; } break;
case TRANSFER_MODE_UNRELIABLE_ORDERED: { case TRANSFER_MODE_UNRELIABLE_ORDERED: {
packet_flags=0; packet_flags=0;
channel=SYSCH_UNRELIABLE;
} break; } break;
case TRANSFER_MODE_RELIABLE: { case TRANSFER_MODE_RELIABLE: {
packet_flags=ENET_PACKET_FLAG_RELIABLE; packet_flags=ENET_PACKET_FLAG_RELIABLE;
channel=SYSCH_RELIABLE;
} break; } break;
} }
@ -402,7 +406,7 @@ Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer,int p_buffer_
if (server) { if (server) {
if (target_peer==0) { if (target_peer==0) {
enet_host_broadcast(host,0,packet); enet_host_broadcast(host,channel,packet);
} else if (target_peer<0) { } else if (target_peer<0) {
//send to all but one //send to all but one
//and make copies for sending //and make copies for sending
@ -416,18 +420,18 @@ Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer,int p_buffer_
ENetPacket* packet2 = enet_packet_create (packet->data,packet->dataLength,packet_flags); ENetPacket* packet2 = enet_packet_create (packet->data,packet->dataLength,packet_flags);
enet_peer_send(F->get(),0,packet2); enet_peer_send(F->get(),channel,packet2);
} }
enet_packet_destroy(packet); //original packet no longer needed enet_packet_destroy(packet); //original packet no longer needed
} else { } else {
enet_peer_send (E->get(), 0, packet); enet_peer_send (E->get(), channel, packet);
} }
} else { } else {
ERR_FAIL_COND_V(!peer_map.has(1),ERR_BUG); ERR_FAIL_COND_V(!peer_map.has(1),ERR_BUG);
enet_peer_send (peer_map[1], 0, packet); //send to server for broadcast.. enet_peer_send (peer_map[1], channel, packet); //send to server for broadcast..
} }

View file

@ -23,6 +23,13 @@ private:
SYSMSG_REMOVE_PEER SYSMSG_REMOVE_PEER
}; };
enum {
SYSCH_CONFIG,
SYSCH_RELIABLE,
SYSCH_UNRELIABLE,
SYSCH_MAX
};
bool active; bool active;
bool server; bool server;