-
Notifications
You must be signed in to change notification settings - Fork 36
/
connection.hpp
77 lines (64 loc) · 2.18 KB
/
connection.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <functional>
#include <memory>
#include <string>
#include "telemetry/endpoint.hpp"
namespace kagome::telemetry {
/**
* Represents a connection to the single telemetry server.
*
* The target URI and OnConnectedCallback are to be passed to the
* implementation class' constructor.
*/
class TelemetryConnection {
public:
/**
* The callback to be called each time the connection (re-)establishes.
*
* Can be called multiple times even after a single call of connect()
* due to losing connection and reconnecting to the backend server.
*
* The callback is used to let the TelemetryService send the greeting
* message.
*/
using OnConnectedCallback =
std::function<void(std::shared_ptr<TelemetryConnection>)>;
virtual ~TelemetryConnection() = default;
/**
* Initiates attempts to connect.
*
* Designed to be called only once by the TelemetryService.
*/
virtual void connect() = 0;
/// Returns the associated telemetry endpoint
virtual const TelemetryEndpoint &endpoint() const = 0;
/**
* Write the data to the websocket if connected
* @param data - the data line to send
*
* The data might be disposed in an outer scope
* as soon as the method returns.
*/
virtual void send(const std::string &data) = 0;
/**
* Write the message pointed by a message handle.
* @param message_handle - message to serve
*
* TelemetryConnection and TelemetryService are tightly related and shares a
* common message pool to avoid redundant memory consumption. That is why
* TelemetryService schedules messages to the pool and passes only handles
* to connections. It is connection's duty to release a message from the
* pool when send/write operation completes.
*/
virtual void send(std::size_t message_handle) = 0;
/// Get the current status of the connection
virtual bool isConnected() const = 0;
/// Request the connection to shutdown
virtual void shutdown() = 0;
};
} // namespace kagome::telemetry