TreeFrogFramework  2.8
twebsocketendpoint.h
Go to the documentation of this file.
1 #pragma once
2 #include <QHostAddress>
3 #include <QObject>
4 #include <QPair>
5 #include <QStringList>
6 #include <QVariant>
7 #include <TGlobal>
8 #include <TSession>
9 #include <TWebSocketSession>
10 
11 
12 class T_CORE_EXPORT TWebSocketEndpoint : public QObject {
13 public:
15  virtual ~TWebSocketEndpoint() { }
16 
17  QString className() const;
18  QString name() const;
19  void sendText(const QString &text);
20  void sendBinary(const QByteArray &binary);
21  void ping(const QByteArray &payload = QByteArray());
22  void sendPing(const QByteArray &payload = QByteArray());
23  void close(int closeCode = Tf::NormalClosure);
24  void sendText(int sid, const QString &text);
25  void sendBinary(int sid, const QByteArray &binary);
26  void closeSocket(int sid, int closeCode = Tf::NormalClosure);
27  void rollbackTransaction();
28  void subscribe(const QString &topic, bool local = true);
29  void unsubscribe(const QString &topic);
30  void unsubscribeFromAll();
31  void publish(const QString &topic, const QString &text);
32  void publish(const QString &topic, const QByteArray &binary);
33  void startKeepAlive(int interval);
34  //void sendHttp(int sid, const QByteArray &data);
35  const TWebSocketSession &session() const { return sessionStore; }
36  TWebSocketSession &session() { return sessionStore; }
37  //int socketId() const { return sid; }
38  QHostAddress peerAddress() const { return peerAddr; }
39  uint16_t peerPort() const { return peerPortNumber; }
40  void reset();
41 
42  static bool isUserLoggedIn(const TSession &session);
43  static QString identityKeyOfLoginUser(const TSession &session);
44  static const QStringList &disabledEndpoints();
45 
46 protected:
47  virtual bool onOpen(const TSession &session);
48  virtual void onClose(int closeCode);
49  virtual void onTextReceived(const QString &text);
50  virtual void onBinaryReceived(const QByteArray &binary);
51  virtual void onPing(const QByteArray &payload);
52  virtual void onPong(const QByteArray &payload);
53  virtual int keepAliveInterval() const { return 0; }
54  virtual bool transactionEnabled() const;
55  void sendPong(const QByteArray &payload = QByteArray());
56 
57 private:
58  enum TaskType {
59  OpenSuccess = 0,
60  OpenError,
61  SendText,
62  SendBinary,
63  SendClose,
64  SendPing,
65  SendPong,
66  SendTextTo,
67  SendBinaryTo,
68  SendCloseTo,
69  Subscribe,
70  Unsubscribe,
71  UnsubscribeFromAll,
72  PublishText,
73  PublishBinary,
74  StartKeepAlive,
75  StopKeepAlive,
76  };
77 
78  bool rollbackRequested() const;
79 
80  TWebSocketSession sessionStore;
81  QList<QPair<int, QVariant>> taskList;
82  bool rollback {false};
83  QHostAddress peerAddr;
84  uint16_t peerPortNumber {0};
85 
86  friend class TWebSocketWorker;
89 };
90 
91 
92 inline QString TWebSocketEndpoint::className() const
93 {
94  return QString(metaObject()->className());
95 }
96 
98 {
99  rollback = true;
100 }
101 
102 inline bool TWebSocketEndpoint::rollbackRequested() const
103 {
104  return rollback;
105 }
106 
108 {
109  return false;
110 }
111 
The TSession class holds information associated with individual visitors.
Definition: tsession.h:7
The TWebSocketEndpoint is the base class of endpoints for WebSocket communication.
Definition: twebsocketendpoint.h:12
uint16_t peerPort() const
Definition: twebsocketendpoint.h:39
virtual ~TWebSocketEndpoint()
Definition: twebsocketendpoint.h:15
void rollbackTransaction()
This function is called to rollback a transaction on the database.
Definition: twebsocketendpoint.h:97
QHostAddress peerAddress() const
Definition: twebsocketendpoint.h:38
const TWebSocketSession & session() const
Returns the current WebSocket session, allows associating information with individual visitors.
Definition: twebsocketendpoint.h:35
TWebSocketSession & session()
Returns the current WebSocket session, allows associating information with individual visitors.
Definition: twebsocketendpoint.h:36
virtual bool transactionEnabled() const
Must be overridden by subclasses to disable transaction mechanism.
Definition: twebsocketendpoint.h:107
QString className() const
Returns the class name.
Definition: twebsocketendpoint.h:92
virtual int keepAliveInterval() const
Definition: twebsocketendpoint.h:53
The TWebSocketSession class holds information associated with individual visitors for WebSocket conne...
Definition: twebsocketsession.h:8
@ NormalClosure
Definition: tfnamespace.h:213
#define T_CORE_EXPORT
Definition: tdeclexport.h:28
#define T_DISABLE_COPY(Class)
Definition: tdeclexport.h:37
#define T_DISABLE_MOVE(Class)
Definition: tdeclexport.h:41