diff --git a/lib/networking/backend/channel.rb b/lib/networking/backend/channel.rb index 060074d..a206154 100644 --- a/lib/networking/backend/channel.rb +++ b/lib/networking/backend/channel.rb @@ -1,2 +1,6 @@ -class Channel +module CyberarmEngine + module Networking + class Channel + end + end end \ No newline at end of file diff --git a/lib/networking/backend/connection.rb b/lib/networking/backend/connection.rb index 6a27a54..6f5f93c 100644 --- a/lib/networking/backend/connection.rb +++ b/lib/networking/backend/connection.rb @@ -1,27 +1,31 @@ -class Connection - def initialize(hostname:, port:, max_clients:, channels: 1) - end +module CyberarmEngine + module Networking + class Connection + def initialize(hostname:, port:, max_clients:, channels: 1) + end - # Callbacks # - def connected - end + # Callbacks # + def connected + end - def disconnected(reason) - end + def disconnected(reason) + end - def reconnected - end + def reconnected + end - def packet_received(message, channel) - end + def packet_received(message, channel) + end - # Functions # - def send_packet(message, reliable, channel = 0) - end + # Functions # + def send_packet(message, reliable, channel = 0) + end - def broadcast_packet(message, reliable, channel = 0) - end + def broadcast_packet(message, reliable, channel = 0) + end - def disconnect(reason = "") + def disconnect(reason = "") + end + end end end \ No newline at end of file diff --git a/lib/networking/backend/packet.rb b/lib/networking/backend/packet.rb new file mode 100644 index 0000000..0b246f5 --- /dev/null +++ b/lib/networking/backend/packet.rb @@ -0,0 +1,19 @@ +module CyberarmEngine + module Networking + class Packet + attr_reader :protocol_version, :type, :peer_id, :message + + def self.type + raise NotImplementedError, "#{self.class}.type must be defined!" + end + + def self.decode(packet) + raise NotImplementedError, "#{self.class}.decode must be defined!" + end + + def encode + raise NotImplementedError, "#{self.class}#encode must be defined!" + end + end + end +end \ No newline at end of file diff --git a/lib/networking/backend/packet_handler.rb b/lib/networking/backend/packet_handler.rb new file mode 100644 index 0000000..a1edd59 --- /dev/null +++ b/lib/networking/backend/packet_handler.rb @@ -0,0 +1,6 @@ +module CyberarmEngine + module Networking + module PacketHandler + end + end +end \ No newline at end of file diff --git a/lib/networking/backend/packets/data_packet.rb b/lib/networking/backend/packets/data_packet.rb new file mode 100644 index 0000000..adf63c9 --- /dev/null +++ b/lib/networking/backend/packets/data_packet.rb @@ -0,0 +1,37 @@ +module CyberarmEngine + module Networking + class DataPacket < Packet + HEADER_PACKER = "CCn" + HEADER_LENGTH = 1 + 1 + 4 # bytes + + def self.type + Protocol::DATA + end + + def self.decode(raw_message) + header = raw_message.unpack(HEADER_PACKER) + message = raw_message[HEADER_LENGTH..raw_message.length - 1] + + DataPacket.new(protocol_version: header[0], type: header[1], message: message) + end + + def initialize(protocol_version:, type:, peer_id:, message:) + @protocol_version = protocol_version + @type = type + @peer_id = peer_id + + @message = message + end + + def encode + header = [ + Protocol::PROTOCOL_VERSION, + @type, + @peer_id, + ].pack(HEADER_PACKER) + + "#{header}#{message}" + end + end + end +end \ No newline at end of file diff --git a/lib/networking/backend/peer.rb b/lib/networking/backend/peer.rb index 97bff3a..91f6638 100644 --- a/lib/networking/backend/peer.rb +++ b/lib/networking/backend/peer.rb @@ -1,10 +1,14 @@ -class Peer - attr_reader :id, :hostname, :port, :data - def initialize(id:, hostname:, port:) - @id = id - @hostname = hostname - @port = port +module CyberarmEngine + module Networking + class Peer + attr_reader :id, :hostname, :port, :data + def initialize(id:, hostname:, port:) + @id = id + @hostname = hostname + @port = port - @data = {} + @data = {} + end + end end end \ No newline at end of file diff --git a/lib/networking/protocol.rb b/lib/networking/backend/protocol.rb similarity index 58% rename from lib/networking/protocol.rb rename to lib/networking/backend/protocol.rb index 98d3010..5d9c389 100644 --- a/lib/networking/protocol.rb +++ b/lib/networking/backend/protocol.rb @@ -1,34 +1,23 @@ -class IMICFPS +module CyberarmEngine module Networking module Protocol MAX_PACKET_SIZE = 1024 PROTOCOL_VERSION = 0 # int - HEARTBEAT_INTERVAL = 250 # ms + HEARTBEAT_INTERVAL = 5_000 # ms TIMEOUT_PERIOD = 30_000 # ms packet_types = %w{ # protocol packets reliable multipart - acknowledgement control data - # protocol control packets - connect - verify_connect + # control packet types disconnect - authenticate + acknowledge heartbeat - - # game data packets - snapshot - player_joined - player_left - play_sound_effect - create_particle - create_entity - remove_entity + ping } # emulate c-like enum diff --git a/lib/networking/packet.rb b/lib/networking/packet.rb deleted file mode 100644 index 57e7403..0000000 --- a/lib/networking/packet.rb +++ /dev/null @@ -1,56 +0,0 @@ -class IMICFPS - module Networking - class Packet - HEADER_PACKER = "CnCnCC" - HEADER_SIZE = 8 - - def self.from_stream(raw) - header = raw[ 0..HEADER_SIZE ].unpack(HEADER_PACKER) - payload = raw[HEADER_SIZE..raw.length - 1] - - new(peer_id: header.last, sequence: header[1], type: header[2], payload: payload) - end - - # TODO: Handle splitting big packets into smaller ones - def self.splinter(packet) - packets = [packet] - - return packets - end - - attr_reader :peer_id, :sequence_number, :type, :parity, :payload, :content_length - def initialize(peer_id:, sequence:, type:, payload:) - @peer_id = peer_id - @sequence_number = sequence - @type = type - @parity = calculate_parity - @payload = payload - - @content_length = payload.length - end - - def header - [ - Protocol::PROTOCOL_VERSION, # char - @sequence_number, # uint16 - @type, # char - @content_length, # uint16 - @parity, # char - @peer_id, # char - ].pack(HEADER_PACKER) - end - - def calculate_parity - return 0 - end - - def encode - "#{header}#{@payload}" - end - - def decode(payload) - payload - end - end - end -end \ No newline at end of file diff --git a/lib/networking/packet_handlers/snapshot.rb b/lib/networking/packet_handlers/snapshot.rb index 49c6f7c..aec656c 100644 --- a/lib/networking/packet_handlers/snapshot.rb +++ b/lib/networking/packet_handlers/snapshot.rb @@ -1,6 +1,6 @@ class IMICFPS module Networking - class SnapshotPacket < Packet + class SnapshotPacket < CyberarmEngine::Networking::Packet end end end \ No newline at end of file