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