Skip to content

Commit

Permalink
unbind and close_connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jakedouglas committed Sep 2, 2009
1 parent 9032bc2 commit d655c31
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
20 changes: 20 additions & 0 deletions ext/rubymain.cpp
Expand Up @@ -24,6 +24,7 @@ static VALUE Intern_connection_completed;
static VALUE Intern_reactor;
static VALUE Intern_receive_data;
static VALUE Intern_initialize;
static VALUE Intern_unbind;

static void evma_callback_loopbreak(VALUE reactor)
{
Expand Down Expand Up @@ -53,6 +54,11 @@ static void evma_callback_receive(VALUE conn, const char *data, const unsigned l
rb_funcall(conn, Intern_receive_data, 1, rb_str_new(data, len));
}

static void evma_callback_unbind(VALUE conn)
{
rb_funcall(conn, Intern_unbind, 0);
}

static void event_callback (const unsigned long a1, int a2, const char *a3, const unsigned long a4)
{
if (a2 == EM_LOOPBREAK_SIGNAL) {
Expand All @@ -67,6 +73,9 @@ static void event_callback (const unsigned long a1, int a2, const char *a3, cons
else if (a2 == EM_CONNECTION_READ) {
evma_callback_receive((VALUE) a1, a3, a4);
}
else if (a2 == EM_CONNECTION_UNBOUND) {
evma_callback_unbind((VALUE) a1);
}
}

void evma_free(VALUE self)
Expand Down Expand Up @@ -163,6 +172,15 @@ static VALUE evma_tcp_send_data(VALUE connection, VALUE data)
return INT2NUM(cd->SendOutboundData(RSTRING_PTR(data), RSTRING_LEN(data)));
}

static VALUE evma_close_connection(int argc, VALUE *argv, VALUE conn)
{
VALUE after_writing = Qfalse;
rb_scan_args(argc, argv, "01", &after_writing);
EventableDescriptor *ed = (EventableDescriptor*) DATA_PTR(conn);
ed->ScheduleClose((after_writing == Qtrue) ? true : false);
return Qnil;
}

extern "C" void Init_rubyeventmachine()
{
EmModule = rb_define_module ("EventMachine");
Expand All @@ -176,6 +194,7 @@ extern "C" void Init_rubyeventmachine()
Intern_connection_completed = rb_intern("connection_completed");
Intern_receive_data = rb_intern("receive_data");
Intern_initialize = rb_intern("initialize");
Intern_unbind = rb_intern("unbind");

rb_define_alloc_func(EmReactor, evma_reactor_alloc);

Expand All @@ -186,4 +205,5 @@ extern "C" void Init_rubyeventmachine()
rb_define_method(EmReactor, "connect", (VALUE(*)(...))evma_connect_tcp, -1);

rb_define_method(EmConnection, "send_data", (VALUE(*)(...))evma_tcp_send_data, 1);
rb_define_method(EmConnection, "close_connection", (VALUE(*)(...))evma_close_connection, -1);
}
16 changes: 6 additions & 10 deletions lib/eventmachine.rb
Expand Up @@ -24,17 +24,13 @@ def run blk=nil, tail=nil, &block
@acceptors = {}
@timers = {}
@wrapped_exception = nil
begin
@reactor_running = true
(b = blk || block) and add_timer(0, b)
if @next_tick_queue && !@next_tick_queue.empty?
add_timer(0) { signal_loopbreak }
end
@reactor_thread = Thread.current
run_machine
ensure
machine_stopped
@reactor_running = true
(b = blk || block) and add_timer(0, b)
if @next_tick_queue && !@next_tick_queue.empty?
add_timer(0) { signal_loopbreak }
end
@reactor_thread = Thread.current
run_machine

raise @wrapped_exception if @wrapped_exception
end
Expand Down
2 changes: 1 addition & 1 deletion newtest/tests/basic.rb
Expand Up @@ -20,7 +20,7 @@
it "Reactor#add_timer should work" do
@fired = false
@reactor.run {
@reactor.add_timer(1) {
@reactor.add_timer(0.25) {
@fired = true
@reactor.stop
}
Expand Down
19 changes: 13 additions & 6 deletions newtest/tests/tcp.rb
@@ -1,4 +1,5 @@
describe "tcp connection" do

it "connection_completed should work" do
module Handler
def initialize(reactor)
Expand All @@ -15,23 +16,29 @@ def connection_completed
}
$completed.should.equal true
end
it "blah" do

it "receive_data and unbind should work" do
module Handler
def connection_completed
send_data "GET / HTTP/1.1\r\n\r\n"
end
def receive_data(data)
p data
$data_received = true
close_connection
end
def unbind
$unbound = true
@reactor.stop
end
end

@reactor = EM::Reactor.new
@reactor.run {
@c = @reactor.connect("google.com", 80, Handler)
}
true.should.equal true

$data_received.should.equal true
$unbound.should.equal true
end

end

0 comments on commit d655c31

Please sign in to comment.