Skip to content

Commit

Permalink
Add string description of broker response to onConnect callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mgdm committed Dec 3, 2013
1 parent 4349fa2 commit b369ba6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ The callback should take parameters of the form:
| Parameter | Type | Description |
| --- | --- | ---- |
| rc | int | Response code from the broker. |
| message | string | String description of the response code. |

Response codes are as follows:

Expand Down
4 changes: 2 additions & 2 deletions examples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

$client->loopForever();

function connect($r) {
echo "I got code {$r}\n";
function connect($r, $message) {
echo "I got code {$r} and message {$message}\n";
}

function subscribe() {
Expand Down
13 changes: 10 additions & 3 deletions mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,9 @@ void php_mosquitto_handle_errno(int retval, int err TSRMLS_DC) {
PHP_MOSQUITTO_API void php_mosquitto_connect_callback(struct mosquitto *mosq, void *obj, int rc)
{
mosquitto_client_object *object = (mosquitto_client_object *) obj;
zval *retval_ptr = NULL, *rc_zval = NULL;
zval **params[1];
zval *retval_ptr = NULL, *rc_zval = NULL, *message_zval = NULL;
zval **params[2];
const char *message;
#ifdef ZTS
TSRMLS_D = object->TSRMLS_C;
#endif
Expand All @@ -758,8 +759,13 @@ PHP_MOSQUITTO_API void php_mosquitto_connect_callback(struct mosquitto *mosq, vo
ZVAL_LONG(rc_zval, rc);
params[0] = &rc_zval;

message = mosquitto_connack_string(rc);
MAKE_STD_ZVAL(message_zval);
ZVAL_STRINGL(message_zval, message, strlen(message), 1);
params[1] = &message_zval;

object->connect_callback.params = params;
object->connect_callback.param_count = 1;
object->connect_callback.param_count = 2;
object->connect_callback.retval_ptr_ptr = &retval_ptr;

if (zend_call_function(&object->connect_callback, &object->connect_callback_cache TSRMLS_CC) == FAILURE) {
Expand All @@ -769,6 +775,7 @@ PHP_MOSQUITTO_API void php_mosquitto_connect_callback(struct mosquitto *mosq, vo
}

zval_ptr_dtor(&rc_zval);
zval_ptr_dtor(&message_zval);

if (retval_ptr != NULL) {
zval_ptr_dtor(&retval_ptr);
Expand Down

0 comments on commit b369ba6

Please sign in to comment.