trikRuntime
connection.h
Go to the documentation of this file.
1 /* Copyright 2014 - 2015 CyberTech Labs Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14 
15 #pragma once
16 
17 #include <QtCore/QObject>
18 #include <QtCore/QScopedPointer>
19 #include <QtCore/QTimer>
20 #include <QtNetwork/QTcpSocket>
21 #include <QtNetwork/QHostAddress>
22 
23 #include "declSpec.h"
24 
25 namespace trikNetwork {
26 
28 enum class Protocol
29 {
32 
35 };
36 
38 enum class Heartbeat
39 {
41  use
42 
45  , dontUse
46 };
47 
50 class TRIKNETWORK_EXPORT Connection : public QObject
51 {
52  Q_OBJECT
53 
54 public:
58  explicit Connection(Protocol connectionProtocol, Heartbeat useHeartbeat);
59 
61  bool isConnected() const;
62 
67  bool isValid() const;
68 
70  QHostAddress peerAddress() const;
71 
73  int peerPort() const;
74 
78  Q_INVOKABLE void init(qintptr socketDescriptor);
79 
81  Q_INVOKABLE void send(const QByteArray &data);
82 signals:
84  void disconnected(trikNetwork::Connection *self);
85 
87  void connected(trikNetwork::Connection *self);
88 
89 protected:
94  void init(const QHostAddress &ip, int port);
95 
96 private slots:
98  void onReadyRead();
99 
101  void onConnect();
102 
104  void onDisconnect();
105 
107  void onError(QAbstractSocket::SocketError error);
108 
110  void keepAlive();
111 
113  void onHeartbeatTimeout();
114 
115 private:
117  virtual void processData(const QByteArray &data) = 0;
118 
120  void handleIncomingData(const QByteArray &data);
121 
123  void connectSlots();
124 
126  void processBuffer();
127 
129  void doDisconnect();
130 
132  void initKeepalive();
133 
135  QByteArray mBuffer;
136 
138  int mExpectedBytes = 0;
139 
141  Protocol mProtocol;
142 
144  QScopedPointer<QTimer> mKeepAliveTimer;
145 
147  QScopedPointer<QTimer> mHeartbeatTimer;
148 
150  bool mDisconnectReported = false;
151 
153  bool mUseHeartbeat = false;
154 
156  QScopedPointer<QTcpSocket> mSocket;
157 };
158 
159 }
static const int port
Definition: trikCommunicatorTest.cpp:24
Wait for a packet every N milliseconds, if none is received, assume connection lost and close socket...
Heartbeat
Heartbeat protocol option.
Definition: connection.h:38
Protocol
Connection protocol variants.
Definition: connection.h:28
Definition: trikCommunicator.h:31
#define TRIKNETWORK_EXPORT
Definition: declSpec.h:23
Message is in form "<data length in bytes>:<data>".
Abstract class that serves one client of TrikServer.
Definition: connection.h:50