Unverified Commit b6ff870f authored by Ian Craggs's avatar Ian Craggs Committed by GitHub

Merge pull request #403 from fpagliughi/callback_contexts

Added separate context pointers for each of the global callbacks [#399]
parents ca279453 e798b9ce
......@@ -318,7 +318,9 @@ typedef struct MQTTAsync_struct
MQTTAsync_connectionLost* cl;
MQTTAsync_messageArrived* ma;
MQTTAsync_deliveryComplete* dc;
void* context; /* the context to be associated with the main callbacks*/
void* clContext; /* the context to be associated with the conn lost callback*/
void* maContext; /* the context to be associated with the msg arrived callback*/
void* dcContext; /* the context to be associated with the deliv complete callback*/
MQTTAsync_connected* connected;
void* connected_context; /* the context to be associated with the connected callback*/
......@@ -1021,7 +1023,7 @@ static void MQTTAsync_checkDisconnect(MQTTAsync handle, MQTTAsync_command* comma
if (m->cl && was_connected)
{
Log(TRACE_MIN, -1, "Calling connectionLost for client %s", m->c->clientID);
(*(m->cl))(m->context, NULL);
(*(m->cl))(m->clContext, NULL);
}
MQTTAsync_startConnectRetry(m);
}
......@@ -2365,7 +2367,7 @@ int MQTTAsync_setCallbacks(MQTTAsync handle, void* context,
rc = MQTTASYNC_FAILURE;
else
{
m->context = context;
m->clContext = m->maContext = m->dcContext = context;
m->cl = cl;
m->ma = ma;
m->dc = dc;
......@@ -2376,6 +2378,74 @@ int MQTTAsync_setCallbacks(MQTTAsync handle, void* context,
return rc;
}
int MQTTAsync_setConnectionLostCallback(MQTTAsync handle, void* context,
MQTTAsync_connectionLost* cl)
{
int rc = MQTTASYNC_SUCCESS;
MQTTAsyncs* m = handle;
FUNC_ENTRY;
MQTTAsync_lock_mutex(mqttasync_mutex);
if (m == NULL || m->c->connect_state != 0)
rc = MQTTASYNC_FAILURE;
else
{
m->clContext = context;
m->cl = cl;
}
MQTTAsync_unlock_mutex(mqttasync_mutex);
FUNC_EXIT_RC(rc);
return rc;
}
int MQTTAsync_setMessageArrivedCallback(MQTTAsync handle, void* context,
MQTTAsync_messageArrived* ma)
{
int rc = MQTTASYNC_SUCCESS;
MQTTAsyncs* m = handle;
FUNC_ENTRY;
MQTTAsync_lock_mutex(mqttasync_mutex);
if (m == NULL || ma == NULL || m->c->connect_state != 0)
rc = MQTTASYNC_FAILURE;
else
{
m->maContext = context;
m->ma = ma;
}
MQTTAsync_unlock_mutex(mqttasync_mutex);
FUNC_EXIT_RC(rc);
return rc;
}
int MQTTAsync_setDeliveryCompleteCallback(MQTTAsync handle, void* context,
MQTTAsync_deliveryComplete* dc)
{
int rc = MQTTASYNC_SUCCESS;
MQTTAsyncs* m = handle;
FUNC_ENTRY;
MQTTAsync_lock_mutex(mqttasync_mutex);
if (m == NULL || m->c->connect_state != 0)
rc = MQTTASYNC_FAILURE;
else
{
m->dcContext = context;
m->dc = dc;
}
MQTTAsync_unlock_mutex(mqttasync_mutex);
FUNC_EXIT_RC(rc);
return rc;
}
int MQTTAsync_setDisconnected(MQTTAsync handle, void* context, MQTTAsync_disconnected* disconnected)
{
......@@ -2507,7 +2577,7 @@ static int MQTTAsync_deliverMessage(MQTTAsyncs* m, char* topicName, size_t topic
Log(TRACE_MIN, -1, "Calling messageArrived for client %s, queue depth %d",
m->c->clientID, m->c->messageQueue->count);
rc = (*(m->ma))(m->context, topicName, (int)topicLen, mm);
rc = (*(m->ma))(m->maContext, topicName, (int)topicLen, mm);
/* if 0 (false) is returned by the callback then it failed, so we don't remove the message from
* the queue, and it will be retried later. If 1 is returned then the message data may have been freed,
* so we must be careful how we use it.
......@@ -3564,7 +3634,7 @@ static MQTTPacket* MQTTAsync_cycle(int* sock, unsigned long timeout, int* rc)
if (m->dc)
{
Log(TRACE_MIN, -1, "Calling deliveryComplete for client %s, msgid %d", m->c->clientID, msgid);
(*(m->dc))(m->context, msgid);
(*(m->dc))(m->dcContext, msgid);
}
/* use the msgid to find the callback to be called */
while (ListNextElement(m->responses, &current))
......
......@@ -650,8 +650,76 @@ typedef struct MQTTAsync_responseOptions MQTTAsync_callOptions;
* ::MQTTASYNC_FAILURE if an error occurred.
*/
DLLExport int MQTTAsync_setCallbacks(MQTTAsync handle, void* context, MQTTAsync_connectionLost* cl,
MQTTAsync_messageArrived* ma, MQTTAsync_deliveryComplete* dc);
MQTTAsync_messageArrived* ma, MQTTAsync_deliveryComplete* dc);
/**
* This function sets the callback function for a connection lost event for
* a specific client. Any necessary message acknowledgements and status
* communications are handled in the background without any intervention
* from the client application.
*
* <b>Note:</b> The MQTT client must be disconnected when this function is
* called.
* @param handle A valid client handle from a successful call to
* MQTTAsync_create().
* @param context A pointer to any application-specific context. The
* the <i>context</i> pointer is passed the callback functions to provide
* access to the context information in the callback.
* @param cl A pointer to an MQTTAsync_connectionLost() callback
* function. You can set this to NULL if your application doesn't handle
* disconnections.
* @return ::MQTTASYNC_SUCCESS if the callbacks were correctly set,
* ::MQTTASYNC_FAILURE if an error occurred.
*/
DLLExport int MQTTAsync_setConnectionLostCallback(MQTTAsync handle, void* context,
MQTTAsync_connectionLost* cl);
/**
* This function sets the callback function for a message arrived event for
* a specific client. Any necessary message acknowledgements and status
* communications are handled in the background without any intervention
* from the client application. If you do not set a messageArrived callback
* function, you will not be notified of the receipt of any messages as a
* result of a subscription.
*
* <b>Note:</b> The MQTT client must be disconnected when this function is
* called.
* @param handle A valid client handle from a successful call to
* MQTTAsync_create().
* @param context A pointer to any application-specific context. The
* the <i>context</i> pointer is passed to the callback functions to provide
* access to the context information in the callback.
* @param ma A pointer to an MQTTAsync_messageArrived() callback
* function. You can set this to NULL if your application doesn't handle
* receipt of messages.
* @return ::MQTTASYNC_SUCCESS if the callbacks were correctly set,
* ::MQTTASYNC_FAILURE if an error occurred.
*/
DLLExport int MQTTAsync_setMessageArrivedCallback(MQTTAsync handle, void* context,
MQTTAsync_messageArrived* ma);
/**
* This function sets the callback function for a delivery complete event
* for a specific client. Any necessary message acknowledgements and status
* communications are handled in the background without any intervention
* from the client application.
*
* <b>Note:</b> The MQTT client must be disconnected when this function is
* called.
* @param handle A valid client handle from a successful call to
* MQTTAsync_create().
* @param context A pointer to any application-specific context. The
* the <i>context</i> pointer is passed to the callback functions to provide
* access to the context information in the callback.
* @param dc A pointer to an MQTTAsync_deliveryComplete() callback
* function. You can set this to NULL if you do not want to check
* for successful delivery.
* @return ::MQTTASYNC_SUCCESS if the callbacks were correctly set,
* ::MQTTASYNC_FAILURE if an error occurred.
*/
DLLExport int MQTTAsync_setDeliveryCompleteCallback(MQTTAsync handle, void* context,
MQTTAsync_deliveryComplete* dc);
/**
* Sets the MQTTAsync_connected() callback function for a client.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment