ATLAS Offline Software
Loading...
Searching...
No Matches
xAOD::TSocket Class Reference

Very simple wrapper around POSIX sockets. More...

#include <TSocket.h>

Collaboration diagram for xAOD::TSocket:

Public Member Functions

 TSocket ()
 Constructor with an address and a port.
 ~TSocket ()
 Destructor.
 TSocket (const TSocket &)=delete
 Do not allow object copying.
TSocketoperator= (const TSocket &)=delete
 Do not allow assignment.
StatusCode connect (const TInetAddress &address, int port)
 Function connecting to the specified address.
StatusCode close ()
 Close the current connection.
bool isConnected () const
 Check if the socket is connected to some address at the moment.
StatusCode send (const TString &payload)
 Function sending a message to the connected address.
StatusCode receive (std::size_t maxSize, TString &response)
 Function receiving a message from the connected address.

Private Attributes

int m_socket
 The underlying socket.

Detailed Description

Very simple wrapper around POSIX sockets.

Since ROOT's TSocket is not fit to be used in TFileAccessTracer's destructor (as some ROOT infrastructure may not be available by the time the destructor is called), we need our own simple implementation instead.

Most of the implementation is simply a copy of the code from TUnixSystem. Slightly simplified in some cases where we don't need the flexibility of the ROOT implementation.

Author
Attila Krasznahorkay Attil.nosp@m.a.Kr.nosp@m.aszna.nosp@m.hork.nosp@m.ay@ce.nosp@m.rn.c.nosp@m.h

Definition at line 32 of file TSocket.h.

Constructor & Destructor Documentation

◆ TSocket() [1/2]

xAOD::TSocket::TSocket ( )

Constructor with an address and a port.

Definition at line 26 of file TSocket.cxx.

26: m_socket(-1) {}
int m_socket
The underlying socket.
Definition TSocket.h:61

◆ ~TSocket()

xAOD::TSocket::~TSocket ( )

Destructor.

Definition at line 28 of file TSocket.cxx.

28 {
29
30 // Close the socket if it's open:
31 if (isConnected()) {
32 close().ignore();
33 }
34}
bool isConnected() const
Check if the socket is connected to some address at the moment.
Definition TSocket.cxx:108
StatusCode close()
Close the current connection.
Definition TSocket.cxx:91

◆ TSocket() [2/2]

xAOD::TSocket::TSocket ( const TSocket & )
delete

Do not allow object copying.

Member Function Documentation

◆ close()

StatusCode xAOD::TSocket::close ( )

Close the current connection.

Definition at line 91 of file TSocket.cxx.

91 {
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}

◆ connect()

StatusCode xAOD::TSocket::connect ( const TInetAddress & address,
int port )

Function connecting to the specified address.

It turns out that translating a string address name into an integer address that the connect(...) function understands, is no easy feat.

So instead we leave that up to ROOT. This translation just needs to be done "early enough" in the job when ROOT can still do it.

Parameters
addressThe address to connect to
portThe port number to connect to
Returns
The usual return codes...

Definition at line 45 of file TSocket.cxx.

45 {
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}
static Double_t sp
StatusCode connect(const TInetAddress &address, int port)
Function connecting to the specified address.
Definition TSocket.cxx:45

◆ isConnected()

bool xAOD::TSocket::isConnected ( ) const

Check if the socket is connected to some address at the moment.

Definition at line 108 of file TSocket.cxx.

108 {
109
110 return (m_socket != -1);
111}

◆ operator=()

TSocket & xAOD::TSocket::operator= ( const TSocket & )
delete

Do not allow assignment.

◆ receive()

StatusCode xAOD::TSocket::receive ( std::size_t maxSize,
TString & response )

Function receiving a message from the connected address.

Definition at line 147 of file TSocket.cxx.

147 {
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}
MDT_Response response

◆ send()

StatusCode xAOD::TSocket::send ( const TString & payload)

Function sending a message to the connected address.

This function is custom made for our use case.

It sends HTML style information to the connected server.

Parameters
payloadThe HTML style payload to send to the server
Returns
The usual return codes...

Definition at line 119 of file TSocket.cxx.

119 {
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}
double length(const pvec &v)
StatusCode send(const TString &payload)
Function sending a message to the connected address.
Definition TSocket.cxx:119

Member Data Documentation

◆ m_socket

int xAOD::TSocket::m_socket
private

The underlying socket.

Definition at line 61 of file TSocket.h.


The documentation for this class was generated from the following files: