mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Fixed Networking.milliseconds not returning milliseconds, fixed ReadBuffer not deleting processed buffers, misc.
This commit is contained in:
@@ -15,7 +15,7 @@ class IMICFPS
|
||||
HARD_PEER_LIMIT = 254
|
||||
|
||||
def self.milliseconds
|
||||
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
||||
end
|
||||
|
||||
# https://github.com/jpignata/blog/blob/master/articles/multicast-in-ruby.md
|
||||
|
||||
@@ -11,6 +11,8 @@ class IMICFPS
|
||||
@read_buffer = ReadBuffer.new
|
||||
@packet_write_queue = []
|
||||
|
||||
@peer_id = 0
|
||||
|
||||
@last_read_time = Networking.milliseconds
|
||||
@last_write_time = Networking.milliseconds
|
||||
|
||||
@@ -51,7 +53,13 @@ class IMICFPS
|
||||
# puts "#{Networking.milliseconds} Total sent: #{@total_packets_sent} packets, #{@total_data_sent} data"
|
||||
# puts "#{Networking.milliseconds} Total received: #{@total_packets_received} packets, #{@total_data_received} data"
|
||||
@read_buffer.reconstruct_packets.each do |packet, addr_info|
|
||||
# @packet_write_queue.push( packet )
|
||||
if packet.peer_id == 0 && packet.type == Protocol::VERIFY_CONNECT
|
||||
@peer_id = packet.payload.unpack1("C")
|
||||
end
|
||||
end
|
||||
|
||||
if @peer_id > 0 && Networking.milliseconds - @last_read_time >= Protocol::HEARTBEAT_INTERVAL
|
||||
send_packet(Packet.new(peer_id: @peer_id, sequence: 0, type: Protocol::HEARTBEAT, payload: ""))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -18,11 +18,11 @@ class IMICFPS
|
||||
return packets
|
||||
end
|
||||
|
||||
attr_reader :peer_id, :sequence_number, :packet_type, :parity, :payload, :content_length
|
||||
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
|
||||
@packet_type = type
|
||||
@type = type
|
||||
@parity = calculate_parity
|
||||
@payload = payload
|
||||
|
||||
@@ -33,7 +33,7 @@ class IMICFPS
|
||||
[
|
||||
Protocol::PROTOCOL_VERSION, # char
|
||||
@sequence_number, # uint16
|
||||
@packet_type, # char
|
||||
@type, # char
|
||||
@content_length, # uint16
|
||||
@parity, # char
|
||||
@peer_id, # char
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class Peer
|
||||
attr_reader :address, :port, :packet_read_queue, :packet_write_queue
|
||||
attr_reader :id, :address, :port, :packet_read_queue, :packet_write_queue
|
||||
attr_accessor :total_packets_sent, :total_packets_received, :total_data_sent, :total_data_received, :last_read_time, :last_write_time
|
||||
def initialize(peer_id:, address:, port:)
|
||||
def initialize(id:, address:, port:)
|
||||
@id = id
|
||||
@address, @port = address, port
|
||||
@peer_id = peer_id
|
||||
|
||||
@packet_write_queue = []
|
||||
@packet_read_queue = []
|
||||
|
||||
@@ -16,6 +16,7 @@ class IMICFPS
|
||||
|
||||
# protocol control packets
|
||||
connect
|
||||
verify_connect
|
||||
disconnect
|
||||
authenticate
|
||||
heartbeat
|
||||
|
||||
@@ -18,7 +18,7 @@ class IMICFPS
|
||||
|
||||
if true#packet.valid?
|
||||
pairs << [packet, addr]
|
||||
@buffer.delete(buffer)
|
||||
@buffer.delete(hash)
|
||||
else
|
||||
puts "Invalid packet: #{packet}"
|
||||
@buffer.delete(buffer)
|
||||
|
||||
@@ -25,7 +25,7 @@ class IMICFPS
|
||||
def create_peer(address:, port:)
|
||||
@peers.each_with_index do |peer, i|
|
||||
unless peer
|
||||
new_peer = Peer.new(peer_id: i + 1, address: address, port: port)
|
||||
new_peer = Peer.new(id: i + 1, address: address, port: port)
|
||||
@peers[i + 1] = new_peer
|
||||
|
||||
return new_peer
|
||||
@@ -36,7 +36,7 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def get_peer(peer_id)
|
||||
@peers[peer_id + 1]
|
||||
@peers[peer_id]
|
||||
end
|
||||
|
||||
def remove_peer(peer_id)
|
||||
@@ -52,7 +52,7 @@ class IMICFPS
|
||||
if peer = get_peer(peer_id)
|
||||
packets = Packet.splinter(packet)
|
||||
|
||||
packets.each { |pkt| peer.write_queue.add(pkt) }
|
||||
packets.each { |pkt| peer.packet_write_queue.push(pkt) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -60,7 +60,7 @@ class IMICFPS
|
||||
@peers.each do |peer|
|
||||
next unless peer
|
||||
|
||||
send_packet(peer.peer_id, packet)
|
||||
send_packet(peer.id, packet)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -82,12 +82,23 @@ class IMICFPS
|
||||
peer = nil
|
||||
|
||||
# initial connection
|
||||
if packet.peer_id == 0
|
||||
if packet.peer_id == 0 && packet.type == Protocol::CONNECT
|
||||
peer = create_peer(address: addr_info[2], port: addr_info[1])
|
||||
send_packet(
|
||||
peer.id,
|
||||
Packet.new(
|
||||
peer_id: 0,
|
||||
sequence: 0,
|
||||
type: Protocol::VERIFY_CONNECT,
|
||||
payload: [peer.id].pack("C")
|
||||
)
|
||||
)
|
||||
else
|
||||
peer = get_peer(peer.peer_id)
|
||||
peer = get_peer(packet.peer_id)
|
||||
end
|
||||
end
|
||||
|
||||
# broadcast_packet(Packet.new(peer_id: 0, sequence: 0, type: Protocol::HEARTBEAT, payload: [Networking.milliseconds].pack("G")))
|
||||
end
|
||||
|
||||
def close
|
||||
|
||||
Reference in New Issue
Block a user