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
|
class IMICFPS
|
||||||
module Networking
|
module Networking
|
||||||
class Client
|
class Client
|
||||||
attr_reader :uuid,
|
attr_reader :packets_sent, :packets_received,
|
||||||
:packets_sent, :packets_received,
|
|
||||||
:data_sent, :data_received
|
:data_sent, :data_received
|
||||||
def initialize(socket:)
|
def initialize(socket:)
|
||||||
@socket = socket
|
@socket = socket
|
||||||
@write_queue = []
|
@write_queue = []
|
||||||
@read_queue = []
|
@read_queue = []
|
||||||
|
|
||||||
@uuid = "not_defined"
|
|
||||||
|
|
||||||
@packets_sent = 0
|
@packets_sent = 0
|
||||||
@packets_received = 0
|
@packets_received = 0
|
||||||
@data_sent = 0
|
@data_sent = 0
|
||||||
@@ -18,9 +15,23 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def read
|
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
|
end
|
||||||
|
|
||||||
def write
|
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
|
end
|
||||||
|
|
||||||
def puts(packet)
|
def puts(packet)
|
||||||
@@ -28,7 +39,7 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def gets
|
def gets
|
||||||
@socket.recvfrom_nonblock(Protocol::MAX_PACKET_SIZE)
|
@read_queue.shift
|
||||||
end
|
end
|
||||||
|
|
||||||
def close
|
def close
|
||||||
|
|||||||
@@ -1,17 +1,43 @@
|
|||||||
class IMICFPS
|
class IMICFPS
|
||||||
module Networking
|
module Networking
|
||||||
class Packet
|
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
|
end
|
||||||
|
|
||||||
def self.encode(packet)
|
def initialize(sequence:, type:, payload:)
|
||||||
"#{packet.type}|#{packet.payload}"
|
@sequence_number = sequence
|
||||||
|
@packet_type = type
|
||||||
|
@content_length = payload.length
|
||||||
|
@parity = calculate_parity
|
||||||
|
@payload = payload
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.decode(string)
|
def header
|
||||||
split = string.split("|")
|
[
|
||||||
|
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
|
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
|
class IMICFPS
|
||||||
module Networking
|
module Networking
|
||||||
module Protocol
|
module Protocol
|
||||||
MAX_CLIENTS = 32
|
|
||||||
MAX_PACKET_SIZE = 1024
|
MAX_PACKET_SIZE = 1024
|
||||||
PROTOCOL_VERSION = 0 # int
|
PROTOCOL_VERSION = 0 # int
|
||||||
HEARTBEAT_INTERVAL = 250 # ms
|
HEARTBEAT_INTERVAL = 250 # ms
|
||||||
@@ -22,11 +21,13 @@ class IMICFPS
|
|||||||
heartbeat
|
heartbeat
|
||||||
|
|
||||||
# game data packets
|
# game data packets
|
||||||
client_connected
|
snapshot
|
||||||
client_disconnected
|
player_joined
|
||||||
entity_move
|
player_left
|
||||||
play_sound_effect
|
play_sound_effect
|
||||||
create_particle
|
create_particle
|
||||||
|
create_entity
|
||||||
|
remove_entity
|
||||||
}
|
}
|
||||||
|
|
||||||
# emulate c-like enum
|
# emulate c-like enum
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
class IMICFPS
|
class IMICFPS
|
||||||
module Networking
|
module Networking
|
||||||
MAX_CLIENTS = 32
|
|
||||||
|
|
||||||
class Server
|
class Server
|
||||||
|
MAX_CLIENTS = 32
|
||||||
|
|
||||||
attr_reader :hostname, :port, :max_clients, :clients
|
attr_reader :hostname, :port, :max_clients, :clients
|
||||||
def initialize(hostname:, port:, max_clients: MAX_CLIENTS)
|
def initialize(hostname:, port:, max_clients: MAX_CLIENTS)
|
||||||
@hostname = hostname
|
@hostname = hostname
|
||||||
|
|||||||
@@ -109,13 +109,5 @@ eos
|
|||||||
|
|
||||||
@camera.button_up(id)
|
@camera.button_up(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def needs_cursor?
|
|
||||||
@needs_cursor
|
|
||||||
end
|
|
||||||
|
|
||||||
def lose_focus
|
|
||||||
puts 'Bye'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user