libnavajo
web server for static and dynamic pages developement in C++
WebSocketClient.hh
Go to the documentation of this file.
1 //****************************************************************************
12 //****************************************************************************
13 
14 #ifndef WEBSOCKETCLIENT_HH_
15 #define WEBSOCKETCLIENT_HH_
16 
17 #include <queue>
18 #include <sys/timeb.h>
19 #include "libnavajo/HttpRequest.hh"
20 #include "libnavajo/nvjThread.h"
21 #include "libnavajo/nvjGzip.h"
22 
23 class WebSocket;
25 {
26  typedef struct
27  {
28  u_int8_t opcode;
29  unsigned char* message;
30  size_t length;
31  bool fin;
32  timeb date;
33  } MessageContent;
34 
35  typedef struct
36  {
37  unsigned char z_dictionary_inflate[32768];
38  unsigned int dictInfLength;
39  z_stream strm_deflate;
40  } GzipContext;
41 
42  GzipContext gzipcontext;
43  std::queue<MessageContent *> sendingQueue;
44  pthread_mutex_t sendingQueueMutex;
45  pthread_cond_t sendingNotification;
46  void addSendingQueue(MessageContent *msgContent);
47 
48  WebSocket *websocket;
49  HttpRequest *request;
50  volatile bool closing;
51  pthread_t receivingThreadId, sendingThreadId;
52 
53  void receivingThread();
54  void sendingThread();
55 
56  bool sendMessage(const MessageContent *msg);
57 
58  void updateSessionExpiration(HttpRequest *request);
59 
60  inline static void* startReceivingThread(void* t)
61  {
62  WebSocketClient *_this=static_cast<WebSocketClient *>(t);
63  _this->receivingThread();
64  pthread_exit(NULL);
65  return NULL;
66  };
67 
68  inline static void* startSendingThread(void* t)
69  {
70  WebSocketClient *_this=static_cast<WebSocketClient *>(t);
71  _this->sendingThread();
72  pthread_exit(NULL);
73  return NULL;
74  };
75 
76  void startWebSocketThreads()
77  {
78  create_thread( &receivingThreadId, WebSocketClient::startReceivingThread, static_cast<void *>(this) );
79  create_thread( &sendingThreadId, WebSocketClient::startSendingThread, static_cast<void *>(this) );
80  }
81 
82  unsigned short snd_maxLatency;
83 
84  void noSessionExpiration(HttpRequest *request);
85  void restoreSessionExpiration(HttpRequest *request);
86 
87 
88  public:
90 
92  {
93  nvj_end_stream(&(gzipcontext.strm_deflate));
94  pthread_mutex_destroy(&sendingQueueMutex);
95  pthread_cond_destroy(&sendingNotification);
96  }
97 
104  void sendTextMessage(const std::string &message, bool fin = true);
105 
113  void sendBinaryMessage(const unsigned char *message, size_t length, bool fin = true);
114 
121  void sendCloseCtrlFrame(const unsigned char *message, size_t length);
122 
128  void sendCloseCtrlFrame(const std::string &reasonMsg = "");
129 
136  void sendPingCtrlFrame(const unsigned char *message, size_t length);
137 
143  void sendPingCtrlFrame(const std::string &message);
144 
151  void sendPongCtrlFrame(const unsigned char *message, size_t length);
152 
158  void sendPongCtrlFrame(const std::string &message);
159 
160  HttpRequest *getHttpRequest() { return request; };
161 
162  void closeWS();
163  void closeSend();
164  void closeRecv();
165 };
166 
167 #endif //WEBSOCKETCLIENT_HH_
Definition: WebSocketClient.hh:24
void sendBinaryMessage(const unsigned char *message, size_t length, bool fin=true)
Definition: WebSocketClient.cc:452
void sendTextMessage(const std::string &message, bool fin=true)
Definition: WebSocketClient.cc:437
void sendPongCtrlFrame(const unsigned char *message, size_t length)
Definition: WebSocketClient.cc:487
Definition: HttpRequest.hh:42
The Http Request Parameters class.
zip compression&#39;s facilities
Definition: WebSocket.hh:27
thread&#39;s facilities
void sendCloseCtrlFrame(const unsigned char *message, size_t length)
Definition: WebSocketClient.cc:507
void sendPingCtrlFrame(const unsigned char *message, size_t length)
Definition: WebSocketClient.cc:467