/
XrdClAsyncSocketHandler.cc
716 lines (633 loc) · 24.4 KB
/
XrdClAsyncSocketHandler.cc
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//------------------------------------------------------------------------------
// Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
// Author: Lukasz Janyst <ljanyst@cern.ch>
//------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
#include "XrdCl/XrdClStream.hh"
#include "XrdCl/XrdClConstants.hh"
#include "XrdCl/XrdClLog.hh"
#include "XrdCl/XrdClMessage.hh"
#include "XrdCl/XrdClAsyncSocketHandler.hh"
namespace XrdCl
{
//----------------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------------
AsyncSocketHandler::AsyncSocketHandler( Poller *poller,
TransportHandler *transport,
AnyObject *channelData,
uint16_t subStreamNum ):
pPoller( poller ),
pTransport( transport ),
pChannelData( channelData ),
pSubStreamNum( subStreamNum ),
pStream( 0 ),
pSocket( 0 ),
pIncoming( 0 ),
pOutgoing( 0 ),
pHandShakeData( 0 ),
pHandShakeDone( false ),
pConnectionStarted( 0 ),
pConnectionTimeout( 0 ),
pHeaderDone( false ),
pOutMsgDone( false ),
pOutHandler( 0 ),
pIncMsgSize( 0 ),
pOutMsgSize( 0 ),
pSocketDomain( AF_INET6 )
{
Env *env = DefaultEnv::GetEnv();
int timeoutResolution = DefaultTimeoutResolution;
env->GetInt( "TimeoutResolution", timeoutResolution );
pTimeoutResolution = timeoutResolution;
pSocket = new Socket();
pIncHandler = std::make_pair( (IncomingMsgHandler*)0, false );
pLastActivity = time(0);
}
//----------------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------------
AsyncSocketHandler::~AsyncSocketHandler()
{
Close();
delete pSocket;
}
//----------------------------------------------------------------------------
// Connect to given address
//----------------------------------------------------------------------------
Status AsyncSocketHandler::Connect( time_t timeout )
{
Log *log = DefaultEnv::GetLog();
pLastActivity = pConnectionStarted = ::time(0);
pConnectionTimeout = timeout;
//--------------------------------------------------------------------------
// Initialize the socket
//--------------------------------------------------------------------------
Status st = pSocket->Initialize( pSocketDomain );
if( !st.IsOK() )
{
log->Error( AsyncSockMsg, "[%s] Unable to initialize socket: %s",
pStreamName.c_str(), st.ToString().c_str() );
st.status = stFatal;
return st;
}
pHandShakeDone = false;
//--------------------------------------------------------------------------
// Initiate async connection to the address
//--------------------------------------------------------------------------
char nameBuff[256];
pSockAddr.Format( nameBuff, sizeof(nameBuff), XrdNetAddrInfo::fmtAdv6 );
log->Debug( AsyncSockMsg, "[%s] Attempting connection to %s",
pStreamName.c_str(), nameBuff );
st = pSocket->ConnectToAddress( pSockAddr, 0 );
if( !st.IsOK() )
{
log->Error( AsyncSockMsg, "[%s] Unable to initiate the connection: %s",
pStreamName.c_str(), st.ToString().c_str() );
return st;
}
pSocket->SetStatus( Socket::Connecting );
//--------------------------------------------------------------------------
// We should get the ready to write event once we're really connected
// so we need to listen to it
//--------------------------------------------------------------------------
if( !pPoller->AddSocket( pSocket, this ) )
{
Status st( stFatal, errPollerError );
pSocket->Close();
return st;
}
if( !pPoller->EnableWriteNotification( pSocket, true, pTimeoutResolution ) )
{
Status st( stFatal, errPollerError );
pPoller->RemoveSocket( pSocket );
pSocket->Close();
return st;
}
return Status();
}
//----------------------------------------------------------------------------
// Close the connection
//----------------------------------------------------------------------------
Status AsyncSocketHandler::Close()
{
Log *log = DefaultEnv::GetLog();
log->Debug( AsyncSockMsg, "[%s] Closing the socket", pStreamName.c_str() );
pTransport->Disconnect( *pChannelData, pStream->GetStreamNumber(),
pSubStreamNum );
pPoller->RemoveSocket( pSocket );
pSocket->Close();
return Status();
}
//----------------------------------------------------------------------------
// Set a stream object to be notified about the status of the operations
//----------------------------------------------------------------------------
void AsyncSocketHandler::SetStream( Stream *stream )
{
pStream = stream;
std::ostringstream o;
o << pStream->GetURL()->GetHostId();
o << " #" << pStream->GetStreamNumber();
o << "." << pSubStreamNum;
pStreamName = o.str();
}
//----------------------------------------------------------------------------
// Handler a socket event
//----------------------------------------------------------------------------
void AsyncSocketHandler::Event( uint8_t type, XrdCl::Socket */*socket*/ )
{
pLastActivity = time(0);
//--------------------------------------------------------------------------
// Read event
//--------------------------------------------------------------------------
if( type & ReadyToRead )
{
if( likely( pHandShakeDone ) )
OnRead();
else
OnReadWhileHandshaking();
}
//--------------------------------------------------------------------------
// Read timeout
//--------------------------------------------------------------------------
else if( type & ReadTimeOut )
{
if( likely( pHandShakeDone ) )
OnReadTimeout();
else
OnTimeoutWhileHandshaking();
}
//--------------------------------------------------------------------------
// Write event
//--------------------------------------------------------------------------
if( type & ReadyToWrite )
{
if( unlikely( pSocket->GetStatus() == Socket::Connecting ) )
OnConnectionReturn();
else if( likely( pHandShakeDone ) )
OnWrite();
else
OnWriteWhileHandshaking();
}
//--------------------------------------------------------------------------
// Write timeout
//--------------------------------------------------------------------------
else if( type & WriteTimeOut )
{
if( likely( pHandShakeDone ) )
OnWriteTimeout();
else
OnTimeoutWhileHandshaking();
}
}
//----------------------------------------------------------------------------
// Connect returned
//----------------------------------------------------------------------------
void AsyncSocketHandler::OnConnectionReturn()
{
//--------------------------------------------------------------------------
// Check whether we were able to connect
//--------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
log->Debug( AsyncSockMsg, "[%s] Async connection call returned",
pStreamName.c_str() );
int errorCode = 0;
socklen_t optSize = sizeof( errorCode );
Status st = pSocket->GetSockOpt( SOL_SOCKET, SO_ERROR, &errorCode,
&optSize );
//--------------------------------------------------------------------------
// This is an internal error really (either logic or system fault),
// so we call it a day and don't retry
//--------------------------------------------------------------------------
if( !st.IsOK() )
{
log->Error( AsyncSockMsg, "[%s] Unable to get the status of the "
"connect operation: %s", pStreamName.c_str(),
strerror( errno ) );
pStream->OnConnectError( pSubStreamNum,
Status( stFatal, errSocketOptError, errno ) );
return;
}
//--------------------------------------------------------------------------
// We were unable to connect
//--------------------------------------------------------------------------
if( errorCode )
{
log->Error( AsyncSockMsg, "[%s] Unable to connect: %s",
pStreamName.c_str(), strerror( errorCode ) );
pStream->OnConnectError( pSubStreamNum,
Status( stError, errConnectionError ) );
return;
}
pSocket->SetStatus( Socket::Connected );
//--------------------------------------------------------------------------
// Initialize the handshake
//--------------------------------------------------------------------------
pHandShakeData = new HandShakeData( pStream->GetURL(),
pStream->GetStreamNumber(),
pSubStreamNum );
pHandShakeData->serverAddr = &pSocket->GetServerAddress();
pHandShakeData->clientName = pSocket->GetSockName();
pHandShakeData->streamName = pStreamName;
st = pTransport->HandShake( pHandShakeData, *pChannelData );
++pHandShakeData->step;
if( !st.IsOK() )
{
log->Error( AsyncSockMsg, "[%s] Connection negotiation failed",
pStreamName.c_str() );
pStream->OnConnectError( pSubStreamNum, st );
return;
}
//--------------------------------------------------------------------------
// Transport has given us something to send
//--------------------------------------------------------------------------
if( pHandShakeData->out )
{
pOutgoing = pHandShakeData->out;
pHandShakeData->out = 0;
}
//--------------------------------------------------------------------------
// Listen to what the server has to say
//--------------------------------------------------------------------------
if( !pPoller->EnableReadNotification( pSocket, true, pTimeoutResolution ) )
{
pStream->OnConnectError( pSubStreamNum,
Status( stFatal, errPollerError ) );
return;
}
}
//----------------------------------------------------------------------------
// Got a write readiness event
//----------------------------------------------------------------------------
void AsyncSocketHandler::OnWrite()
{
//--------------------------------------------------------------------------
// Pick up a message if we're not in process of writing something
//--------------------------------------------------------------------------
if( !pOutgoing )
{
pOutMsgDone = false;
std::pair<Message *, OutgoingMsgHandler *> toBeSent;
toBeSent = pStream->OnReadyToWrite( pSubStreamNum );
pOutgoing = toBeSent.first; pOutHandler = toBeSent.second;
if( !pOutgoing )
return;
pOutgoing->SetCursor( 0 );
pOutMsgSize = pOutgoing->GetSize();
}
//--------------------------------------------------------------------------
// Write the message if not already written
//--------------------------------------------------------------------------
Status st;
if( !pOutMsgDone )
{
if( !(st = WriteCurrentMessage()).IsOK() )
{
OnFault( st );
return;
}
if( st.code == suRetry )
return;
Log *log = DefaultEnv::GetLog();
if( pOutHandler && pOutHandler->IsRaw() )
{
log->Dump( AsyncSockMsg, "[%s] Will call raw handler to write payload "
"for message: %s (0x%x).", pStreamName.c_str(),
pOutgoing->GetDescription().c_str(), pOutgoing );
}
pOutMsgDone = true;
}
//--------------------------------------------------------------------------
// Check if the handler needs to be called
//--------------------------------------------------------------------------
if( pOutHandler && pOutHandler->IsRaw() )
{
uint32_t bytesWritten = 0;
st = pOutHandler->WriteMessageBody( pSocket->GetFD(), bytesWritten );
pOutMsgSize += bytesWritten;
if( !st.IsOK() )
{
OnFault( st );
return;
}
if( st.code == suRetry )
return;
}
Log *log = DefaultEnv::GetLog();
log->Dump( AsyncSockMsg, "[%s] Successfully sent message: %s (0x%x).",
pStreamName.c_str(), pOutgoing->GetDescription().c_str(),
pOutgoing );
pStream->OnMessageSent( pSubStreamNum, pOutgoing, pOutMsgSize );
pOutgoing = 0;
}
//----------------------------------------------------------------------------
// Got a write readiness event while handshaking
//----------------------------------------------------------------------------
void AsyncSocketHandler::OnWriteWhileHandshaking()
{
Status st;
if( !pOutgoing )
{
if( !(st = DisableUplink()).IsOK() )
OnFaultWhileHandshaking( st );
return;
}
if( !(st = WriteCurrentMessage()).IsOK() )
{
OnFaultWhileHandshaking( st );
return;
}
if( st.code != suRetry )
{
delete pOutgoing;
pOutgoing = 0;
if( !(st = DisableUplink()).IsOK() )
OnFaultWhileHandshaking( st );
return;
}
}
//----------------------------------------------------------------------------
// Write the current message
//----------------------------------------------------------------------------
Status AsyncSocketHandler::WriteCurrentMessage()
{
Log *log = DefaultEnv::GetLog();
//--------------------------------------------------------------------------
// Try to write down the current message
//--------------------------------------------------------------------------
Message *msg = pOutgoing;
uint32_t leftToBeWritten = msg->GetSize()-msg->GetCursor();
while( leftToBeWritten )
{
int status = pSocket->Send( msg->GetBufferAtCursor(), leftToBeWritten );
if( status <= 0 )
{
//----------------------------------------------------------------------
// Writing operation would block! So we are done for now, but we will
// return
//----------------------------------------------------------------------
if( errno == EAGAIN || errno == EWOULDBLOCK )
return Status( stOK, suRetry );
//----------------------------------------------------------------------
// Actual socket error error!
//----------------------------------------------------------------------
pOutgoing->SetCursor( 0 );
return Status( stError, errSocketError, errno );
}
msg->AdvanceCursor( status );
leftToBeWritten -= status;
}
//--------------------------------------------------------------------------
// We have written the message successfully
//--------------------------------------------------------------------------
log->Dump( AsyncSockMsg, "[%s] Wrote a message: %s (0x%x), %d bytes",
pStreamName.c_str(), pOutgoing->GetDescription().c_str(),
pOutgoing, pOutgoing->GetSize() );
return Status();
}
//----------------------------------------------------------------------------
// Got a read readiness event
//----------------------------------------------------------------------------
void AsyncSocketHandler::OnRead()
{
//--------------------------------------------------------------------------
// There is no incoming message currently being processed so we create
// a new one
//--------------------------------------------------------------------------
if( !pIncoming )
{
pHeaderDone = false;
pIncoming = new Message();
pIncHandler = std::make_pair( (IncomingMsgHandler*)0, false );
pIncMsgSize = 0;
}
Status st;
Log *log = DefaultEnv::GetLog();
//--------------------------------------------------------------------------
// We need to read the header first
//--------------------------------------------------------------------------
if( !pHeaderDone )
{
st = pTransport->GetHeader( pIncoming, pSocket->GetFD() );
if( !st.IsOK() )
{
OnFault( st );
return;
}
if( st.code == suRetry )
return;
log->Dump( AsyncSockMsg, "[%s] Received message header for 0x%x size: %d",
pStreamName.c_str(), pIncoming, pIncoming->GetCursor() );
pIncMsgSize = pIncoming->GetCursor();
pHeaderDone = true;
std::pair<IncomingMsgHandler *, bool> raw;
pIncHandler = pStream->InstallIncHandler( pIncoming, pSubStreamNum );
if( pIncHandler.first )
{
log->Dump( AsyncSockMsg, "[%s] Will use the raw handler to read body "
"of message 0x%x", pStreamName.c_str(), pIncoming );
}
}
//--------------------------------------------------------------------------
// We need to call a raw message handler to get the data from the socket
//--------------------------------------------------------------------------
if( pIncHandler.first )
{
uint32_t bytesRead = 0;
st = pIncHandler.first->ReadMessageBody( pIncoming, pSocket->GetFD(),
bytesRead );
if( !st.IsOK() )
{
OnFault( st );
return;
}
pIncMsgSize += bytesRead;
if( st.code == suRetry )
return;
}
//--------------------------------------------------------------------------
// No raw handler, so we read the message to the buffer
//--------------------------------------------------------------------------
else
{
st = pTransport->GetBody( pIncoming, pSocket->GetFD() );
if( !st.IsOK() )
{
OnFault( st );
return;
}
if( st.code == suRetry )
return;
pIncMsgSize = pIncoming->GetSize();
}
//--------------------------------------------------------------------------
// Report the incoming message
//--------------------------------------------------------------------------
log->Dump( AsyncSockMsg, "[%s] Received message 0x%x of %d bytes",
pStreamName.c_str(), pIncoming, pIncMsgSize );
pStream->OnIncoming( pSubStreamNum, pIncoming, pIncMsgSize );
pIncoming = 0;
}
//----------------------------------------------------------------------------
// Got a read readiness event while handshaking
//----------------------------------------------------------------------------
void AsyncSocketHandler::OnReadWhileHandshaking()
{
//--------------------------------------------------------------------------
// Read the message and let the transport handler look at it when
// reading has finished
//--------------------------------------------------------------------------
Status st = ReadMessage();
if( !st.IsOK() )
{
OnFaultWhileHandshaking( st );
return;
}
if( st.code != suDone )
return;
//--------------------------------------------------------------------------
// OK, we have a new message, let's deal with it;
//--------------------------------------------------------------------------
pHandShakeData->in = pIncoming;
pIncoming = 0;
st = pTransport->HandShake( pHandShakeData, *pChannelData );
++pHandShakeData->step;
delete pHandShakeData->in;
pHandShakeData->in = 0;
if( !st.IsOK() )
{
OnFaultWhileHandshaking( st );
return;
}
//--------------------------------------------------------------------------
// The transport handler gave us something to write
//--------------------------------------------------------------------------
if( pHandShakeData->out )
{
pOutgoing = pHandShakeData->out;
pHandShakeData->out = 0;
Status st;
if( !(st = EnableUplink()).IsOK() )
{
OnFaultWhileHandshaking( st );
return;
}
}
//--------------------------------------------------------------------------
// The hand shake process is done
//--------------------------------------------------------------------------
if( st.IsOK() && st.code == suDone )
{
delete pHandShakeData;
if( !(st = EnableUplink()).IsOK() )
{
OnFaultWhileHandshaking( Status( stFatal, errPollerError ) );
return;
}
pHandShakeDone = true;
pStream->OnConnect( pSubStreamNum );
}
if( !st.IsOK() )
{
OnFaultWhileHandshaking( st );
return;
}
}
//----------------------------------------------------------------------------
// Read a message
//----------------------------------------------------------------------------
Status AsyncSocketHandler::ReadMessage()
{
if( !pIncoming )
{
pHeaderDone = false;
pIncoming = new Message();
}
Status st;
Log *log = DefaultEnv::GetLog();
if( !pHeaderDone )
{
st = pTransport->GetHeader( pIncoming, pSocket->GetFD() );
if( st.IsOK() && st.code == suDone )
{
log->Dump( AsyncSockMsg,
"[%s] Received message header, size: %d",
pStreamName.c_str(), pIncoming->GetCursor() );
pHeaderDone = true;
}
else
return st;
}
st = pTransport->GetBody( pIncoming, pSocket->GetFD() );
if( st.IsOK() && st.code == suDone )
{
log->Dump( AsyncSockMsg, "[%s] Received a message of %d bytes",
pStreamName.c_str(), pIncoming->GetSize() );
}
return st;
}
//----------------------------------------------------------------------------
// Handle fault
//----------------------------------------------------------------------------
void AsyncSocketHandler::OnFault( Status st )
{
Log *log = DefaultEnv::GetLog();
log->Error( AsyncSockMsg, "[%s] Socket error encountered: %s",
pStreamName.c_str(), st.ToString().c_str() );
if( !pIncHandler.second )
delete pIncoming;
pIncoming = 0;
pOutgoing = 0;
pOutHandler = 0;
pStream->OnError( pSubStreamNum, st );
}
//----------------------------------------------------------------------------
// Handle fault while handshaking
//----------------------------------------------------------------------------
void AsyncSocketHandler::OnFaultWhileHandshaking( Status st )
{
Log *log = DefaultEnv::GetLog();
log->Error( AsyncSockMsg, "[%s] Socket error while handshaking: %s",
pStreamName.c_str(), st.ToString().c_str() );
delete pIncoming;
delete pOutgoing;
pIncoming = 0;
pOutgoing = 0;
pStream->OnConnectError( pSubStreamNum, st );
}
//----------------------------------------------------------------------------
// Handle write timeout
//----------------------------------------------------------------------------
void AsyncSocketHandler::OnWriteTimeout()
{
pStream->OnWriteTimeout( pSubStreamNum );
}
//----------------------------------------------------------------------------
// Handler read timeout
//----------------------------------------------------------------------------
void AsyncSocketHandler::OnReadTimeout()
{
pStream->OnReadTimeout( pSubStreamNum );
}
//----------------------------------------------------------------------------
// Handle timeout while handshaking
//----------------------------------------------------------------------------
void AsyncSocketHandler::OnTimeoutWhileHandshaking()
{
time_t now = time(0);
if( now > pConnectionStarted+pConnectionTimeout )
OnFaultWhileHandshaking( Status( stError, errSocketTimeout ) );
}
}