Barracuda Application Server C/C++ Reference
NO
SMQ.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 ****************************************************************************
10 * HEADER
11 *
12 * $Id: SMQ.h 5692 2025-10-30 20:22:59Z wini $
13 *
14 * COPYRIGHT: Real Time Logic LLC, 2014 - 2022
15 *
16 * This software is copyrighted by and is the sole property of Real
17 * Time Logic LLC. All rights, title, ownership, or other interests in
18 * the software remain the property of Real Time Logic LLC. This
19 * software may only be used in accordance with the terms and
20 * conditions stipulated in the corresponding license agreement under
21 * which the software has been supplied. Any unauthorized use,
22 * duplication, transmission, distribution, or disclosure of this
23 * software is expressly forbidden.
24 *
25 * This Copyright notice may not be removed or modified without prior
26 * written consent of Real Time Logic LLC.
27 *
28 * Real Time Logic LLC. reserves the right to modify this software
29 * without notice.
30 *
31 * https://realtimelogic.com
32 ****************************************************************************
33 * SMQ C library:
34 * https://realtimelogic.com/ba/doc/en/C/reference/html/group__SMQClient.html
35 */
36
37#ifndef __SMQClient_h
38#define __SMQClient_h
39
40#define SMQ_NONSEC
41
42#include "selib.h"
43
44#ifndef ISDIGIT
45#define ISDIGIT isdigit
46#endif
47
48#ifndef TRUE
49#define TRUE 1
50#define FALSE 0
51#endif
52
65#define SMQE_BUF_OVERFLOW -10000
66
69#define SMQE_INVALID_URL -10002
70
73#define SMQE_PROTOCOL_ERROR -10003
74
77#define SMQE_DISCONNECT -10004
78
79
82#define SMQE_PONGTIMEOUT -10005
83 /* end SMQClientErrorCodes */
85
86
100#define SMQ_SUBACK -20000
101
110#define SMQ_CREATEACK -20001
111
117#define SMQ_CREATESUBACK -20002
118
125#define SMQ_SUBCHANGE -20003
126
129#define SMQ_TIMEOUT -20004
130
131 /* end SMQClientRespCodes */
133
134
135#define SMQSTR(str) str, (sizeof(str)-1)
136
139typedef struct SMQ
140{
141 SOCKET sock;
142
143 U8* buf;
144#ifdef SMQ_ENABLE_SENDBUF
145 U8* sBuf;
146#endif
147
151 S32 pingTmoCounter,pingTmo;
153 U32 tid;
154 U32 ptid;
155 U32 subtid;
156 int status;
157 U16 bufLen;
158 U16 rBufIx;
159#ifdef SMQ_ENABLE_SENDBUF
160 U16 sBufIx;
161#endif
165 U8 inRecv; /* boolean set to true when thread blocked in SMQ_recv */
166#ifdef __cplusplus
167
171 SMQ(U8* buf, U16 bufLen);
172
173
177 int init(const char* url, U32* rnd);
178
182 int connect(const char* uid, int uidLen, const char* credentials,
183 U8 credLen, const char* info, int infoLen);
184
185
189 void disconnect();
190
191
195 ~SMQ();
196
197
201 int create(const char* topic);
202
203
207 int createsub(const char* subtopic);
208
209
214 int subscribe(const char* topic);
215
216
220 int unsubscribe(U32 tid);
221
222
228 int publish(const void* data, int len, U32 tid, U32 subtid);
229
230
235 int wrtstr(const char* str);
236
241 int write( const void* data, int len);
242
247 int pubflush(U32 tid, U32 subtid);
248
249
255 int observe(U32 tid);
256
257
261 int unobserve(U32 tid);
262
263
267 int getMessage(U8** msg);
268
269
273 int getMsgSize();
274
275#endif
277
278#ifdef __cplusplus
279extern "C" {
280#endif
281
296void SMQ_constructor(SMQ* o, U8* buf, U16 bufLen);
297
298
304#define SMQ_setCtx(o, ctx) SOCKET_constructor(&(o)->sock, ctx)
305
306
319int SMQ_init(SMQ* o, const char* url, U32* rnd);
320
351int SMQ_connect(SMQ* o, const char* uid, int uidLen, const char* credentials,
352 U8 credLen, const char* info, int infoLen);
353
354
360
361
366
367
379int SMQ_create(SMQ* o, const char* topic);
380
381
390int SMQ_createsub(SMQ* o, const char* subtopic);
391
392
401int SMQ_subscribe(SMQ* o, const char* topic);
402
403
408int SMQ_unsubscribe(SMQ* o, U32 tid);
409
410
420int SMQ_publish(SMQ* o, const void* data, int len, U32 tid, U32 subtid);
421
422
434int SMQ_wrtstr(SMQ* o, const char* str);
435
449int SMQ_write(SMQ* o, const void* data, int len);
450
468int SMQ_pubflush(SMQ* o, U32 tid, U32 subtid);
469
470
488int SMQ_observe(SMQ* o, U32 tid);
489
490
495int SMQ_unobserve(SMQ* o, U32 tid);
496
497
527int SMQ_getMessage(SMQ* o, U8** msg);
528
529
533#define SMQ_getMsgSize(o) ((o)->frameLen-15)
534 /* end group SMQClient_C */
536
537#ifdef __cplusplus
538}
539inline SMQ::SMQ(U8* buf, U16 bufLen) {
540 SMQ_constructor(this,buf, bufLen);
541}
542
543inline int SMQ::init(const char* url, U32* rnd) {
544 return SMQ_init(this, url, rnd);
545}
546
547inline int SMQ::connect(const char* uid, int uidLen, const char* credentials,
548 U8 credLen, const char* info, int infoLen) {
549 return SMQ_connect(this, uid, uidLen, credentials, credLen, info, infoLen);
550}
551
552inline void SMQ::disconnect() {
553 return SMQ_disconnect(this);
554}
555
556inline SMQ::~SMQ() {
557 SMQ_destructor(this);
558}
559
560inline int SMQ::create(const char* topic) {
561 return SMQ_create(this, topic);
562}
563
564inline int SMQ::createsub(const char* subtopic) {
565 return SMQ_createsub(this, subtopic);
566}
567
568inline int SMQ::subscribe(const char* topic) {
569 return SMQ_subscribe(this, topic);
570}
571
572inline int SMQ::unsubscribe(U32 _tid) {
573 return SMQ_unsubscribe(this, _tid);
574}
575
576inline int SMQ::publish(const void* data, int len, U32 _tid, U32 _subtid) {
577 return SMQ_publish(this, data, len, _tid, _subtid);
578}
579
580inline int SMQ::wrtstr(const char* str) {
581 return SMQ_wrtstr(this, str);
582}
583
584inline int SMQ::write( const void* data, int len) {
585 return SMQ_write(this, data, len);
586}
587
588inline int SMQ::pubflush(U32 _tid, U32 _subtid) {
589 return SMQ_pubflush(this, _tid, _subtid);
590}
591
592inline int SMQ::observe(U32 _tid) {
593 return SMQ_observe(this, _tid);
594}
595
596inline int SMQ::unobserve(U32 _tid) {
597 return SMQ_unobserve(this, _tid);
598}
599
600inline int SMQ::getMessage(U8** msg) {
601 return SMQ_getMessage(this, msg);
602}
603
604inline int SMQ::getMsgSize() {
605 return SMQ_getMsgSize(this);
606}
607
608#endif
609
610
611/*
612 The SharkMQ (secure SMQ) compatibility API makes it easy to write
613 code that can later be upgraded to a secure version, if needed.
614*/
615#ifndef SHARKMQ_COMPAT
616#define SHARKMQ_COMPAT 1
617#endif
618
619#if SHARKMQ_COMPAT
620#define SharkMQ SMQ
621#define SharkSslCon void
622#define SharkMQ_constructor(o, buf, bufLen) SMQ_constructor(o, buf, bufLen)
623#define SharkMQ_setCtx(o, ctx) SMQ_setCtx(o, ctx)
624#define SharkMQ_init(o, scon, url, rnd) SMQ_init(o, url, rnd)
625#define SharkMQ_connect(o, uid, uidLen, credentials, credLen, info, infoLen, NotApplicable) \
626 SMQ_connect(o, uid, uidLen, credentials, credLen, info, infoLen)
627#define SharkMQ_disconnect(o) SMQ_disconnect(o)
628#define SharkMQ_destructor(o) SMQ_destructor(o)
629#define SharkMQ_create(o, topic) SMQ_create(o, topic)
630#define SharkMQ_createsub(o, subtopic) SMQ_createsub(o, subtopic)
631#define SharkMQ_subscribe(o, topic) SMQ_subscribe(o, topic)
632#define SharkMQ_unsubscribe(o, tid) SMQ_unsubscribe(o, tid)
633#define SharkMQ_publish(o, data, len, tid, subtid) \
634 SMQ_publish(o, data, len, tid, subtid)
635#define SharkMQ_wrtstr(o, str) SMQ_wrtstr(o, str)
636#define SharkMQ_write(o, data, len) SMQ_write(o, data, len)
637#define SharkMQ_pubflush(o, tid, subtid) SMQ_pubflush(o, tid, subtid)
638#define SharkMQ_observe(o, tid) SMQ_observe(o, tid)
639#define SharkMQ_unobserve(o, tid) SMQ_unobserve(o, tid)
640#define SharkMQ_getMessage(o, msg) SMQ_getMessage(o, msg)
641#define SharkMQ_getMsgSize(o) SMQ_getMsgSize(o)
642#endif
643
644 /* end group selib */
646
647#endif
int SMQ_unobserve(SMQ *o, U32 tid)
Stop receiving change notifications for a topic ID or ephemeral topic ID.
int SMQ_connect(SMQ *o, const char *uid, int uidLen, const char *credentials, U8 credLen, const char *info, int infoLen)
Connect/establish a persistent SimpleMQ connection.
void SMQ_constructor(SMQ *o, U8 *buf, U16 bufLen)
Create a SimpleMQ client instance.
int SMQ_init(SMQ *o, const char *url, U32 *rnd)
Initiate the SMQ server connection.
int SMQ_getMessage(SMQ *o, U8 **msg)
Wait for messages sent from the broker.
void SMQ_destructor(SMQ *o)
Terminate a SimpleMQ instance.
int SMQ_subscribe(SMQ *o, const char *topic)
The response to SMQ_subscribe is asynchronous and returned as status SMQ_SUBACK via SMQ_getMessage.
int SMQ_unsubscribe(SMQ *o, U32 tid)
Requests the broker to unsubscribe the server from a topic.
int SMQ_observe(SMQ *o, U32 tid)
Request the broker to provide change notification events when the number of subscribers to a specific...
void SMQ_disconnect(SMQ *o)
Gracefully close the connection.
#define SMQ_getMsgSize(o)
Returns the message size, which is SMQ::frameLen - 15.
Definition: SMQ.h:533
int SMQ_pubflush(SMQ *o, U32 tid, U32 subtid)
Flush the internal buffer and request the broker to assemble all stored fragments as one message.
int SMQ_write(SMQ *o, const void *data, int len)
Publish a message in chunks and request the broker to assemble the message before publishing to the s...
int SMQ_wrtstr(SMQ *o, const char *str)
Publish a message in chunks and request the broker to assemble the message before publishing to the s...
int SMQ_createsub(SMQ *o, const char *subtopic)
Create a sub-topic and fetch the subtopic ID.
int SMQ_publish(SMQ *o, const void *data, int len, U32 tid, U32 subtid)
Publish messages to a topic and optionally to a sub-topic.
int SMQ_create(SMQ *o, const char *topic)
Create a topic an fetch the topic ID (tid).
int getMsgSize()
Returns the message size, which is SMQ::frameLen - 15.
Definition: SMQ.h:604
void disconnect()
Gracefully close the connection.
Definition: SMQ.h:552
int publish(const void *data, int len, U32 tid, U32 subtid)
Publish messages to a topic and optionally to a sub-topic.
Definition: SMQ.h:576
int pubflush(U32 tid, U32 subtid)
Flush the internal buffer and request the broker to assemble all stored fragments as one message.
Definition: SMQ.h:588
int createsub(const char *subtopic)
Create a sub-topic and fetch the subtopic ID.
Definition: SMQ.h:564
int observe(U32 tid)
Request the broker to provide change notification events when the number of subscribers to a specific...
Definition: SMQ.h:592
int getMessage(U8 **msg)
Wait for messages sent from the broker.
Definition: SMQ.h:600
int unsubscribe(U32 tid)
Requests the broker to unsubscribe the server from a topic.
Definition: SMQ.h:572
~SMQ()
Terminate a SimpleMQ instance.
Definition: SMQ.h:556
SMQ(U8 *buf, U16 bufLen)
Create a SimpleMQ client instance.
Definition: SMQ.h:539
struct SMQ SMQ
SimpleMQ structure.
int write(const void *data, int len)
Publish a message in chunks and request the broker to assemble the message before publishing to the s...
Definition: SMQ.h:584
int create(const char *topic)
Create a topic an fetch the topic ID (tid).
Definition: SMQ.h:560
int wrtstr(const char *str)
Publish a message in chunks and request the broker to assemble the message before publishing to the s...
Definition: SMQ.h:580
int init(const char *url, U32 *rnd)
Initiate the SMQ server connection.
Definition: SMQ.h:543
int unobserve(U32 tid)
Stop receiving change notifications for a topic ID or ephemeral topic ID.
Definition: SMQ.h:596
int subscribe(const char *topic)
The response to SMQ_subscribe is asynchronous and returned as status SMQ_SUBACK via SMQ_getMessage.
Definition: SMQ.h:568
int connect(const char *uid, int uidLen, const char *credentials, U8 credLen, const char *info, int infoLen)
Connect/establish a persistent SimpleMQ connection.
Definition: SMQ.h:547
#define SOCKET
The SOCKET object/handle is an 'int' when using a BSD compatible TCP/IP stack.
Definition: selib.h:142
SimpleMQ structure.
Definition: SMQ.h:140
U32 ptid
Publisher's tid: Set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:154
U16 frameLen
The SimpleMQ frame size for the incomming data.
Definition: SMQ.h:162
U32 tid
Topic: set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:153
U16 bytesRead
Read frame data using SMQ_getMessage until: frameLen - bytesRead = 0.
Definition: SMQ.h:164
U32 subtid
Sub-tid: set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:155
U32 timeout
Timeout in milliseconds to wait in functions waiting for server data.
Definition: SMQ.h:150
int status
Last known error code.
Definition: SMQ.h:156
U32 clientTid
Client's unique topic ID.
Definition: SMQ.h:152
U8 * buf
The buffer set via the constructor.
Definition: SMQ.h:143