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 5029 2022-01-16 21:32:09Z 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 
139 typedef struct SMQ
140 {
141  SOCKET sock;
142 
143  U8* buf;
144 #ifdef SMQ_ENABLE_SENDBUF
145  U8* sBuf;
146 #endif
147 
150  U32 timeout;
151  S32 pingTmoCounter,pingTmo;
152  U32 clientTid;
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
162  U16 frameLen;
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
276 } SMQ;
277 
278 #ifdef __cplusplus
279 extern "C" {
280 #endif
281 
296 void SMQ_constructor(SMQ* o, U8* buf, U16 bufLen);
297 
298 
304 #define SMQ_setCtx(o, ctx) SOCKET_constructor(&(o)->sock, ctx)
305 
306 
319 int SMQ_init(SMQ* o, const char* url, U32* rnd);
320 
351 int 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 
379 int SMQ_create(SMQ* o, const char* topic);
380 
381 
390 int SMQ_createsub(SMQ* o, const char* subtopic);
391 
392 
401 int SMQ_subscribe(SMQ* o, const char* topic);
402 
403 
408 int SMQ_unsubscribe(SMQ* o, U32 tid);
409 
410 
420 int SMQ_publish(SMQ* o, const void* data, int len, U32 tid, U32 subtid);
421 
422 
434 int SMQ_wrtstr(SMQ* o, const char* str);
435 
449 int SMQ_write(SMQ* o, const void* data, int len);
450 
468 int SMQ_pubflush(SMQ* o, U32 tid, U32 subtid);
469 
470 
485 int SMQ_observe(SMQ* o, U32 tid);
486 
487 
492 int SMQ_unobserve(SMQ* o, U32 tid);
493 
494 
524 int SMQ_getMessage(SMQ* o, U8** msg);
525 
526 
530 #define SMQ_getMsgSize(o) ((o)->frameLen-15)
531  /* end group SMQClient_C */
533 
534 #ifdef __cplusplus
535 }
536 inline SMQ::SMQ(U8* buf, U16 bufLen) {
537  SMQ_constructor(this,buf, bufLen);
538 }
539 
540 inline int SMQ::init(const char* url, U32* rnd) {
541  return SMQ_init(this, url, rnd);
542 }
543 
544 inline int SMQ::connect(const char* uid, int uidLen, const char* credentials,
545  U8 credLen, const char* info, int infoLen) {
546  return SMQ_connect(this, uid, uidLen, credentials, credLen, info, infoLen);
547 }
548 
549 inline void SMQ::disconnect() {
550  return SMQ_disconnect(this);
551 }
552 
553 inline SMQ::~SMQ() {
554  SMQ_destructor(this);
555 }
556 
557 inline int SMQ::create(const char* topic) {
558  return SMQ_create(this, topic);
559 }
560 
561 inline int SMQ::createsub(const char* subtopic) {
562  return SMQ_createsub(this, subtopic);
563 }
564 
565 inline int SMQ::subscribe(const char* topic) {
566  return SMQ_subscribe(this, topic);
567 }
568 
569 inline int SMQ::unsubscribe(U32 tid) {
570  return SMQ_unsubscribe(this, tid);
571 }
572 
573 inline int SMQ::publish(const void* data, int len, U32 tid, U32 subtid) {
574  return SMQ_publish(this, data, len, tid, subtid);
575 }
576 
577 inline int SMQ::wrtstr(const char* str) {
578  return SMQ_wrtstr(this, str);
579 }
580 
581 inline int SMQ::write( const void* data, int len) {
582  return SMQ_write(this, data, len);
583 }
584 
585 inline int SMQ::pubflush(U32 tid, U32 subtid) {
586  return SMQ_pubflush(this, tid, subtid);
587 }
588 
589 inline int SMQ::observe(U32 tid) {
590  return SMQ_observe(this, tid);
591 }
592 
593 inline int SMQ::unobserve(U32 tid) {
594  return SMQ_unobserve(this, tid);
595 }
596 
597 inline int SMQ::getMessage(U8** msg) {
598  return SMQ_getMessage(this, msg);
599 }
600 
601 inline int SMQ::getMsgSize() {
602  return SMQ_getMsgSize(this);
603 }
604 
605 #endif
606 
607 
608 /*
609  The SharkMQ (secure SMQ) compatibility API makes it easy to write
610  code that can later be upgraded to a secure version, if needed.
611 */
612 #ifndef SHARKMQ_COMPAT
613 #define SHARKMQ_COMPAT 1
614 #endif
615 
616 #if SHARKMQ_COMPAT
617 #define SharkMQ SMQ
618 #define SharkSslCon void
619 #define SharkMQ_constructor(o, buf, bufLen) SMQ_constructor(o, buf, bufLen)
620 #define SharkMQ_setCtx(o, ctx) SMQ_setCtx(o, ctx)
621 #define SharkMQ_init(o, scon, url, rnd) SMQ_init(o, url, rnd)
622 #define SharkMQ_connect(o, uid, uidLen, credentials, credLen, info, infoLen, NotApplicable) \
623  SMQ_connect(o, uid, uidLen, credentials, credLen, info, infoLen)
624 #define SharkMQ_disconnect(o) SMQ_disconnect(o)
625 #define SharkMQ_destructor(o) SMQ_destructor(o)
626 #define SharkMQ_create(o, topic) SMQ_create(o, topic)
627 #define SharkMQ_createsub(o, subtopic) SMQ_createsub(o, subtopic)
628 #define SharkMQ_subscribe(o, topic) SMQ_subscribe(o, topic)
629 #define SharkMQ_unsubscribe(o, tid) SMQ_unsubscribe(o, tid)
630 #define SharkMQ_publish(o, data, len, tid, subtid) \
631  SMQ_publish(o, data, len, tid, subtid)
632 #define SharkMQ_wrtstr(o, str) SMQ_wrtstr(o, str)
633 #define SharkMQ_write(o, data, len) SMQ_write(o, data, len)
634 #define SharkMQ_pubflush(o, tid, subtid) SMQ_pubflush(o, tid, subtid)
635 #define SharkMQ_observe(o, tid) SMQ_observe(o, tid)
636 #define SharkMQ_unobserve(o, tid) SMQ_unobserve(o, tid)
637 #define SharkMQ_getMessage(o, msg) SMQ_getMessage(o, msg)
638 #define SharkMQ_getMsgSize(o) SMQ_getMsgSize(o)
639 #endif
640 
641  /* end group selib */
643 
644 #endif
SMQ::createsub
int createsub(const char *subtopic)
Create a sub-topic and fetch the subtopic ID.
Definition: SMQ.h:561
SMQ::frameLen
U16 frameLen
The SimpleMQ frame size for the incomming data.
Definition: SMQ.h:162
SMQ::unobserve
int unobserve(U32 tid)
Stop receiving change notifications for a topic ID or ephemeral topic ID.
Definition: SMQ.h:593
SMQ::status
int status
Last known error code.
Definition: SMQ.h:156
SMQ_init
int SMQ_init(SMQ *o, const char *url, U32 *rnd)
Initiate the SMQ server connection.
SMQ_pubflush
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.
SMQ::disconnect
void disconnect()
Gracefully close the connection.
Definition: SMQ.h:549
SMQ::unsubscribe
int unsubscribe(U32 tid)
Requests the broker to unsubscribe the server from a topic.
Definition: SMQ.h:569
SMQ_disconnect
void SMQ_disconnect(SMQ *o)
Gracefully close the connection.
SMQ_unobserve
int SMQ_unobserve(SMQ *o, U32 tid)
Stop receiving change notifications for a topic ID or ephemeral topic ID.
SMQ_constructor
void SMQ_constructor(SMQ *o, U8 *buf, U16 bufLen)
Create a SimpleMQ client instance.
SMQ::init
int init(const char *url, U32 *rnd)
Initiate the SMQ server connection.
Definition: SMQ.h:540
SMQ::publish
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:573
SMQ::ptid
U32 ptid
Publisher's tid: Set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:154
SMQ::subscribe
int subscribe(const char *topic)
The response to SMQ_subscribe is asynchronous and returned as status SMQ_SUBACK via SMQ_getMessage.
Definition: SMQ.h:565
SMQ::wrtstr
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:577
SMQ::write
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:581
SMQ::create
int create(const char *topic)
Create a topic an fetch the topic ID (tid).
Definition: SMQ.h:557
SOCKET
#define SOCKET
The SOCKET object/handle is an 'int' when using a BSD compatible TCP/IP stack.
Definition: selib.h:142
SMQ_publish
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.
SMQ::timeout
U32 timeout
Timeout in milliseconds to wait in functions waiting for server data.
Definition: SMQ.h:150
SMQ::getMsgSize
int getMsgSize()
Returns the message size, which is SMQ::frameLen - 15.
Definition: SMQ.h:601
SMQ::buf
U8 * buf
The buffer set via the constructor.
Definition: SMQ.h:143
SMQ_create
int SMQ_create(SMQ *o, const char *topic)
Create a topic an fetch the topic ID (tid).
SMQ::observe
int observe(U32 tid)
Request the broker to provide change notification events when the number of subscribers to a specific...
Definition: SMQ.h:589
SMQ_subscribe
int SMQ_subscribe(SMQ *o, const char *topic)
The response to SMQ_subscribe is asynchronous and returned as status SMQ_SUBACK via SMQ_getMessage.
SMQ::clientTid
U32 clientTid
Client's unique topic ID.
Definition: SMQ.h:152
SMQ::bytesRead
U16 bytesRead
Read frame data using SMQ_getMessage until: frameLen - bytesRead = 0.
Definition: SMQ.h:164
SMQ_destructor
void SMQ_destructor(SMQ *o)
Terminate a SimpleMQ instance.
SMQ::connect
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:544
SMQ
SimpleMQ structure.
Definition: SMQ.h:140
SMQ::subtid
U32 subtid
Sub-tid: set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:155
SMQ_getMsgSize
#define SMQ_getMsgSize(o)
Returns the message size, which is SMQ::frameLen - 15.
Definition: SMQ.h:530
SMQ_unsubscribe
int SMQ_unsubscribe(SMQ *o, U32 tid)
Requests the broker to unsubscribe the server from a topic.
SMQ_wrtstr
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...
SMQ::pubflush
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:585
SMQ::getMessage
int getMessage(U8 **msg)
Wait for messages sent from the broker.
Definition: SMQ.h:597
SMQ::~SMQ
~SMQ()
Terminate a SimpleMQ instance.
Definition: SMQ.h:553
SMQ::SMQ
SMQ(U8 *buf, U16 bufLen)
Create a SimpleMQ client instance.
Definition: SMQ.h:536
SMQ_observe
int SMQ_observe(SMQ *o, U32 tid)
Request the broker to provide change notification events when the number of subscribers to a specific...
SMQ::tid
U32 tid
Topic: set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:153
SMQ_write
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...
SMQ_getMessage
int SMQ_getMessage(SMQ *o, U8 **msg)
Wait for messages sent from the broker.
SMQ
struct SMQ SMQ
SimpleMQ structure.
SMQ_connect
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.
SMQ_createsub
int SMQ_createsub(SMQ *o, const char *subtopic)
Create a sub-topic and fetch the subtopic ID.