Skip to content

Instantly share code, notes, and snippets.

@jakedouglas
Created June 5, 2009 04:53
Show Gist options
  • Save jakedouglas/124068 to your computer and use it in GitHub Desktop.
Save jakedouglas/124068 to your computer and use it in GitHub Desktop.
diff --git a/ext/rubymain.cpp b/ext/rubymain.cpp
index 892f0fd..f0cf588 100644
--- a/ext/rubymain.cpp
+++ b/ext/rubymain.cpp
@@ -20,6 +20,7 @@ See the file COPYING for complete licensing information.
#include "project.h"
#include "eventmachine.h"
#include <ruby.h>
+#include <st.h>
#ifndef RFLOAT_VALUE
#define RFLOAT_VALUE(arg) RFLOAT(arg)->value
@@ -52,6 +53,8 @@ static VALUE Intern_proxy_target_unbound;
static VALUE rb_cProcStatus;
+static struct st_table *conns;
+
struct em_event {
const char *a1;
int a2;
@@ -69,24 +72,22 @@ static void event_callback (struct em_event* e)
int a2 = e->a2;
const char *a3 = e->a3;
int a4 = e->a4;
+
+ VALUE q = Qnil;
+ if (!st_lookup(conns, (st_data_t) a1, (st_data_t*) q))
+ rb_raise (EM_eConnectionNotBound, "didnt find anything in st_lookup wtfff");
if (a2 == EM_CONNECTION_READ) {
- VALUE t = rb_ivar_get (EmModule, Intern_at_conns);
- VALUE q = rb_hash_aref (t, rb_str_new2(a1));
if (q == Qnil)
rb_raise (EM_eConnectionNotBound, "received %d bytes of data for unknown signature: %s", a4, a1);
rb_funcall (q, Intern_receive_data, 1, rb_str_new (a3, a4));
}
else if (a2 == EM_CONNECTION_NOTIFY_READABLE) {
- VALUE t = rb_ivar_get (EmModule, Intern_at_conns);
- VALUE q = rb_hash_aref (t, rb_str_new2(a1));
if (q == Qnil)
rb_raise (EM_eConnectionNotBound, "unknown connection: %s", a1);
rb_funcall (q, Intern_notify_readable, 0);
}
else if (a2 == EM_CONNECTION_NOTIFY_WRITABLE) {
- VALUE t = rb_ivar_get (EmModule, Intern_at_conns);
- VALUE q = rb_hash_aref (t, rb_str_new2(a1));
if (q == Qnil)
rb_raise (EM_eConnectionNotBound, "unknown connection: %s", a1);
rb_funcall (q, Intern_notify_writable, 0);
@@ -107,15 +108,11 @@ static void event_callback (struct em_event* e)
}
#ifdef WITH_SSL
else if (a2 == EM_SSL_HANDSHAKE_COMPLETED) {
- VALUE t = rb_ivar_get (EmModule, Intern_at_conns);
- VALUE q = rb_hash_aref (t, rb_str_new2(a1));
if (q == Qnil)
rb_raise (EM_eConnectionNotBound, "unknown connection: %s", a1);
rb_funcall (q, Intern_ssl_handshake_completed, 0);
}
else if (a2 == EM_SSL_VERIFY) {
- VALUE t = rb_ivar_get (EmModule, Intern_at_conns);
- VALUE q = rb_hash_aref (t, rb_str_new2(a1));
if (q == Qnil)
rb_raise (EM_eConnectionNotBound, "unknown connection: %s", a1);
VALUE r = rb_funcall (q, Intern_ssl_verify_peer, 1, rb_str_new(a3, a4));
@@ -124,8 +121,6 @@ static void event_callback (struct em_event* e)
}
#endif
else if (a2 == EM_PROXY_TARGET_UNBOUND) {
- VALUE t = rb_ivar_get (EmModule, Intern_at_conns);
- VALUE q = rb_hash_aref (t, rb_str_new2(a1));
if (q == Qnil)
rb_raise (EM_eConnectionNotBound, "unknown connection: %s", a1);
rb_funcall (q, Intern_proxy_target_unbound, 0);
@@ -169,6 +164,7 @@ t_initialize_event_machine
static VALUE t_initialize_event_machine (VALUE self)
{
evma_initialize_library (event_callback_wrapper);
+ conns = st_init_strtable();
return Qnil;
}
@@ -923,6 +919,18 @@ static VALUE t_set_heartbeat_interval (VALUE self, VALUE interval)
}
+/**********
+t_register
+***********/
+
+static VALUE t_register (VALUE self, VALUE conobj, VALUE signature)
+{
+ if (st_insert(conns, (st_data_t) StringValuePtr(signature), (st_data_t) conobj))
+ return Qtrue;
+ return Qfalse;
+}
+
+
/*********************
Init_rubyeventmachine
*********************/
@@ -977,6 +985,7 @@ extern "C" void Init_rubyeventmachine()
rb_define_module_function (EmModule, "connect_server", (VALUE(*)(...))t_connect_server, 2);
rb_define_module_function (EmModule, "bind_connect_server", (VALUE(*)(...))t_bind_connect_server, 4);
rb_define_module_function (EmModule, "connect_unix_server", (VALUE(*)(...))t_connect_unix_server, 1);
+ rb_define_module_function (EmModule, "register", (VALUE(*)(...))t_register, 2);
rb_define_module_function (EmModule, "attach_fd", (VALUE (*)(...))t_attach_fd, 3);
rb_define_module_function (EmModule, "detach_fd", (VALUE (*)(...))t_detach_fd, 1);
diff --git a/lib/em/connection.rb b/lib/em/connection.rb
index 57a3312..10b7170 100644
--- a/lib/em/connection.rb
+++ b/lib/em/connection.rb
@@ -39,6 +39,7 @@ module EventMachine
# Store signature and run #post_init
@signature = sig
+ EM.register(self, sig)
associate_callback_target sig
post_init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment