Class: GameOverseer::ENetServer

Inherits:
Object
  • Object
show all
Defined in:
lib/gameoverseer/server/renet_server.rb

Overview

GameOverseers’ connection to the world

This server uses the renet library, which is C bindings for the Enet networking library

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, packet_handler, encryption_handler, max_clients = 4, channels = 4, download_bandwidth = 0, upload_bandwidth = 0) ⇒ Thread

Parameters:

  • host (String)

    host or ip for the server to run on

  • port (Integer)

    port for the server to run on

  • max_clients (Integer) (defaults to: 4)

    max number of clients that can be connected at one time

  • channels (Integer) (defaults to: 4)

    number of channels (See Enet documentation)

  • download_bandwidth (Integer) (defaults to: 0)

    max bandwidth for downloading per-second (0 is unlimited)

  • upload_bandwidth (Integer) (defaults to: 0)

    max bandwidth for uploading per-second (0 is unlimited)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gameoverseer/server/renet_server.rb', line 14

def initialize(host, port, packet_handler, encryption_handler, max_clients = 4, channels = 4, download_bandwidth = 0, upload_bandwidth = 0)
  GameOverseer::Console.log("Server> Started on: #{host}:#{port}.")
  GameOverseer::Services.enable
  GameOverseer::ENetServer.instance = self

  @message_manager = GameOverseer::MessageManager.instance
  @channel_manager = GameOverseer::ChannelManager.instance
  @client_manager = GameOverseer::ClientManager.instance
  @packet_handler = packet_handler.new
  @encryption_handler = encryption_handler.instance if encryption_handler

  @server = ENet::Server.new(port, max_clients, channels, download_bandwidth, upload_bandwidth) # Port, max clients, channels, download bandwidth, upload bandwith
  @server.use_compression(true)
  @terminate = false

  @server.on_connection(method(:on_connect))
  @server.on_packet_receive(method(:on_packet))
  @server.on_disconnection(method(:on_disconnect))

  run
end

Class Method Details

.instanceObject



111
112
113
# File 'lib/gameoverseer/server/renet_server.rb', line 111

def self.instance
  @instance
end

.instance=(_instance) ⇒ Object



115
116
117
# File 'lib/gameoverseer/server/renet_server.rb', line 115

def self.instance=(_instance)
  @instance = _instance
end

Instance Method Details

#broadcast(message, reliable = false, channel = ChannelManager::CHAT) ⇒ Object

send message to all connected clients

Parameters:

  • message (String)

    message to be sent to clients

  • reliable (Boolean) (defaults to: false)

    whether or not the packet is guaranteed to be received by the clients

  • channel (Integer) (defaults to: ChannelManager::CHAT)

    what channel to send on



82
83
84
# File 'lib/gameoverseer/server/renet_server.rb', line 82

def broadcast(message, reliable = false, channel = ChannelManager::CHAT)
  @server.broadcast_packet(message, reliable, channel)
end

#handle_connection(client_id, data, channel) ⇒ Object

Handles received packets from clients and sends them through the PacketHandler for pre-processing, then sends it on to #process_data

Parameters:

  • client_id (Integer)
  • data (String)

    data received from client

  • channel (Integer)

    channel that this packet was sent along



97
98
99
100
101
102
103
104
105
# File 'lib/gameoverseer/server/renet_server.rb', line 97

def handle_connection(client_id, data, channel)
  _data = @packet_handler.receive(client_id, data)
  if _data
    process_data(client_id, _data)
  else
    # TODO: Better error handling :D
    transmit(client_id, '{"channel":"_error", "mode":"_error", "data":{"code":400, "message":"something went wrong, likely bad data!"}}', true, ChannelManager::FAULT)
  end
end

#on_connect(client_id, ip_address) ⇒ Object

callled when a client connects

Parameters:

  • client_id (Integer)

    ID of client

  • ip_address (String)

    address of client



59
60
61
# File 'lib/gameoverseer/server/renet_server.rb', line 59

def on_connect(client_id, ip_address)
  @client_manager.add(client_id, ip_address)
end

#on_disconnect(client_id) ⇒ Object

callled when a client disconnects

Parameters:

  • client_id (Integer)

    ID of client



65
66
67
# File 'lib/gameoverseer/server/renet_server.rb', line 65

def on_disconnect(client_id)
  @client_manager.remove(client_id)
end

#on_packet(client_id, data, channel) ⇒ Object

Called when a packet is received

Parameters:

  • client_id (Integer)

    ID of client

  • data (String)

    data client sent

  • channel (Integer)

    channel that this was sent to



52
53
54
# File 'lib/gameoverseer/server/renet_server.rb', line 52

def on_packet(client_id, data, channel)
  handle_connection(client_id, data, channel)
end

#process_data(client_id, data) ⇒ Object

send data to the InputHandler for processing

Parameters:

  • data (Hash)
  • client_id (Integer)

    ID of client that sent the data



89
90
91
# File 'lib/gameoverseer/server/renet_server.rb', line 89

def process_data(client_id, data)
  GameOverseer::InputHandler.process_data(client_id, data)
end

#runThread

Runs the server in a Thread,, in a loop, calling update on the server.

Returns:

  • (Thread)


39
40
41
42
43
44
45
46
# File 'lib/gameoverseer/server/renet_server.rb', line 39

def run
  Thread.new {
    loop do
      @server.update(1000)
      break if @terminate
    end
  }
end

#terminateObject



107
108
109
# File 'lib/gameoverseer/server/renet_server.rb', line 107

def terminate
  @terminate = true
end

#transmit(client_id, message, reliable = false, channel = ChannelManager::CHAT) ⇒ Object

send message to a specific client

Parameters:

  • client_id (Integer)

    ID of client

  • message (String)

    message to be sent to client

  • reliable (Boolean) (defaults to: false)

    whether or not the packet is guaranteed to be received by the client

  • channel (Integer) (defaults to: ChannelManager::CHAT)

    what channel to send on



74
75
76
# File 'lib/gameoverseer/server/renet_server.rb', line 74

def transmit(client_id, message, reliable = false, channel = ChannelManager::CHAT)
  @server.send_packet(client_id, message, reliable, channel)
end