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

27  : m_socket( -1 ) {
28 
29  }

◆ ~TSocket()

xAOD::TSocket::~TSocket ( )

Destructor.

Definition at line 31 of file TSocket.cxx.

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

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

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

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

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

◆ isConnected()

bool xAOD::TSocket::isConnected ( ) const

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

Definition at line 111 of file TSocket.cxx.

111  {
112 
113  return ( m_socket != -1 );
114  }

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

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

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:94
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:12
lumiFormat.i
int i
Definition: lumiFormat.py:85
python.DecayParser.buf
buf
print ("=> [%s]"cmd)
Definition: DecayParser.py:27
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
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