Ejemplo n.º 1
0
/**
 * MQTT retry protocol and socket pending writes processing.
 * @param now current time
 * @param doRetry boolean - retries as well as pending writes?
 * @return not actually used
 */
int MQTTProtocol_retry(time_t now, int doRetry)
{
	Node* current = NULL;
	int rc = 0;

	FUNC_ENTRY;
	current = TreeNextElement(bstate->clients, current);
	/* look through the outbound message list of each client, checking to see if a retry is necessary */
	while (current)
	{
		Clients* client = (Clients*)(current->content);
		current = TreeNextElement(bstate->clients, current);
		if (client->connected == 0)
		{
#if defined(MQTTS)
			if (client->protocol == PROTOCOL_MQTTS)
			{
				if (difftime(now,client->lastContact) > bstate->retry_interval)
				{
					int rc2 = 0;
					/* NB: no dup bit for these packets */
					if (client->connect_state == 1) /* TODO: handle err */
						rc2 = MQTTSPacket_send_willTopicReq(client);
					else if (client->connect_state == 2) /* TODO: handle err */
						rc2 = MQTTSPacket_send_willMsgReq(client);
					if (rc2 == SOCKET_ERROR)
					{
						client->good = 0;
						Log(LOG_WARNING, 29, NULL, client->clientID, client->socket);
						MQTTProtocol_closeSession(client, 1);
						client = NULL;
					}
				}
			}
#endif
			continue;
		}
		if (client->good == 0)
		{
			MQTTProtocol_closeSession(client, 1);
			continue;
		}
		if (Socket_noPendingWrites(client->socket) == 0)
			continue;
		if (doRetry)
			MQTTProtocol_retries(now, client);
		if (client)
		{
			if (MQTTProtocol_processQueued(client))
				rc = 1;
		}
	}
	FUNC_EXIT_RC(rc);
	return rc;
}
Ejemplo n.º 2
0
int MQTTSProtocol_handleWillTopics(void* pack, int sock, char* clientAddr, Clients* client)
{
	MQTTS_WillTopic* willTopic = (MQTTS_WillTopic*)pack;
	int rc = 0;

	FUNC_ENTRY;
	Log(LOG_PROTOCOL, 45, NULL, sock, clientAddr, client->clientID,
				willTopic->flags.QoS, willTopic->flags.retain, willTopic->willTopic);
	if (client->connect_state == 1)
	{
		MQTTProtocol_setWillTopic(client, willTopic->willTopic, willTopic->flags.retain, willTopic->flags.QoS);
		willTopic->willTopic = NULL;
		client->connect_state = 2;
		rc = MQTTSPacket_send_willMsgReq(client);
	}
	MQTTSPacket_free_packet(pack);
	time( &(client->lastContact) );
	FUNC_EXIT_RC(rc);
	return rc;
}