Class: ENet::Server

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, max_clients: 16, channels: 8, download_bandwidth: 0, upload_bandwidth: 0) ⇒ Server

Returns a new instance of Server.



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

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

  @enet_event = LibENet::ENetEvent.new
  @clients = {}

  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 = FFI::AutoPointer.new(LibENet.enet_host_create(@_address, @max_clients, @channels, @download_bandwidth, @upload_bandwidth).to_ptr, Server.method(:release))
  raise "Failed to create server" if @_host.nil?
end

Class Method Details

.release(pointer) ⇒ Object



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

def self.release(pointer)
end

Instance Method Details

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



60
61
62
63
# File 'lib/ffi-enet/renet/server.rb', line 60

def broadcast_packet(data, reliable:, channel:)
  packet = LibENet.enet_packet_create(data, data.length, reliable ? 1 : 0)
  LibENet.enet_host_broadcast(@_host, channel, packet)
end

#disconnect_client(client) ⇒ Object



29
30
31
# File 'lib/ffi-enet/renet/server.rb', line 29

def disconnect_client(client)
  LibENet.enet_peer_disconnect(client._peer, 0)
end

#disconnect_client!(client) ⇒ Object



33
34
35
36
37
# File 'lib/ffi-enet/renet/server.rb', line 33

def disconnect_client!(client)
  LibENet.enet_peer_disconnect_now(client._peer, 0)

  on_disconnection(client)
end

#flushObject



69
70
71
# File 'lib/ffi-enet/renet/server.rb', line 69

def flush
  send_queued_packets
end

#on_connection(client) ⇒ Object



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

def on_connection(client)
end

#on_disconnection(client) ⇒ Object



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

def on_disconnection(client)
end

#on_packet_received(client, data, channel) ⇒ Object



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

def on_packet_received(client, data, channel)
end

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



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

def send_packet(client, data, reliable:, channel:)
  packet = LibENet.enet_packet_create(data, data.length, reliable ? 1 : 0)
  LibENet.enet_peer_send(client._peer, channel, packet)

  # pp [
  #   client.packets_sent,
  #   client.packets_received,
  #   client.data_sent,
  #   client.data_received,

  #   client.packets_lost,
  #   client.packet_loss,

  #   client.round_trip_time,
  #   client.last_round_trip_time,

  #   client.last_receive_time,
  #   client.last_send_time
  # ]
end

#send_queued_packetsObject



65
66
67
# File 'lib/ffi-enet/renet/server.rb', line 65

def send_queued_packets
  LibENet.enet_host_flush(@_host)
end

#shutdownObject



105
106
107
108
# File 'lib/ffi-enet/renet/server.rb', line 105

def shutdown
  LibENet.enet_host_flush(@_host)
  LibENet.enet_host_destroy(@_host)
end

#update(timeout_ms) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ffi-enet/renet/server.rb', line 73

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_CONNECT
      client = Client.new(@enet_event[:peer])
      @clients[client] = @enet_event[:peer]

      on_connection(client)

    when :ENET_EVENT_TYPE_RECEIVE
      client = @clients.find { |k, peer| peer.to_ptr == @enet_event[:peer].to_ptr }.first
      data = @enet_event[:packet][:data].read_string(@enet_event[:packet][:length])

      client.update_stats
      on_packet_received(client, data, @enet_event[:channel_id])

      LibENet.enet_packet_destroy(@enet_event[:packet])

    when :ENET_EVENT_TYPE_DISCONNECT
      # FIXME: This might not work if peer pointer is invalid
      client = @clients.find { |k, peer| peer.to_ptr == @enet_event[:peer].to_ptr }.first
      @clients.delete(client)

      on_disconnection(client)
    end
  elsif result.negative?
    warn "Server: An error occurred: #{result}"
  end
end

#use_compression(bool) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/ffi-enet/renet/server.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