trikRuntime
trikServer.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 <functional>
18 
19 #include <QtCore/QHash>
20 #include <QtCore/QThread>
21 #include <QtNetwork/QTcpServer>
22 #include <QtCore/QTimer>
23 
24 #include "declSpec.h"
25 
26 namespace trikNetwork {
27 
28 class Connection;
29 
31 class TRIKNETWORK_EXPORT TrikServer : public QTcpServer
32 {
33  Q_OBJECT
34 
35 public:
38  explicit TrikServer(const std::function<Connection *()> &connectionFactory);
39 
40  ~TrikServer() override;
41 
43  int activeConnections() const;
44 
46  Q_INVOKABLE void startServer(quint16 port);
47 
48 public slots:
50  void sendMessage(const QString &message);
51 
52 signals:
54  void connected();
55 
57  void disconnected();
58 
59 protected:
60  void incomingConnection(qintptr socketDescriptor) override;
61 
63  void startConnection(Connection * connectionWorker);
64 
67  Connection *connection(const QHostAddress &ip, int port) const;
68 
72  Connection *connection(const QHostAddress &ip) const;
73 
74 private slots:
76  void onConnectionClosed(trikNetwork::Connection *connection);
77 
78 private:
80  QHash<QThread *, Connection *> mConnections; // Has ownership over threads and connections.
81 
83  std::function<Connection *()> mConnectionFactory;
84 };
85 
86 }
static const int port
Definition: trikCommunicatorTest.cpp:24
Server that can handle multiple clients. Actual work is done in separate threads by Connection object...
Definition: trikServer.h:31
Definition: trikCommunicator.h:31
#define TRIKNETWORK_EXPORT
Definition: declSpec.h:23
Abstract class that serves one client of TrikServer.
Definition: connection.h:50