Skip to content

Commit

Permalink
Make exception messages work (grrrrr GNU strerror_r)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgdm committed Sep 18, 2013
1 parent 334b735 commit 77d2395
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
33 changes: 16 additions & 17 deletions mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ PHP_METHOD(Mosquitto_Client, __construct)
object->client = mosquitto_new(id, clean_session, object);

if (!object->client) {
char buf[0x100];
strerror_r(errno, buf, 0x100);
zend_throw_exception(mosquitto_ce_exception, buf, 1 TSRMLS_CC);
char *message = strerror_wrapper(errno);
zend_throw_exception(mosquitto_ce_exception, message, 1 TSRMLS_CC);
}
}
/* }}} */
Expand Down Expand Up @@ -69,7 +68,6 @@ PHP_METHOD(Mosquitto_Client, connect)
object->connect_callback_cache = connect_callback_cache;

if (connect_callback.object_ptr != NULL) {
php_printf("Adding callback reference\n");
Z_ADDREF_P(connect_callback.object_ptr);
}

Expand All @@ -82,11 +80,10 @@ PHP_METHOD(Mosquitto_Client, connect)
}

if (retval == MOSQ_ERR_INVAL) {
zend_throw_exception(mosquitto_ce_exception, "Invalid parameters", 0 TSRMLS_CC);
zend_throw_exception(mosquitto_ce_exception, "Invalid parameters", 1 TSRMLS_CC);
} else if (retval == MOSQ_ERR_ERRNO) {
char buf[0x100];
strerror_r(errno, buf, 0x100);
zend_throw_exception(mosquitto_ce_exception, buf, 1 TSRMLS_CC);
char *message = strerror_wrapper(errno);
zend_throw_exception(mosquitto_ce_exception, message, 1 TSRMLS_CC);
}
}
/* }}} */
Expand All @@ -111,12 +108,6 @@ PHP_METHOD(Mosquitto_Client, publish)
object = (mosquitto_client_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
retval = mosquitto_publish(object->client, &mid, topic, payload_len, (void *) payload, qos, retain);

if (retval != MOSQ_ERR_SUCCESS) {
char buf[0x100];
strerror_r(errno, buf, 0x100);
zend_throw_exception(mosquitto_ce_exception, buf, 1 TSRMLS_CC);
}

char *message = NULL;
switch (retval) {
case MOSQ_ERR_SUCCESS:
Expand Down Expand Up @@ -146,7 +137,6 @@ PHP_METHOD(Mosquitto_Client, publish)

}
zend_throw_exception(mosquitto_ce_exception, message, 0 TSRMLS_CC);
efree(message);
}
/* }}} */

Expand Down Expand Up @@ -198,12 +188,22 @@ PHP_METHOD(Mosquitto_Client, loop)
strerror_r(errno, message, 256);
}
zend_throw_exception(mosquitto_ce_exception, message, 0 TSRMLS_CC);
efree(message);
}
/* }}} */

/* Internal functions */

char *strerror_wrapper(int err)
{
char *buf = ecalloc(256, sizeof(char));
POSSIBLY_UNUSED char *bbuf = buf;
#ifdef _GNU_SOURCE
bbuf =
#endif
strerror_r(err, buf, 256);
return bbuf;
}

static void mosquitto_client_object_destroy(void *object TSRMLS_DC)
{
mosquitto_client_object *client = (mosquitto_client_object *) object;
Expand Down Expand Up @@ -237,7 +237,6 @@ static zend_object_value mosquitto_client_object_new() {

PHP_MOSQUITTO_API void php_mosquitto_connect_callback(struct mosquitto *mosq, void *obj, int rc)
{
php_printf("Called function\n");
mosquitto_client_object *object = (mosquitto_client_object *) obj;
zval *params[1], *retval_ptr = NULL;

Expand Down
8 changes: 8 additions & 0 deletions php_mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ extern zend_module_entry mosquitto_module_entry;
# define PHP_MOSQUITTO_API
#endif

#ifdef __GLIBC__
#define POSSIBLY_UNUSED __attribute__((unused))
#else
#define POSSIBLY_UNUSED
#endif

#ifdef ZTS
#include "TSRM.h"
#endif
Expand All @@ -33,6 +39,8 @@ typedef struct _mosquitto_client_object {

PHP_MOSQUITTO_API void php_mosquitto_connect_callback(struct mosquitto *mosq, void *obj, int rc);

char *strerror_wrapper(int err);

PHP_MINIT_FUNCTION(mosquitto);
PHP_MSHUTDOWN_FUNCTION(mosquitto);
PHP_MINFO_FUNCTION(mosquitto);
Expand Down
5 changes: 4 additions & 1 deletion test.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

$client = new Mosquitto\Client("test");
$client->connect("localhost", 1883, 5, function($r) { echo "Got code {$r}\n"; });
$client->connect("localhost", 1883, 5, 'callback');
$client->loop();

function callback($r) {
echo "I got code {$r}\n";
}

0 comments on commit 77d2395

Please sign in to comment.