The Pubnub C-core library. It's home is on https://github.com/pubnub/c_core, this is a copy

Dependents:   Pubnub_c_core_mbed2_pal Pubnub_c_core_mbed2_pal Pubnub_c_core_mbed2_pal2

Committer:
sveljko
Date:
Tue Nov 22 22:21:39 2016 +0000
Revision:
2:d85e42c1125d
Parent:
0:d13755cfb705
Added `pubnub_helper` module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sveljko 0:d13755cfb705 1 /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
sveljko 0:d13755cfb705 2 #if !defined INC_PUBNUB_INTERNAL_COMMON
sveljko 0:d13755cfb705 3 #define INC_PUBNUB_INTERNAL_COMMON
sveljko 0:d13755cfb705 4
sveljko 0:d13755cfb705 5 #include "pubnub_config.h"
sveljko 0:d13755cfb705 6 #include "pubnub_ccore.h"
sveljko 0:d13755cfb705 7 #include "pubnub_netcore.h"
sveljko 0:d13755cfb705 8 #include "pubnub_mutex.h"
sveljko 0:d13755cfb705 9
sveljko 0:d13755cfb705 10 #if defined(PUBNUB_CALLBACK_API)
sveljko 0:d13755cfb705 11 #include "pubnub_ntf_callback.h"
sveljko 0:d13755cfb705 12 #endif
sveljko 0:d13755cfb705 13
sveljko 0:d13755cfb705 14 #if !defined PUBNUB_PROXY_API
sveljko 0:d13755cfb705 15 #define PUBNUB_PROXY_API 0
sveljko 0:d13755cfb705 16 #elif PUBNUB_PROXY_API
sveljko 0:d13755cfb705 17 #include "pubnub_proxy.h"
sveljko 0:d13755cfb705 18 #endif
sveljko 0:d13755cfb705 19
sveljko 0:d13755cfb705 20 #include <stdint.h>
sveljko 0:d13755cfb705 21
sveljko 0:d13755cfb705 22
sveljko 0:d13755cfb705 23 #if !defined PUBNUB_USE_ADNS
sveljko 0:d13755cfb705 24 #define PUBNUB_USE_ADNS 0
sveljko 0:d13755cfb705 25 #endif
sveljko 0:d13755cfb705 26
sveljko 0:d13755cfb705 27
sveljko 0:d13755cfb705 28 /** State of a Pubnub socket. Some states are specific to some
sveljko 0:d13755cfb705 29 PALs.
sveljko 0:d13755cfb705 30 */
sveljko 0:d13755cfb705 31 enum PBSocketState {
sveljko 0:d13755cfb705 32 /** Socket idle - unused */
sveljko 0:d13755cfb705 33 STATE_NONE = 0,
sveljko 0:d13755cfb705 34 /** ACK received on the socket */
sveljko 0:d13755cfb705 35 STATE_ACKED = 1,
sveljko 0:d13755cfb705 36 /** Reading a number of octets */
sveljko 0:d13755cfb705 37 STATE_READ = 2,
sveljko 0:d13755cfb705 38 /** Block reading of new data, even though we have an indicator
sveljko 0:d13755cfb705 39 that there is data to read.
sveljko 0:d13755cfb705 40 */
sveljko 0:d13755cfb705 41 STATE_BLOCKED_NEWDATA = 3,
sveljko 0:d13755cfb705 42 /** All of the data that has arrived has been read */
sveljko 0:d13755cfb705 43 STATE_NEWDATA_EXHAUSTED = 4,
sveljko 0:d13755cfb705 44 /** All of the data that was to be sent has been sent. */
sveljko 0:d13755cfb705 45 STATE_DATA_SENT = 6,
sveljko 0:d13755cfb705 46 /** Reading a line */
sveljko 0:d13755cfb705 47 STATE_READ_LINE = 7
sveljko 0:d13755cfb705 48 };
sveljko 0:d13755cfb705 49
sveljko 0:d13755cfb705 50
sveljko 0:d13755cfb705 51 /** The Pubnub context
sveljko 0:d13755cfb705 52
sveljko 0:d13755cfb705 53 @note Don't declare any members as `bool`, as there may be
sveljko 0:d13755cfb705 54 alignment issues when this is included from both C and C++
sveljko 0:d13755cfb705 55 compilers, especially pre-C99 C compilers (like MSVC (at least
sveljko 0:d13755cfb705 56 until MSVC 2013)).
sveljko 0:d13755cfb705 57 */
sveljko 0:d13755cfb705 58 struct pubnub_ {
sveljko 0:d13755cfb705 59 struct pbcc_context core;
sveljko 0:d13755cfb705 60
sveljko 0:d13755cfb705 61 /** Network communication state */
sveljko 0:d13755cfb705 62 enum pubnub_state state;
sveljko 0:d13755cfb705 63 /** Type of current transaction */
sveljko 0:d13755cfb705 64 enum pubnub_trans trans;
sveljko 0:d13755cfb705 65
sveljko 0:d13755cfb705 66 /** Pointer to the next data to be sent. */
sveljko 0:d13755cfb705 67 uint8_t const *sendptr;
sveljko 0:d13755cfb705 68
sveljko 0:d13755cfb705 69 /** The number of bytes left to be sent. */
sveljko 0:d13755cfb705 70 uint16_t sendlen;
sveljko 0:d13755cfb705 71
sveljko 0:d13755cfb705 72 /** The number of bytes left to be read. */
sveljko 0:d13755cfb705 73 uint16_t readlen;
sveljko 0:d13755cfb705 74
sveljko 0:d13755cfb705 75 /** Pointer to next free byte in the read buffer*/
sveljko 0:d13755cfb705 76 uint8_t *ptr;
sveljko 0:d13755cfb705 77
sveljko 0:d13755cfb705 78 /** Number of bytes left (empty) in the read buffer */
sveljko 0:d13755cfb705 79 uint16_t left;
sveljko 0:d13755cfb705 80
sveljko 0:d13755cfb705 81 /** The state of the socket. */
sveljko 0:d13755cfb705 82 enum PBSocketState sock_state;
sveljko 0:d13755cfb705 83
sveljko 0:d13755cfb705 84 /** Number of bytes to read - given by the user */
sveljko 0:d13755cfb705 85 unsigned len;
sveljko 0:d13755cfb705 86
sveljko 0:d13755cfb705 87 /** Indicates whether we are receiving chunked or regular HTTP
sveljko 0:d13755cfb705 88 * response
sveljko 0:d13755cfb705 89 */
sveljko 0:d13755cfb705 90 uint16_t http_chunked;
sveljko 0:d13755cfb705 91
sveljko 0:d13755cfb705 92 /** Last received HTTP (result) code */
sveljko 0:d13755cfb705 93 uint16_t http_code;
sveljko 0:d13755cfb705 94
sveljko 0:d13755cfb705 95 #if defined PUBNUB_ORIGIN_SETTABLE
sveljko 0:d13755cfb705 96 char const *origin;
sveljko 0:d13755cfb705 97 #endif
sveljko 0:d13755cfb705 98
sveljko 0:d13755cfb705 99 #if 0
sveljko 0:d13755cfb705 100 /** Process that started last transaction */
sveljko 0:d13755cfb705 101 struct process *initiator;
sveljko 0:d13755cfb705 102
sveljko 0:d13755cfb705 103 uint8_t *readptr; /* Pointer to the next data to be read. */
sveljko 0:d13755cfb705 104 #endif
sveljko 0:d13755cfb705 105
sveljko 0:d13755cfb705 106 struct pubnub_pal pal;
sveljko 0:d13755cfb705 107
sveljko 0:d13755cfb705 108 struct pubnub_options {
sveljko 0:d13755cfb705 109 /** Indicates whether to use blocking I/O. Ignored if
sveljko 0:d13755cfb705 110 choosing between blocking and non-blocking is not supported
sveljko 0:d13755cfb705 111 on a platform. Would be ifdef-ed out, but then it would be
sveljko 0:d13755cfb705 112 possible for this struct to have no members which is
sveljko 0:d13755cfb705 113 prohibited by the ISO C standard.
sveljko 0:d13755cfb705 114 */
sveljko 0:d13755cfb705 115 bool use_blocking_io : 1;
sveljko 0:d13755cfb705 116
sveljko 0:d13755cfb705 117 #if PUBNUB_USE_SSL
sveljko 0:d13755cfb705 118 /** Should the PubNub client establish the connection to
sveljko 0:d13755cfb705 119 * PubNub using SSL? */
sveljko 0:d13755cfb705 120 bool useSSL : 1;
sveljko 0:d13755cfb705 121 /** When SSL is enabled, should PubNub client ignore all SSL
sveljko 0:d13755cfb705 122 * certificate-handshake issues and still continue in SSL mode
sveljko 0:d13755cfb705 123 * if it experiences issues handshaking across local proxies,
sveljko 0:d13755cfb705 124 * firewalls, etc?
sveljko 0:d13755cfb705 125 */
sveljko 0:d13755cfb705 126 bool ignoreSSL : 1;
sveljko 0:d13755cfb705 127 /** When SSL is enabled, should the client fallback to a
sveljko 0:d13755cfb705 128
sveljko 0:d13755cfb705 129 * non-SSL connection if it experiences issues handshaking
sveljko 0:d13755cfb705 130 * across local proxies, firewalls, etc?
sveljko 0:d13755cfb705 131 */
sveljko 0:d13755cfb705 132 bool fallbackSSL : 1;
sveljko 0:d13755cfb705 133 #endif
sveljko 0:d13755cfb705 134 } options;
sveljko 0:d13755cfb705 135
sveljko 0:d13755cfb705 136 #if PUBNUB_THREADSAFE
sveljko 0:d13755cfb705 137 pubnub_mutex_t monitor;
sveljko 0:d13755cfb705 138 #endif
sveljko 0:d13755cfb705 139
sveljko 0:d13755cfb705 140 #if PUBNUB_TIMERS_API
sveljko 0:d13755cfb705 141 /** Duration of the transaction timeout, in milliseconds */
sveljko 0:d13755cfb705 142 int transaction_timeout_ms;
sveljko 0:d13755cfb705 143
sveljko 0:d13755cfb705 144 #if defined(PUBNUB_CALLBACK_API)
sveljko 0:d13755cfb705 145 struct pubnub_ *previous;
sveljko 0:d13755cfb705 146 struct pubnub_ *next;
sveljko 0:d13755cfb705 147 int timeout_left_ms;
sveljko 0:d13755cfb705 148 #endif
sveljko 0:d13755cfb705 149
sveljko 0:d13755cfb705 150 #endif
sveljko 0:d13755cfb705 151
sveljko 0:d13755cfb705 152 #if defined(PUBNUB_CALLBACK_API)
sveljko 0:d13755cfb705 153 pubnub_callback_t cb;
sveljko 0:d13755cfb705 154 void *user_data;
sveljko 0:d13755cfb705 155 #endif
sveljko 0:d13755cfb705 156
sveljko 0:d13755cfb705 157 #if PUBNUB_PROXY_API
sveljko 0:d13755cfb705 158
sveljko 0:d13755cfb705 159 /** The type (protocol) of the proxy to use */
sveljko 0:d13755cfb705 160 enum pubnub_proxy_type proxy_type;
sveljko 0:d13755cfb705 161
sveljko 0:d13755cfb705 162 /** Hostname (address) of the proxy server to use */
sveljko 0:d13755cfb705 163 char proxy_hostname[PUBNUB_MAX_PROXY_HOSTNAME_LENGTH + 1];
sveljko 0:d13755cfb705 164
sveljko 0:d13755cfb705 165 /** The (TCP) port to use on the proxy. */
sveljko 0:d13755cfb705 166 uint16_t proxy_port;
sveljko 0:d13755cfb705 167
sveljko 0:d13755cfb705 168 /** Indicates whether this is the "first" HTTP request - that is,
sveljko 0:d13755cfb705 169 the `CONNECT` one. The first is sent to the proxy, while the
sveljko 0:d13755cfb705 170 second (if the first succeeds) is sent to the "real" HTTP
sveljko 0:d13755cfb705 171 server (to which the proxy established a "tunnel".
sveljko 0:d13755cfb705 172 */
sveljko 0:d13755cfb705 173 bool proxy_tunnel_established;
sveljko 0:d13755cfb705 174
sveljko 0:d13755cfb705 175 /** The saved path part of the URL for the Pubnub transaction.
sveljko 0:d13755cfb705 176 */
sveljko 0:d13755cfb705 177 char proxy_saved_path[PUBNUB_BUF_MAXLEN];
sveljko 0:d13755cfb705 178
sveljko 0:d13755cfb705 179 #endif
sveljko 0:d13755cfb705 180 };
sveljko 0:d13755cfb705 181
sveljko 0:d13755cfb705 182
sveljko 0:d13755cfb705 183
sveljko 0:d13755cfb705 184 /** Internal function, to be called when the outcome of a
sveljko 0:d13755cfb705 185 REST call / transaction has been reached.
sveljko 0:d13755cfb705 186 */
sveljko 0:d13755cfb705 187 void pbntf_trans_outcome(pubnub_t *pb);
sveljko 0:d13755cfb705 188
sveljko 0:d13755cfb705 189 int pbntf_init(void);
sveljko 0:d13755cfb705 190
sveljko 0:d13755cfb705 191 int pbntf_got_socket(pubnub_t *pb, pb_socket_t socket);
sveljko 0:d13755cfb705 192
sveljko 0:d13755cfb705 193 void pbntf_update_socket(pubnub_t *pb, pb_socket_t socket);
sveljko 0:d13755cfb705 194
sveljko 0:d13755cfb705 195 void pbntf_lost_socket(pubnub_t *pb, pb_socket_t socket);
sveljko 0:d13755cfb705 196
sveljko 0:d13755cfb705 197 int pbntf_enqueue_for_processing(pubnub_t *pb);
sveljko 0:d13755cfb705 198
sveljko 0:d13755cfb705 199 int pbntf_requeue_for_processing(pubnub_t *pb);
sveljko 0:d13755cfb705 200
sveljko 0:d13755cfb705 201 int pbntf_watch_in_events(pubnub_t *pb);
sveljko 0:d13755cfb705 202 int pbntf_watch_out_events(pubnub_t *pb);
sveljko 0:d13755cfb705 203
sveljko 0:d13755cfb705 204
sveljko 0:d13755cfb705 205 /** Internal function. Checks if the given pubnub context pointer
sveljko 0:d13755cfb705 206 is valid.
sveljko 0:d13755cfb705 207 */
sveljko 0:d13755cfb705 208 bool pb_valid_ctx_ptr(pubnub_t const *pb);
sveljko 0:d13755cfb705 209
sveljko 0:d13755cfb705 210 /** Internal function, only available in the "static" context
sveljko 0:d13755cfb705 211 allocator. Gives a context with the given index.
sveljko 0:d13755cfb705 212 */
sveljko 0:d13755cfb705 213 pubnub_t *pballoc_get_ctx(unsigned idx);
sveljko 0:d13755cfb705 214
sveljko 0:d13755cfb705 215 /** Internal function, the "bottom half" of pubnub_free(), which is
sveljko 0:d13755cfb705 216 done asynchronously in the callback mode. */
sveljko 0:d13755cfb705 217 void pballoc_free_at_last(pubnub_t *pb);
sveljko 0:d13755cfb705 218
sveljko 0:d13755cfb705 219
sveljko 0:d13755cfb705 220 #endif /* !defined INC_PUBNUB_INTERNAL_COMMON */