cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
Socket.h
1//
2// Copyright 2014 - 2024 (C). Alex Robenko. All rights reserved.
3//
4
5// This file is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
19#pragma once
20
21#include <cstdint>
22#include <cstddef>
23#include <vector>
24#include <functional>
25
26#include <QtCore/QString>
27
28#include "Api.h"
29#include "DataInfo.h"
30
31namespace cc_tools_qt
32{
33
38class CC_API Socket
39{
40public:
43 {
44 ConnectionProperty_Autoconnect = 0x1,
45 ConnectionProperty_NonDisconnectable = 0x2
46 };
47
50
52 virtual ~Socket() noexcept;
53
60 bool start();
61
66 void stop();
67
69 bool isRunning() const;
70
78 bool socketConnect();
79
85 void socketDisconnect();
86
88 bool isSocketConnected() const;
89
94 void sendData(DataInfoPtr dataPtr);
95
103 unsigned connectionProperties() const;
104
110 void applyInterPluginConfig(const QVariantMap& props);
111
113 using DataReceivedCallback = std::function<void (DataInfoPtr)>;
114
117 template <typename TFunc>
118 void setDataReceivedCallback(TFunc&& func)
119 {
120 m_dataReceivedCallback = std::forward<TFunc>(func);
121 }
122
124 using ErrorReportCallback = std::function<void (const QString& msg)>;
125
128 template <typename TFunc>
129 void setErrorReportCallback(TFunc&& func)
130 {
131 m_errorReportCallback = std::forward<TFunc>(func);
132 }
133
135 using ConnectionStatusReportCallback = std::function <void (bool)>;
136
139 template <typename TFunc>
141 {
142 m_connectionStatusReportCallback = std::forward<TFunc>(func);
143 }
144
146 using InterPluginConfigReportCallback = std::function <void (const QVariantMap&)>;
147
150 template <typename TFunc>
152 {
153 m_interPluginConfigReportCallback = std::forward<TFunc>(func);
154 }
155
158 void setDebugOutputLevel(unsigned level = 0U);
159
160protected:
165 virtual bool startImpl();
166
170 virtual void stopImpl();
171
176 virtual bool socketConnectImpl();
177
181 virtual void socketDisconnectImpl();
182
186 virtual void sendDataImpl(DataInfoPtr dataPtr) = 0;
187
192 virtual unsigned connectionPropertiesImpl() const;
193
197 virtual void applyInterPluginConfigImpl(const QVariantMap& props);
198
205
211 void reportError(const QString& msg);
212
219
226 void reportInterPluginConfig(const QVariantMap& props);
227
229 static unsigned long long currTimestamp();
230
232 unsigned getDebugOutputLevel() const;
233
234private:
235 DataReceivedCallback m_dataReceivedCallback;
236 ErrorReportCallback m_errorReportCallback;
237 ConnectionStatusReportCallback m_connectionStatusReportCallback;
238 InterPluginConfigReportCallback m_interPluginConfigReportCallback;
239
240 unsigned m_debugLevel = 0U;
241 bool m_running = false;
242 bool m_connected = false;
243};
244
246using SocketPtr = std::shared_ptr<Socket>;
247
248} // namespace cc_tools_qt
249
Main polymorphic interface class for sockets.
Definition Socket.h:39
virtual bool socketConnectImpl()
Polymorphic socket connection functionality implementation.
virtual ~Socket() noexcept
Destructor.
void reportInterPluginConfig(const QVariantMap &props)
Report inter-plugin configuration.
void setDebugOutputLevel(unsigned level=0U)
Set debug output level.
void setErrorReportCallback(TFunc &&func)
Set callback to report errors.
Definition Socket.h:129
std::function< void(const QVariantMap &)> InterPluginConfigReportCallback
Callback to report inter-plugin configuration updates.
Definition Socket.h:146
virtual void stopImpl()
Polymorphic stop functionality implementation.
std::function< void(const QString &msg)> ErrorReportCallback
Callback to report errors.
Definition Socket.h:124
Socket()
Constructor.
void setConnectionStatusReportCallback(TFunc &&func)
Set callback to report socket connection.
Definition Socket.h:140
virtual void applyInterPluginConfigImpl(const QVariantMap &props)
Polymorphic inter-plugin configuration application.
void reportError(const QString &msg)
Report I/O operation error.
virtual void sendDataImpl(DataInfoPtr dataPtr)=0
Polymorphic data send functionality implementation.
void setInterPluginConfigReportCallback(TFunc &&func)
Set callback to report inter-plugin configuration.
Definition Socket.h:151
virtual unsigned connectionPropertiesImpl() const
Polymorphic connection properties functionality implementation.
static unsigned long long currTimestamp()
Get current timestamp.
void reportDisconnected()
Report socket disconnection.
virtual bool startImpl()
Polymorphic start functionality implementation.
virtual void socketDisconnectImpl()
Polymorphic socket disconnection functionality implementation.
void reportDataReceived(DataInfoPtr dataPtr)
Report new data has been received.
std::function< void(DataInfoPtr)> DataReceivedCallback
Callback to report incoming data.
Definition Socket.h:113
std::function< void(bool)> ConnectionStatusReportCallback
Callback to report socket connection status.
Definition Socket.h:135
ConnectionProperty
Available socket connection properties values.
Definition Socket.h:43
unsigned getDebugOutputLevel() const
Get current debug output level.
Main namespace for all classes / functions of the shared library.
std::shared_ptr< Socket > SocketPtr
Pointer to Socket object.
Definition Socket.h:246
std::shared_ptr< DataInfo > DataInfoPtr
Pointer to DataInfo.
Definition DataInfo.h:57