diff -r e86ee1f4227d -r 21547232c875 lm_connection.c --- a/lm_connection.c Sat Jan 16 14:17:55 2010 +0200 +++ b/lm_connection.c Sun Jan 17 02:16:30 2010 +0200 @@ -128,8 +128,8 @@ { llm_connection_t *object = luaL_checklm_connection (L, 1); llm_callback_t *cb; + GError *err = NULL; luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); - GError *err; cb = luaL_malloc (L, sizeof (llm_callback_t)); cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); @@ -141,7 +141,8 @@ else { lua_pushnil (L); lua_pushstring (L, err->message); - I ("Connection opening failed"); + I ("Connection opening failed: %s", err -> message); + g_error_free (err); return 2; } } @@ -174,6 +175,7 @@ lua_pushnil (L); lua_pushstring (L, err->message); I ("Connection authentication failed: %s", err->message); + g_error_free (err); return 2; } } @@ -311,6 +313,7 @@ I ("Connection close failed: %s", err->message); lua_pushnil (L); lua_pushstring (L, err->message); + g_error_free (err); return 2; } } @@ -376,6 +379,7 @@ I ("Message sending failed: %s", err->message); lua_pushnil (L); lua_pushstring (L, err->message); + g_error_free (err); return 2; } } @@ -383,45 +387,59 @@ /// connection:handler /// Registers or unregisters handler function for a given type of messages. /// To unregister handler, omit the priority argument. +/// To unregister reply handler, omit message type argument too. /// Handler function can be specified as plain function or message handler object. -/// Though, you can unregister only a message handler object. -/// A: message handler callback function or lm message handler object, message type, handler priority (optional) +/// A: message handler callback function or lm message handler object, message type (optional), handler priority (optional) /// R: lm connection object static int handler_lm_connection (lua_State *L) { llm_connection_t *object = luaL_checklm_connection (L, 1); - int type = luaL_checkenum (L, 3, type_lm_message); + int top = lua_gettop (L); + if (top > 2) { // Have message type + int type = luaL_checkenum (L, 3, type_lm_message); - if (lua_gettop (L) > 3) { // Register - int priority = luaL_checkenum (L, 4, priority_lm_handler); + if (lua_gettop (L) > 3) { // Register + int priority = luaL_checkenum (L, 4, priority_lm_handler); - if (lua_isfunction (L, 2)) { // Function - LmMessageHandler *handler; - llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t)); - lua_pushvalue (L, 2); - cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); - cb->L = L; + if (lua_isfunction (L, 2)) { // Function + LmMessageHandler *handler; + llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t)); + lua_pushvalue (L, 2); + cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); + cb->L = L; - handler = lm_message_handler_new ( - (LmHandleMessageFunction)callback_lm_handler, - cb, (GDestroyNotify)llm_callback_destroy); - lm_connection_register_message_handler (object->connection, - handler, type, priority); - lm_message_handler_unref (handler); - } else { // Object + handler = lm_message_handler_new ( + (LmHandleMessageFunction)callback_lm_handler, + cb, (GDestroyNotify)llm_callback_destroy); + lm_connection_register_message_handler (object->connection, + handler, type, priority); + lm_message_handler_unref (handler); + } else { // Object + llm_handler_t *handler = luaL_checklm_handler (L, 2); + lm_connection_register_message_handler (object->connection, + handler->handler, + type, priority); + } + lua_pop (L, 1); + } else { // Unregister message handler llm_handler_t *handler = luaL_checklm_handler (L, 2); - lm_connection_register_message_handler (object->connection, - handler->handler, - type, priority); + lm_connection_unregister_message_handler (object->connection, + handler->handler, + type); } lua_pop (L, 1); - } else { // Unregister + } else { // Unregister reply handler +#if 0 llm_handler_t *handler = luaL_checklm_handler (L, 2); - lm_connection_unregister_message_handler (object->connection, - handler->handler, - type); + lm_connection_unregister_reply_handler (object -> connection, + handler -> handler); +#else + lua_pushnil (L); + lua_pushliteral (L, "Sorry, lm_connection_unregister_reply_handler is not yet supported"); + return 2; +#endif } - lua_pop (L, 2); + lua_pop (L, 1); return 1; } @@ -473,6 +491,7 @@ I ("Synchronous connection opening failed: %s", err->message); lua_pushnil (L); lua_pushstring (L, err->message); + g_error_free (err); return 2; } } @@ -495,6 +514,7 @@ I ("Synchronous authentication failed: %s", err->message); lua_pushnil (L); lua_pushstring (L, err->message); + g_error_free (err); return 2; } } @@ -514,6 +534,7 @@ I ("Synchronous message sending failed: %s", err->message); lua_pushnil (L); lua_pushstring (L, err->message); + g_error_free (err); return 2; } else { bless_lm_message (L, new);