From 01bdfe1ff6033c012d0833ee165f6055fed69724 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sun, 14 Aug 2016 14:06:51 -0300 Subject: Missing some bits, and added connection status. --- modules/enet/networked_multiplayer_enet.cpp | 54 +++++++++++++++++++++++++---- modules/enet/networked_multiplayer_enet.h | 3 ++ 2 files changed, 50 insertions(+), 7 deletions(-) (limited to 'modules/enet') diff --git a/modules/enet/networked_multiplayer_enet.cpp b/modules/enet/networked_multiplayer_enet.cpp index 2c898bbcf1..64f08a90ef 100644 --- a/modules/enet/networked_multiplayer_enet.cpp +++ b/modules/enet/networked_multiplayer_enet.cpp @@ -50,12 +50,37 @@ Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int active=true; server=true; + connection_status=CONNECTION_CONNECTED; return OK; } Error NetworkedMultiplayerENet::create_client(const IP_Address& p_ip,int p_port, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth){ ERR_FAIL_COND_V(active,ERR_ALREADY_IN_USE); + host = enet_host_create (NULL /* create a client host */, + 1 /* only allow 1 outgoing connection */, + p_max_channels /* allow up 2 channels to be used, 0 and 1 */, + p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */, + p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */); + + ERR_FAIL_COND_V(!host,ERR_CANT_CREATE); + + + ENetAddress address; + address.host=p_ip.host; + address.port=p_port; + + /* Initiate the connection, allocating the two channels 0 and 1. */ + ENetPeer *peer = enet_host_connect (host, & address, p_max_channels, 0); + + if (peer == NULL) { + enet_host_destroy(host); + ERR_FAIL_COND_V(!peer,ERR_CANT_CREATE); + } + + //technically safe to ignore the peer or anything else. + + connection_status=CONNECTION_CONNECTING; return OK; } @@ -83,6 +108,8 @@ void NetworkedMultiplayerENet::poll(){ peer_map[*new_id]=event.peer; + connection_status=CONNECTION_CONNECTED; //if connecting, this means it connected t something! + emit_signal("peer_connected",*new_id); } break; @@ -110,6 +137,9 @@ void NetworkedMultiplayerENet::poll(){ //destroy packet later.. }break; + case ENET_EVENT_TYPE_NONE: { + //do nothing + } break; } } } @@ -123,14 +153,8 @@ void NetworkedMultiplayerENet::disconnect() { enet_host_destroy(host); active=false; incoming_packets.clear(); -} - -void NetworkedMultiplayerENet::_bind_methods() { - - ObjectTypeDB::bind_method(_MD("create_server","port","max_clients","max_channels","in_bandwidth","out_bandwidth"),&NetworkedMultiplayerENet::create_server,DEFVAL(32),DEFVAL(1),DEFVAL(0),DEFVAL(0)); - ObjectTypeDB::bind_method(_MD("create_client","ip","port","max_channels","in_bandwidth","out_bandwidth"),&NetworkedMultiplayerENet::create_client,DEFVAL(1),DEFVAL(0),DEFVAL(0)); - ObjectTypeDB::bind_method(_MD("disconnect"),&NetworkedMultiplayerENet::disconnect); + connection_status=CONNECTION_DISCONNECTED; } int NetworkedMultiplayerENet::get_available_packet_count() const { @@ -207,6 +231,20 @@ void NetworkedMultiplayerENet::_pop_current_packet() const { } +NetworkedMultiplayerPeer::ConnectionStatus NetworkedMultiplayerENet::get_connection_status() const { + + return connection_status; +} + +void NetworkedMultiplayerENet::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("create_server","port","max_clients","max_channels","in_bandwidth","out_bandwidth"),&NetworkedMultiplayerENet::create_server,DEFVAL(32),DEFVAL(1),DEFVAL(0),DEFVAL(0)); + ObjectTypeDB::bind_method(_MD("create_client","ip","port","max_channels","in_bandwidth","out_bandwidth"),&NetworkedMultiplayerENet::create_client,DEFVAL(1),DEFVAL(0),DEFVAL(0)); + ObjectTypeDB::bind_method(_MD("disconnect"),&NetworkedMultiplayerENet::disconnect); + +} + + NetworkedMultiplayerENet::NetworkedMultiplayerENet(){ active=false; @@ -214,7 +252,9 @@ NetworkedMultiplayerENet::NetworkedMultiplayerENet(){ send_channel=0; current_packet.packet=NULL; transfer_mode=TRANSFER_MODE_ORDERED; + connection_status=CONNECTION_DISCONNECTED; } + NetworkedMultiplayerENet::~NetworkedMultiplayerENet(){ if (active) { diff --git a/modules/enet/networked_multiplayer_enet.h b/modules/enet/networked_multiplayer_enet.h index 3921c1c9ca..20eb53990d 100644 --- a/modules/enet/networked_multiplayer_enet.h +++ b/modules/enet/networked_multiplayer_enet.h @@ -19,6 +19,8 @@ class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer { ENetPeer *peer; ENetHost *host; + ConnectionStatus connection_status; + Map peer_map; struct Packet { @@ -60,6 +62,7 @@ public: virtual int get_max_packet_size() const; + virtual ConnectionStatus get_connection_status() const; NetworkedMultiplayerENet(); ~NetworkedMultiplayerENet(); -- cgit v1.2.3