Skip to content

Commit

Permalink
Add onDisconnect callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Maclean committed Sep 23, 2013
1 parent 5b193c5 commit 64309f4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
90 changes: 89 additions & 1 deletion mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,40 @@ PHP_METHOD(Mosquitto_Client, onMessage)
}
/* }}} */

/* {{{ */
PHP_METHOD(Mosquitto_Client, onDisconnect)
{
mosquitto_client_object *object;
zend_fcall_info disconnect_callback = empty_fcall_info;
zend_fcall_info_cache disconnect_callback_cache = empty_fcall_info_cache;

PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!",
&disconnect_callback, &disconnect_callback_cache) == FAILURE) {

PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();

object = (mosquitto_client_object *) zend_object_store_get_object(getThis() TSRMLS_CC);

if (!ZEND_FCI_INITIALIZED(disconnect_callback)) {
zend_throw_exception(mosquitto_ce_exception, "Need a valid callback", 0 TSRMLS_CC);
}

object->disconnect_callback = disconnect_callback;
object->disconnect_callback_cache = disconnect_callback_cache;
Z_ADDREF_P(disconnect_callback.function_name);

if (disconnect_callback.object_ptr != NULL) {
Z_ADDREF_P(disconnect_callback.object_ptr);
}

mosquitto_disconnect_callback_set(object->client, php_mosquitto_disconnect_callback);
}
/* }}} */

/* {{{ */
PHP_METHOD(Mosquitto_Client, publish)
{
Expand Down Expand Up @@ -259,7 +293,28 @@ PHP_METHOD(Mosquitto_Client, loop)
PHP_MOSQUITTO_RESTORE_ERRORS();

object = (mosquitto_client_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
retval = mosquitto_loop(object->client, 1000, 1);
retval = mosquitto_loop(object->client, timeout, 1);
php_mosquitto_handle_errno(retval, errno);
}
/* }}} */

/* {{{ */
PHP_METHOD(Mosquitto_Client, loopForever)
{
mosquitto_client_object *object;
long timeout = 1000, max_packets = 0, retval = 0;
char *message = NULL;

PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l!l!",
&timeout, &max_packets) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();

object = (mosquitto_client_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
retval = mosquitto_loop_forever(object->client, timeout, 1);
php_mosquitto_handle_errno(retval, errno);
}
/* }}} */
Expand Down Expand Up @@ -454,16 +509,49 @@ PHP_MOSQUITTO_API void php_mosquitto_subscribe_callback(struct mosquitto *mosq,
}
}

PHP_MOSQUITTO_API void php_mosquitto_disconnect_callback(struct mosquitto *mosq, void *obj, int rc)
{
mosquitto_client_object *object = (mosquitto_client_object *) obj;
zval *retval_ptr = NULL;
zval **params = ecalloc(1, sizeof (zval));

if (!ZEND_FCI_INITIALIZED(object->disconnect_callback)) {
return;
}

ALLOC_INIT_ZVAL(params[0]);
ZVAL_LONG(params[0], rc);

object->disconnect_callback.params = &params;
object->disconnect_callback.param_count = 1;
object->disconnect_callback.retval_ptr_ptr = &retval_ptr;
object->disconnect_callback.no_separation = 1;

if (zend_call_function(&object->disconnect_callback, &object->disconnect_callback_cache TSRMLS_CC) == FAILURE) {
if (!EG(exception)) {
zend_throw_exception_ex(mosquitto_ce_exception, 0 TSRMLS_CC, "Failed to invoke connect callback %s()", Z_STRVAL_P(object->disconnect_callback.function_name));
}
}

zval_ptr_dtor(params);

if (retval_ptr != NULL) {
zval_ptr_dtor(&retval_ptr);
}
}

/* {{{ mosquitto_client_methods */
const zend_function_entry mosquitto_client_methods[] = {
PHP_ME(Mosquitto_Client, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Mosquitto_Client, connect, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, onConnect, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, onSubscribe, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, onMessage, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, onDisconnect, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, publish, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, subscribe, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, loop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, loopForever, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
Expand Down
1 change: 1 addition & 0 deletions php_mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ extern zend_class_entry *mosquitto_ce_message;
PHP_MOSQUITTO_API void php_mosquitto_connect_callback(struct mosquitto *mosq, void *obj, int rc);
PHP_MOSQUITTO_API void php_mosquitto_message_callback(struct mosquitto *mosq, void *client_obj, const struct mosquitto_message *message);
PHP_MOSQUITTO_API void php_mosquitto_subscribe_callback(struct mosquitto *mosq, void *client_obj, int mid, int qos_count, const int *granted_qos);
PHP_MOSQUITTO_API void php_mosquitto_disconnect_callback(struct mosquitto *mosq, void *obj, int rc);


char *php_mosquitto_strerror_wrapper(int err);
Expand Down

0 comments on commit 64309f4

Please sign in to comment.