Class: ENet::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, channels: 8, download_bandwidth: 0, upload_bandwidth: 0) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ffi-enet/renet/connection.rb', line 5

def initialize(host:, port:, channels: 8, download_bandwidth: 0, upload_bandwidth: 0)
  @host = host
  @port = port
  @channels = channels
  @download_bandwidth = download_bandwidth
  @upload_bandwidth = upload_bandwidth

  @enet_event = LibENet::ENetEvent.new
  @online = false

  ENet.init

  @_address = LibENet::ENetAddress.new
  if LibENet.enet_address_set_host(@_address, @host) != 0
    raise "Failed to set host"
  end
  @_address[:port] = @port

  @_host = LibENet.enet_host_create(nil, 1, @channels, @download_bandwidth, @upload_bandwidth)

  raise "Failed to create host" if @_host.nil?

  @client = nil
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/ffi-enet/renet/connection.rb', line 3

def client
  @client
end

Instance Method Details

#connect(timeout_ms) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ffi-enet/renet/connection.rb', line 34

def connect(timeout_ms)
  @_connection = LibENet.enet_host_connect(@_host, @_address, @channels, 0)
  raise "Cannot connect to remote host" if @_connection.nil?

  result = LibENet.enet_host_service(@_host, @enet_event, timeout_ms)

  if result.positive? && @enet_event[:type] == :ENET_EVENT_TYPE_CONNECT
    @online = true
    @client = Client.new(@_connection)

    on_connection
  end
end

#disconnect(timeout_ms) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ffi-enet/renet/connection.rb', line 48

def disconnect(timeout_ms)
  connected = online?

  if connected
    LibENet.enet_peer_disconnect(@_connection, 0)

    while (result = LibENet.enet_host_service(@_host, @enet_event, 0)) && result.positive?

      case @enet_event[:type]
      when :ENET_EVENT_TYPE_RECEIVE
        LibENet.enet_packet_destroy(@enet_event[:packet])

      when :ENET_EVENT_TYPE_DISCONNECT
        connected = false
      end

      break unless connected
    end

    LibENet.enet_peer_disconnect_now(@_connection, 0) if connected
  end

  connected
end

#flushObject



84
85
86
# File 'lib/ffi-enet/renet/connection.rb', line 84

def flush
  send_queued_packets
end

#on_connectionObject



118
119
# File 'lib/ffi-enet/renet/connection.rb', line 118

def on_connection
end

#on_disconnectionObject



124
125
# File 'lib/ffi-enet/renet/connection.rb', line 124

def on_disconnection
end

#on_packet_received(data, channel) ⇒ Object



121
122
# File 'lib/ffi-enet/renet/connection.rb', line 121

def on_packet_received(data, channel)
end

#online?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/ffi-enet/renet/connection.rb', line 30

def online?
  @online
end

#send_packet(data, reliable:, channel:) ⇒ Object



73
74
75
76
77
78
# File 'lib/ffi-enet/renet/connection.rb', line 73

def send_packet(data, reliable:, channel:)
  return unless online?

  packet = LibENet.enet_packet_create(data, data.length, reliable ? 1 : 0)
  LibENet.enet_peer_send(@_connection, channel, packet)
end

#send_queued_packetsObject



80
81
82
# File 'lib/ffi-enet/renet/connection.rb', line 80

def send_queued_packets
  LibENet.enet_host_flush(@_host) if online?
end

#update(timeout_ms) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ffi-enet/renet/connection.rb', line 88

def update(timeout_ms)
  result = LibENet.enet_host_service(@_host, @enet_event, timeout_ms)

  if result.positive?
    case @enet_event[:type]
    when :ENET_EVENT_TYPE_RECEIVE
      data = @enet_event[:packet][:data].read_string(@enet_event[:packet][:length])

      on_packet_received(data, @enet_event[:channel_id])

      LibENet.enet_packet_destroy(@enet_event[:packet])

    when :ENET_EVENT_TYPE_DISCONNECT
      @online = false
    end
  elsif result.negative?
    warn "Connection: An error occurred: #{result}"
  end

  @client.update_stats if online?
end

#use_compression(bool) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/ffi-enet/renet/connection.rb', line 110

def use_compression(bool)
  if bool
    LibENet.enet_host_compress_with_range_coder(@_host)
  else
    LibENet.enet_host_compress(@_host, nil)
  end
end