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
|
HARD_PEER_LIMIT = 254
|
||||||
|
|
||||||
def self.milliseconds
|
def self.milliseconds
|
||||||
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
||||||
end
|
end
|
||||||
|
|
||||||
# https://github.com/jpignata/blog/blob/master/articles/multicast-in-ruby.md
|
# https://github.com/jpignata/blog/blob/master/articles/multicast-in-ruby.md
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ class IMICFPS
|
|||||||
@read_buffer = ReadBuffer.new
|
@read_buffer = ReadBuffer.new
|
||||||
@packet_write_queue = []
|
@packet_write_queue = []
|
||||||
|
|
||||||
|
@peer_id = 0
|
||||||
|
|
||||||
@last_read_time = Networking.milliseconds
|
@last_read_time = Networking.milliseconds
|
||||||
@last_write_time = Networking.milliseconds
|
@last_write_time = Networking.milliseconds
|
||||||
|
|
||||||
@@ -48,10 +50,16 @@ class IMICFPS
|
|||||||
|
|
||||||
write
|
write
|
||||||
|
|
||||||
# puts "#{Networking.milliseconds} Total sent: #{@total_packets_sent} packets, #{@total_data_sent} data"
|
# 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"
|
# puts "#{Networking.milliseconds} Total received: #{@total_packets_received} packets, #{@total_data_received} data"
|
||||||
@read_buffer.reconstruct_packets.each do |packet, addr_info|
|
@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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -18,11 +18,11 @@ class IMICFPS
|
|||||||
return packets
|
return packets
|
||||||
end
|
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:)
|
def initialize(peer_id:, sequence:, type:, payload:)
|
||||||
@peer_id = peer_id
|
@peer_id = peer_id
|
||||||
@sequence_number = sequence
|
@sequence_number = sequence
|
||||||
@packet_type = type
|
@type = type
|
||||||
@parity = calculate_parity
|
@parity = calculate_parity
|
||||||
@payload = payload
|
@payload = payload
|
||||||
|
|
||||||
@@ -33,10 +33,10 @@ class IMICFPS
|
|||||||
[
|
[
|
||||||
Protocol::PROTOCOL_VERSION, # char
|
Protocol::PROTOCOL_VERSION, # char
|
||||||
@sequence_number, # uint16
|
@sequence_number, # uint16
|
||||||
@packet_type, # char
|
@type, # char
|
||||||
@content_length, # uint16
|
@content_length, # uint16
|
||||||
@parity, # char
|
@parity, # char
|
||||||
@peer_id, # char
|
@peer_id, # char
|
||||||
].pack(HEADER_PACKER)
|
].pack(HEADER_PACKER)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
class IMICFPS
|
class IMICFPS
|
||||||
module Networking
|
module Networking
|
||||||
class Peer
|
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
|
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
|
@address, @port = address, port
|
||||||
@peer_id = peer_id
|
|
||||||
|
|
||||||
@packet_write_queue = []
|
@packet_write_queue = []
|
||||||
@packet_read_queue = []
|
@packet_read_queue = []
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ class IMICFPS
|
|||||||
|
|
||||||
# protocol control packets
|
# protocol control packets
|
||||||
connect
|
connect
|
||||||
|
verify_connect
|
||||||
disconnect
|
disconnect
|
||||||
authenticate
|
authenticate
|
||||||
heartbeat
|
heartbeat
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class IMICFPS
|
|||||||
|
|
||||||
if true#packet.valid?
|
if true#packet.valid?
|
||||||
pairs << [packet, addr]
|
pairs << [packet, addr]
|
||||||
@buffer.delete(buffer)
|
@buffer.delete(hash)
|
||||||
else
|
else
|
||||||
puts "Invalid packet: #{packet}"
|
puts "Invalid packet: #{packet}"
|
||||||
@buffer.delete(buffer)
|
@buffer.delete(buffer)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class IMICFPS
|
|||||||
def create_peer(address:, port:)
|
def create_peer(address:, port:)
|
||||||
@peers.each_with_index do |peer, i|
|
@peers.each_with_index do |peer, i|
|
||||||
unless peer
|
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
|
@peers[i + 1] = new_peer
|
||||||
|
|
||||||
return new_peer
|
return new_peer
|
||||||
@@ -36,7 +36,7 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get_peer(peer_id)
|
def get_peer(peer_id)
|
||||||
@peers[peer_id + 1]
|
@peers[peer_id]
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_peer(peer_id)
|
def remove_peer(peer_id)
|
||||||
@@ -52,7 +52,7 @@ class IMICFPS
|
|||||||
if peer = get_peer(peer_id)
|
if peer = get_peer(peer_id)
|
||||||
packets = Packet.splinter(packet)
|
packets = Packet.splinter(packet)
|
||||||
|
|
||||||
packets.each { |pkt| peer.write_queue.add(pkt) }
|
packets.each { |pkt| peer.packet_write_queue.push(pkt) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ class IMICFPS
|
|||||||
@peers.each do |peer|
|
@peers.each do |peer|
|
||||||
next unless peer
|
next unless peer
|
||||||
|
|
||||||
send_packet(peer.peer_id, packet)
|
send_packet(peer.id, packet)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -82,12 +82,23 @@ class IMICFPS
|
|||||||
peer = nil
|
peer = nil
|
||||||
|
|
||||||
# initial connection
|
# 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])
|
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
|
else
|
||||||
peer = get_peer(peer.peer_id)
|
peer = get_peer(packet.peer_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# broadcast_packet(Packet.new(peer_id: 0, sequence: 0, type: Protocol::HEARTBEAT, payload: [Networking.milliseconds].pack("G")))
|
||||||
end
|
end
|
||||||
|
|
||||||
def close
|
def close
|
||||||
|
|||||||
Reference in New Issue
Block a user