Ocean
Loading...
Searching...
No Matches
MaintenanceTCPConnector.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef FACEBOOK_NETWORK_MAINTENANCE_TCP_CONNECTOR_H
9#define FACEBOOK_NETWORK_MAINTENANCE_TCP_CONNECTOR_H
10
14
16#include "ocean/base/Thread.h"
18
19#include <deque>
20
21namespace Ocean
22{
23
24namespace Network
25{
26
27/**
28 * This class implements a network connector between two maintenance managers using the TCP protocol.
29 * The connector allows to transmit maintenance data from one manager to another manager by application of a network.<br>
30 * This connector can be either a sender or a receiver (not both concurrently) depending on the configuration.<br>
31 * The sender extracts maintenance data from the local maintenance manager and sends this data to the remote connector (configured as receiver).<br>
32 * The receiver receives maintenance data from a remote connector (configured as sender) and places this data into the local maintenance manager.<br>
33 * Beware: The maintenance connector must be released explicitly before the program terminates.<br>
34 * This connector does not use the PackagedSocket framing, it defines its own length prefix on top of a plain TCP stream:
35 * @code
36 * |< 8 byte prefix >|<------------------------- payload -------------------------->|
37 * +-----------------+--------------------------------------------------------------+
38 * | totalSize | payload, as produced by Maintenance::Connector::encodeData() |
39 * | uint64_t | totalSize minus 8 byte |
40 * | host byte order | |
41 * +-----------------+--------------------------------------------------------------+
42 * @endcode
43 * `totalSize` is the length of the entire message including the 8 byte prefix itself, so it is always larger than 8.
44 * Unlike PackagedSocket::PackageHeader there is no tag and no version, so a receiver cannot tell a desynchronized stream from a valid length and has to trust whatever arrives.
45 * The receiver buffers whatever TCP delivers and only hands a message on once that many bytes have accumulated.
46 * This is what allows a message to span several segments.
47 * @see configurateAsSender(), configurateAsReceiver(), release(), MaintenanceUDPConnector.
48 * @ingroup network
49 */
50class OCEAN_NETWORK_EXPORT MaintenanceTCPConnector :
52 protected Thread
53{
54 protected:
55
56 /**
57 * Definition of a vector holding bytes.
58 */
59 using Buffer = std::vector<uint8_t>;
60
61 /**
62 * Definition of a vector storing buffers.
63 */
64 using Buffers = std::vector<Buffer>;
65
66 /**
67 * Definition of a buffer queue.
68 */
69 using BufferQueue = std::deque<Buffer>;
70
71 /**
72 * Definition of a map mapping connection ids to buffer queues.
73 */
74 using BufferQueueMap = std::unordered_map<TCPServer::ConnectionId, BufferQueue>;
75
76 public:
77
78 /**
79 * Creates a new maintenance connector object.
80 */
82
83 /**
84 * Destructs a maintenance connector object.
85 */
87
88 /**
89 * Returns whether this connector is configured as sender.
90 * @return True, if so
91 * @see configurateAsSender(), isReceiver().
92 */
93 inline bool isSender() const;
94
95 /**
96 * Returns whether this connector is configured as receiver.
97 * @return True, if so
98 * @see configurateAsReceiver(), isSender().
99 */
100 inline bool isReceiver() const;
101
102 /**
103 * Configures this connector as sender.
104 * @param targetAddress The address of the remote host to which the maintenance data will be sent
105 * @param targetPort The port of the remote host to which the maintenance data will be sent
106 * @see configurateAsReceiver().
107 */
108 void configurateAsSender(const Address4& targetAddress, const Port& targetPort);
109
110 /**
111 * Configures this connector as receiver.
112 * @param port The local port of this receiver connector
113 */
114 void configurateAsReceiver(const Port& port);
115
116 protected:
117
118 /**
119 * Internal thread run function.
120 */
121 void threadRun() override;
122
123 /**
124 * TCP data receive event function.
125 * @param connectionId The id of the connection from which the data has been received
126 * @param data The data which has been received
127 * @param size The size of the data, in bytes
128 */
129 void onReceiveTCPData(const TCPServer::ConnectionId connectionId, const void* data, const size_t size);
130
131 /**
132 * Connection request event function.
133 * @param address The Address of the remote client requesting the connection
134 * @param port The port of the remote client requesting the connection
135 * @param connectionId The id of the connection
136 * @return True, to accept the connection
137 */
138 bool onConnectionRequest(const Address4& address, const Port& port, TCPServer::ConnectionId connectionId);
139
140 /**
141 * Extracts one related buffer from a queue of messages.
142 * @param bufferQueue The queue of buffers from which the related buffer will be extracted
143 * @param buffer The resulting related buffer, if any
144 * @return True, if one related buffer could be extracted
145 */
146 static bool extractRelatedBuffer(BufferQueue& bufferQueue, Buffer& buffer);
147
148 protected:
149
150 /// The lock of this connector.
151 mutable Lock lock_;
152
153 /// The target address if this connector is a sender.
155
156 /// The target port if this connector is a sender.
158
159 /// The source port if this connector is a receiver.
161
162 /// The TCP client of this connector, used if this connector is a sender.
164
165 /// The TCP server of this connector, used if this connector is a receiver.
167
168 /// The map of received buffers.
170
171 /// The encoded buffer for the sender
173};
174
176{
177 const ScopedLock scopedLock(lock_);
178
180
182}
183
185{
186 const ScopedLock scopedLock(lock_);
187
189
190 return serverSourcePort_.isValid();
191}
192
193}
194
195}
196
197#endif // FACEBOOK_NETWORK_MAINTENANCE_TCP_CONNECTOR_H
This class implements a recursive lock object.
Definition Lock.h:31
This class is the base class for all maintenance connectors.
Definition Maintenance.h:49
This class wraps an address number with 32 bits.
Definition Address4.h:26
bool isValid() const
Returns whether this address hold a valid address.
Definition Address4.h:154
unsigned int ConnectionId
Definition of a connection id.
Definition ConnectionOrientedServer.h:34
This class implements a network connector between two maintenance managers using the TCP protocol.
Definition MaintenanceTCPConnector.h:53
~MaintenanceTCPConnector() override
Destructs a maintenance connector object.
Port serverSourcePort_
The source port if this connector is a receiver.
Definition MaintenanceTCPConnector.h:160
void configurateAsReceiver(const Port &port)
Configures this connector as receiver.
Port clientTargetPort_
The target port if this connector is a sender.
Definition MaintenanceTCPConnector.h:157
Lock lock_
The lock of this connector.
Definition MaintenanceTCPConnector.h:151
bool onConnectionRequest(const Address4 &address, const Port &port, TCPServer::ConnectionId connectionId)
Connection request event function.
static bool extractRelatedBuffer(BufferQueue &bufferQueue, Buffer &buffer)
Extracts one related buffer from a queue of messages.
bool isSender() const
Returns whether this connector is configured as sender.
Definition MaintenanceTCPConnector.h:175
std::deque< Buffer > BufferQueue
Definition of a buffer queue.
Definition MaintenanceTCPConnector.h:69
TCPClient tcpClient_
The TCP client of this connector, used if this connector is a sender.
Definition MaintenanceTCPConnector.h:163
void onReceiveTCPData(const TCPServer::ConnectionId connectionId, const void *data, const size_t size)
TCP data receive event function.
bool isReceiver() const
Returns whether this connector is configured as receiver.
Definition MaintenanceTCPConnector.h:184
MaintenanceTCPConnector()
Creates a new maintenance connector object.
std::vector< Buffer > Buffers
Definition of a vector storing buffers.
Definition MaintenanceTCPConnector.h:64
Buffer encodedSenderBuffer_
The encoded buffer for the sender.
Definition MaintenanceTCPConnector.h:172
std::unordered_map< TCPServer::ConnectionId, BufferQueue > BufferQueueMap
Definition of a map mapping connection ids to buffer queues.
Definition MaintenanceTCPConnector.h:74
TCPServer tcpServer_
The TCP server of this connector, used if this connector is a receiver.
Definition MaintenanceTCPConnector.h:166
void threadRun() override
Internal thread run function.
void configurateAsSender(const Address4 &targetAddress, const Port &targetPort)
Configures this connector as sender.
Address4 clientTargetAddress_
The target address if this connector is a sender.
Definition MaintenanceTCPConnector.h:154
BufferQueueMap bufferQueueMap_
The map of received buffers.
Definition MaintenanceTCPConnector.h:169
std::vector< uint8_t > Buffer
Definition of a vector holding bytes.
Definition MaintenanceTCPConnector.h:59
This class wraps a port number with 16 bits.
Definition Port.h:26
bool isValid() const
Returns whether this port hold a non-zero value.
Definition Port.h:131
This class implements a TCP client.
Definition TCPClient.h:27
This class implements a TCP server.
Definition TCPServer.h:25
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:147
This class implements a thread.
Definition Thread.h:115
The namespace covering the entire Ocean framework.
Definition Accessor.h:15