RDK Documentation (Open Sourced RDK Components)
qt_websocketproxy.h
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 /**
22 * @defgroup trm
23 * @{
24 * @defgroup wsproxy
25 * @{
26 **/
27 
28 
29 #ifndef TRM_QT_WEBSOCKET_PROXY_H_
30 #define TRM_QT_WEBSOCKET_PROXY_H_
31 
32 #include <time.h>
33 #include <sys/time.h>
34 #include <stddef.h>
35 #include <sys/syscall.h>
36 #include <unistd.h>
37 
38 #include <QtCore/QObject>
39 #include <QtCore/QList>
40 #include <QtCore/QMap>
41 #include <QtCore/QTimer>
42 #include <QtCore/QTime>
43 #include <QtCore/QByteArray>
44 #include <QAbstractSocket>
45 #ifdef TRM_USE_SSL
46 #include <QtNetwork/QSslError>
47 #endif
48 
49 #include "tcpOpensslProxyServer.h"
50 
51 class QWebSocketServer;
52 class QWebSocket;
53 class PingPongTask;
54 class QStringList;
55 
56 class WebSocketProxy : public QObject
57 {
58  Q_OBJECT
59 public:
60  explicit WebSocketProxy(const QStringList &boundIPs, quint16 port, QObject *parent = Q_NULLPTR);
61 
62  int onWebsocketHasDataToWriteSSL(int clientId, char* data, int len);
63  void onRemoveAllOpenSslConnections();
64  std::unordered_map <int, PingPongTask*>sslProxyPingPongTasks;
65 
66 Q_SIGNALS:
67 
68 private Q_SLOTS:
69 
70 #ifdef TRM_USE_SSL
71  void onSslErrors(const QList<QSslError> &errors);
72  void onAcceptError(QAbstractSocket::SocketError socketError);
73  void onPeerVerifyError(const QSslError &error);
74 #endif
75  void onNewConnection(void);
76  void onWebsocketConnect(void);
77  void onWebsocketBinaryMessageReceived(QByteArray byteArray);
78  void onWebsocketTextMessageReceived(QString);
79  void onWebsocketBytesWritten(qint64);
80  void onWebsocketDisconnected(void);
81  void onWebsocketStateChanged(QAbstractSocket::SocketState);
82  void onWebSocketAboutClose(void);
83  void onWebsocketError(QAbstractSocket::SocketError);
84  void onWebsocketHasDataToWrite(void *, void*);
85  void onRemoveConnection(void *);
86 
87  void onNewConnectionSSl(int clientId, tcpOpensslProxyServer *ss);
88  void onWebsocketConnectSSL(int clientId, QString msg);
89  void onSslErrorsSSL(QString err);
90  void onAcceptErrorSSL(QString err);
91  void onPeerVerifyErrorSSL(QString err);
92  void onWebsocketDisconnectedSSL(int clientId);
93  void onWebsocketErrorSSL(QString err);
94 
95 private:
96  QMap<QString, QWebSocketServer *>proxyServers;
97  QMap<QString, tcpOpensslProxyServer *>sslProxyServers;
98  std::unordered_map <int, tcpOpensslProxyServer *>sslProxyMap;
99  QList<QWebSocket *> connections;
100  QMap<QWebSocket *, PingPongTask *> pingPongTasks;
101 };
102 
103 class PingPongTask : public QObject {
104  Q_OBJECT
105 public:
106  PingPongTask();
107  PingPongTask(QWebSocket* wssocket);
108  PingPongTask(tcpOpensslProxyServer *sslProxyServer, int clientId);
109  virtual ~PingPongTask(void);
110  void start(void);
111  void stop(void);
112 
113 public Q_SLOTS:
114  void onTimeout(void);
115  void onPong(quint64 elapsedTime, QByteArray);
116 
117 private:
118  QWebSocket* wssocket;
119  QTimer timer;
120  int retry;
121  bool stopped;
122  int m_clientId;
123  tcpOpensslProxyServer *m_sslProxyServer;
124 };
125 
126 #define _TRMPRX_OUT_ stdout
127 #define __TIMESTAMP() do { /*YYMMDD-HH:MM:SS:usec*/ \
128  struct tm __tm; \
129  struct timeval __tv; \
130  gettimeofday(&__tv, NULL); \
131  localtime_r(&__tv.tv_sec, &__tm); \
132  fprintf(_TRMPRX_OUT_, "\r\n[tid=%ld]: %02d%02d%02d-%02d:%02d:%02d:%06d ", \
133  syscall(SYS_gettid), \
134  __tm.tm_year+1900-2000, \
135  __tm.tm_mon+1, \
136  __tm.tm_mday, \
137  __tm.tm_hour, \
138  __tm.tm_min, \
139  __tm.tm_sec, \
140  (int)__tv.tv_usec); \
141 } while(0)
142 
143 #endif //TRM_WEBSOCKET_PROXY_H_
144 
145 
146 /** @} */
147 /** @} */
tcpOpensslProxyServer
Definition: tcpOpensslProxyServer.h:15
PingPongTask
Definition: qt_websocketproxy.h:103
WebSocketProxy
Definition: qt_websocketproxy.h:56