rbuv 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/echo_client_tcp.rb +18 -0
- data/examples/echo_server_tcp.rb +35 -0
- data/ext/rbuv/rbuv_stream.c +28 -16
- data/ext/rbuv/rbuv_tcp.c +55 -0
- data/lib/rbuv/version.rb +1 -1
- data/spec/tcp_spec.rb +29 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a1dfffa85f5a82907b4f1f80843450aecd5f8a4
|
4
|
+
data.tar.gz: 3ee4a61701cbc7cfbdc7f111ba42f5ca7856ad07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cca669ba3307ed5adb8b863e97a2933fe4d0adcc919b6546e5053e0ab2865b207f48fdbbc05ae75d5c2fa7caff24acba9adfdb440b62418097a3e423641c9f81
|
7
|
+
data.tar.gz: 791a1ebff000cbe65ebde2066ad334088f6c2401251c3de5dad9a8b13a2503bc5e8bb03583fa99320d9e4f9c483c68180d4741c5dc4f28776d9f8ade94672604
|
@@ -0,0 +1,18 @@
|
|
1
|
+
lib = File.expand_path('../../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'rbuv'
|
4
|
+
|
5
|
+
puts "Rbuv version #{Rbuv::VERSION}"
|
6
|
+
|
7
|
+
Rbuv.run do
|
8
|
+
client = Rbuv::Tcp.new
|
9
|
+
client.connect "127.0.0.1", 1234 do
|
10
|
+
client.read_start do |data, error|
|
11
|
+
puts data unless error
|
12
|
+
client.close
|
13
|
+
end
|
14
|
+
client.write "Hello, world"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
puts "Stopped!"
|
@@ -0,0 +1,35 @@
|
|
1
|
+
lib = File.expand_path('../../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'rbuv'
|
4
|
+
|
5
|
+
puts "Rbuv version #{Rbuv::VERSION}"
|
6
|
+
|
7
|
+
clients = []
|
8
|
+
|
9
|
+
Rbuv.run do
|
10
|
+
signal = Rbuv::Signal.new
|
11
|
+
server = Rbuv::Tcp.new
|
12
|
+
|
13
|
+
signal.start Rbuv::Signal::INT do
|
14
|
+
clients.each(&:close)
|
15
|
+
signal.close
|
16
|
+
server.close
|
17
|
+
end
|
18
|
+
|
19
|
+
server.bind "127.0.0.1", 1234
|
20
|
+
server.listen 10 do
|
21
|
+
client = Rbuv::Tcp.new
|
22
|
+
server.accept client
|
23
|
+
clients << client
|
24
|
+
client.read_start do |data, error|
|
25
|
+
if error
|
26
|
+
client.close
|
27
|
+
clients.delete client
|
28
|
+
else
|
29
|
+
client.write data
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
puts "Stopped!"
|
data/ext/rbuv/rbuv_stream.c
CHANGED
@@ -40,6 +40,7 @@ static VALUE rbuv_stream_write(VALUE self, VALUE data);
|
|
40
40
|
/* Private methods */
|
41
41
|
static void _uv_stream_on_connection(uv_stream_t *server, int status);
|
42
42
|
static void _uv_stream_on_connection_no_gvl(_uv_stream_on_connection_arg_t *arg);
|
43
|
+
void __uv_stream_on_connection_no_gvl(uv_stream_t *stream, int status);
|
43
44
|
static uv_buf_t _uv_alloc_buffer(uv_handle_t *handle, size_t suggested_size);
|
44
45
|
static void _uv_stream_on_read(uv_stream_t *stream, ssize_t nread, uv_buf_t buf);
|
45
46
|
static void _uv_stream_on_read_no_gvl(_uv_stream_on_read_arg_t *arg);
|
@@ -166,33 +167,44 @@ VALUE rbuv_stream_write(VALUE self, VALUE data) {
|
|
166
167
|
return data;
|
167
168
|
}
|
168
169
|
|
169
|
-
void _uv_stream_on_connection(uv_stream_t *
|
170
|
-
_uv_stream_on_connection_arg_t arg = { .uv_stream =
|
170
|
+
void _uv_stream_on_connection(uv_stream_t *uv_stream, int status) {
|
171
|
+
_uv_stream_on_connection_arg_t arg = { .uv_stream = uv_stream, .status = status };
|
171
172
|
rb_thread_call_with_gvl((rbuv_rb_blocking_function_t)_uv_stream_on_connection_no_gvl, &arg);
|
172
173
|
}
|
173
174
|
|
174
175
|
void _uv_stream_on_connection_no_gvl(_uv_stream_on_connection_arg_t *arg) {
|
175
|
-
uv_stream_t *
|
176
|
+
uv_stream_t *uv_stream = arg->uv_stream;
|
176
177
|
int status = arg->status;
|
177
178
|
|
178
|
-
|
179
|
-
|
179
|
+
__uv_stream_on_connection_no_gvl(uv_stream, status);
|
180
|
+
}
|
181
|
+
|
182
|
+
void __uv_stream_on_connection_no_gvl(uv_stream_t *uv_stream, int status) {
|
183
|
+
VALUE stream;
|
184
|
+
rbuv_stream_t *rbuv_stream;
|
180
185
|
VALUE on_connection;
|
186
|
+
VALUE error;
|
187
|
+
uv_err_t uv_err;
|
181
188
|
|
182
|
-
RBUV_DEBUG_LOG("
|
183
|
-
if (status == -1) {
|
184
|
-
return;
|
185
|
-
}
|
189
|
+
RBUV_DEBUG_LOG("uv_stream: %p, status: %d", uv_stream, status);
|
186
190
|
|
187
|
-
|
188
|
-
Data_Get_Struct(
|
189
|
-
on_connection =
|
191
|
+
stream = (VALUE)uv_stream->data;
|
192
|
+
Data_Get_Struct(stream, rbuv_stream_t, rbuv_stream);
|
193
|
+
on_connection = rbuv_stream->cb_on_connection;
|
190
194
|
|
191
|
-
RBUV_DEBUG_LOG_DETAIL("
|
192
|
-
RSTRING_PTR(rb_inspect(
|
195
|
+
RBUV_DEBUG_LOG_DETAIL("stream: %s, on_connection: %s",
|
196
|
+
RSTRING_PTR(rb_inspect(stream)),
|
193
197
|
RSTRING_PTR(rb_inspect(on_connection)));
|
194
198
|
|
195
|
-
|
199
|
+
if (status == -1) {
|
200
|
+
uv_err = uv_last_error(uv_default_loop());
|
201
|
+
RBUV_DEBUG_LOG_DETAIL("uv_stream: %p, status: %d, error: %s", uv_stream, status,
|
202
|
+
uv_strerror(uv_err));
|
203
|
+
error = rb_exc_new2(eRbuvError, uv_strerror(uv_err));
|
204
|
+
rb_funcall(on_connection, id_call, 2, stream, error);
|
205
|
+
} else {
|
206
|
+
rb_funcall(on_connection, id_call, 1, stream);
|
207
|
+
}
|
196
208
|
}
|
197
209
|
|
198
210
|
uv_buf_t _uv_alloc_buffer(uv_handle_t *handle, size_t suggested_size) {
|
@@ -215,7 +227,7 @@ void _uv_stream_on_read_no_gvl(_uv_stream_on_read_arg_t *arg) {
|
|
215
227
|
uv_err_t uv_err;
|
216
228
|
VALUE error;
|
217
229
|
|
218
|
-
RBUV_DEBUG_LOG("uv_stream: %p, nread: %
|
230
|
+
RBUV_DEBUG_LOG("uv_stream: %p, nread: %lu", uv_stream, nread);
|
219
231
|
|
220
232
|
stream = (VALUE)uv_stream->data;
|
221
233
|
Data_Get_Struct(stream, rbuv_stream_t, rbuv_stream);
|
data/ext/rbuv/rbuv_tcp.c
CHANGED
@@ -7,6 +7,11 @@ struct rbuv_tcp_s {
|
|
7
7
|
VALUE cb_on_read;
|
8
8
|
};
|
9
9
|
|
10
|
+
typedef struct {
|
11
|
+
uv_connect_t *uv_connect;
|
12
|
+
int status;
|
13
|
+
} _uv_tcp_on_connect_arg_t;
|
14
|
+
|
10
15
|
VALUE cRbuvTcp;
|
11
16
|
|
12
17
|
/* Allocator/deallocator */
|
@@ -30,6 +35,13 @@ static void rbuv_tcp_free(rbuv_tcp_t *rbuv_tcp);
|
|
30
35
|
*/
|
31
36
|
static VALUE rbuv_tcp_bind(VALUE self, VALUE ip, VALUE port);
|
32
37
|
//static VALUE rbuv_tcp_bind6(VALUE self, VALUE ip, VALUE port);
|
38
|
+
static VALUE rbuv_tcp_connect(VALUE self, VALUE ip, VALUE port);
|
39
|
+
//static VALUE rbuv_tcp_connect6(VALUE self, VALUE ip, VALUE port);
|
40
|
+
|
41
|
+
/* Private methods */
|
42
|
+
static void _uv_tcp_on_connect(uv_connect_t *uv_connect, int status);
|
43
|
+
static void _uv_tcp_on_connect_no_gvl(_uv_tcp_on_connect_arg_t *arg);
|
44
|
+
extern void __uv_stream_on_connection_no_gvl(uv_stream_t *uv_stream, int status);
|
33
45
|
|
34
46
|
void Init_rbuv_tcp() {
|
35
47
|
cRbuvTcp = rb_define_class_under(mRbuv, "Tcp", cRbuvStream);
|
@@ -37,6 +49,8 @@ void Init_rbuv_tcp() {
|
|
37
49
|
|
38
50
|
rb_define_method(cRbuvTcp, "bind", rbuv_tcp_bind, 2);
|
39
51
|
//rb_define_method(cRbuvTcp, "bind6", rbuv_tcp_bind6, 2);
|
52
|
+
rb_define_method(cRbuvTcp, "connect", rbuv_tcp_connect, 2);
|
53
|
+
//rb_define_method(cRbuvTcp, "connect6", rbuv_tcp_connect6, 2);
|
40
54
|
}
|
41
55
|
|
42
56
|
VALUE rbuv_tcp_alloc(VALUE klass) {
|
@@ -100,3 +114,44 @@ VALUE rbuv_tcp_bind(VALUE self, VALUE ip, VALUE port) {
|
|
100
114
|
|
101
115
|
return self;
|
102
116
|
}
|
117
|
+
|
118
|
+
VALUE rbuv_tcp_connect(VALUE self, VALUE ip, VALUE port) {
|
119
|
+
VALUE block;
|
120
|
+
const char *uv_ip;
|
121
|
+
int uv_port;
|
122
|
+
rbuv_tcp_t *rbuv_tcp;
|
123
|
+
struct sockaddr_in connect_addr;
|
124
|
+
uv_connect_t uv_connect;
|
125
|
+
|
126
|
+
rb_need_block();
|
127
|
+
block = rb_block_proc();
|
128
|
+
|
129
|
+
uv_ip = RSTRING_PTR(ip);
|
130
|
+
uv_port = FIX2INT(port);
|
131
|
+
|
132
|
+
Data_Get_Struct(self, rbuv_tcp_t, rbuv_tcp);
|
133
|
+
rbuv_tcp->cb_on_connection = block;
|
134
|
+
|
135
|
+
connect_addr = uv_ip4_addr(uv_ip, uv_port);
|
136
|
+
|
137
|
+
RBUV_CHECK_UV_RETURN(uv_tcp_connect(&uv_connect, rbuv_tcp->uv_handle,
|
138
|
+
connect_addr, _uv_tcp_on_connect));
|
139
|
+
|
140
|
+
RBUV_DEBUG_LOG_DETAIL("self: %s, ip: %s, port: %d, rbuv_tcp: %p, uv_handle: %p",
|
141
|
+
RSTRING_PTR(rb_inspect(self)), uv_ip, uv_port, rbuv_tcp,
|
142
|
+
rbuv_tcp->uv_handle);
|
143
|
+
|
144
|
+
return self;
|
145
|
+
}
|
146
|
+
|
147
|
+
void _uv_tcp_on_connect(uv_connect_t *uv_connect, int status) {
|
148
|
+
_uv_tcp_on_connect_arg_t arg = { .uv_connect = uv_connect, .status = status };
|
149
|
+
rb_thread_call_with_gvl((rbuv_rb_blocking_function_t)_uv_tcp_on_connect_no_gvl, &arg);
|
150
|
+
}
|
151
|
+
|
152
|
+
void _uv_tcp_on_connect_no_gvl(_uv_tcp_on_connect_arg_t *arg) {
|
153
|
+
uv_connect_t *uv_connect = arg->uv_connect;
|
154
|
+
int status = arg->status;
|
155
|
+
|
156
|
+
__uv_stream_on_connection_no_gvl(uv_connect->handle, status);
|
157
|
+
}
|
data/lib/rbuv/version.rb
CHANGED
data/spec/tcp_spec.rb
CHANGED
@@ -155,6 +155,35 @@ describe Rbuv::Tcp do
|
|
155
155
|
no_on_close.call
|
156
156
|
end
|
157
157
|
end
|
158
|
+
end # context "#close"
|
159
|
+
|
160
|
+
context "#connect" do
|
161
|
+
it "when server does not exist" do
|
162
|
+
Rbuv.run do
|
163
|
+
c = Rbuv::Tcp.new
|
164
|
+
c.connect('127.0.0.1', 60000) do |client, error|
|
165
|
+
error.should be_a_kind_of Rbuv::Error
|
166
|
+
c.close
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
it "when server exists" do
|
172
|
+
s = TCPServer.new '127.0.0.1', 60000
|
173
|
+
s.listen 10
|
174
|
+
|
175
|
+
on_connect = mock
|
176
|
+
on_connect.should_receive(:call).once
|
177
|
+
|
178
|
+
Rbuv.run do
|
179
|
+
c = Rbuv::Tcp.new
|
180
|
+
c.connect('127.0.0.1', 60000) do
|
181
|
+
on_connect.call
|
182
|
+
c.close
|
183
|
+
s.close
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
158
187
|
end
|
159
188
|
|
160
189
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbuv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hanfei Shen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-04-
|
11
|
+
date: 2013-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -301,6 +301,8 @@ files:
|
|
301
301
|
- deps/libuv/test/test-walk-handles.c
|
302
302
|
- deps/libuv/uv.gyp
|
303
303
|
- deps/libuv/vcbuild.bat
|
304
|
+
- examples/echo_client_tcp.rb
|
305
|
+
- examples/echo_server_tcp.rb
|
304
306
|
- ext/rbuv/extconf.rb
|
305
307
|
- ext/rbuv/libuv.mk
|
306
308
|
- ext/rbuv/rbuv.c
|