ATLAS Offline Software
TSocket.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 // System include(s):
7 extern "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 
24 namespace 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, ( struct 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 
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
xAOD::TSocket::TSocket
TSocket()
Constructor with an address and a port.
Definition: TSocket.cxx:26
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
xAOD::TSocket::close
StatusCode close()
Close the current connection.
Definition: TSocket.cxx:94
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:12
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
xAOD::TSocket::m_socket
int m_socket
The underlying socket.
Definition: TSocket.h:55
RTTAlgmain.address
address
Definition: RTTAlgmain.py:55
PixelModuleFeMask_create_db.payload
string payload
Definition: PixelModuleFeMask_create_db.py:69
xAOD::TSocket::isConnected
bool isConnected() const
Check if the socket is connected to some address at the moment.
Definition: TSocket.cxx:111
TSocket.h
xAOD::TSocket::~TSocket
~TSocket()
Destructor.
Definition: TSocket.cxx:31
python.html.AtlRunQueryDQSummary.server
server
Definition: AtlRunQueryDQSummary.py:22
length
double length(const pvec &v)
Definition: FPGATrackSimLLPDoubletHoughTransformTool.cxx:26
xAOD::TSocket::send
StatusCode send(const TString &payload)
Function sending a message to the connected address.
Definition: TSocket.cxx:122
xAOD::TSocket::connect
StatusCode connect(const TInetAddress &address, int port)
Function connecting to the specified address.
Definition: TSocket.cxx:48