ThingWorx C SDK
twWebsocket.h
Go to the documentation of this file.
1 /***************************************
2  * Copyright (C) 2015 ThingWorx Inc. *
3  ***************************************/
4 
13 #include "twOSPort.h"
14 #include "twDefinitions.h"
15 
16 #ifndef TW_WEBSOCKET_H
17 #define TW_WEBSOCKET_H
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /*
24 HTTP Parser declarations for the
25 joyent http-parser library
26 */
27 struct http_parser;
28 struct http_parser_settings;
29 
30 /*
31 Forward declarations of the struct and call back functions
32 used by the http-parser library
33 */
34 struct twWs;
35 typedef int (*ws_cb) (struct twWs * ws);
36 typedef int (*ws_data_cb) (struct twWs * ws, const char *at, size_t length);
37 
38 /*
39 Helper macros
40 */
41 #define WS_TLS_CONN(a) (twTlsClient *)a->connection
42 
48  ,NORMAL_CLOSE = 1000
60 };
61 
65 typedef struct twWs{
67  uint32_t messageChunkSize;
68  int32_t bytesNeeded;
69  char read_state;
70  uint16_t frameSize;
71  char * frameBuffer;
72  char * frameBufferPtr;
73  unsigned char ws_header[64];
74  unsigned char * headerPtr;
75  char * host;
76  uint16_t port;
77  char * api_key;
78  char * gatewayName;
80  char * gatewayType;
82  unsigned char * security_key;
83  uint32_t sessionId;
84  char * resource;
88  signed char connect_state;
89  signed char isConnected;
91  ws_data_cb on_ws_binaryMessage;
92  ws_data_cb on_ws_textMessage;
93  ws_data_cb on_ws_ping;
94  ws_data_cb on_ws_pong;
95  ws_data_cb on_ws_close;
96 } twWs;
97 
129 int twWs_Create(char * host, uint16_t port, char * resource, char * api_key, char * gatewayName,
130  uint32_t messageChunkSize, uint16_t frameSize, twWs ** entity);
131 
144 int twWs_Delete(twWs * ws);
145 
156 int twWs_Connect(twWs * ws, uint32_t timeout);
157 
168 int twWs_Disconnect(twWs * ws, enum close_status code, char * reason);
169 
178 char twWs_IsConnected(twWs * ws);
179 
190 int twWs_RegisterConnectCallback(twWs * ws, ws_cb cb);
191 
202 int twWs_RegisterCloseCallback(twWs * ws, ws_data_cb cb);
203 
214 int twWs_RegisterBinaryMessageCallback(twWs * ws, ws_data_cb cb);
215 
226 int twWs_RegisterTextMessageCallback(twWs * ws, ws_data_cb cb);
227 
238 int twWs_RegisterPingCallback(twWs * ws, ws_data_cb cb);
239 
250 int twWs_RegisterPongCallback(twWs * ws, ws_data_cb cb);
251 
267 int twWs_Receive(twWs * ws, uint32_t timeout);
268 
284 int twWs_SendMessage(twWs * ws, char * buf, uint32_t length, char isText);
285 
299 int twWs_SendPing(twWs * ws, char * msg);
300 
314 int twWs_SendPong(twWs * ws, char * msg);
315 
316 #ifdef __cplusplus
317 }
318 #endif
319 
320 #endif
321 
322 
323 
struct twWs twWs
Websocket entity structure definition.
int twWs_RegisterBinaryMessageCallback(twWs *ws, ws_data_cb cb)
Registers a function to be called when the websocket receives a complete binary message.
Definition: twWebsocket.c:531
ws_data_cb on_ws_binaryMessage
Definition: twWebsocket.h:91
Definition: twWebsocket.h:54
Definition: twWebsocket.h:59
int twWs_Create(char *host, uint16_t port, char *resource, char *api_key, char *gatewayName, uint32_t messageChunkSize, uint16_t frameSize, twWs **entity)
Creates a new websocket struct and the underlying dependent components.
Definition: twWebsocket.c:124
int32_t bytesNeeded
Definition: twWebsocket.h:68
int twWs_RegisterPongCallback(twWs *ws, ws_data_cb cb)
Registers a function to be called when the websocket receives a Pong message.
Definition: twWebsocket.c:558
char twWs_IsConnected(twWs *ws)
Gets the connection status of a websocket entity structure.
Definition: twWebsocket.c:449
#define TW_MUTEX
For Linux builds a TW_MUTEX is a pthread_mutex_t.
Definition: twLinux-openssl.h:81
uint32_t sessionId
Definition: twWebsocket.h:83
signed char isConnected
Definition: twWebsocket.h:89
uint16_t frameSize
Definition: twWebsocket.h:70
char * gatewayType
Definition: twWebsocket.h:80
int twWs_SendPing(twWs *ws, char *msg)
Send a Ping message over the websocket.
Definition: twWebsocket.c:875
int twWs_Disconnect(twWs *ws, enum close_status code, char *reason)
Disconnect a websocket connection from the server.
Definition: twWebsocket.c:453
Wrappers for OS-specific functionality.
unsigned char * security_key
Definition: twWebsocket.h:82
Definition: twWebsocket.h:55
ws_cb on_ws_connected
Definition: twWebsocket.h:90
char * frameBufferPtr
Definition: twWebsocket.h:72
int twWs_RegisterPingCallback(twWs *ws, ws_data_cb cb)
Registers a function to be called when the websocket receives a Ping message.
Definition: twWebsocket.c:549
struct twTlsClient * connection
Definition: twWebsocket.h:66
Definition: twWebsocket.h:56
Definition: twWebsocket.h:52
int twWs_RegisterCloseCallback(twWs *ws, ws_data_cb cb)
Registers a function to be called when the websocket is closed by the server.
Definition: twWebsocket.c:522
uint32_t messageChunkSize
Definition: twWebsocket.h:67
uint16_t port
Definition: twWebsocket.h:76
signed char connect_state
Definition: twWebsocket.h:88
Definition: twWebsocket.h:47
int twWs_SendMessage(twWs *ws, char *buf, uint32_t length, char isText)
Send a message over the websocket.
Definition: twWebsocket.c:815
char read_state
Definition: twWebsocket.h:69
Common definitions for C SDK.
Definition: twWebsocket.h:48
int twWs_RegisterConnectCallback(twWs *ws, ws_cb cb)
Registers a function to be called when the websocket is successfully connected.
Definition: twWebsocket.c:513
ws_data_cb on_ws_close
Definition: twWebsocket.h:95
Definition: twWebsocket.h:49
Definition: twWebsocket.h:57
int twWs_Receive(twWs *ws, uint32_t timeout)
Check the websocket for data and drive the state machine of the websocket.
Definition: twWebsocket.c:568
Definition: twWebsocket.h:58
close_status
Websocket close reasoning enumeration.
Definition: twWebsocket.h:46
ws_data_cb on_ws_pong
Definition: twWebsocket.h:94
char * api_key
Definition: twWebsocket.h:77
int twWs_SendPong(twWs *ws, char *msg)
Send a Pong message over the websocket.
Definition: twWebsocket.c:885
char * resource
Definition: twWebsocket.h:84
int twWs_RegisterTextMessageCallback(twWs *ws, ws_data_cb cb)
Registers a function to be called when the websocket receives a complete text message.
Definition: twWebsocket.c:540
ws_data_cb on_ws_textMessage
Definition: twWebsocket.h:92
TW_MUTEX sendMessageMutex
Definition: twWebsocket.h:85
TW_MUTEX recvMutex
Definition: twWebsocket.h:87
unsigned char ws_header[64]
Definition: twWebsocket.h:73
TW_MUTEX sendFrameMutex
Definition: twWebsocket.h:86
Definition: twWebsocket.h:53
TLS client structure definition.
Definition: twTls.h:34
Websocket entity structure definition.
Definition: twWebsocket.h:65
Definition: twWebsocket.h:50
int twWs_Delete(twWs *ws)
Frees all memory associated with a twWs structure and all its owned substructures.
Definition: twWebsocket.c:205
char * gatewayName
Definition: twWebsocket.h:78
ws_data_cb on_ws_ping
Definition: twWebsocket.h:93
char * host
Definition: twWebsocket.h:75
int twWs_Connect(twWs *ws, uint32_t timeout)
Establishes a websocket connection to the server.
Definition: twWebsocket.c:228
Definition: twWebsocket.h:51
char * frameBuffer
Definition: twWebsocket.h:71
unsigned char * headerPtr
Definition: twWebsocket.h:74