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