From 78b65054ff4941945b576859b316348d6bcd59b6 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Tue, 12 May 2020 00:08:32 -0500 Subject: [PATCH] Added base client and server enet comonents --- enetcomponents/__init__.py | 1 + enetcomponents/client.py | 40 +++++++++++++++++++++ enetcomponents/server.py | 71 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 enetcomponents/__init__.py create mode 100644 enetcomponents/client.py create mode 100644 enetcomponents/server.py diff --git a/enetcomponents/__init__.py b/enetcomponents/__init__.py new file mode 100644 index 0000000..7c68785 --- /dev/null +++ b/enetcomponents/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- \ No newline at end of file diff --git a/enetcomponents/client.py b/enetcomponents/client.py new file mode 100644 index 0000000..eaf2b8f --- /dev/null +++ b/enetcomponents/client.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +import time +import json +import enet + +class client(object): + + def __init__(self, host=b"localhost", port=33333, peer_count=1, channel_limit=4, incoming_bandwidth=0, outgoing_bandwidth=0): + self.host = enet.Host(None, peer_count, channel_limit, incoming_bandwidth, outgoing_bandwidth) + address = enet.Address(host, port) + self.peer = self.host.connect(address, channel_limit) + + def run(self): + running = True + while running: + event = self.host.service(0) + if event.type == enet.EVENT_TYPE_CONNECT: + print("%s: CONNECT" % event.peer.address) + elif event.type == enet.EVENT_TYPE_DISCONNECT: + print("%s: DISCONNECT" % event.peer.address) + elif event.type == enet.EVENT_TYPE_RECEIVE: + print("%s: IN: %r" % (event.peer.address, event.packet.data)) + time.sleep(0.001) + + def send_data(self, channel, data, reliable=True): + data_str = json.dumps(data, ensure_ascii=False) + data_bytes = bytes(data_str, "utf-8") + if reliable: + flags = enet.PACKET_FLAG_RELIABLE + else: + flags = enet.PACKET_FLAG_UNSEQUENCED + packet = enet.Packet(data_bytes, flags) + self.peer.send(channel, packet) + + def disconnect(self): + self.peer.disconnect() + + def __del__(self): + del self.host + del self.peer \ No newline at end of file diff --git a/enetcomponents/server.py b/enetcomponents/server.py new file mode 100644 index 0000000..e364cad --- /dev/null +++ b/enetcomponents/server.py @@ -0,0 +1,71 @@ +import enet +import time +import json + +class channel(object): + + def __init__(self, peer): + self.peer = peer + + def send_data(self, channel, data, reliable=True): + data_str = json.dumps(data, ensure_ascii=False) + data_bytes = bytes(data_str, "utf-8") + if reliable: + flags = enet.PACKET_FLAG_RELIABLE + else: + flags = enet.PACKET_FLAG_UNSEQUENCED + packet = enet.Packet(data_bytes, flags) + self.peer.send(channel, packet) + + def network(self, event, data): + print(data) + + def disconnect(self): + self.peer.disconnect() + +class server(object): + + def __init__(self, host=b"localhost", port=33333, peer_count=10, channel_limit=4, incoming_bandwidth=0, outgoing_bandwidth=0): + address = enet.Address(host, port) + self.host = enet.Host(address, peer_count, channel_limit, incoming_bandwidth, outgoing_bandwidth) + self.peers = {} + + def run(self): + running = True + while running: + event = self.host.service(0) + if event.type == enet.EVENT_TYPE_CONNECT: + self.connected(event.peer) + elif event.type == enet.EVENT_TYPE_DISCONNECT: + self.disconnected(event.peer) + elif event.type == enet.EVENT_TYPE_RECEIVE: + channel_object = self.get_channel(event.peer) + if channel_object == None: + print("Error receiving packet for invalid channel: %r" % (event.data)) + return + data = event.packet.data + data_dict = json.loads(data, encoding="utf-8") + channel_object.network(event, data_dict) + time.sleep(0.0001) + + def get_channel(self, peer): + for channel in self.peers: + if peer.incomingPeerID == channel.peer.incomingPeerID: + return channel + + def connected(self, peer): + p = channel(peer) + self.peers[p] = True + print(len(self.peers)) + p.send_data(0, dict(action="connected"), False) + print("%s: CONNECTED" % peer.address) + + def disconnected(self, peer): + for channel in self.peers: + if peer.incomingPeerID == channel.peer.incomingPeerID: + del self.peers[channel] + print("%s: DISCONNECT" % peer.address) + break + + def __del__(self): + del self.host \ No newline at end of file