Class: GameOverseer::ENetServer
- Inherits:
-
Object
- Object
- GameOverseer::ENetServer
- 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
-
#broadcast(message, reliable = false, channel = ChannelManager::CHAT) ⇒ Object
send message to all connected clients.
-
#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.
- #initialize(host, port, packet_handler, encryption_handler, max_clients = 4, channels = 4, download_bandwidth = 0, upload_bandwidth = 0) ⇒ Thread constructor
-
#on_connect(client_id, ip_address) ⇒ Object
callled when a client connects.
-
#on_disconnect(client_id) ⇒ Object
callled when a client disconnects.
-
#on_packet(client_id, data, channel) ⇒ Object
Called when a packet is received.
-
#process_data(client_id, data) ⇒ Object
send data to the InputHandler for processing.
-
#run ⇒ Thread
Runs the server in a Thread,, in a loop, calling update on the server.
- #terminate ⇒ Object
-
#transmit(client_id, message, reliable = false, channel = ChannelManager::CHAT) ⇒ Object
send message to a specific client.
Constructor Details
#initialize(host, port, packet_handler, encryption_handler, max_clients = 4, channels = 4, download_bandwidth = 0, upload_bandwidth = 0) ⇒ Thread
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
.instance ⇒ Object
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
82 83 84 |
# File 'lib/gameoverseer/server/renet_server.rb', line 82 def broadcast(, reliable = false, channel = ChannelManager::CHAT) @server.broadcast_packet(, 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
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
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
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
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
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 |
#run ⇒ Thread
Runs the server in a Thread,, in a loop, calling update on the server.
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 |
#terminate ⇒ Object
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
74 75 76 |
# File 'lib/gameoverseer/server/renet_server.rb', line 74 def transmit(client_id, , reliable = false, channel = ChannelManager::CHAT) @server.send_packet(client_id, , reliable, channel) end |