3
3
4
4
#include " fdb5/LibFdb5.h"
5
5
#include " fdb5/remote/Connection.h"
6
+ #include " fdb5/remote/Messages.h"
7
+ #include < cstdint>
8
+ #include < mutex>
9
+ #include < string_view>
6
10
7
11
namespace fdb5 ::remote {
8
12
9
13
// ----------------------------------------------------------------------------------------------------------------------
10
14
11
- Connection::Connection () : single_(false ) {}
12
- Connection::~Connection () {}
15
+ Connection::Connection () : single_(false ) { }
13
16
14
17
void Connection::teardown () {
15
18
@@ -19,20 +22,20 @@ void Connection::teardown() {
19
22
// all done - disconnecting
20
23
Connection::write (Message::Exit, false , 0 , 0 );
21
24
} catch (...) {
22
- // if connection is already down, no need to escalate
25
+ // if connection is already down, no need to escalate
23
26
}
24
27
}
25
28
try {
26
29
// all done - disconnecting
27
30
Connection::write (Message::Exit, true , 0 , 0 );
28
31
} catch (...) {
29
- // if connection is already down, no need to escalate
32
+ // if connection is already down, no need to escalate
30
33
}
31
34
}
32
35
33
36
// ----------------------------------------------------------------------------------------------------------------------
34
37
35
- void Connection::writeUnsafe (bool control, const void * data, size_t length) {
38
+ void Connection::writeUnsafe (const bool control, const void * const data, const size_t length) const {
36
39
long written = 0 ;
37
40
if (control || single_) {
38
41
written = controlSocket ().write (data, length);
@@ -51,7 +54,7 @@ void Connection::writeUnsafe(bool control, const void* data, size_t length) {
51
54
}
52
55
}
53
56
54
- void Connection::readUnsafe (bool control, void * data, size_t length) {
57
+ void Connection::readUnsafe (bool control, void * data, size_t length) const {
55
58
long read = 0 ;
56
59
if (control || single_) {
57
60
read = controlSocket ().read (data, length);
@@ -70,14 +73,14 @@ void Connection::readUnsafe(bool control, void* data, size_t length) {
70
73
}
71
74
}
72
75
73
- eckit::Buffer Connection::read (bool control, MessageHeader& hdr) {
76
+ eckit::Buffer Connection::read (const bool control, MessageHeader& hdr) const {
74
77
eckit::FixedString<4 > tail;
75
78
76
79
std::lock_guard<std::mutex> lock ((control || single_) ? readControlMutex_ : readDataMutex_);
77
80
readUnsafe (control, &hdr, sizeof (hdr));
78
81
79
- ASSERT (hdr.marker == StartMarker);
80
- ASSERT (hdr.version == CurrentVersion );
82
+ ASSERT (hdr.marker == MessageHeader:: StartMarker);
83
+ ASSERT (hdr.version == MessageHeader::currentVersion );
81
84
ASSERT (single_ || hdr.control () == control);
82
85
83
86
eckit::Buffer payload{hdr.payloadSize };
@@ -86,7 +89,7 @@ eckit::Buffer Connection::read(bool control, MessageHeader& hdr) {
86
89
}
87
90
// Ensure we have consumed exactly the correct amount from the socket.
88
91
readUnsafe (control, &tail, sizeof (tail));
89
- ASSERT (tail == EndMarker);
92
+ ASSERT (tail == MessageHeader:: EndMarker);
90
93
91
94
if (hdr.message == Message::Error) {
92
95
@@ -99,40 +102,43 @@ eckit::Buffer Connection::read(bool control, MessageHeader& hdr) {
99
102
return payload;
100
103
}
101
104
102
- void Connection::write (remote:: Message msg, bool control, uint32_t clientID, uint32_t requestID, const void * data, uint32_t length) {
103
- write (msg, control, clientID, requestID, std::vector<std::pair< const void *, uint32_t >>{{data, length}});
104
- }
105
-
106
- void Connection::write (remote::Message msg, bool control, uint32_t clientID, uint32_t requestID, std::vector<std::pair< const void *, uint32_t >> data) {
105
+ void Connection::write (const Message msg,
106
+ const bool control,
107
+ const uint32_t clientID,
108
+ const uint32_t requestID,
109
+ const PayloadList payloads) const {
107
110
108
111
uint32_t payloadLength = 0 ;
109
- for (auto d: data ) {
110
- ASSERT (d. first );
111
- payloadLength += d. second ;
112
+ for (const auto & payload : payloads ) {
113
+ ASSERT (payload. data );
114
+ payloadLength += payload. length ;
112
115
}
113
116
114
117
MessageHeader message{msg, control, clientID, requestID, payloadLength};
115
118
116
- LOG_DEBUG_LIB (LibFdb5) << " Connection::write [message=" << msg << " ,clientID=" << message.clientID () << " ,control=" << control << " ,requestID=" << requestID << " ,data=" << data.size () << " ,payload=" << payloadLength << " ]" << std::endl;
119
+ LOG_DEBUG_LIB (LibFdb5) << " Connection::write [message=" << msg << " ,clientID=" << message.clientID ()
120
+ << " ,control=" << control << " ,requestID=" << requestID << " ,payloadsSize=" << payloads.size ()
121
+ << " ,payloadLength=" << payloadLength << " ]" << std::endl;
117
122
118
123
std::lock_guard<std::mutex> lock ((control || single_) ? controlMutex_ : dataMutex_);
124
+
119
125
writeUnsafe (control, &message, sizeof (message));
120
- for ( auto d: data) {
121
- writeUnsafe (control, d. first , d. second );
122
- }
123
- writeUnsafe (control, &EndMarker, sizeof (EndMarker) );
126
+
127
+ for ( const auto & payload : payloads) { writeUnsafe (control, payload. data , payload. length ); }
128
+
129
+ writeUnsafe (control, &MessageHeader:: EndMarker, MessageHeader::markerBytes );
124
130
}
125
131
126
- void Connection::error (const std::string& msg, uint32_t clientID, uint32_t requestID) {
132
+ void Connection::error (std::string_view msg, uint32_t clientID, uint32_t requestID) const {
127
133
eckit::Log::error () << " [clientID=" << clientID << " ,requestID=" << requestID << " ] " << msg << std::endl;
128
- write (Message::Error, false , clientID, requestID, std::vector<std::pair< const void *, uint32_t >>{{ msg.c_str (), msg.length ()}} );
134
+ write (Message::Error, false , clientID, requestID, msg.data (), msg.length ());
129
135
}
130
136
131
- eckit::Buffer Connection::readControl (MessageHeader& hdr) {
137
+ eckit::Buffer Connection::readControl (MessageHeader& hdr) const {
132
138
return read (true , hdr);
133
139
}
134
140
135
- eckit::Buffer Connection::readData (MessageHeader& hdr) {
141
+ eckit::Buffer Connection::readData (MessageHeader& hdr) const {
136
142
return read (false , hdr);
137
143
}
138
144
0 commit comments