Missing some bits, and added connection status.

This commit is contained in:
Juan Linietsky 2016-08-14 14:06:51 -03:00
parent c21aae65b6
commit 01bdfe1ff6
4 changed files with 64 additions and 8 deletions

View file

@ -12,11 +12,16 @@ void NetworkedMultiplayerPeer::_bind_methods() {
ObjectTypeDB::bind_method(_MD("poll"), &NetworkedMultiplayerPeer::poll );
ObjectTypeDB::bind_method(_MD("get_connection_status"), &NetworkedMultiplayerPeer::get_connection_status );
BIND_CONSTANT( TRANSFER_MODE_UNRELIABLE );
BIND_CONSTANT( TRANSFER_MODE_RELIABLE );
BIND_CONSTANT( TRANSFER_MODE_ORDERED );
BIND_CONSTANT( CONNECTION_DISCONNECTED );
BIND_CONSTANT( CONNECTION_CONNECTING );
BIND_CONSTANT( CONNECTION_CONNECTED );
ADD_SIGNAL( MethodInfo("peer_connected",PropertyInfo(Variant::STRING,"id")));
ADD_SIGNAL( MethodInfo("peer_disconnected",PropertyInfo(Variant::STRING,"id")));
}

View file

@ -17,6 +17,12 @@ public:
TRANSFER_MODE_ORDERED
};
enum ConnectionStatus {
CONNECTION_DISCONNECTED,
CONNECTION_CONNECTING,
CONNECTION_CONNECTED,
};
virtual void set_transfer_mode(TransferMode p_mode)=0;
virtual void set_target_peer(const StringName& p_peer_id)=0;
@ -26,12 +32,14 @@ public:
virtual StringName get_packet_peer() const=0;
virtual int get_packet_channel() const=0;
virtual void poll()=0;
virtual ConnectionStatus get_connection_status() const=0;
NetworkedMultiplayerPeer();
};
VARIANT_ENUM_CAST( NetworkedMultiplayerPeer::TransferMode )
VARIANT_ENUM_CAST( NetworkedMultiplayerPeer::ConnectionStatus )
#endif // NetworkedMultiplayerPeer_H

View file

@ -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) {

View file

@ -19,6 +19,8 @@ class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer {
ENetPeer *peer;
ENetHost *host;
ConnectionStatus connection_status;
Map<StringName,ENetPeer*> peer_map;
struct Packet {
@ -60,6 +62,7 @@ public:
virtual int get_max_packet_size() const;
virtual ConnectionStatus get_connection_status() const;
NetworkedMultiplayerENet();
~NetworkedMultiplayerENet();