Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
paho.mqtt.c
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
eclipse
paho.mqtt.c
Commits
85da5246
Commit
85da5246
authored
May 28, 2013
by
Ian Craggs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bug #409267 - add multiple server list to the connectOptions
parent
adaada7f
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
340 additions
and
112 deletions
+340
-112
MQTTAsync.c
src/MQTTAsync.c
+204
-44
MQTTAsync.h
src/MQTTAsync.h
+35
-18
MQTTClient.c
src/MQTTClient.c
+86
-49
MQTTClient.h
src/MQTTClient.h
+15
-1
No files found.
src/MQTTAsync.c
View file @
85da5246
This diff is collapsed.
Click to expand it.
src/MQTTAsync.h
View file @
85da5246
...
...
@@ -13,6 +13,7 @@
* Contributors:
* Ian Craggs - initial API and implementation
* Ian Craggs, Allan Stockdill-Mander - SSL connections
* Ian Craggs - multiple server connection support
*******************************************************************************/
/********************************************************************/
...
...
@@ -541,7 +542,10 @@ typedef struct
{
/** The eyecatcher for this structure. must be MQTC. */
char
struct_id
[
4
];
/** The version number of this structure. Must be 0 or 1. 0 signifies no SSL options */
/** The version number of this structure. Must be 0, 1 or 2.
* 0 signifies no SSL options and no serverURIs
* 1 signifies no serverURIs
*/
int
struct_version
;
/** The "keep alive" interval, measured in seconds, defines the maximum time
* that should pass without communication between the client and the server
...
...
@@ -607,32 +611,45 @@ typedef struct
*/
int
retryInterval
;
/**
* This is a pointer to an MQTTAsync_SSLOptions structure. If your
* application does not make use of SSL, set this pointer to NULL.
*/
* This is a pointer to an MQTTAsync_SSLOptions structure. If your
* application does not make use of SSL, set this pointer to NULL.
*/
MQTTAsync_SSLOptions
*
ssl
;
/**
* A pointer to a callback function to be called if the connect successfully
* completes. Can be set to NULL, in which case no indication of successful
* completion will be received.
*/
* A pointer to a callback function to be called if the connect successfully
* completes. Can be set to NULL, in which case no indication of successful
* completion will be received.
*/
MQTTAsync_onSuccess
*
onSuccess
;
/**
* A pointer to a callback function to be called if the connect fails.
* Can be set to NULL, in which case no indication of unsuccessful
* completion will be received.
*/
* A pointer to a callback function to be called if the connect fails.
* Can be set to NULL, in which case no indication of unsuccessful
* completion will be received.
*/
MQTTAsync_onFailure
*
onFailure
;
/**
* A pointer to any application-specific context. The
* the <i>context</i> pointer is passed to success or failure callback functions to
* provide access to the context information in the callback.
*/
void
*
context
;
* A pointer to any application-specific context. The
* the <i>context</i> pointer is passed to success or failure callback functions to
* provide access to the context information in the callback.
*/
void
*
context
;
/**
* The number of entries in the serverURIs array.
*/
int
serverURIcount
;
/**
* An array of null-terminated strings specifying the servers to
* which the client will connect. Each string takes the form <i>protocol://host:port</i>.
* <i>protocol</i> must be <i>tcp</i> or <i>ssl</i>. For <i>host</i>, you can
* specify either an IP address or a domain name. For instance, to connect to
* a server running on the local machines with the default MQTT port, specify
* <i>tcp://localhost:1883</i>.
*/
char
**
serverURIs
;
}
MQTTAsync_connectOptions
;
#define MQTTAsync_connectOptions_initializer { {'M', 'Q', 'T', 'C'},
1, 60, 1, 10, NULL, NULL, NULL, 30, 20, NULL, NULL
}
#define MQTTAsync_connectOptions_initializer { {'M', 'Q', 'T', 'C'},
2, 60, 1, 10, NULL, NULL, NULL, 30, 20, NULL, NULL, 0, NULL
}
/**
* This function attempts to connect a previously-created client (see
...
...
src/MQTTClient.c
View file @
85da5246
...
...
@@ -15,6 +15,7 @@
* Ian Craggs - bug 384016 - segv setting will message
* Ian Craggs - bug 384053 - v1.0.0.7 - stop MQTTClient_receive on socket error
* Ian Craggs, Allan Stockdill-Mander - add ability to connect with SSL
* Ian Craggs - multiple server connection support
*******************************************************************************/
#include <stdlib.h>
...
...
@@ -704,55 +705,14 @@ void Protocol_processPublication(Publish* publish, Clients* client)
}
int
MQTTClient_connect
(
MQTTClient
handle
,
MQTTClient_connectOptions
*
options
)
int
MQTTClient_connect
URI
(
MQTTClient
handle
,
MQTTClient_connectOptions
*
options
,
char
*
serverURI
)
{
MQTTClients
*
m
=
handle
;
int
rc
=
SOCKET_ERROR
;
START_TIME_TYPE
start
;
long
millisecsTimeout
=
30000L
;
int
rc
=
SOCKET_ERROR
;
FUNC_ENTRY
;
Thread_lock_mutex
(
mqttclient_mutex
);
if
(
options
==
NULL
)
{
rc
=
MQTTCLIENT_NULL_PARAMETER
;
goto
exit
;
}
if
(
strncmp
(
options
->
struct_id
,
"MQTC"
,
4
)
!=
0
||
(
options
->
struct_version
!=
0
&&
options
->
struct_version
!=
1
))
{
rc
=
MQTTCLIENT_BAD_STRUCTURE
;
goto
exit
;
}
if
(
options
->
will
)
/* check validity of will options structure */
{
if
(
strncmp
(
options
->
will
->
struct_id
,
"MQTW"
,
4
)
!=
0
||
options
->
will
->
struct_version
!=
0
)
{
rc
=
MQTTCLIENT_BAD_STRUCTURE
;
goto
exit
;
}
}
#if defined(OPENSSL)
if
(
options
->
struct_version
!=
0
&&
options
->
ssl
)
/* check validity of SSL options structure */
{
if
(
strncmp
(
options
->
ssl
->
struct_id
,
"MQTS"
,
4
)
!=
0
||
options
->
ssl
->
struct_version
!=
0
)
{
rc
=
MQTTCLIENT_BAD_STRUCTURE
;
goto
exit
;
}
}
#endif
if
((
options
->
username
&&
!
UTF8_validateString
(
options
->
username
))
||
(
options
->
password
&&
!
UTF8_validateString
(
options
->
password
)))
{
rc
=
MQTTCLIENT_BAD_UTF8_STRING
;
goto
exit
;
}
millisecsTimeout
=
options
->
connectTimeout
*
1000
;
start
=
MQTTClient_start_clock
();
if
(
m
->
ma
&&
!
running
)
...
...
@@ -788,11 +748,11 @@ int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options)
m
->
c
->
password
=
options
->
password
;
m
->
c
->
retryInterval
=
options
->
retryInterval
;
Log
(
TRACE_MIN
,
-
1
,
"Connecting to serverURI %s"
,
m
->
serverURI
);
Log
(
TRACE_MIN
,
-
1
,
"Connecting to serverURI %s"
,
serverURI
);
#if defined(OPENSSL)
rc
=
MQTTProtocol_connect
(
m
->
serverURI
,
m
->
c
,
m
->
ssl
);
rc
=
MQTTProtocol_connect
(
serverURI
,
m
->
c
,
m
->
ssl
);
#else
rc
=
MQTTProtocol_connect
(
m
->
serverURI
,
m
->
c
);
rc
=
MQTTProtocol_connect
(
serverURI
,
m
->
c
);
#endif
if
(
rc
==
SOCKET_ERROR
)
goto
exit
;
...
...
@@ -919,11 +879,88 @@ exit:
MQTTClient_disconnect
(
handle
,
0
);
/* not "internal" because we don't want to call connection lost */
Thread_lock_mutex
(
mqttclient_mutex
);
}
FUNC_EXIT_RC
(
rc
);
return
rc
;
}
int
MQTTClient_connect
(
MQTTClient
handle
,
MQTTClient_connectOptions
*
options
)
{
MQTTClients
*
m
=
handle
;
int
rc
=
SOCKET_ERROR
;
FUNC_ENTRY
;
Thread_lock_mutex
(
mqttclient_mutex
);
if
(
options
==
NULL
)
{
rc
=
MQTTCLIENT_NULL_PARAMETER
;
goto
exit
;
}
if
(
strncmp
(
options
->
struct_id
,
"MQTC"
,
4
)
!=
0
||
(
options
->
struct_version
!=
0
&&
options
->
struct_version
!=
1
&&
options
->
struct_version
!=
2
))
{
rc
=
MQTTCLIENT_BAD_STRUCTURE
;
goto
exit
;
}
if
(
options
->
will
)
/* check validity of will options structure */
{
if
(
strncmp
(
options
->
will
->
struct_id
,
"MQTW"
,
4
)
!=
0
||
options
->
will
->
struct_version
!=
0
)
{
rc
=
MQTTCLIENT_BAD_STRUCTURE
;
goto
exit
;
}
}
#if defined(OPENSSL)
if
(
options
->
struct_version
!=
0
&&
options
->
ssl
)
/* check validity of SSL options structure */
{
if
(
strncmp
(
options
->
ssl
->
struct_id
,
"MQTS"
,
4
)
!=
0
||
options
->
ssl
->
struct_version
!=
0
)
{
rc
=
MQTTCLIENT_BAD_STRUCTURE
;
goto
exit
;
}
}
#endif
if
((
options
->
username
&&
!
UTF8_validateString
(
options
->
username
))
||
(
options
->
password
&&
!
UTF8_validateString
(
options
->
password
)))
{
rc
=
MQTTCLIENT_BAD_UTF8_STRING
;
goto
exit
;
}
if
(
options
->
struct_version
<
2
||
options
->
serverURIcount
==
0
)
rc
=
MQTTClient_connectURI
(
handle
,
options
,
m
->
serverURI
);
else
{
int
i
;
for
(
i
=
0
;
i
<
options
->
serverURIcount
;
++
i
)
{
char
*
serverURI
=
options
->
serverURIs
[
i
];
if
(
strncmp
(
URI_TCP
,
serverURI
,
strlen
(
URI_TCP
))
==
0
)
serverURI
+=
strlen
(
URI_TCP
);
#if defined(OPENSSL)
else
if
(
strncmp
(
URI_SSL
,
serverURI
,
strlen
(
URI_SSL
))
==
0
)
{
serverURI
+=
strlen
(
URI_SSL
);
m
->
ssl
=
1
;
}
#endif
if
((
rc
=
MQTTClient_connectURI
(
handle
,
options
,
serverURI
))
==
MQTTCLIENT_SUCCESS
)
break
;
}
}
exit:
if
(
m
->
c
->
will
)
{
free
(
m
->
c
->
will
);
m
->
c
->
will
=
NULL
;
free
(
m
->
c
->
will
);
m
->
c
->
will
=
NULL
;
}
Thread_unlock_mutex
(
mqttclient_mutex
);
FUNC_EXIT_RC
(
rc
);
...
...
@@ -1439,7 +1476,7 @@ MQTTPacket* MQTTClient_waitfor(MQTTClient handle, int packet_type, int* rc, long
socklen_t
len
=
sizeof
(
error
);
if
((
*
rc
=
getsockopt
(
m
->
c
->
net
.
socket
,
SOL_SOCKET
,
SO_ERROR
,
(
char
*
)
&
error
,
&
len
))
==
0
)
*
rc
=
error
;
*
rc
=
error
;
break
;
}
#if defined(OPENSSL)
...
...
src/MQTTClient.h
View file @
85da5246
...
...
@@ -13,6 +13,7 @@
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Ian Craggs, Allan Stockdill-Mander - SSL updates
* Ian Craggs - multiple server connection support
*******************************************************************************/
/**
...
...
@@ -544,9 +545,22 @@ typedef struct
* application does not make use of SSL, set this pointer to NULL.
*/
MQTTClient_SSLOptions
*
ssl
;
/**
* The number of entries in the serverURIs array.
*/
int
serverURIcount
;
/**
* An array of null-terminated strings specifying the servers to
* which the client will connect. Each string takes the form <i>protocol://host:port</i>.
* <i>protocol</i> must be <i>tcp</i> or <i>ssl</i>. For <i>host</i>, you can
* specify either an IP address or a domain name. For instance, to connect to
* a server running on the local machines with the default MQTT port, specify
* <i>tcp://localhost:1883</i>.
*/
char
**
serverURIs
;
}
MQTTClient_connectOptions
;
#define MQTTClient_connectOptions_initializer { {'M', 'Q', 'T', 'C'},
1, 60, 1, 1, NULL, NULL, NULL, 30, 2
0, NULL }
#define MQTTClient_connectOptions_initializer { {'M', 'Q', 'T', 'C'},
2, 60, 1, 1, NULL, NULL, NULL, 30, 20, NULL,
0, NULL }
/**
* MQTTClient_libraryInfo is used to store details relating to the currently used
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment