Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Raise better errors from EM::connect
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Oct 13, 2009
1 parent cb8e438 commit aad3a82
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
10 changes: 5 additions & 5 deletions ext/em.cpp
Expand Up @@ -1021,17 +1021,17 @@ const unsigned long EventMachine_t::ConnectToServer (const char *bind_addr, int
*/

if (!server || !*server || !port)
return NULL;
throw std::runtime_error ("invalid server or port");

int family, bind_size;
struct sockaddr bind_as, *bind_as_ptr = name2address (server, port, &family, &bind_size);
if (!bind_as_ptr)
return NULL;
throw std::runtime_error ("unable to resolve server address");
bind_as = *bind_as_ptr; // copy because name2address points to a static

int sd = socket (family, SOCK_STREAM, 0);
if (sd == INVALID_SOCKET)
return NULL;
throw std::runtime_error ("unable to create new socket");

/*
sockaddr_in pin;
Expand Down Expand Up @@ -1065,7 +1065,7 @@ const unsigned long EventMachine_t::ConnectToServer (const char *bind_addr, int
// Set the new socket nonblocking.
if (!SetSocketNonblocking (sd)) {
closesocket (sd);
return NULL;
throw std::runtime_error ("unable to set socket as non-blocking");
}
// Disable slow-start (Nagle algorithm).
int one = 1;
Expand All @@ -1078,7 +1078,7 @@ const unsigned long EventMachine_t::ConnectToServer (const char *bind_addr, int
struct sockaddr *bind_to = name2address (bind_addr, bind_port, &bind_to_family, &bind_to_size);
if (!bind_to) {
closesocket (sd);
throw std::runtime_error ("bad bind address");
throw std::runtime_error ("invalid bind address");
}
if (bind (sd, bind_to, bind_to_size) < 0) {
closesocket (sd);
Expand Down
25 changes: 15 additions & 10 deletions ext/rubymain.cpp
Expand Up @@ -32,6 +32,7 @@ Statics
static VALUE EmModule;
static VALUE EmConnection;

static VALUE EM_eConnectionError;
static VALUE EM_eUnknownTimerFired;
static VALUE EM_eConnectionNotBound;
static VALUE EM_eUnsupported;
Expand Down Expand Up @@ -458,10 +459,14 @@ static VALUE t_connect_server (VALUE self, VALUE server, VALUE port)
// Specifically, if the value of port comes in as a string rather than an integer,
// NUM2INT will throw a type error, but FIX2INT will generate garbage.

const unsigned long f = evma_connect_to_server (NULL, 0, StringValuePtr(server), NUM2INT(port));
if (!f)
rb_raise (rb_eRuntimeError, "no connection");
return ULONG2NUM (f);
try {
const unsigned long f = evma_connect_to_server (NULL, 0, StringValuePtr(server), NUM2INT(port));
if (!f)
rb_raise (EM_eConnectionError, "no connection");
return ULONG2NUM (f);
} catch (std::runtime_error e) {
rb_raise (EM_eConnectionError, e.what());
}
}

/*********************
Expand All @@ -474,15 +479,14 @@ static VALUE t_bind_connect_server (VALUE self, VALUE bind_addr, VALUE bind_port
// Specifically, if the value of port comes in as a string rather than an integer,
// NUM2INT will throw a type error, but FIX2INT will generate garbage.

long f;
try {
f = evma_connect_to_server (StringValuePtr(bind_addr), NUM2INT(bind_port), StringValuePtr(server), NUM2INT(port));
const unsigned long f = evma_connect_to_server (StringValuePtr(bind_addr), NUM2INT(bind_port), StringValuePtr(server), NUM2INT(port));
if (!f)
rb_raise (rb_eRuntimeError, "no connection");
rb_raise (EM_eConnectionError, "no connection");
return ULONG2NUM (f);
} catch (std::runtime_error e) {
rb_sys_fail(e.what());
rb_raise (EM_eConnectionError, e.what());
}
return ULONG2NUM (f);
}

/*********************
Expand Down Expand Up @@ -1048,7 +1052,8 @@ extern "C" void Init_rubyeventmachine()
EmModule = rb_define_module ("EventMachine");
EmConnection = rb_define_class_under (EmModule, "Connection", rb_cObject);

rb_define_class_under (EmModule, "NoHandlerForAcceptedConnection", rb_eException);
rb_define_class_under (EmModule, "NoHandlerForAcceptedConnection", rb_eRuntimeError);
EM_eConnectionError = rb_define_class_under (EmModule, "ConnectionError", rb_eRuntimeError);
EM_eConnectionNotBound = rb_define_class_under (EmModule, "ConnectionNotBound", rb_eRuntimeError);
EM_eUnknownTimerFired = rb_define_class_under (EmModule, "UnknownTimerFired", rb_eRuntimeError);
EM_eUnsupported = rb_define_class_under (EmModule, "Unsupported", rb_eRuntimeError);
Expand Down

0 comments on commit aad3a82

Please sign in to comment.