gameoverseer 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,35 +1,35 @@
1
- module GameOverseer
2
- class PacketHandler
3
-
4
- def initialize
5
- PacketHandler.instance = self
6
- end
7
-
8
- def pre_processor(packet, sending)
9
- data = nil
10
- if sending
11
- data = MultiJson.dump(packet)
12
- else
13
- data = MultiJson.load(packet)
14
- end
15
-
16
- return data
17
- end
18
-
19
- def receive(client_id, packet)
20
- _packet = pre_processor(packet, false)
21
- end
22
-
23
- def transmit(client_id, data)
24
- _packet = pre_processor(data, true)
25
- end
26
-
27
- def self.instance
28
- @instance
29
- end
30
-
31
- def self.instance=(_instance)
32
- @instance = _instance
33
- end
34
- end
35
- end
1
+ module GameOverseer
2
+ class PacketHandler
3
+
4
+ def initialize
5
+ PacketHandler.instance = self
6
+ end
7
+
8
+ def pre_processor(packet, sending)
9
+ data = nil
10
+ if sending
11
+ data = MultiJson.dump(packet)
12
+ else
13
+ data = MultiJson.load(packet)
14
+ end
15
+
16
+ return data
17
+ end
18
+
19
+ def receive(client_id, packet)
20
+ _packet = pre_processor(packet, false)
21
+ end
22
+
23
+ def transmit(client_id, data)
24
+ _packet = pre_processor(data, true)
25
+ end
26
+
27
+ def self.instance
28
+ @instance
29
+ end
30
+
31
+ def self.instance=(_instance)
32
+ @instance = _instance
33
+ end
34
+ end
35
+ end
@@ -1,126 +1,126 @@
1
- module GameOverseer
2
-
3
- # GameOverseers' connection to the world
4
- #
5
- # This server uses the renet library, which is C bindings for the Enet networking library
6
- class ENetServer
7
- # @param host [String] host or ip for the server to run on
8
- # @param port [Integer] port for the server to run on
9
- # @param max_clients [Integer] max number of clients that can be connected at one time
10
- # @param channels [Integer] number of channels (See Enet documentation)
11
- # @param download_bandwidth [Integer] max bandwidth for downloading per-second (0 is unlimited)
12
- # @param upload_bandwidth [Integer] max bandwidth for uploading per-second (0 is unlimited)
13
- # @return [Thread]
14
- def initialize(host, port, packet_handler, encryption_handler, max_clients = 4, channels = 4, download_bandwidth = 0, upload_bandwidth = 0)
15
- GameOverseer::Console.log("Server> Started on: #{host}:#{port}.")
16
- GameOverseer::Services.enable
17
- GameOverseer::ENetServer.instance = self
18
-
19
- @message_manager = GameOverseer::MessageManager.instance
20
- @channel_manager = GameOverseer::ChannelManager.instance
21
- @client_manager = GameOverseer::ClientManager.instance
22
- @packet_handler = packet_handler.new
23
- @encryption_handler = encryption_handler.instance if encryption_handler
24
-
25
- @server = ENet::Server.new(port, max_clients, channels, download_bandwidth, upload_bandwidth) # Port, max clients, channels, download bandwidth, upload bandwith
26
- @server.use_compression(true)
27
- @terminate = false
28
-
29
- @server.on_connection(method(:on_connect))
30
- @server.on_packet_receive(method(:on_packet))
31
- @server.on_disconnection(method(:on_disconnect))
32
-
33
- run
34
- end
35
-
36
- # Runs the server in a Thread,, in a loop, calling update on the server.
37
- #
38
- # @return [Thread]
39
- def run
40
- Thread.new {
41
- loop do
42
- @server.update(1000)
43
- break if @terminate
44
- end
45
- }
46
- end
47
-
48
- # Called when a packet is received
49
- # @param client_id [Integer] ID of client
50
- # @param data [String] data client sent
51
- # @param channel [Integer] channel that this was sent to
52
- def on_packet(client_id, data, channel)
53
- handle_connection(client_id, data, channel)
54
- end
55
-
56
- # callled when a client connects
57
- # @param client_id [Integer] ID of client
58
- # @param ip_address [String] address of client
59
- def on_connect(client_id, ip_address)
60
- @client_manager.add(client_id, ip_address)
61
- end
62
-
63
- # callled when a client disconnects
64
- # @param client_id [Integer] ID of client
65
- def on_disconnect(client_id)
66
- @client_manager.remove(client_id)
67
- end
68
-
69
- # send message to a specific client
70
- # @param client_id [Integer] ID of client
71
- # @param message [String] message to be sent to client
72
- # @param reliable [Boolean] whether or not the packet is guaranteed to be received by the client
73
- # @param channel [Integer] what channel to send on
74
- def transmit(client_id, message, reliable = false, channel = ChannelManager::CHAT)
75
- @server.send_packet(client_id, message, reliable, channel)
76
- end
77
-
78
- # send message to all connected clients
79
- # @param message [String] message to be sent to clients
80
- # @param reliable [Boolean] whether or not the packet is guaranteed to be received by the clients
81
- # @param channel [Integer] what channel to send on
82
- def broadcast(message, reliable = false, channel = ChannelManager::CHAT)
83
- @server.broadcast_packet(message, reliable, channel)
84
- end
85
-
86
- # send data to the InputHandler for processing
87
- # @param data [Hash]
88
- # @param client_id [Integer] ID of client that sent the data
89
- def process_data(client_id, data)
90
- GameOverseer::InputHandler.process_data(client_id, data)
91
- end
92
-
93
- # Handles received packets from clients and sends them through the {PacketHandler} for pre-processing, then sends it on to {#process_data}
94
- # @param client_id [Integer]
95
- # @param data [String] data received from client
96
- # @param channel [Integer] channel that this packet was sent along
97
- def handle_connection(client_id, data, channel)
98
- _data = @packet_handler.receive(client_id, data)
99
- if _data
100
- process_data(client_id, _data)
101
- else
102
- # TODO: Better error handling :D
103
- transmit(client_id, '{"channel":"_error", "mode":"_error", "data":{"code":400, "message":"something went wrong, likely bad data!"}}', true, ChannelManager::FAULT)
104
- end
105
- end
106
-
107
- def terminate
108
- @terminate = true
109
- end
110
-
111
- def self.instance
112
- @instance
113
- end
114
-
115
- def self.instance=(_instance)
116
- @instance = _instance
117
- end
118
- end
119
-
120
- class ENetServerRunner
121
- attr_reader :supervisor
122
- def start(host, port, packet_handler = PacketHandler, encryption_handler = nil)
123
- @supervisor = GameOverseer::ENetServer.new(host, port, packet_handler, encryption_handler)
124
- end
125
- end
126
- end
1
+ module GameOverseer
2
+
3
+ # GameOverseers' connection to the world
4
+ #
5
+ # This server uses the renet library, which is C bindings for the Enet networking library
6
+ class ENetServer
7
+ # @param host [String] host or ip for the server to run on
8
+ # @param port [Integer] port for the server to run on
9
+ # @param max_clients [Integer] max number of clients that can be connected at one time
10
+ # @param channels [Integer] number of channels (See Enet documentation)
11
+ # @param download_bandwidth [Integer] max bandwidth for downloading per-second (0 is unlimited)
12
+ # @param upload_bandwidth [Integer] max bandwidth for uploading per-second (0 is unlimited)
13
+ # @return [Thread]
14
+ def initialize(host, port, packet_handler, encryption_handler, max_clients = 4, channels = 4, download_bandwidth = 0, upload_bandwidth = 0)
15
+ GameOverseer::Console.log("Server> Started on: #{host}:#{port}.")
16
+ GameOverseer::Services.enable
17
+ GameOverseer::ENetServer.instance = self
18
+
19
+ @message_manager = GameOverseer::MessageManager.instance
20
+ @channel_manager = GameOverseer::ChannelManager.instance
21
+ @client_manager = GameOverseer::ClientManager.instance
22
+ @packet_handler = packet_handler.new
23
+ @encryption_handler = encryption_handler.instance if encryption_handler
24
+
25
+ @server = ENet::Server.new(port, max_clients, channels, download_bandwidth, upload_bandwidth) # Port, max clients, channels, download bandwidth, upload bandwith
26
+ @server.use_compression(true)
27
+ @terminate = false
28
+
29
+ @server.on_connection(method(:on_connect))
30
+ @server.on_packet_receive(method(:on_packet))
31
+ @server.on_disconnection(method(:on_disconnect))
32
+
33
+ run
34
+ end
35
+
36
+ # Runs the server in a Thread,, in a loop, calling update on the server.
37
+ #
38
+ # @return [Thread]
39
+ def run
40
+ Thread.new {
41
+ loop do
42
+ @server.update(1000)
43
+ break if @terminate
44
+ end
45
+ }
46
+ end
47
+
48
+ # Called when a packet is received
49
+ # @param client_id [Integer] ID of client
50
+ # @param data [String] data client sent
51
+ # @param channel [Integer] channel that this was sent to
52
+ def on_packet(client_id, data, channel)
53
+ handle_connection(client_id, data, channel)
54
+ end
55
+
56
+ # callled when a client connects
57
+ # @param client_id [Integer] ID of client
58
+ # @param ip_address [String] address of client
59
+ def on_connect(client_id, ip_address)
60
+ @client_manager.add(client_id, ip_address)
61
+ end
62
+
63
+ # callled when a client disconnects
64
+ # @param client_id [Integer] ID of client
65
+ def on_disconnect(client_id)
66
+ @client_manager.remove(client_id)
67
+ end
68
+
69
+ # send message to a specific client
70
+ # @param client_id [Integer] ID of client
71
+ # @param message [String] message to be sent to client
72
+ # @param reliable [Boolean] whether or not the packet is guaranteed to be received by the client
73
+ # @param channel [Integer] what channel to send on
74
+ def transmit(client_id, message, reliable = false, channel = ChannelManager::CHAT)
75
+ @server.send_packet(client_id, message, reliable, channel)
76
+ end
77
+
78
+ # send message to all connected clients
79
+ # @param message [String] message to be sent to clients
80
+ # @param reliable [Boolean] whether or not the packet is guaranteed to be received by the clients
81
+ # @param channel [Integer] what channel to send on
82
+ def broadcast(message, reliable = false, channel = ChannelManager::CHAT)
83
+ @server.broadcast_packet(message, reliable, channel)
84
+ end
85
+
86
+ # send data to the InputHandler for processing
87
+ # @param data [Hash]
88
+ # @param client_id [Integer] ID of client that sent the data
89
+ def process_data(client_id, data)
90
+ GameOverseer::InputHandler.process_data(client_id, data)
91
+ end
92
+
93
+ # Handles received packets from clients and sends them through the {PacketHandler} for pre-processing, then sends it on to {#process_data}
94
+ # @param client_id [Integer]
95
+ # @param data [String] data received from client
96
+ # @param channel [Integer] channel that this packet was sent along
97
+ def handle_connection(client_id, data, channel)
98
+ _data = @packet_handler.receive(client_id, data)
99
+ if _data
100
+ process_data(client_id, _data)
101
+ else
102
+ # TODO: Better error handling :D
103
+ transmit(client_id, '{"channel":"_error", "mode":"_error", "data":{"code":400, "message":"something went wrong, likely bad data!"}}', true, ChannelManager::FAULT)
104
+ end
105
+ end
106
+
107
+ def terminate
108
+ @terminate = true
109
+ end
110
+
111
+ def self.instance
112
+ @instance
113
+ end
114
+
115
+ def self.instance=(_instance)
116
+ @instance = _instance
117
+ end
118
+ end
119
+
120
+ class ENetServerRunner
121
+ attr_reader :supervisor
122
+ def start(host, port, packet_handler = PacketHandler, encryption_handler = nil)
123
+ @supervisor = GameOverseer::ENetServer.new(host, port, packet_handler, encryption_handler)
124
+ end
125
+ end
126
+ end
@@ -1,111 +1,111 @@
1
- module GameOverseer
2
-
3
- # Services are at the heart on GameOverseer
4
- #
5
- # Subclass this class to implement a service
6
- class Service
7
- attr_accessor :client_id
8
- attr_reader :safe_methods
9
-
10
- # Adds the class that subclassed this class to a list for activation later
11
- # @param subclass [Service]
12
- def self.inherited(subclass)
13
- Services.register(subclass)
14
- GameOverseer::Console.log "Service> added '#{subclass}' to Services::List."
15
- end
16
-
17
- # This method should not be overridden, you should instead implement {#setup} with no arguments
18
- def initialize
19
- if defined?(self.setup)
20
- @client_id = 0
21
- @safe_methods = []
22
- setup
23
- enable
24
- end
25
- end
26
-
27
- # Called before {#enable} there should be no active code here, only setup variables.
28
- def setup
29
- end
30
-
31
- # Called when services are first initialized, put active code here, in a thread.
32
- def enable
33
- end
34
-
35
- # Called when a message is recieved for this channel.
36
- # @param data [Hash] The data from the packet
37
- def process(data)
38
- end
39
-
40
- def version
41
- # Please use the sematic versioning system,
42
- # http://semver.org
43
- #
44
- # e.g.
45
- # "1.5.9"
46
- # (Major.Minor.Patch)
47
- "0.0.0-default"
48
- end
49
-
50
- # Sets methods that are safe for {#data_to_method} to call
51
- # @param array [Array] Array of strings or symbols that match a method in the service class
52
- def set_safe_methods(array)
53
- raise "argument must be an array of strings or symbols" unless array.is_a?(Array)
54
- @safe_methods = array
55
- end
56
-
57
- # @return [ChannelManager] Active instance of ChannelManager
58
- def channel_manager
59
- ChannelManager.instance
60
- end
61
-
62
- # @return [MessageManager] Instance of MessageManager
63
- def message_manager
64
- MessageManager.instance
65
- end
66
-
67
- # @return [ClientManager] Current instance of ClientManager
68
- def client_manager
69
- ClientManager.instance
70
- end
71
-
72
- # Uses the 'mode' from a packet to call the method of the same name
73
- # @param data [Hash] data from packet
74
- def data_to_method(data)
75
- @safe_methods.each do |method|
76
- if data['mode'] == method.to_s
77
- self.send(data['mode'], data)
78
- end
79
- end
80
- end
81
-
82
- # Calls Proc immediately then every milliseconds, async.
83
- # @param milliseconds [Integer][Float] Time to wait before calling the block
84
- # @param block [Proc]
85
- def every(milliseconds, &block)
86
- Thread.new do
87
- loop do
88
- block.call
89
- sleep(milliseconds/1000.0)
90
- end
91
- end
92
- end
93
-
94
- # Calls Proc after milliseconds have passed, async.
95
- # @param milliseconds [Integer][Float] Time to wait before calling the block
96
- # @param block [Proc]
97
- def after(milliseconds, &block)
98
- Thread.new do
99
- sleep(milliseconds/1000.0)
100
- block.call
101
- end
102
- end
103
-
104
- # String to be logged
105
- # @param string [String] text to log
106
- # @param color [Gosu::Color] color of text in console
107
- def log(string, color = Gosu::Color::RED)
108
- GameOverseer::Console.log_with_color(string, color)
109
- end
110
- end
111
- end
1
+ module GameOverseer
2
+
3
+ # Services are at the heart on GameOverseer
4
+ #
5
+ # Subclass this class to implement a service
6
+ class Service
7
+ attr_accessor :client_id
8
+ attr_reader :safe_methods
9
+
10
+ # Adds the class that subclassed this class to a list for activation later
11
+ # @param subclass [Service]
12
+ def self.inherited(subclass)
13
+ Services.register(subclass)
14
+ GameOverseer::Console.log "Service> added '#{subclass}' to Services::List."
15
+ end
16
+
17
+ # This method should not be overridden, you should instead implement {#setup} with no arguments
18
+ def initialize
19
+ if defined?(self.setup)
20
+ @client_id = 0
21
+ @safe_methods = []
22
+ setup
23
+ enable
24
+ end
25
+ end
26
+
27
+ # Called before {#enable} there should be no active code here, only setup variables.
28
+ def setup
29
+ end
30
+
31
+ # Called when services are first initialized, put active code here, in a thread.
32
+ def enable
33
+ end
34
+
35
+ # Called when a message is recieved for this channel.
36
+ # @param data [Hash] The data from the packet
37
+ def process(data)
38
+ end
39
+
40
+ def version
41
+ # Please use the sematic versioning system,
42
+ # http://semver.org
43
+ #
44
+ # e.g.
45
+ # "1.5.9"
46
+ # (Major.Minor.Patch)
47
+ "0.0.0-default"
48
+ end
49
+
50
+ # Sets methods that are safe for {#data_to_method} to call
51
+ # @param array [Array] Array of strings or symbols that match a method in the service class
52
+ def set_safe_methods(array)
53
+ raise "argument must be an array of strings or symbols" unless array.is_a?(Array)
54
+ @safe_methods = array
55
+ end
56
+
57
+ # @return [ChannelManager] Active instance of ChannelManager
58
+ def channel_manager
59
+ ChannelManager.instance
60
+ end
61
+
62
+ # @return [MessageManager] Instance of MessageManager
63
+ def message_manager
64
+ MessageManager.instance
65
+ end
66
+
67
+ # @return [ClientManager] Current instance of ClientManager
68
+ def client_manager
69
+ ClientManager.instance
70
+ end
71
+
72
+ # Uses the 'mode' from a packet to call the method of the same name
73
+ # @param data [Hash] data from packet
74
+ def data_to_method(data)
75
+ @safe_methods.each do |method|
76
+ if data['mode'] == method.to_s
77
+ self.send(data['mode'], data)
78
+ end
79
+ end
80
+ end
81
+
82
+ # Calls Proc immediately then every milliseconds, async.
83
+ # @param milliseconds [Integer][Float] Time to wait before calling the block
84
+ # @param block [Proc]
85
+ def every(milliseconds, &block)
86
+ Thread.new do
87
+ loop do
88
+ block.call
89
+ sleep(milliseconds/1000.0)
90
+ end
91
+ end
92
+ end
93
+
94
+ # Calls Proc after milliseconds have passed, async.
95
+ # @param milliseconds [Integer][Float] Time to wait before calling the block
96
+ # @param block [Proc]
97
+ def after(milliseconds, &block)
98
+ Thread.new do
99
+ sleep(milliseconds/1000.0)
100
+ block.call
101
+ end
102
+ end
103
+
104
+ # String to be logged
105
+ # @param string [String] text to log
106
+ # @param color [Gosu::Color] color of text in console
107
+ def log(string, color = Gosu::Color::RED)
108
+ GameOverseer::Console.log_with_color(string, color)
109
+ end
110
+ end
111
+ end
@@ -1,38 +1,38 @@
1
- module GameOverseer
2
- module Services
3
- LIST = []
4
- ACTIVE=[]
5
-
6
- # Adds klass to list
7
- # @param klass [Service] Service class to add to services list
8
- def self.register(klass)
9
- LIST << klass
10
- end
11
-
12
- # instantiates the Services in 'LIST' and adds them to the 'ACTIVE' list
13
- def self.enable
14
- LIST.each do |service|
15
- _service = service.new
16
- GameOverseer::Console.log "Services> #{_service.class} #{_service.version}"
17
- ACTIVE << _service
18
- end
19
- end
20
-
21
- # Tells all services that have 'client_connected' defined that 'client_id' has connected
22
- # @param client_id [Integer] ID of client
23
- # @param ip_address [String] ip address of client
24
- def self.client_connected(client_id, ip_address)
25
- ACTIVE.each do |service|
26
- service.client_connected(client_id, ip_address) if defined?(service.client_connected)
27
- end
28
- end
29
-
30
- # Tells all services that have 'client_disconnected' defined that 'client_id' is now disconnected
31
- # @param client_id [Integer] ID of client
32
- def self.client_disconnected(client_id)
33
- ACTIVE.each do |service|
34
- service.client_disconnected(client_id) if defined?(service.client_disconnected)
35
- end
36
- end
37
- end
38
- end
1
+ module GameOverseer
2
+ module Services
3
+ LIST = []
4
+ ACTIVE=[]
5
+
6
+ # Adds klass to list
7
+ # @param klass [Service] Service class to add to services list
8
+ def self.register(klass)
9
+ LIST << klass
10
+ end
11
+
12
+ # instantiates the Services in 'LIST' and adds them to the 'ACTIVE' list
13
+ def self.enable
14
+ LIST.each do |service|
15
+ _service = service.new
16
+ GameOverseer::Console.log "Services> #{_service.class} #{_service.version}"
17
+ ACTIVE << _service
18
+ end
19
+ end
20
+
21
+ # Tells all services that have 'client_connected' defined that 'client_id' has connected
22
+ # @param client_id [Integer] ID of client
23
+ # @param ip_address [String] ip address of client
24
+ def self.client_connected(client_id, ip_address)
25
+ ACTIVE.each do |service|
26
+ service.client_connected(client_id, ip_address) if defined?(service.client_connected)
27
+ end
28
+ end
29
+
30
+ # Tells all services that have 'client_disconnected' defined that 'client_id' is now disconnected
31
+ # @param client_id [Integer] ID of client
32
+ def self.client_disconnected(client_id)
33
+ ACTIVE.each do |service|
34
+ service.client_disconnected(client_id) if defined?(service.client_disconnected)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,4 +1,4 @@
1
- module GameOverseer
2
- VERSION = "0.1.4"
3
- RELEASE_NAME = "Blank Slate" # 1.0 is to be "Ice Crystal"
4
- end
1
+ module GameOverseer
2
+ VERSION = "0.1.5"
3
+ RELEASE_NAME = "Blank Slate" # 1.0 is to be "Ice Crystal"
4
+ end