ATLAS Offline Software
Loading...
Searching...
No Matches
TSocket.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5
6// System include(s):
7extern "C" {
8# include <sys/types.h>
9# include <sys/socket.h>
10# include <netinet/in.h>
11# include <arpa/inet.h>
12# include <unistd.h>
13# include <netdb.h>
14}
15#include <cstring>
16
17// ROOT include(s):
18#include <TString.h>
19#include <TInetAddress.h>
20
21// Local include(s):
23
24namespace xAOD {
25
27 : m_socket( -1 ) {
28
29 }
30
32
33 // Close the socket if it's open:
34 if( isConnected() ) {
35 close().ignore();
36 }
37 }
38
48 StatusCode TSocket::connect( const TInetAddress& address, int port ) {
49
50 // If the address is invalid, give up now:
51 if( ! address.IsValid() ) {
52 return StatusCode::FAILURE;
53 }
54
55 // If not, translate it to a network address:
56 const UInt_t adr = htonl( address.GetAddress() );
57
58 // "Translate" the port number:
59 short sport = 0;
60 struct servent sp_buf;
61 struct servent *sp;
62 char buf[1024]; // needs to be large enough for all strings in servent
63 getservbyport_r( htons( port ), "tcp", &sp_buf, buf, sizeof(buf), &sp );
64 if( sp ) {
65 sport = sp->s_port;
66 } else {
67 sport = htons( port );
68 }
69
70 // Create a socket:
71 m_socket = ::socket( AF_INET, SOCK_STREAM, 0 );
72 if( m_socket < 0 ) {
73 return StatusCode::FAILURE;
74 }
75
76 // Create the address that we want to connect to:
77 struct sockaddr_in server;
78 memset( &server, 0, sizeof( server ) );
79 memcpy( &server.sin_addr, &adr, sizeof( adr ) );
80 server.sin_family = address.GetFamily();
81 server.sin_port = sport;
82
83 // Connect to the address:
84 if( ::connect( m_socket, reinterpret_cast<const sockaddr*>( &server),
85 sizeof( server ) ) < 0 ) {
86 m_socket = -1;
87 return StatusCode::FAILURE;
88 }
89
90 // Return gracefully:
91 return StatusCode::SUCCESS;
92 }
93
94 StatusCode TSocket::close() {
95
96 // Check if anything needs to be done:
97 if( ! isConnected() ) {
98 return StatusCode::RECOVERABLE;
99 }
100
101 // Close the socket:
102 if( ::close( m_socket ) != 0 ) {
103 return StatusCode::FAILURE;
104 }
105
106 // Return gracefully:
107 m_socket = -1;
108 return StatusCode::SUCCESS;
109 }
110
111 bool TSocket::isConnected() const {
112
113 return ( m_socket != -1 );
114 }
115
122 StatusCode TSocket::send( const TString& payload ) {
123
124 // Check if we're connected:
125 if( ! isConnected() ) {
126 return StatusCode::FAILURE;
127 }
128
129 // The buffer we are sending from:
130 const char* buffer = payload.Data();
131 // The total length of the message:
132 const auto length = payload.Length();
133 // Number of bytes already sent:
134 int sent = 0;
135
136 // Keep sending the message until everything is through:
137 for( int i = 0; i < length; i += sent ) {
138 sent = ::send( m_socket, buffer + i, length - i, 0 );
139 if( sent < 0 ) {
140 return StatusCode::FAILURE;
141 } else if( sent == 0 ) {
142 break;
143 }
144 }
145
146 // Return gracefully:
147 return StatusCode::SUCCESS;
148 }
149
150} // namespace xAOD
double length(const pvec &v)
static Double_t sp
StatusCode send(const TString &payload)
Function sending a message to the connected address.
Definition TSocket.cxx:122
StatusCode connect(const TInetAddress &address, int port)
Function connecting to the specified address.
Definition TSocket.cxx:48
TSocket()
Constructor with an address and a port.
Definition TSocket.cxx:26
bool isConnected() const
Check if the socket is connected to some address at the moment.
Definition TSocket.cxx:111
StatusCode close()
Close the current connection.
Definition TSocket.cxx:94
int m_socket
The underlying socket.
Definition TSocket.h:55
~TSocket()
Destructor.
Definition TSocket.cxx:31
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.