mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
More fleshing out of networking
This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class Client
|
||||
attr_reader :uuid,
|
||||
:packets_sent, :packets_received,
|
||||
attr_reader :packets_sent, :packets_received,
|
||||
:data_sent, :data_received
|
||||
def initialize(socket:)
|
||||
@socket = socket
|
||||
@write_queue = []
|
||||
@read_queue = []
|
||||
|
||||
@uuid = "not_defined"
|
||||
|
||||
@packets_sent = 0
|
||||
@packets_received = 0
|
||||
@data_sent = 0
|
||||
@@ -18,9 +15,23 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def read
|
||||
data = @socket.recvfrom_nonblock(Protocol::MAX_PACKET_SIZE)
|
||||
@read_queue << Packet.decode(data)
|
||||
|
||||
@packets_received += 1
|
||||
@data_received += data.length
|
||||
rescue IO::WaitReadable
|
||||
end
|
||||
|
||||
def write
|
||||
@write_queue.each do |packet|
|
||||
raw = Packet.encode
|
||||
@socket.send(raw, 0)
|
||||
@write_queue.delete(packet)
|
||||
|
||||
@packets_sent += 1
|
||||
@data_sent += raw.length
|
||||
end
|
||||
end
|
||||
|
||||
def puts(packet)
|
||||
@@ -28,7 +39,7 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def gets
|
||||
@socket.recvfrom_nonblock(Protocol::MAX_PACKET_SIZE)
|
||||
@read_queue.shift
|
||||
end
|
||||
|
||||
def close
|
||||
|
||||
@@ -1,17 +1,43 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class Packet
|
||||
def initialize(type:, payload:)
|
||||
HEADER_PACKER = "CnCnC"
|
||||
HEADER_SIZE = 7
|
||||
|
||||
def self.from_stream(raw)
|
||||
header = raw[ 0..HEADER_SIZE ].unpack(HEADER_PACKER)
|
||||
payload = raw[HEADER_SIZE + 1..raw.length - 1]
|
||||
|
||||
new(header[1], [2], payload)
|
||||
end
|
||||
|
||||
def self.encode(packet)
|
||||
"#{packet.type}|#{packet.payload}"
|
||||
def initialize(sequence:, type:, payload:)
|
||||
@sequence_number = sequence
|
||||
@packet_type = type
|
||||
@content_length = payload.length
|
||||
@parity = calculate_parity
|
||||
@payload = payload
|
||||
end
|
||||
|
||||
def self.decode(string)
|
||||
split = string.split("|")
|
||||
def header
|
||||
[
|
||||
Protocol::PROTOCOL_VERSION, # char
|
||||
@sequence_number, # uint16
|
||||
@packet_type, # char
|
||||
@content_length, # uint16
|
||||
@parity, # char
|
||||
].unpack(HEADER_PACKER)
|
||||
end
|
||||
|
||||
Packet.new(split.first, split.last)
|
||||
def calculate_parity
|
||||
return 0
|
||||
end
|
||||
|
||||
def encode
|
||||
"#{header}#{@payload}"
|
||||
end
|
||||
|
||||
def decode(payload)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
6
lib/networking/packet_handlers/snapshot.rb
Normal file
6
lib/networking/packet_handlers/snapshot.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class SnapshotPacket < Packet
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,7 +1,6 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
module Protocol
|
||||
MAX_CLIENTS = 32
|
||||
MAX_PACKET_SIZE = 1024
|
||||
PROTOCOL_VERSION = 0 # int
|
||||
HEARTBEAT_INTERVAL = 250 # ms
|
||||
@@ -22,11 +21,13 @@ class IMICFPS
|
||||
heartbeat
|
||||
|
||||
# game data packets
|
||||
client_connected
|
||||
client_disconnected
|
||||
entity_move
|
||||
snapshot
|
||||
player_joined
|
||||
player_left
|
||||
play_sound_effect
|
||||
create_particle
|
||||
create_entity
|
||||
remove_entity
|
||||
}
|
||||
|
||||
# emulate c-like enum
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class Server
|
||||
MAX_CLIENTS = 32
|
||||
|
||||
class Server
|
||||
attr_reader :hostname, :port, :max_clients, :clients
|
||||
def initialize(hostname:, port:, max_clients: MAX_CLIENTS)
|
||||
@hostname = hostname
|
||||
|
||||
@@ -109,13 +109,5 @@ eos
|
||||
|
||||
@camera.button_up(id)
|
||||
end
|
||||
|
||||
def needs_cursor?
|
||||
@needs_cursor
|
||||
end
|
||||
|
||||
def lose_focus
|
||||
puts 'Bye'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user