BROKEN: Work on netcode refactor

This commit is contained in:
2020-08-01 08:06:05 -05:00
parent 87b4b8ef92
commit e09bd06d24
9 changed files with 106 additions and 99 deletions

View File

@@ -1,2 +1,6 @@
class Channel
module CyberarmEngine
module Networking
class Channel
end
end
end

View File

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

View File

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

View File

@@ -0,0 +1,6 @@
module CyberarmEngine
module Networking
module PacketHandler
end
end
end

View File

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

View File

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

View File

@@ -0,0 +1,30 @@
module CyberarmEngine
module Networking
module Protocol
MAX_PACKET_SIZE = 1024
PROTOCOL_VERSION = 0 # int
HEARTBEAT_INTERVAL = 5_000 # ms
TIMEOUT_PERIOD = 30_000 # ms
packet_types = %w{
# protocol packets
reliable
multipart
control
data
# control packet types
disconnect
acknowledge
heartbeat
ping
}
# emulate c-like enum
packet_types.each_with_index do |type, i|
next if type.start_with?("#")
self.const_set(:"#{type.upcase}", i)
end
end
end
end