20
20
#include < util/system.h>
21
21
22
22
#include < chrono>
23
+ #include < memory>
23
24
#include < stdexcept>
24
25
#include < string>
25
26
@@ -115,7 +116,8 @@ namespace sam {
115
116
Session::Session (const fs::path& private_key_file,
116
117
const CService& control_host,
117
118
CThreadInterrupt* interrupt)
118
- : m_private_key_file(private_key_file), m_control_host(control_host), m_interrupt(interrupt)
119
+ : m_private_key_file(private_key_file), m_control_host(control_host), m_interrupt(interrupt),
120
+ m_control_sock (std::make_unique<Sock>(INVALID_SOCKET))
119
121
{
120
122
}
121
123
@@ -145,15 +147,15 @@ bool Session::Accept(Connection& conn)
145
147
try {
146
148
while (!*m_interrupt) {
147
149
Sock::Event occurred;
148
- conn.sock . Wait (MAX_WAIT_FOR_IO, Sock::RECV, &occurred);
150
+ conn.sock -> Wait (MAX_WAIT_FOR_IO, Sock::RECV, &occurred);
149
151
150
152
if ((occurred & Sock::RECV) == 0 ) {
151
153
// Timeout, no incoming connections within MAX_WAIT_FOR_IO.
152
154
continue ;
153
155
}
154
156
155
157
const std::string& peer_dest =
156
- conn.sock . RecvUntilTerminator (' \n ' , MAX_WAIT_FOR_IO, *m_interrupt, MAX_MSG_SIZE);
158
+ conn.sock -> RecvUntilTerminator (' \n ' , MAX_WAIT_FOR_IO, *m_interrupt, MAX_MSG_SIZE);
157
159
158
160
conn.peer = CService (DestB64ToAddr (peer_dest), Params ().GetDefaultPort ());
159
161
@@ -171,7 +173,7 @@ bool Session::Connect(const CService& to, Connection& conn, bool& proxy_error)
171
173
proxy_error = true ;
172
174
173
175
std::string session_id;
174
- Sock sock;
176
+ std::unique_ptr< Sock> sock;
175
177
conn.peer = to;
176
178
177
179
try {
@@ -184,12 +186,12 @@ bool Session::Connect(const CService& to, Connection& conn, bool& proxy_error)
184
186
}
185
187
186
188
const Reply& lookup_reply =
187
- SendRequestAndGetReply (sock, strprintf (" NAMING LOOKUP NAME=%s" , to.ToStringIP ()));
189
+ SendRequestAndGetReply (* sock, strprintf (" NAMING LOOKUP NAME=%s" , to.ToStringIP ()));
188
190
189
191
const std::string& dest = lookup_reply.Get (" VALUE" );
190
192
191
193
const Reply& connect_reply = SendRequestAndGetReply (
192
- sock, strprintf (" STREAM CONNECT ID=%s DESTINATION=%s SILENT=false" , session_id, dest),
194
+ * sock, strprintf (" STREAM CONNECT ID=%s DESTINATION=%s SILENT=false" , session_id, dest),
193
195
false );
194
196
195
197
const std::string& result = connect_reply.Get (" RESULT" );
@@ -271,7 +273,7 @@ Session::Reply Session::SendRequestAndGetReply(const Sock& sock,
271
273
return reply;
272
274
}
273
275
274
- Sock Session::Hello () const
276
+ std::unique_ptr< Sock> Session::Hello () const
275
277
{
276
278
auto sock = CreateSock (m_control_host);
277
279
@@ -285,15 +287,15 @@ Sock Session::Hello() const
285
287
286
288
SendRequestAndGetReply (*sock, " HELLO VERSION MIN=3.1 MAX=3.1" );
287
289
288
- return std::move (* sock) ;
290
+ return sock;
289
291
}
290
292
291
293
void Session::CheckControlSock ()
292
294
{
293
295
LOCK (m_mutex);
294
296
295
297
std::string errmsg;
296
- if (!m_control_sock. IsConnected (errmsg)) {
298
+ if (!m_control_sock-> IsConnected (errmsg)) {
297
299
Log (" Control socket error: %s" , errmsg);
298
300
Disconnect ();
299
301
}
@@ -341,26 +343,26 @@ Binary Session::MyDestination() const
341
343
void Session::CreateIfNotCreatedAlready ()
342
344
{
343
345
std::string errmsg;
344
- if (m_control_sock. IsConnected (errmsg)) {
346
+ if (m_control_sock-> IsConnected (errmsg)) {
345
347
return ;
346
348
}
347
349
348
350
Log (" Creating SAM session with %s" , m_control_host.ToString ());
349
351
350
- Sock sock = Hello ();
352
+ auto sock = Hello ();
351
353
352
354
const auto & [read_ok, data] = ReadBinaryFile (m_private_key_file);
353
355
if (read_ok) {
354
356
m_private_key.assign (data.begin (), data.end ());
355
357
} else {
356
- GenerateAndSavePrivateKey (sock);
358
+ GenerateAndSavePrivateKey (* sock);
357
359
}
358
360
359
361
const std::string& session_id = GetRandHash ().GetHex ().substr (0 , 10 ); // full is an overkill, too verbose in the logs
360
362
const std::string& private_key_b64 = SwapBase64 (EncodeBase64 (m_private_key));
361
363
362
- SendRequestAndGetReply (sock, strprintf (" SESSION CREATE STYLE=STREAM ID=%s DESTINATION=%s" ,
363
- session_id, private_key_b64));
364
+ SendRequestAndGetReply (* sock, strprintf (" SESSION CREATE STYLE=STREAM ID=%s DESTINATION=%s" ,
365
+ session_id, private_key_b64));
364
366
365
367
m_my_addr = CService (DestBinToAddr (MyDestination ()), Params ().GetDefaultPort ());
366
368
m_session_id = session_id;
@@ -370,12 +372,12 @@ void Session::CreateIfNotCreatedAlready()
370
372
m_my_addr.ToString ());
371
373
}
372
374
373
- Sock Session::StreamAccept ()
375
+ std::unique_ptr< Sock> Session::StreamAccept ()
374
376
{
375
- Sock sock = Hello ();
377
+ auto sock = Hello ();
376
378
377
379
const Reply& reply = SendRequestAndGetReply (
378
- sock, strprintf (" STREAM ACCEPT ID=%s SILENT=false" , m_session_id), false );
380
+ * sock, strprintf (" STREAM ACCEPT ID=%s SILENT=false" , m_session_id), false );
379
381
380
382
const std::string& result = reply.Get (" RESULT" );
381
383
@@ -393,14 +395,14 @@ Sock Session::StreamAccept()
393
395
394
396
void Session::Disconnect ()
395
397
{
396
- if (m_control_sock. Get () != INVALID_SOCKET) {
398
+ if (m_control_sock-> Get () != INVALID_SOCKET) {
397
399
if (m_session_id.empty ()) {
398
400
Log (" Destroying incomplete session" );
399
401
} else {
400
402
Log (" Destroying session %s" , m_session_id);
401
403
}
402
404
}
403
- m_control_sock. Reset ();
405
+ m_control_sock-> Reset ();
404
406
m_session_id.clear ();
405
407
}
406
408
} // namespace sam
0 commit comments