From 2a36c58abe977858e4c36888fa71b1dc06d257a9 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Mon, 20 Jul 2020 21:36:56 -0500 Subject: [PATCH] Stubbed files for simplified networking system --- lib/networking/backend/README.md | 3 ++ lib/networking/backend/channel.rb | 2 + lib/networking/backend/connection.rb | 27 +++++++++++++ lib/networking/backend/peer.rb | 10 +++++ lib/networking/backend/server.rb | 59 ++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 lib/networking/backend/README.md create mode 100644 lib/networking/backend/channel.rb create mode 100644 lib/networking/backend/connection.rb create mode 100644 lib/networking/backend/peer.rb create mode 100644 lib/networking/backend/server.rb diff --git a/lib/networking/backend/README.md b/lib/networking/backend/README.md new file mode 100644 index 0000000..cfcb8ef --- /dev/null +++ b/lib/networking/backend/README.md @@ -0,0 +1,3 @@ +# I-MIC FPS / CyberarmEngine Networking System +End Goal: Reliable and ordered packets of abitrary size +Current Goal: Unreliable, unordered packets of limited size \ No newline at end of file diff --git a/lib/networking/backend/channel.rb b/lib/networking/backend/channel.rb new file mode 100644 index 0000000..060074d --- /dev/null +++ b/lib/networking/backend/channel.rb @@ -0,0 +1,2 @@ +class Channel +end \ No newline at end of file diff --git a/lib/networking/backend/connection.rb b/lib/networking/backend/connection.rb new file mode 100644 index 0000000..6a27a54 --- /dev/null +++ b/lib/networking/backend/connection.rb @@ -0,0 +1,27 @@ +class Connection + def initialize(hostname:, port:, max_clients:, channels: 1) + end + + # Callbacks # + def connected + end + + def disconnected(reason) + end + + def reconnected + end + + def packet_received(message, channel) + end + + # Functions # + def send_packet(message, reliable, channel = 0) + end + + def broadcast_packet(message, reliable, channel = 0) + end + + def disconnect(reason = "") + end +end \ No newline at end of file diff --git a/lib/networking/backend/peer.rb b/lib/networking/backend/peer.rb new file mode 100644 index 0000000..97bff3a --- /dev/null +++ b/lib/networking/backend/peer.rb @@ -0,0 +1,10 @@ +class Peer + attr_reader :id, :hostname, :port, :data + def initialize(id:, hostname:, port:) + @id = id + @hostname = hostname + @port = port + + @data = {} + end +end \ No newline at end of file diff --git a/lib/networking/backend/server.rb b/lib/networking/backend/server.rb new file mode 100644 index 0000000..8154584 --- /dev/null +++ b/lib/networking/backend/server.rb @@ -0,0 +1,59 @@ +class Server + attr_reader :hostname, :port, :max_clients + def initialize(hostname: "0.0.0.0", port: 56789, max_clients: 32, channels: 1) + @hostname = hostname + @port = port + @max_clients = max_clients + + @socket = UDPSocket.new + @socket.bind(@hostname, @port) + + @channels = Array(0..channels).map { |id| Channel.new(id: id, server: self) } + @peers = [] + end + + # Helpers # + def connected_clients + @peers.size + end + + def clients + @peers + end + + # Callbacks # + + # Called when client connects + def client_connected(peer) + end + + # Called when client times out or explicitly disconnects + def client_disconnected(peer, reason) + end + + ### REMOVE? ### + # Called when client was not sending heartbeats or regular packets for a + # period of time, but was not logically disconnected and removed, and started + # send packets again. + # + # TLDR: Client was temporarily unreachable but did not timeout. + def client_reconnected(peer) + end + + # Called when a (logical) packet is received from client + def packet_received(peer, message, channel = 0) + end + + # Functions # + # Send packet to specified peer + def send_packet(peer, message, reliable, channel = 0) + end + + # Send packet to all connected peer + def broadcast_packet(message, reliable, channel = 0) + end + + # Disconnect peer + def disconnect_client(peer, reason = "") + end +end \ No newline at end of file