Class: ENet::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-enet/renet/client.rb

Defined Under Namespace

Classes: Address

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(peer) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ffi-enet/renet/client.rb', line 17

def initialize(peer)
  @_peer = peer

  @id = @_peer[:connect_id]
  @address = Address.new(Client.enet_host_address_to_ipv4(@_peer[:address]), @_peer[:address][:port])

  @last_send_time = 0
  @last_receive_time = 0
  @last_round_trip_time = 0
  @round_trip_time = 0
  @packets_lost = 0
  @packet_loss = 0

  @total_sent_packets = 0
  @total_sent_data = 0
  @total_received_packets = 0
  @total_received_data = 0

  update_stats
end

Instance Attribute Details

#_peerObject (readonly)

Returns the value of attribute _peer.



5
6
7
# File 'lib/ffi-enet/renet/client.rb', line 5

def _peer
  @_peer
end

#addressObject (readonly)

Returns the value of attribute address.



5
6
7
# File 'lib/ffi-enet/renet/client.rb', line 5

def address
  @address
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/ffi-enet/renet/client.rb', line 5

def id
  @id
end

#last_receive_timeObject (readonly)

Returns the value of attribute last_receive_time.



6
7
8
# File 'lib/ffi-enet/renet/client.rb', line 6

def last_receive_time
  @last_receive_time
end

#last_round_trip_timeObject (readonly)

Returns the value of attribute last_round_trip_time.



6
7
8
# File 'lib/ffi-enet/renet/client.rb', line 6

def last_round_trip_time
  @last_round_trip_time
end

#last_send_timeObject (readonly)

Returns the value of attribute last_send_time.



6
7
8
# File 'lib/ffi-enet/renet/client.rb', line 6

def last_send_time
  @last_send_time
end

#packet_lossObject (readonly)

Returns the value of attribute packet_loss.



6
7
8
# File 'lib/ffi-enet/renet/client.rb', line 6

def packet_loss
  @packet_loss
end

#packets_lostObject (readonly)

Returns the value of attribute packets_lost.



6
7
8
# File 'lib/ffi-enet/renet/client.rb', line 6

def packets_lost
  @packets_lost
end

#round_trip_timeObject (readonly)

Returns the value of attribute round_trip_time.



6
7
8
# File 'lib/ffi-enet/renet/client.rb', line 6

def round_trip_time
  @round_trip_time
end

#total_received_dataObject (readonly)

Returns the value of attribute total_received_data.



6
7
8
# File 'lib/ffi-enet/renet/client.rb', line 6

def total_received_data
  @total_received_data
end

#total_received_packetsObject (readonly)

Returns the value of attribute total_received_packets.



6
7
8
# File 'lib/ffi-enet/renet/client.rb', line 6

def total_received_packets
  @total_received_packets
end

#total_sent_dataObject (readonly)

Returns the value of attribute total_sent_data.



6
7
8
# File 'lib/ffi-enet/renet/client.rb', line 6

def total_sent_data
  @total_sent_data
end

#total_sent_packetsObject (readonly)

Returns the value of attribute total_sent_packets.



6
7
8
# File 'lib/ffi-enet/renet/client.rb', line 6

def total_sent_packets
  @total_sent_packets
end

Class Method Details

.enet_host_address_to_ipv4(address) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/ffi-enet/renet/client.rb', line 9

def self.enet_host_address_to_ipv4(address)
  str_ptr = FFI::MemoryPointer.new(:char, 256)
  result = LibENet.enet_address_get_host_ip(address, str_ptr, str_ptr.size)
  raise "Failed to get IPv4 address of enet address" unless result.zero?

  str_ptr.read_string
end

Instance Method Details

#update_statsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ffi-enet/renet/client.rb', line 38

def update_stats
  # enet Peer data
  @last_send_time = @_peer[:last_send_time]
  @last_receive_time = @_peer[:last_send_time]
  @last_round_trip_time = @_peer[:last_round_trip_time]
  @round_trip_time = @_peer[:round_trip_time]
  @packets_lost = @_peer[:packets_lost]
  @packet_loss = @_peer[:packet_loss] # .to_f / LibENet::ENET_PEER_PACKET_LOSS_SCALE

  # enet Host data
  @total_sent_packets += @_peer[:host][:total_sent_packets]
  @total_sent_data += @_peer[:host][:total_sent_data]
  @total_received_packets += @_peer[:host][:total_received_packets]
  @total_received_data += @_peer[:host][:total_received_data]

  @_peer[:host][:total_sent_packets] = 0
  @_peer[:host][:total_sent_data] = 0
  @_peer[:host][:total_received_packets] = 0
  @_peer[:host][:total_received_data] = 0
end