Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TUDPSocket.cxx
Go to the documentation of this file.
1// @(#)root/net:$Id$
2// Author: Marcelo Sousa 26/10/2011
3
4/*************************************************************************
5 * Copyright (C) 1995-2011, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12//////////////////////////////////////////////////////////////////////////
13// //
14// TUDPSocket //
15// //
16// This class implements UDP client sockets. A socket is an endpoint //
17// for communication between two machines. //
18// The actual work is done via the TSystem class (either TUnixSystem //
19// or TWinNTSystem). //
20// //
21//////////////////////////////////////////////////////////////////////////
22
23#include "Bytes.h"
24#include "Compression.h"
25#include "NetErrors.h"
26#include "TError.h"
27#include "TMessage.h"
28#include "TUDPSocket.h"
29#include "TObjString.h"
30#include "TPluginManager.h"
31#include "TROOT.h"
32#include "TString.h"
33#include "TSystem.h"
34#include "TUrl.h"
35#include "TVirtualAuth.h"
36#include "TStreamerInfo.h"
37#include "TProcessID.h"
38
41
42
43
44////////////////////////////////////////////////////////////////////////////////
45/// Create a socket. Connect to the named service at address addr.
46/// Use tcpwindowsize to specify the size of the receive buffer, it has
47/// to be specified here to make sure the window scale option is set (for
48/// tcpwindowsize > 65KB and for platforms supporting window scaling).
49/// Returns when connection has been accepted by remote side. Use IsValid()
50/// to check the validity of the socket. Every socket is added to the TROOT
51/// sockets list which will make sure that any open sockets are properly
52/// closed on program termination.
53
55 : TNamed(addr.GetHostName(), service), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
56{
59
61 fSecContext = 0;
64 if (fService.Contains("root"))
66 fAddress = addr;
68 fBytesSent = 0;
69 fBytesRecv = 0;
70 fUUIDs = 0;
71 fLastUsageMtx = 0;
73
74 if (fAddress.GetPort() != -1) {
76 -1, "upd");
77
78 if (fSocket != -1) {
80 gROOT->GetListOfSockets()->Add(this);
81 }
82 } else
83 fSocket = -1;
84
85}
86
87
88////////////////////////////////////////////////////////////////////////////////
89/// Create a socket. Connect to the specified port # at address addr.
90/// Use tcpwindowsize to specify the size of the receive buffer, it has
91/// to be specified here to make sure the window scale option is set (for
92/// tcpwindowsize > 65KB and for platforms supporting window scaling).
93/// Returns when connection has been accepted by remote side. Use IsValid()
94/// to check the validity of the socket. Every socket is added to the TROOT
95/// sockets list which will make sure that any open sockets are properly
96/// closed on program termination.
97
99 : TNamed(addr.GetHostName(), ""), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
100{
103
105 fSecContext = 0;
106 fRemoteProtocol= -1;
108 if (fService.Contains("root"))
110 fAddress = addr;
111 fAddress.fPort = port;
113 fBytesSent = 0;
114 fBytesRecv = 0;
115 fUUIDs = 0;
116 fLastUsageMtx = 0;
118
120 -1, "upd");
121 if (fSocket == -1)
122 fAddress.fPort = -1;
123 else {
125 gROOT->GetListOfSockets()->Add(this);
126 }
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Create a socket. Connect to named service on the remote host.
131/// Use tcpwindowsize to specify the size of the receive buffer, it has
132/// to be specified here to make sure the window scale option is set (for
133/// tcpwindowsize > 65KB and for platforms supporting window scaling).
134/// Returns when connection has been accepted by remote side. Use IsValid()
135/// to check the validity of the socket. Every socket is added to the TROOT
136/// sockets list which will make sure that any open sockets are properly
137/// closed on program termination.
138
139TUDPSocket::TUDPSocket(const char *host, const char *service)
140 : TNamed(host, service), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
141{
144
146 fSecContext = 0;
147 fRemoteProtocol= -1;
149 if (fService.Contains("root"))
154 fBytesSent = 0;
155 fBytesRecv = 0;
156 fUUIDs = 0;
157 fLastUsageMtx = 0;
159
160 if (fAddress.GetPort() != -1) {
161 fSocket = gSystem->OpenConnection(host, fAddress.GetPort(), -1, "upd");
162 if (fSocket != -1) {
164 gROOT->GetListOfSockets()->Add(this);
165 }
166 } else
167 fSocket = -1;
168}
169
170////////////////////////////////////////////////////////////////////////////////
171/// Create a socket; see CreateAuthSocket for the form of url.
172/// Connect to the specified port # on the remote host.
173/// If user is specified in url, try authentication as user.
174/// Use tcpwindowsize to specify the size of the receive buffer, it has
175/// to be specified here to make sure the window scale option is set (for
176/// tcpwindowsize > 65KB and for platforms supporting window scaling).
177/// Returns when connection has been accepted by remote side. Use IsValid()
178/// to check the validity of the socket. Every socket is added to the TROOT
179/// sockets list which will make sure that any open sockets are properly
180/// closed on program termination.
181
183 : TNamed(TUrl(url).GetHost(), ""), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
184{
187
188 fUrl = TString(url);
189 TString host(TUrl(fUrl).GetHost());
190
192 fSecContext = 0;
193 fRemoteProtocol= -1;
195 if (fUrl.Contains("root"))
198 fAddress.fPort = port;
201 fBytesSent = 0;
202 fBytesRecv = 0;
203 fUUIDs = 0;
204 fLastUsageMtx = 0;
206
207 fSocket = gSystem->OpenConnection(host, fAddress.GetPort(), -1, "udp");
208 if (fSocket == -1) {
209 fAddress.fPort = -1;
210 } else {
212 gROOT->GetListOfSockets()->Add(this);
213 }
214}
215
216////////////////////////////////////////////////////////////////////////////////
217/// Create a socket in the Unix domain on 'sockpath'.
218/// Returns when connection has been accepted by the server. Use IsValid()
219/// to check the validity of the socket. Every socket is added to the TROOT
220/// sockets list which will make sure that any open sockets are properly
221/// closed on program termination.
222
224 fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
225{
228
229 fUrl = sockpath;
230
231 fService = "unix";
232 fSecContext = 0;
233 fRemoteProtocol= -1;
235 fAddress.fPort = -1;
236 fName.Form("unix:%s", sockpath);
238 fBytesSent = 0;
239 fBytesRecv = 0;
240 fUUIDs = 0;
241 fLastUsageMtx = 0;
243
244 fSocket = gSystem->OpenConnection(sockpath, -1, -1, "udp");
245 if (fSocket > 0) {
247 gROOT->GetListOfSockets()->Add(this);
248 }
249}
250
251////////////////////////////////////////////////////////////////////////////////
252/// Create a socket. The socket will adopt previously opened TCP socket with
253/// descriptor desc.
254
255TUDPSocket::TUDPSocket(Int_t desc) : TNamed("", ""), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
256{
259
260 fSecContext = 0;
261 fRemoteProtocol = 0;
262 fService = (char *)kSOCKD;
264 fBytesSent = 0;
265 fBytesRecv = 0;
266 fUUIDs = 0;
267 fLastUsageMtx = 0;
269
270 if (desc >= 0) {
271 fSocket = desc;
274 gROOT->GetListOfSockets()->Add(this);
275 } else
276 fSocket = -1;
277}
278
279////////////////////////////////////////////////////////////////////////////////
280/// Create a socket. The socket will adopt previously opened Unix socket with
281/// descriptor desc. The sockpath arg is for info purposes only. Use
282/// this method to adopt e.g. a socket created via socketpair().
283
285 fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
286{
289
290 fUrl = sockpath;
291
292 fService = "unix";
293 fSecContext = 0;
294 fRemoteProtocol= -1;
296 fAddress.fPort = -1;
297 fName.Form("unix:%s", sockpath);
299 fBytesSent = 0;
300 fBytesRecv = 0;
301 fUUIDs = 0;
302 fLastUsageMtx = 0;
304
305 if (desc >= 0) {
306 fSocket = desc;
308 gROOT->GetListOfSockets()->Add(this);
309 } else
310 fSocket = -1;
311}
312
313
314////////////////////////////////////////////////////////////////////////////////
315/// TUDPSocket copy ctor.
316
318{
319 fSocket = s.fSocket;
320 fService = s.fService;
321 fAddress = s.fAddress;
329 fUUIDs = 0;
330 fLastUsageMtx = 0;
332
333 if (fSocket != -1) {
335 gROOT->GetListOfSockets()->Add(this);
336 }
337}
338
339////////////////////////////////////////////////////////////////////////////////
340/// Close the socket. If option is "force", calls shutdown(id,2) to
341/// shut down the connection. This will close the connection also
342/// for the parent of this process. Also called via the dtor (without
343/// option "force", call explicitly Close("force") if this is desired).
344
346{
347 Bool_t force = option ? (!strcmp(option, "force") ? kTRUE : kFALSE) : kFALSE;
348
349 if (fSocket != -1) {
352 gROOT->GetListOfSockets()->Remove(this);
353 }
354 fSocket = -1;
355
358}
359
360////////////////////////////////////////////////////////////////////////////////
361/// Return internet address of local host to which the socket is bound.
362/// In case of error TInetAddress::IsValid() returns kFALSE.
363
365{
366 if (IsValid()) {
367 if (fLocalAddress.GetPort() == -1)
369 return fLocalAddress;
370 }
371 return TInetAddress();
372}
373
374////////////////////////////////////////////////////////////////////////////////
375/// Return the local port # to which the socket is bound.
376/// In case of error return -1.
377
379{
380 if (IsValid()) {
381 if (fLocalAddress.GetPort() == -1)
383 return fLocalAddress.GetPort();
384 }
385 return -1;
386}
387
388////////////////////////////////////////////////////////////////////////////////
389/// Waits for this socket to change status. If interest=kRead,
390/// the socket will be watched to see if characters become available for
391/// reading; if interest=kWrite the socket will be watched to
392/// see if a write will not block.
393/// The argument 'timeout' specifies a maximum time to wait in millisec.
394/// Default no timeout.
395/// Returns 1 if a change of status of interest has been detected within
396/// timeout; 0 in case of timeout; < 0 if an error occured.
397
399{
400 Int_t rc = 1;
401
402 // Associate a TFileHandler to this socket
404
405 // Wait for an event now
406 rc = gSystem->Select(&fh, timeout);
407
408 return rc;
409}
410
411////////////////////////////////////////////////////////////////////////////////
412/// Send a single message opcode. Use kind (opcode) to set the
413/// TMessage "what" field. Returns the number of bytes that were sent
414/// (always sizeof(Int_t)) and -1 in case of error. In case the kind has
415/// been or'ed with kMESS_ACK, the call will only return after having
416/// received an acknowledgement, making the sending process synchronous.
417
419{
420 TMessage mess(kind);
421
422 Int_t nsent;
423 if ((nsent = Send(mess)) < 0)
424 return -1;
425
426 return nsent;
427}
428
429////////////////////////////////////////////////////////////////////////////////
430/// Send a status and a single message opcode. Use kind (opcode) to set the
431/// TMessage "what" field. Returns the number of bytes that were sent
432/// (always 2*sizeof(Int_t)) and -1 in case of error. In case the kind has
433/// been or'ed with kMESS_ACK, the call will only return after having
434/// received an acknowledgement, making the sending process synchronous.
435
437{
438 TMessage mess(kind);
439 mess << status;
440
441 Int_t nsent;
442 if ((nsent = Send(mess)) < 0)
443 return -1;
444
445 return nsent;
446}
447
448////////////////////////////////////////////////////////////////////////////////
449/// Send a character string buffer. Use kind to set the TMessage "what" field.
450/// Returns the number of bytes in the string str that were sent and -1 in
451/// case of error. In case the kind has been or'ed with kMESS_ACK, the call
452/// will only return after having received an acknowledgement, making the
453/// sending process synchronous.
454
455Int_t TUDPSocket::Send(const char *str, Int_t kind)
456{
457 TMessage mess(kind);
458 if (str) mess.WriteString(str);
459
460 Int_t nsent;
461 if ((nsent = Send(mess)) < 0)
462 return -1;
463
464 return nsent - sizeof(Int_t); // - TMessage::What()
465}
466
467////////////////////////////////////////////////////////////////////////////////
468/// Send a TMessage object. Returns the number of bytes in the TMessage
469/// that were sent and -1 in case of error. In case the TMessage::What
470/// has been or'ed with kMESS_ACK, the call will only return after having
471/// received an acknowledgement, making the sending process synchronous.
472/// Returns -4 in case of kNoBlock and errno == EWOULDBLOCK.
473/// Returns -5 if pipe broken or reset by peer (EPIPE || ECONNRESET).
474/// support for streaming TStreamerInfo added by Rene Brun May 2008
475/// support for streaming TProcessID added by Rene Brun June 2008
476
478{
480
481 if (fSocket == -1) return -1;
482
483 if (mess.IsReading()) {
484 Error("Send", "cannot send a message used for reading");
485 return -1;
486 }
487
488 // send streamer infos in case schema evolution is enabled in the TMessage
490
491 // send the process id's so TRefs work
493
494 mess.SetLength(); //write length in first word of buffer
495
496 if (GetCompressionLevel() > 0 && mess.GetCompressionLevel() == 0)
498
499 if (mess.GetCompressionLevel() > 0)
500 const_cast<TMessage&>(mess).Compress();
501
502 char *mbuf = mess.Buffer();
503 Int_t mlen = mess.Length();
504 if (mess.CompBuffer()) {
505 mbuf = mess.CompBuffer();
506 mlen = mess.CompLength();
507 }
508
510 Int_t nsent;
511 if ((nsent = gSystem->SendRaw(fSocket, mbuf, mlen, 0)) <= 0) {
512 if (nsent == -5) {
513 // Connection reset by peer or broken
515 Close();
516 }
517 return nsent;
518 }
519
520 fBytesSent += nsent;
522
523 // If acknowledgement is desired, wait for it
524 if (mess.What() & kMESS_ACK) {
527 char buf[2];
528 Int_t n = 0;
529 if ((n = gSystem->RecvRaw(fSocket, buf, sizeof(buf), 0)) < 0) {
530 if (n == -5) {
531 // Connection reset by peer or broken
533 Close();
534 } else
535 n = -1;
536 return n;
537 }
538 if (strncmp(buf, "ok", 2)) {
539 Error("Send", "bad acknowledgement");
540 return -1;
541 }
542 fBytesRecv += 2;
543 fgBytesRecv += 2;
544 }
545
546 Touch(); // update usage timestamp
547
548 return nsent - sizeof(UInt_t); //length - length header
549}
550
551////////////////////////////////////////////////////////////////////////////////
552/// Send an object. Returns the number of bytes sent and -1 in case of error.
553/// In case the "kind" has been or'ed with kMESS_ACK, the call will only
554/// return after having received an acknowledgement, making the sending
555/// synchronous.
556
558{
559 //stream object to message buffer
560 TMessage mess(kind);
561 mess.WriteObject(obj);
562
563 //now sending the object itself
564 Int_t nsent;
565 if ((nsent = Send(mess)) < 0)
566 return -1;
567
568 return nsent;
569}
570
571////////////////////////////////////////////////////////////////////////////////
572/// Send a raw buffer of specified length. Using option kOob one can send
573/// OOB data. Returns the number of bytes sent or -1 in case of error.
574/// Returns -4 in case of kNoBlock and errno == EWOULDBLOCK.
575/// Returns -5 if pipe broken or reset by peer (EPIPE || ECONNRESET).
576
578{
580
581 if (fSocket == -1) return -1;
582
584 Int_t nsent;
585 if ((nsent = gSystem->SendRaw(fSocket, buffer, length, (int) opt)) <= 0) {
586 if (nsent == -5) {
587 // Connection reset or broken: close
589 Close();
590 }
591 return nsent;
592 }
593
594 fBytesSent += nsent;
596
597 Touch(); // update usage timestamp
598
599 return nsent;
600}
601
602////////////////////////////////////////////////////////////////////////////////
603/// Check if TStreamerInfo must be sent. The list of TStreamerInfo of classes
604/// in the object in the message is in the fInfos list of the message.
605/// We send only the TStreamerInfos not yet sent on this socket.
606
608{
609 if (mess.fInfos && mess.fInfos->GetEntries()) {
610 TIter next(mess.fInfos);
612 TList *minilist = 0;
613 while ((info = (TStreamerInfo*)next())) {
614 Int_t uid = info->GetNumber();
615 if (fBitsInfo.TestBitNumber(uid))
616 continue; //TStreamerInfo had already been sent
618 if (!minilist)
619 minilist = new TList();
620 if (gDebug > 0)
621 Info("SendStreamerInfos", "sending TStreamerInfo: %s, version = %d",
622 info->GetName(),info->GetClassVersion());
623 minilist->Add(info);
624 }
625 if (minilist) {
627 messinfo.WriteObject(minilist);
628 delete minilist;
629 if (messinfo.fInfos)
630 messinfo.fInfos->Clear();
631 if (Send(messinfo) < 0)
632 Warning("SendStreamerInfos", "problems sending TStreamerInfo's ...");
633 }
634 }
635}
636
637////////////////////////////////////////////////////////////////////////////////
638/// Check if TProcessIDs must be sent. The list of TProcessIDs
639/// in the object in the message is found by looking in the TMessage bits.
640/// We send only the TProcessIDs not yet send on this socket.
641
643{
644 if (mess.TestBitNumber(0)) {
646 Int_t npids = pids->GetEntries();
647 TProcessID *pid;
648 TList *minilist = 0;
649 for (Int_t ipid = 0; ipid < npids; ipid++) {
650 pid = (TProcessID*)pids->At(ipid);
651 if (!pid || !mess.TestBitNumber(pid->GetUniqueID()+1))
652 continue;
653 //check if a pid with this title has already been sent through the socket
654 //if not add it to the fUUIDs list
655 if (!fUUIDs) {
656 fUUIDs = new TList();
657 } else {
658 if (fUUIDs->FindObject(pid->GetTitle()))
659 continue;
660 }
661 fUUIDs->Add(new TObjString(pid->GetTitle()));
662 if (!minilist)
663 minilist = new TList();
664 if (gDebug > 0)
665 Info("SendProcessIDs", "sending TProcessID: %s", pid->GetTitle());
666 minilist->Add(pid);
667 }
668 if (minilist) {
670 messpid.WriteObject(minilist);
671 delete minilist;
672 if (Send(messpid) < 0)
673 Warning("SendProcessIDs", "problems sending TProcessID's ...");
674 }
675 }
676}
677
678////////////////////////////////////////////////////////////////////////////////
679/// Receive a character string message of maximum max length. The expected
680/// message must be of type kMESS_STRING. Returns length of received string
681/// (can be 0 if otherside of connection is closed) or -1 in case of error
682/// or -4 in case a non-blocking socket would block (i.e. there is nothing
683/// to be read).
684
686{
687 Int_t n, kind;
688
690 if ((n = Recv(str, max, kind)) <= 0) {
691 if (n == -5) {
693 n = -1;
694 }
695 return n;
696 }
697
698 if (kind != kMESS_STRING) {
699 Error("Recv", "got message of wrong kind (expected %d, got %d)",
700 kMESS_STRING, kind);
701 return -1;
702 }
703
704 return n;
705}
706
707////////////////////////////////////////////////////////////////////////////////
708/// Receive a character string message of maximum max length. Returns in
709/// kind the message type. Returns length of received string+4 (can be 0 if
710/// other side of connection is closed) or -1 in case of error or -4 in
711/// case a non-blocking socket would block (i.e. there is nothing to be read).
712
713Int_t TUDPSocket::Recv(char *str, Int_t max, Int_t &kind)
714{
715 Int_t n;
716 TMessage *mess;
717
719 if ((n = Recv(mess)) <= 0) {
720 if (n == -5) {
722 n = -1;
723 }
724 return n;
725 }
726
727 kind = mess->What();
728 if (str) {
729 if (mess->BufferSize() > (Int_t)sizeof(Int_t)) // if mess contains more than kind
730 mess->ReadString(str, max);
731 else
732 str[0] = 0;
733 }
734
735 delete mess;
736
737 return n; // number of bytes read (len of str + sizeof(kind)
738}
739
740////////////////////////////////////////////////////////////////////////////////
741/// Receives a status and a message type. Returns length of received
742/// integers, 2*sizeof(Int_t) (can be 0 if other side of connection
743/// is closed) or -1 in case of error or -4 in case a non-blocking
744/// socket would block (i.e. there is nothing to be read).
745
747{
748 Int_t n;
749 TMessage *mess;
750
752 if ((n = Recv(mess)) <= 0) {
753 if (n == -5) {
755 n = -1;
756 }
757 return n;
758 }
759
760 kind = mess->What();
761 (*mess) >> status;
762
763 delete mess;
764
765 return n; // number of bytes read (2 * sizeof(Int_t)
766}
767
768////////////////////////////////////////////////////////////////////////////////
769/// Receive a TMessage object. The user must delete the TMessage object.
770/// Returns length of message in bytes (can be 0 if other side of connection
771/// is closed) or -1 in case of error or -4 in case a non-blocking socket
772/// would block (i.e. there is nothing to be read) or -5 if pipe broken
773/// or reset by peer (EPIPE || ECONNRESET). In those case mess == 0.
774
776{
778
779 if (fSocket == -1) {
780 mess = 0;
781 return -1;
782 }
783
786 Int_t n;
787 UInt_t len;
788 if ((n = gSystem->RecvRaw(fSocket, &len, sizeof(UInt_t), 0)) <= 0) {
789 if (n == 0 || n == -5) {
790 // Connection closed, reset or broken
792 Close();
793 }
794 mess = 0;
795 return n;
796 }
797 len = net2host(len); //from network to host byte order
798
800 char *buf = new char[len+sizeof(UInt_t)];
801 if ((n = gSystem->RecvRaw(fSocket, buf+sizeof(UInt_t), len, 0)) <= 0) {
802 if (n == 0 || n == -5) {
803 // Connection closed, reset or broken
805 Close();
806 }
807 delete [] buf;
808 mess = 0;
809 return n;
810 }
811
812 fBytesRecv += n + sizeof(UInt_t);
813 fgBytesRecv += n + sizeof(UInt_t);
814
815 mess = new TMessage(buf, len+sizeof(UInt_t));
816
817 // receive any streamer infos
819 goto oncemore;
820
821 // receive any process ids
822 if (RecvProcessIDs(mess))
823 goto oncemore;
824
825 if (mess->What() & kMESS_ACK) {
827 char ok[2] = { 'o', 'k' };
828 Int_t n2 = 0;
829 if ((n2 = gSystem->SendRaw(fSocket, ok, sizeof(ok), 0)) < 0) {
830 if (n2 == -5) {
831 // Connection reset or broken
833 Close();
834 }
835 delete mess;
836 mess = 0;
837 return n2;
838 }
839 mess->SetWhat(mess->What() & ~kMESS_ACK);
840
841 fBytesSent += 2;
842 fgBytesSent += 2;
843 }
844
845 Touch(); // update usage timestamp
846
847 return n;
848}
849
850////////////////////////////////////////////////////////////////////////////////
851/// Receive a raw buffer of specified length bytes. Using option kPeek
852/// one can peek at incoming data. Returns number of received bytes.
853/// Returns -1 in case of error. In case of opt == kOob: -2 means
854/// EWOULDBLOCK and -3 EINVAL. In case of non-blocking mode (kNoBlock)
855/// -4 means EWOULDBLOCK. Returns -5 if pipe broken or reset by
856/// peer (EPIPE || ECONNRESET).
857
859{
861
862 if (fSocket == -1) return -1;
863 if (length == 0) return 0;
864
866 Int_t n;
867 if ((n = gSystem->RecvRaw(fSocket, buffer, length, (int) opt)) <= 0) {
868 if (n == 0 || n == -5) {
869 // Connection closed, reset or broken
871 Close();
872 }
873 return n;
874 }
875
876 fBytesRecv += n;
877 fgBytesRecv += n;
878
879 Touch(); // update usage timestamp
880
881 return n;
882}
883
884////////////////////////////////////////////////////////////////////////////////
885/// Receive a message containing streamer infos. In case the message contains
886/// streamer infos they are imported, the message will be deleted and the
887/// method returns kTRUE.
888
890{
891 if (mess->What() == kMESS_STREAMERINFO) {
892 TList *list = (TList*)mess->ReadObject(TList::Class());
893 TIter next(list);
895 TObjLink *lnk = list->FirstLink();
896 // First call BuildCheck for regular class
897 while (lnk) {
898 info = (TStreamerInfo*)lnk->GetObject();
899 TObject *element = info->GetElements()->UncheckedAt(0);
900 Bool_t isstl = element && strcmp("This",element->GetName())==0;
901 if (!isstl) {
902 info->BuildCheck();
903 if (gDebug > 0)
904 Info("RecvStreamerInfos", "importing TStreamerInfo: %s, version = %d",
905 info->GetName(), info->GetClassVersion());
906 }
907 lnk = lnk->Next();
908 }
909 // Then call BuildCheck for stl class
910 lnk = list->FirstLink();
911 while (lnk) {
912 info = (TStreamerInfo*)lnk->GetObject();
913 TObject *element = info->GetElements()->UncheckedAt(0);
914 Bool_t isstl = element && strcmp("This",element->GetName())==0;
915 if (isstl) {
916 info->BuildCheck();
917 if (gDebug > 0)
918 Info("RecvStreamerInfos", "importing TStreamerInfo: %s, version = %d",
919 info->GetName(), info->GetClassVersion());
920 }
921 lnk = lnk->Next();
922 }
923 delete list;
924 delete mess;
925
926 return kTRUE;
927 }
928 return kFALSE;
929}
930
931////////////////////////////////////////////////////////////////////////////////
932/// Receive a message containing process ids. In case the message contains
933/// process ids they are imported, the message will be deleted and the
934/// method returns kTRUE.
935
937{
938 if (mess->What() == kMESS_PROCESSID) {
939 TList *list = (TList*)mess->ReadObject(TList::Class());
940 TIter next(list);
941 TProcessID *pid;
942 while ((pid = (TProcessID*)next())) {
943 // check that a similar pid is not already registered in fgPIDs
946 TProcessID *p;
947 while ((p = (TProcessID*)nextpid())) {
948 if (!strcmp(p->GetTitle(), pid->GetTitle())) {
949 delete pid;
950 pid = 0;
951 break;
952 }
953 }
954 if (pid) {
955 if (gDebug > 0)
956 Info("RecvProcessIDs", "importing TProcessID: %s", pid->GetTitle());
957 pid->IncrementCount();
958 pidslist->Add(pid);
959 Int_t ind = pidslist->IndexOf(pid);
960 pid->SetUniqueID((UInt_t)ind);
961 }
962 }
963 delete list;
964 delete mess;
965
966 return kTRUE;
967 }
968 return kFALSE;
969}
970
971////////////////////////////////////////////////////////////////////////////////
972/// Set socket options.
973
975{
976 if (fSocket == -1) return -1;
977
978 return gSystem->SetSockOpt(fSocket, opt, val);
979}
980
981////////////////////////////////////////////////////////////////////////////////
982/// Get socket options. Returns -1 in case of error.
983
985{
986 if (fSocket == -1) return -1;
987
988 return gSystem->GetSockOpt(fSocket, opt, &val);
989}
990
991////////////////////////////////////////////////////////////////////////////////
992/// Returns error code. Meaning depends on context where it is called.
993/// If no error condition returns 0 else a value < 0.
994/// For example see TServerSocket ctor.
995
997{
998 if (!IsValid())
999 return fSocket;
1000
1001 return 0;
1002}
1003
1004////////////////////////////////////////////////////////////////////////////////
1005/// See comments for function SetCompressionSettings
1006
1008{
1010 if (fCompress < 0) {
1011 // if the level is not defined yet use 4 as a default (with ZLIB was 1)
1013 } else {
1014 int level = fCompress % 100;
1015 fCompress = 100 * algorithm + level;
1016 }
1017}
1018
1019////////////////////////////////////////////////////////////////////////////////
1020/// See comments for function SetCompressionSettings
1021
1023{
1024 if (level < 0) level = 0;
1025 if (level > 99) level = 99;
1026 if (fCompress < 0) {
1027 // if the algorithm is not defined yet use 0 as a default
1028 fCompress = level;
1029 } else {
1030 int algorithm = fCompress / 100;
1032 fCompress = 100 * algorithm + level;
1033 }
1034}
1035
1036////////////////////////////////////////////////////////////////////////////////
1037/// Used to specify the compression level and algorithm:
1038/// settings = 100 * algorithm + level
1039///
1040/// level = 0, objects written to this file will not be compressed.
1041/// level = 1, minimal compression level but fast.
1042/// ....
1043/// level = 9, maximal compression level but slower and might use more memory.
1044/// (For the currently supported algorithms, the maximum level is 9)
1045/// If compress is negative it indicates the compression level is not set yet.
1046///
1047/// The enumeration ROOT::RCompressionSetting::EAlgorithm associates each
1048/// algorithm with a number. There is a utility function to help
1049/// to set the value of the argument. For example,
1050/// ROOT::CompressionSettings(ROOT::kLZMA, 1)
1051/// will build an integer which will set the compression to use
1052/// the LZMA algorithm and compression level 1. These are defined
1053/// in the header file Compression.h.
1054///
1055/// Note that the compression settings may be changed at any time.
1056/// The new compression settings will only apply to branches created
1057/// or attached after the setting is changed and other objects written
1058/// after the setting is changed.
1059
1064
1065////////////////////////////////////////////////////////////////////////////////
1066/// Print error string depending on error code.
1067
1068void TUDPSocket::NetError(const char *where, Int_t err)
1069{
1070 // Make sure it is in range
1071 err = (err < kErrError) ? ((err > -1) ? err : 0) : kErrError;
1072
1073 if (gDebug > 0)
1074 ::Error(where, "%s", gRootdErrStr[err]);
1075}
1076
1077////////////////////////////////////////////////////////////////////////////////
1078/// Get total number of bytes sent via all sockets.
1079
1084
1085////////////////////////////////////////////////////////////////////////////////
1086/// Get total number of bytes received via all sockets.
1087
UShort_t net2host(UShort_t x)
Definition Bytes.h:575
@ kMESS_STRING
@ kMESS_ACK
@ kMESS_PROCESSID
@ kMESS_STREAMERINFO
R__EXTERN const char * gRootdErrStr[]
Definition NetErrors.h:72
@ kErrError
Definition NetErrors.h:69
#define SafeDelete(p)
Definition RConfig.hxx:533
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
Definition RtypesCore.h:84
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Int_t gDebug
Global variable setting the debug level. Set to 0 to disable, increase it in steps of 1 to increase t...
Definition TROOT.cxx:627
R__EXTERN TVirtualMutex * gROOTMutex
Definition TROOT.h:63
#define gROOT
Definition TROOT.h:411
ESockOptions
Definition TSystem.h:229
ESendRecvOptions
Definition TSystem.h:242
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
#define R__LOCKGUARD(mutex)
Bool_t TestBitNumber(UInt_t bitnumber) const
Definition TBits.h:222
void SetBitNumber(UInt_t bitnumber, Bool_t value=kTRUE)
Definition TBits.h:206
This class represents an Internet Protocol (IP) address.
Int_t GetPort() const
const char * GetHostName() const
A doubly linked list.
Definition TList.h:38
static TClass * Class()
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:575
void Add(TObject *obj) override
Definition TList.h:81
Int_t Compress()
Compress the message.
Definition TMessage.cxx:318
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
TString fName
Definition TNamed.h:32
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
An array of TObjects.
Definition TObjArray.h:31
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:41
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition TObject.cxx:475
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1057
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:864
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition TObject.cxx:875
void ResetBit(UInt_t f)
Definition TObject.h:201
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1045
A TProcessID identifies a ROOT job in a unique way in time and space.
Definition TProcessID.h:74
Int_t IncrementCount()
Increase the reference count to this object.
static TObjArray * GetPIDs()
static: returns array of TProcessIDs
Describes a persistent version of a class.
Basic string class.
Definition TString.h:138
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2362
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
virtual int GetServiceByName(const char *service)
Get port # of internet service.
Definition TSystem.cxx:2329
virtual TInetAddress GetSockName(int sock)
Get Internet Protocol (IP) address of host and port #.
Definition TSystem.cxx:2320
static void ResetErrno()
Static function resetting system error number.
Definition TSystem.cxx:282
virtual char * GetServiceByPort(int port)
Get name of internet service.
Definition TSystem.cxx:2338
virtual int SetSockOpt(int sock, int kind, int val)
Set socket option.
Definition TSystem.cxx:2447
virtual TInetAddress GetPeerName(int sock)
Get Internet Protocol (IP) address of remote host and port #.
Definition TSystem.cxx:2311
virtual int OpenConnection(const char *server, int port, int tcpwindowsize=-1, const char *protocol="tcp")
Open a connection to another host.
Definition TSystem.cxx:2347
virtual int GetSockOpt(int sock, int kind, int *val)
Get socket option.
Definition TSystem.cxx:2456
virtual int RecvRaw(int sock, void *buffer, int length, int flag)
Receive exactly length bytes into buffer.
Definition TSystem.cxx:2410
virtual Int_t Select(TList *active, Long_t timeout)
Select on active file descriptors (called by TMonitor).
Definition TSystem.cxx:443
virtual TInetAddress GetHostByName(const char *server)
Get Internet Protocol (IP) address of host.
Definition TSystem.cxx:2302
virtual int SendRaw(int sock, const void *buffer, int length, int flag)
Send exactly length bytes from buffer.
Definition TSystem.cxx:2420
virtual void CloseConnection(int sock, Bool_t force=kFALSE)
Close socket connection.
Definition TSystem.cxx:2401
void SetCompressionSettings(Int_t settings=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault)
Used to specify the compression level and algorithm: settings = 100 * algorithm + level.
UInt_t fBytesRecv
Definition TUDPSocket.h:50
Int_t GetErrorCode() const
Returns error code.
UInt_t fBytesSent
Definition TUDPSocket.h:51
Int_t fCompress
Definition TUDPSocket.h:52
Bool_t RecvStreamerInfos(TMessage *mess)
Receive a message containing streamer infos.
virtual TInetAddress GetLocalInetAddress()
Return internet address of local host to which the socket is bound.
EServiceType fServType
Definition TUDPSocket.h:58
TInetAddress fLocalAddress
Definition TUDPSocket.h:53
void SetCompressionLevel(Int_t level=ROOT::RCompressionSetting::ELevel::kUseMin)
See comments for function SetCompressionSettings.
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
TVirtualMutex * fLastUsageMtx
Definition TUDPSocket.h:64
virtual Int_t GetLocalPort()
Return the local port # to which the socket is bound.
TInetAddress fAddress
Definition TUDPSocket.h:49
TString fService
Definition TUDPSocket.h:57
static ULong64_t fgBytesRecv
Definition TUDPSocket.h:67
Bool_t RecvProcessIDs(TMessage *mess)
Receive a message containing process ids.
virtual Bool_t IsValid() const
Definition TUDPSocket.h:119
Int_t fRemoteProtocol
Definition TUDPSocket.h:54
static void NetError(const char *where, Int_t error)
Print error string depending on error code.
void SetCompressionAlgorithm(Int_t algorithm=ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
See comments for function SetCompressionSettings.
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
virtual void Close(Option_t *opt="")
Close the socket.
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
static ULong64_t fgBytesSent
Definition TUDPSocket.h:68
Int_t GetCompressionLevel() const
Definition TUDPSocket.h:161
void SendStreamerInfos(const TMessage &mess)
Check if TStreamerInfo must be sent.
virtual Int_t SendObject(const TObject *obj, Int_t kind=kMESS_OBJECT)
Send an object.
TList * fUUIDs
Definition TUDPSocket.h:62
void Touch()
Definition TUDPSocket.h:144
TBits fBitsInfo
Definition TUDPSocket.h:61
static ULong64_t GetSocketBytesRecv()
Get total number of bytes received via all sockets.
TString fUrl
Definition TUDPSocket.h:60
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Option_t * GetOption() const override
Definition TUDPSocket.h:83
static ULong64_t GetSocketBytesSent()
Get total number of bytes sent via all sockets.
Int_t fSocket
Definition TUDPSocket.h:59
virtual Int_t Select(Int_t interest=kRead, Long_t timeout=-1)
Waits for this socket to change status.
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
void SendProcessIDs(const TMessage &mess)
Check if TProcessIDs must be sent.
TSecContext * fSecContext
Definition TUDPSocket.h:55
This class represents a WWW compatible URL.
Definition TUrl.h:33
const Int_t n
Definition legend1.C:16
@ kUndefined
Undefined compression algorithm (must be kept the last of the list in case a new algorithm is added).
@ kUseMin
Compression level reserved when we are not sure what to use (1 is for the fastest compression)
Definition Compression.h:72