renet 0.1.0-universal-darwin
Sign up to get free protection for your applications and to get access to all the features.
- data/README +81 -0
- data/examples/Console-Server.rb +47 -0
- data/examples/Gosu-Client.rb +71 -0
- data/ext/renet/enet/callbacks.c +47 -0
- data/ext/renet/enet/callbacks.h +27 -0
- data/ext/renet/enet/compress.c +654 -0
- data/ext/renet/enet/enet.h +540 -0
- data/ext/renet/enet/host.c +479 -0
- data/ext/renet/enet/list.c +75 -0
- data/ext/renet/enet/list.h +43 -0
- data/ext/renet/enet/packet.c +157 -0
- data/ext/renet/enet/peer.c +816 -0
- data/ext/renet/enet/protocol.c +1671 -0
- data/ext/renet/enet/protocol.h +196 -0
- data/ext/renet/enet/time.h +18 -0
- data/ext/renet/enet/types.h +13 -0
- data/ext/renet/enet/unix.c +438 -0
- data/ext/renet/enet/unix.h +45 -0
- data/ext/renet/enet/utility.h +12 -0
- data/ext/renet/enet/win32.c +348 -0
- data/ext/renet/enet/win32.h +58 -0
- data/ext/renet/extconf.rb +21 -0
- data/ext/renet/renet.c +69 -0
- data/ext/renet/renet.h +35 -0
- data/ext/renet/renet_connection.c +322 -0
- data/ext/renet/renet_connection.h +58 -0
- data/ext/renet/renet_server.c +280 -0
- data/ext/renet/renet_server.h +58 -0
- data/lib/renet.rb +10 -0
- metadata +76 -0
data/README
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
=================================================
|
2
|
+
rENet
|
3
|
+
Ruby library for games networking
|
4
|
+
-------------------------------------------------
|
5
|
+
-= Work in Progress =-
|
6
|
+
No docs or comments right now :|
|
7
|
+
Just this readme ;x
|
8
|
+
=================================================
|
9
|
+
|
10
|
+
Planned:
|
11
|
+
- ENet::Connection (client)
|
12
|
+
- ENet::Server (server)
|
13
|
+
- Others
|
14
|
+
|
15
|
+
More or less finished:
|
16
|
+
- ENet::Connection
|
17
|
+
- ENet::Server
|
18
|
+
|
19
|
+
-------------------------------------------------
|
20
|
+
Available binaries:
|
21
|
+
- Ruby 1.9 Windows 32bits (Mingw)
|
22
|
+
|
23
|
+
=================================================
|
24
|
+
Under MIT license
|
25
|
+
------------------------------------------------
|
26
|
+
Uses ENet as backend < http://enet.bespin.org/ >
|
27
|
+
=================================================
|
28
|
+
|
29
|
+
Library overview
|
30
|
+
-------------------------------------------------
|
31
|
+
module ENet
|
32
|
+
|- ENET_VERSION (string)
|
33
|
+
|- RENET_VERSION (string)
|
34
|
+
|
|
35
|
+
|- Connection (class)
|
36
|
+
| |- attr_reader
|
37
|
+
| | |- online?
|
38
|
+
| | |- connected?
|
39
|
+
| |
|
40
|
+
| |- attr_accessor
|
41
|
+
| | |- total_sent_data
|
42
|
+
| | |- total_received_data
|
43
|
+
| | |- total_sent_packets
|
44
|
+
| | |- total_received_packets
|
45
|
+
| |
|
46
|
+
| |- methods
|
47
|
+
| |- initialize (remote host, remote port, channels to allocate, download bw, upload bw)
|
48
|
+
| |- connect (timeout in ms)
|
49
|
+
| |- disconnect (timeout in ms)
|
50
|
+
| |- send_packet (data to send, reliable or not, channel id)
|
51
|
+
| |- send_queued_packets (nothing)
|
52
|
+
| |- flush (nothing)
|
53
|
+
| |- update (timeout in ms)
|
54
|
+
| |- use_compression (true or false)
|
55
|
+
| |- on_connection (method to call)
|
56
|
+
| |- on_packet_receive (method to call)
|
57
|
+
| |- on_disconnection (method to call)
|
58
|
+
|
|
59
|
+
|- Server (class)
|
60
|
+
|- attr_reader
|
61
|
+
| |- max_clients
|
62
|
+
| |- clients_count
|
63
|
+
|
|
64
|
+
|- attr_accessor
|
65
|
+
| |- total_sent_data
|
66
|
+
| |- total_received_data
|
67
|
+
| |- total_sent_packets
|
68
|
+
| |- total_received_packets
|
69
|
+
|
|
70
|
+
|- methods
|
71
|
+
|- initialize (local port, n� of allowed clients, channels to allocate, download bw, upload bw)
|
72
|
+
|- disconnect_client (client id)
|
73
|
+
|- send_packet (client id, data to send, reliable or not, channel id)
|
74
|
+
|- broadcast_packet (data to send, reliable or not, channel id)
|
75
|
+
|- send_queued_packets (nothing)
|
76
|
+
|- flush (nothing)
|
77
|
+
|- update (timeout in ms)
|
78
|
+
|- use_compression (true or false)
|
79
|
+
|- on_connection (method to call)
|
80
|
+
|- on_packet_receive (method to call)
|
81
|
+
|- on_disconnection (method to call)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# require the lib!
|
2
|
+
require './../bin/renet'
|
3
|
+
|
4
|
+
# define a custom handler for new client connections, it will be automatically called
|
5
|
+
def connections_handler(id, ip)
|
6
|
+
# show who connected
|
7
|
+
puts "[ID #{id}] connected from #{ip}"
|
8
|
+
# delete the hash entry if it already exists
|
9
|
+
@clients.delete_if {|key, value| key == id }
|
10
|
+
# add the new entry to the clients hash
|
11
|
+
@clients[id] = { :ip => ip }
|
12
|
+
#show how many clients we have
|
13
|
+
puts "Clients connected: #{@server.clients_count} of #{@server.max_clients}"
|
14
|
+
end
|
15
|
+
|
16
|
+
# define a custom handler for incoming data packets, it will be automatically called
|
17
|
+
def packets_handler(id, data, channel)
|
18
|
+
# show who sent the packet, the data inside and the channel used
|
19
|
+
puts "packet from [peer #{id}] -> [#{data}] - [#{channel}]"
|
20
|
+
# answer sending the same data to the client (client ID, data, reliable or not, channel ID)
|
21
|
+
@server.send_packet(id, data, true, channel)
|
22
|
+
end
|
23
|
+
|
24
|
+
# define a custom handler for client disconnections, it will be automatically called
|
25
|
+
def disconnections_handler(id)
|
26
|
+
# show who left
|
27
|
+
puts "#{@clients[id][:ip]} with ID #{id} disconnected"
|
28
|
+
# delete its entry in the clients hash
|
29
|
+
@clients.delete_if {|key, value| key == id }
|
30
|
+
# sends a packet to all clients informing about the disconnection (data, reliable or not, channel ID)
|
31
|
+
@server.broadcast_packet("#{id} disconnected", true, 1)
|
32
|
+
end
|
33
|
+
# create a hash to store clients information
|
34
|
+
@clients = {}
|
35
|
+
# create a new server object ( port, clients allowed, number of channels, download bandwidth, upload badnwidth)
|
36
|
+
@server = ENet::Server.new(8000, 32, 2, 0, 0)
|
37
|
+
|
38
|
+
# Set our handlers to the server
|
39
|
+
@server.on_connection(method(:connections_handler))
|
40
|
+
@server.on_packet_receive(method(:packets_handler))
|
41
|
+
@server.on_disconnection(method(:disconnections_handler))
|
42
|
+
|
43
|
+
# loop forever
|
44
|
+
loop do
|
45
|
+
# send queued packets and wait for new packets 1000 milliseconds
|
46
|
+
@server.update(1000)
|
47
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# require gosu and renet libs!
|
2
|
+
require 'gosu'
|
3
|
+
require './../bin/renet'
|
4
|
+
|
5
|
+
# define a new window class
|
6
|
+
class GameWindow < Gosu::Window
|
7
|
+
def initialize
|
8
|
+
super(640, 480, false)
|
9
|
+
# set a descriptive caption
|
10
|
+
self.caption = "Gosu & ENet Test"
|
11
|
+
# create a font to draw the information
|
12
|
+
@font = Gosu::Font.new(self, Gosu::default_font_name, 20)
|
13
|
+
# create a new connection object (remote host address, remote host port, channels, download bandwidth, upload bandwidth)
|
14
|
+
@socket = ENet::Connection.new("localhost", 8000, 2, 0, 0)
|
15
|
+
# set the handler for the packets
|
16
|
+
@socket.on_packet_receive(method(:on_packet))
|
17
|
+
# an array to store the incoming packets
|
18
|
+
@packets = []
|
19
|
+
end
|
20
|
+
|
21
|
+
def update
|
22
|
+
# send queued packets and receive incoming ones (0 = non-blocking)
|
23
|
+
@socket.update(0)
|
24
|
+
# show our client FPS
|
25
|
+
self.caption = "Gosu & ENet Test - [FPS: #{Gosu::fps.to_s}]"
|
26
|
+
end
|
27
|
+
|
28
|
+
def draw
|
29
|
+
# show if we are connected to the server or not (@socket.connected? is valid too)
|
30
|
+
@font.draw("Connected: #{@socket.online?}", 10, 10, 0, 1.0, 1.0, 0xffffff00)
|
31
|
+
# show the controls
|
32
|
+
@font.draw("Press C to connect Press P to send a packet", 10, 440, 0, 1.0, 1.0, 0xffffff00)
|
33
|
+
# Show the socket information
|
34
|
+
@font.draw("Packets Sent: #{@socket.total_sent_packets}", 450, 380, 0, 1.0, 1.0, 0xffffff00)
|
35
|
+
@font.draw("Packets Recv: #{@socket.total_received_packets}", 450, 400, 0, 1.0, 1.0, 0xffffff00)
|
36
|
+
@font.draw("Bytes Sent: #{@socket.total_sent_data}", 450, 420, 0, 1.0, 1.0, 0xffffff00)
|
37
|
+
@font.draw("Bytes Recv: #{@socket.total_received_data}", 450, 440, 0, 1.0, 1.0, 0xffffff00)
|
38
|
+
|
39
|
+
# show the storaged packets
|
40
|
+
if !@packets.empty?
|
41
|
+
@packets.each_index do |i|
|
42
|
+
@font.draw("[PACKET] From channel #{@packets[i][1]}, containing \"#{@packets[i][0]}\"", 10, 30*(i+1), 0, 1.0, 1.0, @packets[i][2])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def button_down(id)
|
48
|
+
case id
|
49
|
+
# connect to the server if we press C
|
50
|
+
when Gosu::KbC
|
51
|
+
# only connects if we are not already connected
|
52
|
+
@socket.connect(2000)
|
53
|
+
# send a packet if we press P
|
54
|
+
when Gosu::KbP
|
55
|
+
# send a packet to the server (data, reliable or not, channel ID)
|
56
|
+
@socket.send_packet("test packet", true, 0)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# define a custom handler to manage the packets
|
61
|
+
def on_packet(data, channel)
|
62
|
+
# delete the old packets if we get more than 10
|
63
|
+
@packets.slice!(0) if @packets.size >= 11
|
64
|
+
#storage the packet data, channel and a representative color
|
65
|
+
@packets << [data, channel, Gosu::Color.argb(255, rand(100)+155, rand(100)+155, rand(100)+155)]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# show the window
|
70
|
+
window = GameWindow.new
|
71
|
+
window.show
|
@@ -0,0 +1,47 @@
|
|
1
|
+
/**
|
2
|
+
@file callbacks.c
|
3
|
+
@brief ENet callback functions
|
4
|
+
*/
|
5
|
+
#define ENET_BUILDING_LIB 1
|
6
|
+
#include "enet/enet.h"
|
7
|
+
|
8
|
+
static ENetCallbacks callbacks = { malloc, free, abort };
|
9
|
+
|
10
|
+
int
|
11
|
+
enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits)
|
12
|
+
{
|
13
|
+
if (version < ENET_VERSION_CREATE (1, 3, 0))
|
14
|
+
return -1;
|
15
|
+
|
16
|
+
if (inits -> malloc != NULL || inits -> free != NULL)
|
17
|
+
{
|
18
|
+
if (inits -> malloc == NULL || inits -> free == NULL)
|
19
|
+
return -1;
|
20
|
+
|
21
|
+
callbacks.malloc = inits -> malloc;
|
22
|
+
callbacks.free = inits -> free;
|
23
|
+
}
|
24
|
+
|
25
|
+
if (inits -> no_memory != NULL)
|
26
|
+
callbacks.no_memory = inits -> no_memory;
|
27
|
+
|
28
|
+
return enet_initialize ();
|
29
|
+
}
|
30
|
+
|
31
|
+
void *
|
32
|
+
enet_malloc (size_t size)
|
33
|
+
{
|
34
|
+
void * memory = callbacks.malloc (size);
|
35
|
+
|
36
|
+
if (memory == NULL)
|
37
|
+
callbacks.no_memory ();
|
38
|
+
|
39
|
+
return memory;
|
40
|
+
}
|
41
|
+
|
42
|
+
void
|
43
|
+
enet_free (void * memory)
|
44
|
+
{
|
45
|
+
callbacks.free (memory);
|
46
|
+
}
|
47
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
/**
|
2
|
+
@file callbacks.h
|
3
|
+
@brief ENet callbacks
|
4
|
+
*/
|
5
|
+
#ifndef __ENET_CALLBACKS_H__
|
6
|
+
#define __ENET_CALLBACKS_H__
|
7
|
+
|
8
|
+
#include <stdlib.h>
|
9
|
+
|
10
|
+
typedef struct _ENetCallbacks
|
11
|
+
{
|
12
|
+
void * (ENET_CALLBACK * malloc) (size_t size);
|
13
|
+
void (ENET_CALLBACK * free) (void * memory);
|
14
|
+
void (ENET_CALLBACK * no_memory) (void);
|
15
|
+
} ENetCallbacks;
|
16
|
+
|
17
|
+
/** @defgroup callbacks ENet internal callbacks
|
18
|
+
@{
|
19
|
+
@ingroup private
|
20
|
+
*/
|
21
|
+
extern void * enet_malloc (size_t);
|
22
|
+
extern void enet_free (void *);
|
23
|
+
|
24
|
+
/** @} */
|
25
|
+
|
26
|
+
#endif /* __ENET_CALLBACKS_H__ */
|
27
|
+
|