ATLAS Offline Software
Public Member Functions | Private Attributes | List of all members
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. More...
 
 ~TSocket ()
 Destructor. More...
 
 TSocket (const TSocket &)=delete
 Do not allow object copying. More...
 
TSocketoperator= (const TSocket &)=delete
 Do not allow assignment. More...
 
StatusCode connect (const TInetAddress &address, int port)
 Function connecting to the specified address. More...
 
StatusCode close ()
 Close the current connection. More...
 
bool isConnected () const
 Check if the socket is connected to some address at the moment. More...
 
StatusCode send (const TString &payload)
 Function sending a message to the connected address. More...
 

Private Attributes

int m_socket
 The underlying socket. More...
 

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 29 of file TSocket.h.

Constructor & Destructor Documentation

◆ TSocket() [1/2]

xAOD::TSocket::TSocket ( )

Constructor with an address and a port.

Definition at line 27 of file TSocket.cxx.

28  : m_socket( -1 ) {
29 
30  }

◆ ~TSocket()

xAOD::TSocket::~TSocket ( )

Destructor.

Definition at line 32 of file TSocket.cxx.

32  {
33 
34  // Close the socket if it's open:
35  if( isConnected() ) {
36  close().ignore();
37  }
38  }

◆ 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 92 of file TSocket.cxx.

92  {
93 
94  // Check if anything needs to be done:
95  if( ! isConnected() ) {
96  return StatusCode::RECOVERABLE;
97  }
98 
99  // Close the socket:
100  if( ::close( m_socket ) != 0 ) {
101  return StatusCode::FAILURE;
102  }
103 
104  // Return gracefully:
105  m_socket = -1;
106  return StatusCode::SUCCESS;
107  }

◆ 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 send the data to
portThe port number to connect to #returns The usual return codes...

Definition at line 49 of file TSocket.cxx.

49  {
50 
51  // If the address is invalid, give up now:
52  if( ! address.IsValid() ) {
53  return StatusCode::FAILURE;
54  }
55 
56  // If not, translate it to a network address:
57  const UInt_t adr = htonl( address.GetAddress() );
58 
59  // "Translate" the port number:
60  short sport = 0;
61  struct servent *sp = 0;
62  if( ( sp = ::getservbyport( htons( port ), "tcp" ) ) ) {
63  sport = sp->s_port;
64  } else {
65  sport = htons( port );
66  }
67 
68  // Create a socket:
69  m_socket = ::socket( AF_INET, SOCK_STREAM, 0 );
70  if( m_socket < 0 ) {
71  return StatusCode::FAILURE;
72  }
73 
74  // Create the address that we want to connect to:
75  struct sockaddr_in server;
76  memset( &server, 0, sizeof( server ) );
77  memcpy( &server.sin_addr, &adr, sizeof( adr ) );
78  server.sin_family = address.GetFamily();
79  server.sin_port = sport;
80 
81  // Connect to the address:
82  if( ::connect( m_socket, ( struct sockaddr* ) &server,
83  sizeof( server ) ) < 0 ) {
84  m_socket = -1;
85  return StatusCode::FAILURE;
86  }
87 
88  // Return gracefully:
89  return StatusCode::SUCCESS;
90  }

◆ isConnected()

bool xAOD::TSocket::isConnected ( ) const

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

Definition at line 109 of file TSocket.cxx.

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

◆ operator=()

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

Do not allow assignment.

◆ 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 120 of file TSocket.cxx.

120  {
121 
122  // Check if we're connected:
123  if( ! isConnected() ) {
124  return StatusCode::FAILURE;
125  }
126 
127  // The buffer we are sending from:
128  const char* buffer = payload.Data();
129  // The total length of the message:
130  const auto length = payload.Length();
131  // Number of bytes already sent:
132  int sent = 0;
133 
134  // Keep sending the message until everything is through:
135  for( int i = 0; i < length; i += sent ) {
136  sent = ::send( m_socket, buffer + i, length - i, 0 );
137  if( sent < 0 ) {
138  return StatusCode::FAILURE;
139  } else if( sent == 0 ) {
140  break;
141  }
142  }
143 
144  // Return gracefully:
145  return StatusCode::SUCCESS;
146  }

Member Data Documentation

◆ m_socket

int xAOD::TSocket::m_socket
private

The underlying socket.

Definition at line 55 of file TSocket.h.


The documentation for this class was generated from the following files:
xAOD::TSocket::close
StatusCode close()
Close the current connection.
Definition: TSocket.cxx:92
beamspotCoolDiff.payload
payload
Definition: beamspotCoolDiff.py:141
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:12
lumiFormat.i
int i
Definition: lumiFormat.py:92
xAOD::TSocket::m_socket
int m_socket
The underlying socket.
Definition: TSocket.h:55
RTTAlgmain.address
address
Definition: RTTAlgmain.py:55
xAOD::TSocket::isConnected
bool isConnected() const
Check if the socket is connected to some address at the moment.
Definition: TSocket.cxx:109
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:120
xAOD::TSocket::connect
StatusCode connect(const TInetAddress &address, int port)
Function connecting to the specified address.
Definition: TSocket.cxx:49