Loading...
Searching...
No Matches
XrdCl::Socket Class Reference

A network socket. More...

`#include <XrdClSocket.hh>`

Collaboration diagram for XrdCl::Socket:

## Public Types

enum | SocketStatus { Disconnected = 1 , Connected = 2 , Connecting = 3 }
| Status of the socket. More...

## Public Member Functions

| Socket (int socket=-1, SocketStatus status=Disconnected)
virtual | ~Socket ()
| Desctuctor.
void | Close ()
| Disconnect.
XRootDStatus | Connect (const std::string &host, uint16_t port, uint16_t timout=10)
XRootDStatus | ConnectToAddress (const XrdNetAddr &addr, uint16_t timout=10)
XRootDStatus | Cork ()
XRootDStatus | Flash ()
const AnyObject * | GetChannelID () const
int | GetFD () const
| Get the file descriptor.
XRootDStatus | GetFlags (int &flags)
| Get the socket flags (man fcntl).
std::string | GetName () const
| Get the string representation of the socket.
std::string | GetPeerName () const
| Get the name of the remote peer.
const XrdNetAddr * | GetServerAddress () const
| Get the server address.
std::string | GetSockName () const
| Get the name of the socket.
XRootDStatus | GetSockOpt (int level, int optname, void *optval, socklen_t *optlen)
| Get socket options.
SocketStatus | GetStatus () const
| Get the socket status.
XRootDStatus | Initialize (int family=AF_INET)
| Initialize the socket.
bool | IsCorked () const
bool | IsEncrypted ()
uint8_t | MapEvent (uint8_t event)
virtual XRootDStatus | Read (char *buffer, size_t size, int &bytesRead)
XRootDStatus | ReadRaw (void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesRead)
| Read raw bytes from the socket.
XRootDStatus | ReadV (iovec *iov, int iocnt, int &bytesRead)
virtual XRootDStatus | Send (const char *buffer, size_t size, int &bytesWritten)
XRootDStatus | Send (Message &msg, const std::string &strmname)
XRootDStatus | Send (XrdSys::KernelBuffer &kbuff, int &bytesWritten)
void | SetChannelID (AnyObject *channelID)
XRootDStatus | SetFlags (int flags)
| Set the socket flags (man fcntl).
XRootDStatus | SetSockOpt (int level, int optname, const void *optval, socklen_t optlen)
| Set socket options.
void | SetStatus (SocketStatus status)
| Set socket status - do not use unless you know what you're doing.
XRootDStatus | TlsHandShake (AsyncSocketHandler *socketHandler, const std::string &thehost=std::string())
XRootDStatus | Uncork ()
XRootDStatus | WriteRaw (void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesWritten)

## Static Public Member Functions

static XRootDStatus | ClassifyErrno (int error)

## Protected Member Functions

XRootDStatus | Poll (bool readyForReading, bool readyForWriting, int32_t timeout)

## Protected Attributes

AnyObject * | pChannelID
bool | pCorked
std::string | pName
std::string | pPeerName
int | pProtocolFamily
std::unique_ptr< XrdNetAddr > | pServerAddr
int | pSocket
std::string | pSockName
SocketStatus | pStatus
std::unique_ptr< Tls > | pTls

## Detailed Description

A network socket.

Definition at line 42 of file XrdClSocket.hh.

## Member Enumeration Documentation

## SocketStatus

enum XrdCl::Socket::SocketStatus

Status of the socket.

Enumerator
Disconnected | The socket is disconnected.
Connected | The socket is connected.
Connecting | The connection process is in progress.

Definition at line 48 of file XrdClSocket.hh.

{
Disconnected = 1,
Connected = 2,
Connecting = 3
};
XrdCl::Socket::Disconnected
@ Disconnected
The socket is disconnected.
Definition XrdClSocket.hh:50
XrdCl::Socket::Connected
@ Connected
The socket is connected.
Definition XrdClSocket.hh:51
XrdCl::Socket::Connecting
@ Connecting
The connection process is in progress.
Definition XrdClSocket.hh:52

## Constructor & Destructor Documentation

## Socket()

XrdCl::Socket::Socket | ( | int | socket = -1,
|  | SocketStatus | status = Disconnected )

Constructor

Parameters

  socket | already connected socket if available, -1 otherwise
  status | status of a socket if available

Definition at line 44 of file XrdClSocket.cc.

:
pSocket(socket), pStatus( status ),
pProtocolFamily( AF_INET ),
pChannelID( 0 ),
pCorked( false )
{
};
XrdCl::Socket::pStatus
SocketStatus pStatus
Definition XrdClSocket.hh:328
XrdCl::Socket::pChannelID
AnyObject * pChannelID
Definition XrdClSocket.hh:334
XrdCl::Socket::pSocket
int pSocket
Definition XrdClSocket.hh:327
XrdCl::Socket::pProtocolFamily
int pProtocolFamily
Definition XrdClSocket.hh:333
XrdCl::Socket::pCorked
bool pCorked
Definition XrdClSocket.hh:335

References pChannelID, pCorked, pProtocolFamily, pSocket, and pStatus.

## ~Socket()

XrdCl::Socket::~Socket | ( |  | ) |

virtual

Desctuctor.

Definition at line 55 of file XrdClSocket.cc.

{
Close();
};
XrdCl::Socket::Close
void Close()
Disconnect.
Definition XrdClSocket.cc:262

References Close().

Here is the call graph for this function:

## Member Function Documentation

## ClassifyErrno()

XRootDStatus XrdCl::Socket::ClassifyErrno | ( | int | error | ) |

static

Definition at line 692 of file XrdClSocket.cc.

{
switch( errno )
{
case EAGAIN:
#if EAGAIN != EWOULDBLOCK
case EWOULDBLOCK:
#endif
{
//------------------------------------------------------------------
// Reading/writing operation would block! So we are done for now,
// but we will be back ;-)
//------------------------------------------------------------------
return XRootDStatus( stOK, suRetry );
}
case ECONNRESET:
case EDESTADDRREQ:
case EMSGSIZE:
case ENOTCONN:
case ENOTSOCK:
{
//------------------------------------------------------------------
// Actual socket error error!
//------------------------------------------------------------------
return XRootDStatus( stError, errSocketError, errno );
}
case EFAULT:
{
//------------------------------------------------------------------
// The buffer provided by the user for reading/writing is invalid
//------------------------------------------------------------------
return XRootDStatus( stError, errInvalidArgs );
}
default:
{
//------------------------------------------------------------------
// Not a socket error
//------------------------------------------------------------------
return XRootDStatus( stError, errInternal, errno );
}
}
}
XrdCl::suRetry
const uint16_t suRetry
Definition XrdClStatus.hh:40
XrdCl::stError
const uint16_t stError
An error occurred that could potentially be retried.
Definition XrdClStatus.hh:32
XrdCl::errInternal
const uint16_t errInternal
Internal error.
Definition XrdClStatus.hh:56
XrdCl::stOK
const uint16_t stOK
Everything went OK.
Definition XrdClStatus.hh:31
XrdCl::errInvalidArgs
const uint16_t errInvalidArgs
Definition XrdClStatus.hh:58
XrdCl::errSocketError
const uint16_t errSocketError
Definition XrdClStatus.hh:72

References XrdCl::errInternal, XrdCl::errInvalidArgs, XrdCl::errSocketError, XrdCl::stError, XrdCl::stOK, and XrdCl::suRetry.

Referenced by Read(), ReadV(), Send(), and Send().

Here is the caller graph for this function:

## Close()

void XrdCl::Socket::Close | ( |  | ) |

Disconnect.

Definition at line 262 of file XrdClSocket.cc.

{
if( pTls ) pTls->Shutdown();
if( pSocket != -1 )
{
close( pSocket );
pStatus = Disconnected;
pSocket = -1;
pSockName = "";
pPeerName = "";
pName = "";
}
}
close
#define close(a)
Definition XrdPosix.hh:48
XrdCl::Socket::pSockName
std::string pSockName
Definition XrdClSocket.hh:330
XrdCl::Socket::pTls
std::unique_ptr< Tls > pTls
Definition XrdClSocket.hh:337
XrdCl::Socket::pName
std::string pName
Definition XrdClSocket.hh:332
XrdCl::Socket::pPeerName
std::string pPeerName
Definition XrdClSocket.hh:331

References close, Disconnected, pName, pPeerName, pSocket, pSockName, pStatus, and pTls.

Referenced by ~Socket(), ConnectToAddress(), Initialize(), ReadRaw(), and WriteRaw().

Here is the caller graph for this function:

## Connect()

XRootDStatus XrdCl::Socket::Connect | ( | const std::string & | host,
|  | uint16_t | port,
|  | uint16_t | timout = 10 )

Connect to the given host name

Parameters

  host | name of the host to connect to
  port | port to connect to
  timout | timeout in seconds, 0 for no timeout handling (may be used for non blocking IO)

Definition at line 183 of file XrdClSocket.cc.

{
if( pSocket == -1 || pStatus == Connected || pStatus == Connecting )
return XRootDStatus( stError, errInvalidOp );
std::vector<XrdNetAddr> addrs;
std::ostringstream o; o << host << ":" << port;
XRootDStatus st;
if( pProtocolFamily == AF_INET6 )
st = Utils::GetHostAddresses( addrs, URL( o.str() ), Utils::IPAll );
else
st = Utils::GetHostAddresses( addrs, URL( o.str() ), Utils::IPv4 );
if( !st.IsOK() )
return st;
Utils::LogHostAddresses( DefaultEnv::GetLog(), PostMasterMsg, o.str(),
addrs );
return ConnectToAddress( addrs[0], timeout );
}
XrdCl::DefaultEnv::GetLog
static Log * GetLog()
Get default log.
Definition XrdClDefaultEnv.cc:493
XrdCl::Socket::ConnectToAddress
XRootDStatus ConnectToAddress(const XrdNetAddr &addr, uint16_t timout=10)
Definition XrdClSocket.cc:212
XrdCl::Utils::LogHostAddresses
static void LogHostAddresses(Log *log, uint64_t type, const std::string &hostId, std::vector< XrdNetAddr > &addresses)
Log all the addresses on the list.
Definition XrdClUtils.cc:234
XrdCl::Utils::GetHostAddresses
static Status GetHostAddresses(std::vector< XrdNetAddr > &addresses, const URL &url, AddressType type)
Resolve IP addresses.
Definition XrdClUtils.cc:140
XrdCl::Utils::IPAll
@ IPAll
Definition XrdClUtils.hh:89
XrdCl::Utils::IPv4
@ IPv4
Definition XrdClUtils.hh:91
XrdCl::PostMasterMsg
const uint64_t PostMasterMsg
Definition XrdClConstants.hh:36
XrdCl::errInvalidOp
const uint16_t errInvalidOp
Definition XrdClStatus.hh:51

References Connected, Connecting, ConnectToAddress(), XrdCl::errInvalidOp, XrdCl::Utils::GetHostAddresses(), XrdCl::DefaultEnv::GetLog(), XrdCl::Utils::IPAll, XrdCl::Utils::IPv4, XrdCl::Status::IsOK(), XrdCl::Utils::LogHostAddresses(), XrdCl::PostMasterMsg, pProtocolFamily, pSocket, pStatus, and XrdCl::stError.

Here is the call graph for this function:

## ConnectToAddress()

XRootDStatus XrdCl::Socket::ConnectToAddress | ( | const XrdNetAddr & | addr,
|  | uint16_t | timout = 10 )

Connect to the given host address

Parameters

  addr | address of the host to connect to
  timout | timeout in seconds, 0 for no timeout handling (may be used for non blocking IO)

Definition at line 212 of file XrdClSocket.cc.

{
if( pSocket == -1 || pStatus == Connected || pStatus == Connecting )
return XRootDStatus( stError, errInvalidOp );
pServerAddr.reset( new XrdNetAddr( addr ) );;
//--------------------------------------------------------------------------
// Make sure TLS is off when the physical connection is newly established
//--------------------------------------------------------------------------
pTls.reset();
//--------------------------------------------------------------------------
// Connect
//--------------------------------------------------------------------------
int status = XrdNetConnect::Connect( pSocket, pServerAddr->SockAddr(),
pServerAddr->SockSize(), timeout );
if( status != 0 )
{
XRootDStatus st( stError );
//------------------------------------------------------------------------
// If we connect asynchronously this is not really an error
//------------------------------------------------------------------------
if( !timeout && status == EINPROGRESS )
{
pStatus = Connecting;
return XRootDStatus();
}
//------------------------------------------------------------------------
// Errors
//------------------------------------------------------------------------
else if( status == ETIMEDOUT )
st.code = errSocketTimeout;
else
st.code = errSocketError;
st.errNo = status;
Close();
return st;
}
pStatus = Connected;
return XRootDStatus();
}
XrdCl::Socket::pServerAddr
std::unique_ptr< XrdNetAddr > pServerAddr
Definition XrdClSocket.hh:329
XrdNetConnect::Connect
static int Connect(int fd, const struct sockaddr *name, int namelen, int tsec=-1)
Definition XrdNetConnect.cc:47
XrdCl::errSocketTimeout
const uint16_t errSocketTimeout
Definition XrdClStatus.hh:73

References Close(), XrdCl::Status::code, XrdNetConnect::Connect(), Connected, Connecting, XrdCl::errInvalidOp, XrdCl::Status::errNo, XrdCl::errSocketError, XrdCl::errSocketTimeout, pServerAddr, pSocket, pStatus, pTls, and XrdCl::stError.

Referenced by Connect().

Here is the call graph for this function:
Here is the caller graph for this function:

## Cork()

XRootDStatus XrdCl::Socket::Cork | ( |  | ) |

Definition at line 782 of file XrdClSocket.cc.

{
#if defined(TCP_CORK) && !defined(__GNU__)
// it's not defined on mac, we might want explore the possibility of using TCP_NOPUSH
if( pCorked ) return XRootDStatus();
int state = 1;
int rc = setsockopt( pSocket, IPPROTO_TCP, TCP_CORK, &state, sizeof( state ) );
if( rc != 0 )
return XRootDStatus( stFatal, errSocketOptError, errno );
#endif
pCorked = true;
return XRootDStatus();
}
IPPROTO_TCP
#define IPPROTO_TCP
Definition XrdNetUtils.cc:835
XrdCl::errSocketOptError
const uint16_t errSocketOptError
Definition XrdClStatus.hh:76
XrdCl::stFatal
const uint16_t stFatal
Fatal error, it's still an error.
Definition XrdClStatus.hh:33

References XrdCl::errSocketOptError, IPPROTO_TCP, pCorked, pSocket, and XrdCl::stFatal.

Referenced by Flash().

Here is the caller graph for this function:

## Flash()

XRootDStatus XrdCl::Socket::Flash | ( |  | ) |

Definition at line 818 of file XrdClSocket.cc.

{
//----------------------------------------------------------------------
// Uncork the socket in order to flash the socket
//----------------------------------------------------------------------
XRootDStatus st = Uncork();
if( !st.IsOK() ) return st;
//----------------------------------------------------------------------
// Once the data has been flashed we can cork the socket back
//----------------------------------------------------------------------
return Cork();
}
XrdCl::Socket::Uncork
XRootDStatus Uncork()
Definition XrdClSocket.cc:800
XrdCl::Socket::Cork
XRootDStatus Cork()
Definition XrdClSocket.cc:782

References Cork(), XrdCl::Status::IsOK(), and Uncork().

Here is the call graph for this function:

## GetChannelID()

const AnyObject * XrdCl::Socket::GetChannelID | ( |  | ) | const

inline

Get Channel ID (an object that allows to identify all sockets corresponding to the same channel)

Definition at line 255 of file XrdClSocket.hh.

{
return pChannelID;
}

References pChannelID.

## GetFD()

int XrdCl::Socket::GetFD | ( |  | ) | const

inline

Get the file descriptor.

Definition at line 214 of file XrdClSocket.hh.

{
return pSocket;
}

References pSocket.

Referenced by XrdCl::PollerBuiltIn::AddSocket(), and XrdCl::PollerBuiltIn::Start().

Here is the caller graph for this function:

## GetFlags()

XRootDStatus XrdCl::Socket::GetFlags | ( | int & | flags | ) |

Get the socket flags (man fcntl).

Definition at line 137 of file XrdClSocket.cc.

{
if( pSocket == -1 )
return XRootDStatus( stError, errInvalidOp );
int st = ::fcntl( pSocket, F_GETFL, 0 );
if( st == -1 )
return XRootDStatus( stError, errSocketError, errno );
flags = st;
return XRootDStatus();
}

References XrdCl::errInvalidOp, XrdCl::errSocketError, pSocket, and XrdCl::stError.

## GetName()

std::string XrdCl::Socket::GetName | ( |  | ) | const

Get the string representation of the socket.

Definition at line 672 of file XrdClSocket.cc.

{
if( pStatus != Connected )
return "<x><--><x>";
if( pName.length() )
return pName;
pName = "<";
pName += GetSockName();
pName += "><--><";
pName += GetPeerName();
pName += ">";
return pName;
}
XrdCl::Socket::GetSockName
std::string GetSockName() const
Get the name of the socket.
Definition XrdClSocket.cc:632
XrdCl::Socket::GetPeerName
std::string GetPeerName() const
Get the name of the remote peer.
Definition XrdClSocket.cc:652

References Connected, GetPeerName(), GetSockName(), pName, and pStatus.

Referenced by XrdCl::PollerBuiltIn::AddSocket(), XrdCl::PollerBuiltIn::EnableReadNotification(), XrdCl::PollerBuiltIn::EnableWriteNotification(), XrdCl::PollerBuiltIn::RemoveSocket(), and XrdCl::PollerBuiltIn::Stop().

Here is the call graph for this function:
Here is the caller graph for this function:

## GetPeerName()

std::string XrdCl::Socket::GetPeerName | ( |  | ) | const

Get the name of the remote peer.

Definition at line 652 of file XrdClSocket.cc.

{
if( pStatus != Connected )
return "";
if( pPeerName.length() )
return pPeerName;
char nameBuff[256];
int len = XrdNetUtils::IPFormat( pSocket, nameBuff, sizeof(nameBuff) );
if( len == 0 )
return "";
pPeerName = nameBuff;
return pPeerName;
}
XrdNetUtils::IPFormat
static int IPFormat(const struct sockaddr *sAddr, char *bP, int bL, int opts=0)
Definition XrdNetUtils.cc:619

References Connected, XrdNetUtils::IPFormat(), pPeerName, pSocket, and pStatus.

Referenced by GetName().

Here is the call graph for this function:
Here is the caller graph for this function:

## GetServerAddress()

const XrdNetAddr * XrdCl::Socket::GetServerAddress | ( |  | ) | const

inline

Get the server address.

Definition at line 237 of file XrdClSocket.hh.

{
return pServerAddr.get();
}

References pServerAddr.

## GetSockName()

std::string XrdCl::Socket::GetSockName | ( |  | ) | const

Get the name of the socket.

Definition at line 632 of file XrdClSocket.cc.

{
if( pStatus != Connected )
return "";
if( pSockName.length() )
return pSockName;
char nameBuff[256];
int len = XrdNetUtils::IPFormat( -pSocket, nameBuff, sizeof(nameBuff) );
if( len == 0 )
return "";
pSockName = nameBuff;
return pSockName;
}

References Connected, XrdNetUtils::IPFormat(), pSocket, pSockName, and pStatus.

Referenced by GetName().

Here is the call graph for this function:
Here is the caller graph for this function:

## GetSockOpt()

XRootDStatus XrdCl::Socket::GetSockOpt | ( | int | level,
|  | int | optname,
|  | void * | optval,
|  | socklen_t * | optlen )

Get socket options.

Definition at line 152 of file XrdClSocket.cc.

{
if( pSocket == -1 )
return XRootDStatus( stError, errInvalidOp );
if( ::getsockopt( pSocket, level, optname, optval, optlen ) != 0 )
return XRootDStatus( stError, errSocketOptError, errno );
return XRootDStatus();
}

References XrdCl::errInvalidOp, XrdCl::errSocketOptError, pSocket, and XrdCl::stError.

## GetStatus()

SocketStatus XrdCl::Socket::GetStatus | ( |  | ) | const

inline

Get the socket status.

Definition at line 125 of file XrdClSocket.hh.

{
return pStatus;
}

References pStatus.

Referenced by XrdCl::PollerBuiltIn::AddSocket().

Here is the caller graph for this function:

## Initialize()

XRootDStatus XrdCl::Socket::Initialize | ( | int | family = AF_INET | ) |

Initialize the socket.

Definition at line 63 of file XrdClSocket.cc.

{
if( pSocket != -1 )
return XRootDStatus( stError, errInvalidOp );
pSocket = XrdSysFD_Socket( family, SOCK_STREAM, 0 );
if( pSocket < 0 )
{
pSocket = -1;
return XRootDStatus( stError, errSocketError );
}
pProtocolFamily = family;
//--------------------------------------------------------------------------
// Make the socket non blocking and disable the Nagle algorithm since
// we will be using this for transmitting messages not handling streams
//--------------------------------------------------------------------------
int flags;
if( (flags = ::fcntl( pSocket, F_GETFL, 0 )) == -1 )
flags = 0;
if( ::fcntl( pSocket, F_SETFL, flags | O_NONBLOCK | O_NDELAY ) == -1 )
{
Close();
return XRootDStatus( stError, errFcntl, errno );
}
XrdCl::Env *env = XrdCl::DefaultEnv::GetEnv();
flags = DefaultNoDelay;
env->GetInt( "NoDelay", flags );
if( setsockopt( pSocket, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof( int ) ) < 0 )
{
Close();
return XRootDStatus( stError, errFcntl, errno );
}
//--------------------------------------------------------------------------
// We use send with MSG_NOSIGNAL to avoid SIGPIPEs on Linux, on MacOSX
// we set SO_NOSIGPIPE option, on Solaris we ignore the SIGPIPE
//--------------------------------------------------------------------------
#ifdef __APPLE__
int set = 1;
XRootDStatus st = SetSockOpt( SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof(int) );
if( !st.IsOK() )
{
Close();
return st;
}
#elif __solaris__
struct sigaction act;
act.sa_handler = SIG_IGN;
sigaction( SIGPIPE, &act, NULL );
#endif
return XRootDStatus();
}
XrdCl::DefaultEnv::GetEnv
static Env * GetEnv()
Get default client environment.
Definition XrdClDefaultEnv.cc:445
XrdCl::Env::GetInt
bool GetInt(const std::string &key, int &value)
Definition XrdClEnv.cc:89
XrdCl::Socket::SetSockOpt
XRootDStatus SetSockOpt(int level, int optname, const void *optval, socklen_t optlen)
Set socket options.
Definition XrdClSocket.cc:167
XrdCl::errFcntl
const uint16_t errFcntl
failed manipulate file descriptor
Definition XrdClStatus.hh:53
XrdCl::DefaultNoDelay
const int DefaultNoDelay
Definition XrdClConstants.hh:82

References Close(), XrdCl::DefaultNoDelay, XrdCl::errFcntl, XrdCl::errInvalidOp, XrdCl::errSocketError, XrdCl::DefaultEnv::GetEnv(), XrdCl::Env::GetInt(), IPPROTO_TCP, XrdCl::Status::IsOK(), pProtocolFamily, pSocket, SetSockOpt(), and XrdCl::stError.

Here is the call graph for this function:

## IsCorked()

bool XrdCl::Socket::IsCorked | ( |  | ) | const

inline

Definition at line 286 of file XrdClSocket.hh.

{
return pCorked;
}

References pCorked.

## IsEncrypted()

bool XrdCl::Socket::IsEncrypted | ( |  | ) |

Definition at line 867 of file XrdClSocket.cc.

{
return bool( pTls.get() );
}

References pTls.

Referenced by XrdCl::XRootDMsgHandler::WriteMessageBody().

Here is the caller graph for this function:

## MapEvent()

uint8_t XrdCl::Socket::MapEvent | ( | uint8_t | event | ) |

Definition at line 835 of file XrdClSocket.cc.

{
if( pTls ) return pTls->MapEvent( event );
return event;
}

References pTls.

## Poll()

XRootDStatus XrdCl::Socket::Poll | ( | bool | readyForReading,
|  | bool | readyForWriting,
|  | int32_t | timeout )

protected

Poll the socket to see whether it is ready for IO

Parameters

  readyForReading | poll for readiness to read
  readyForWriting | poll for readiness to write
  timeout | timeout in seconds, -1 to wait indefinitely

Returns
  stOK - ready for IO errSocketDisconnected - on disconnection errSocketError - on socket error errSocketTimeout - on socket timeout errInvalidOp - when called on a non connected socket

Definition at line 541 of file XrdClSocket.cc.

{
//--------------------------------------------------------------------------
// Check if we're connected
//--------------------------------------------------------------------------
if( pStatus != Connected )
return XRootDStatus( stError, errInvalidOp );
//--------------------------------------------------------------------------
// Prepare the stuff
//--------------------------------------------------------------------------
pollfd pollDesc;
int pollRet;
bool useTimeout = (timeout!=-1);
time_t now = 0;
time_t newNow = 0;
short hupEvents = POLLHUP;
#ifdef __linux__
hupEvents |= POLLRDHUP;
#endif
if( useTimeout )
now = ::time(0);
pollDesc.fd = pSocket;
pollDesc.events = POLLERR | POLLNVAL | hupEvents;
if( readyForReading )
pollDesc.events |= (POLLIN | POLLPRI);
if( readyForWriting )
pollDesc.events |= POLLOUT;
//--------------------------------------------------------------------------
// We loop on poll because it may return -1 even thought no fatal error
// has occurred, these may be:
// * a signal interrupting the execution (errno == EINTR)
// * a failure to initialize some internal structures (Solaris only)
// (errno == EAGAIN)
//--------------------------------------------------------------------------
do
{
pollRet = poll( &pollDesc, 1, (useTimeout ? timeout*1000 : -1) );
if( (pollRet < 0) && (errno != EINTR) && (errno != EAGAIN) )
return XRootDStatus( stError, errPoll, errno );
//------------------------------------------------------------------------
// Check if we did not time out in the case where we are not supposed
// to wait indefinitely
//------------------------------------------------------------------------
if( useTimeout )
{
newNow = time(0);
timeout -= (newNow-now);
now = newNow;
if( timeout < 0 )
return XRootDStatus( stError, errSocketTimeout );
}
}
while( pollRet == -1 );
//--------------------------------------------------------------------------
// Check if we have timed out
//--------------------------------------------------------------------------
if( pollRet == 0 )
return XRootDStatus( stError, errSocketTimeout );
//--------------------------------------------------------------------------
// We have some events
//--------------------------------------------------------------------------
if( pollDesc.revents & (POLLIN | POLLPRI | POLLOUT) )
return XRootDStatus( stOK );
//--------------------------------------------------------------------------
// We've been hang up on
//--------------------------------------------------------------------------
if( pollDesc.revents & hupEvents )
return XRootDStatus( stError, errSocketDisconnected );
//--------------------------------------------------------------------------
// We're messed up, either because we messed up ourselves (POLLNVAL) or
// got messed up by the network (POLLERR)
//--------------------------------------------------------------------------
return XRootDStatus( stError, errSocketError );
}
XrdCl::errPoll
const uint16_t errPoll
error while polling descriptors
Definition XrdClStatus.hh:54
XrdCl::errSocketDisconnected
const uint16_t errSocketDisconnected
Definition XrdClStatus.hh:74

References Connected, XrdCl::errInvalidOp, XrdCl::errPoll, XrdCl::errSocketDisconnected, XrdCl::errSocketError, XrdCl::errSocketTimeout, pSocket, pStatus, XrdCl::stError, and XrdCl::stOK.

Referenced by ReadRaw(), and WriteRaw().

Here is the caller graph for this function:

## Read()

XRootDStatus XrdCl::Socket::Read | ( | char * | buffer,
|  | size_t | size,
|  | int & | bytesRead )

virtual

Read helper for raw socket

Parameters

  buffer | : the sink for the data
  size | : size of the sink
  bytesRead | : number of bytes actually written into the sink

Returns
  : success : ( stOK ) EAGAIN : ( stOK, suRetry ) EWOULDBLOCK : ( stOK, suRetry ) other error : ( stError, errSocketError )

Definition at line 740 of file XrdClSocket.cc.

{
if( pTls ) return pTls->Read( buffer, size, bytesRead );
int status = ::read( pSocket, buffer, size );
// if the server shut down the socket declare a socket error (it
// will trigger a re-connect)
if( status == 0 )
return XRootDStatus( stError, errSocketError, errno );
if( status < 0 )
return ClassifyErrno( errno );
bytesRead = status;
return XRootDStatus();
}
read
#define read(a, b, c)
Definition XrdPosix.hh:82
XrdCl::Socket::ClassifyErrno
static XRootDStatus ClassifyErrno(int error)
Definition XrdClSocket.cc:692

References ClassifyErrno(), XrdCl::errSocketError, pSocket, pTls, read, and XrdCl::stError.

Referenced by XrdCl::XRootDTransport::GetBody(), XrdCl::XRootDTransport::GetHeader(), XrdCl::XRootDTransport::GetMore(), and XrdCl::AsyncRawReaderIntfc::ReadBytesAsync().

Here is the call graph for this function:
Here is the caller graph for this function:

## ReadRaw()

XRootDStatus XrdCl::Socket::ReadRaw | ( | void * | buffer,
|  | uint32_t | size,
|  | int32_t | timeout,
|  | uint32_t & | bytesRead )

Read raw bytes from the socket.

Read raw bytes from the socket

Parameters

  buffer | data to be sent
  size | size of the data buffer
  timeout | timout value in seconds, -1 to wait indefinitely
  bytesRead | the amount of data actually read

Definition at line 280 of file XrdClSocket.cc.

{
//--------------------------------------------------------------------------
// Check if we're connected
//--------------------------------------------------------------------------
if( pStatus != Connected )
return XRootDStatus( stError, errInvalidOp );
//--------------------------------------------------------------------------
// Some useful variables
//--------------------------------------------------------------------------
bytesRead = 0;
char *current = (char *)buffer;
bool useTimeout = (timeout!=-1);
time_t now = 0;
time_t newNow = 0;
XRootDStatus sc;
if( useTimeout )
now = ::time(0);
//--------------------------------------------------------------------------
// Repeat the following until we have read all the requested data
//--------------------------------------------------------------------------
while ( bytesRead < size )
{
//------------------------------------------------------------------------
// Check if we can read something
//------------------------------------------------------------------------
sc = Poll( true, false, useTimeout ? timeout : -1 );
//------------------------------------------------------------------------
// It looks like we've got an event. Let's check if we can read something.
//------------------------------------------------------------------------
if( sc.status == stOK )
{
ssize_t n = ::read( pSocket, current, (size-bytesRead) );
if( n > 0 )
{
bytesRead += n;
current += n;
}
//----------------------------------------------------------------------
// We got a close here - this means that there is no more data in
// the buffer so we disconnect
//----------------------------------------------------------------------
if( n == 0 )
{
Close();
return XRootDStatus( stError, errSocketDisconnected );
}
//----------------------------------------------------------------------
// Error
//----------------------------------------------------------------------
if( (n < 0) && (errno != EAGAIN) && (errno != EWOULDBLOCK) )
{
Close();
return XRootDStatus( stError, errSocketError, errno );
}
}
else
{
Close();
return sc;
}
//------------------------------------------------------------------------
// Do we still have time to wait for data?
//------------------------------------------------------------------------
if( useTimeout )
{
newNow = ::time(0);
timeout -= (newNow-now);
now = newNow;
if( timeout < 0 )
break;
}
}
//--------------------------------------------------------------------------
// Have we managed to read everything?
//--------------------------------------------------------------------------
if( bytesRead < size )
return XRootDStatus( stError, errSocketTimeout );
return XRootDStatus( stOK );
}
XrdCl::Socket::Poll
XRootDStatus Poll(bool readyForReading, bool readyForWriting, int32_t timeout)
Definition XrdClSocket.cc:541

References Close(), Connected, XrdCl::errInvalidOp, XrdCl::errSocketDisconnected, XrdCl::errSocketError, XrdCl::errSocketTimeout, Poll(), pSocket, pStatus, read, XrdCl::Status::status, XrdCl::stError, and XrdCl::stOK.

Here is the call graph for this function:

## ReadV()

XRootDStatus XrdCl::Socket::ReadV | ( | iovec * | iov,
|  | int | iocnt,
|  | int & | bytesRead )

ReadV helper for raw socket

Parameters

  iov | : the buffers for the data
  iocnt | : number of buffers
  bytesRead | : number of bytes actually written into the sink

Returns
  : success : ( stOK ) EAGAIN : ( stOK, suRetry ) EWOULDBLOCK : ( stOK, suRetry ) other error : ( stError, errSocketError )

Definition at line 761 of file XrdClSocket.cc.

{
if( pTls ) return pTls->ReadV( iov, iovcnt, bytesRead );
int status = ::readv( pSocket, iov, iovcnt );
// if the server shut down the socket declare a socket error (it
// will trigger a re-connect)
if( status == 0 )
return XRootDStatus( stError, errSocketError, errno );
if( status < 0 )
return ClassifyErrno( errno );
bytesRead = status;
return XRootDStatus();
}
readv
#define readv(a, b, c)
Definition XrdPosix.hh:84

References ClassifyErrno(), XrdCl::errSocketError, pSocket, pTls, readv, and XrdCl::stError.

Referenced by XrdCl::AsyncPageReader::Read().

Here is the call graph for this function:
Here is the caller graph for this function:

## Send() [1/3]

XRootDStatus XrdCl::Socket::Send | ( | const char * | buffer,
|  | size_t | size,
|  | int & | bytesWritten )

virtual

Portable wrapper around SIGPIPE free send

Parameters

  buffer | : data to be written
  size | : size of the data buffer
  bytesWritten | : the amount of data actually written

Definition at line 461 of file XrdClSocket.cc.

{
if( pTls ) return pTls->Send( buffer, size, bytesWritten );
//--------------------------------------------------------------------------
// We use send with MSG_NOSIGNAL to avoid SIGPIPEs on Linux
//--------------------------------------------------------------------------
#if defined(__linux__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
int status = ::send( pSocket, buffer, size, MSG_NOSIGNAL );
#else
int status = ::write( pSocket, buffer, size );
#endif
if( status <= 0 )
return ClassifyErrno( errno );
bytesWritten = status;
return XRootDStatus();
}
write
#define write(a, b, c)
Definition XrdPosix.hh:115

References ClassifyErrno(), pSocket, pTls, and write.

Referenced by Send(), and XrdCl::XRootDMsgHandler::WriteMessageBody().

Here is the call graph for this function:
Here is the caller graph for this function:

## Send() [2/3]

XRootDStatus XrdCl::Socket::Send | ( | Message & | msg,
|  | const std::string & | strmname )

Write message to the socket

Parameters

  msg | : message (request) to be sent
  strmname | : stream name (for logging purposes)

Definition at line 503 of file XrdClSocket.cc.

{
//----------------------------------------------------------------------
// Try to write down the current message
//----------------------------------------------------------------------
size_t btsleft = msg.GetSize() - msg.GetCursor();
if( !btsleft ) return XRootDStatus();
while( btsleft )
{
int wrtcnt = 0;
XRootDStatus st = Send( msg.GetBufferAtCursor(), btsleft, wrtcnt );
if( !st.IsOK() )
{
msg.SetCursor( 0 );
return st;
}
if( st.code == suRetry ) return st;
msg.AdvanceCursor( wrtcnt );
btsleft -= wrtcnt;
}
//----------------------------------------------------------------------
// We have written the message successfully
//----------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
log->Dump( AsyncSockMsg, "[%s] Wrote a message: %s (%p), %d bytes",
strmname.c_str(), msg.GetObfuscatedDescription().c_str(),
(void*)&msg, msg.GetSize() );
return XRootDStatus();
}
XrdCl::Socket::Send
virtual XRootDStatus Send(const char *buffer, size_t size, int &bytesWritten)
Definition XrdClSocket.cc:461
XrdCl::AsyncSockMsg
const uint64_t AsyncSockMsg
Definition XrdClConstants.hh:41
XrdGlobal::Log
XrdSysError Log
Definition XrdConfig.cc:113

References XrdCl::Buffer::AdvanceCursor(), XrdCl::AsyncSockMsg, XrdCl::Status::code, XrdCl::Log::Dump(), XrdCl::Buffer::GetBufferAtCursor(), XrdCl::Buffer::GetCursor(), XrdCl::DefaultEnv::GetLog(), XrdCl::Message::GetObfuscatedDescription(), XrdCl::Buffer::GetSize(), XrdCl::Status::IsOK(), Send(), XrdCl::Buffer::SetCursor(), and XrdCl::suRetry.

Here is the call graph for this function:

## Send() [3/3]

XRootDStatus XrdCl::Socket::Send | ( | XrdSys::KernelBuffer & | kbuff,
|  | int & | bytesWritten )

Write data from a kernel buffer to the socket

Parameters

  kbuff | : data to be written
  bytesWritten | : the amount of data actually written

Write data from a kernel buffer to the socket

Parameters

  kbuff | : data to be written

Returns
  : the amount of data actually written

Definition at line 487 of file XrdClSocket.cc.

{
if( pTls ) return XRootDStatus( stError, errNotSupported, 0,
"Cannot send a kernel-buffer over TLS." );
ssize_t status = XrdSys::Send( pSocket, kbuff );
if( status <= 0 )
return ClassifyErrno( errno );
bytesWritten += status;
return XRootDStatus();
}
XrdCl::errNotSupported
const uint16_t errNotSupported
Definition XrdClStatus.hh:62
XrdSys::Send
ssize_t Send(int fd, KernelBuffer &buffer)
Definition XrdSysKernelBuffer.hh:442

References ClassifyErrno(), XrdCl::errNotSupported, pSocket, pTls, XrdSys::Send(), and XrdCl::stError.

Here is the call graph for this function:

## SetChannelID()

void XrdCl::Socket::SetChannelID | ( | AnyObject * | channelID | ) |

inline

Set Channel ID (an object that allows to identify all sockets corresponding to the same channel)

Definition at line 246 of file XrdClSocket.hh.

{
pChannelID = channelID;
}

References pChannelID.

## SetFlags()

XRootDStatus XrdCl::Socket::SetFlags | ( | int | flags | ) |

Set the socket flags (man fcntl).

Definition at line 123 of file XrdClSocket.cc.

{
if( pSocket == -1 )
return XRootDStatus( stError, errInvalidOp );
int st = ::fcntl( pSocket, F_SETFL, flags );
if( st == -1 )
return XRootDStatus( stError, errSocketError, errno );
return XRootDStatus();
}

References XrdCl::errInvalidOp, XrdCl::errSocketError, pSocket, and XrdCl::stError.

## SetSockOpt()

XRootDStatus XrdCl::Socket::SetSockOpt | ( | int | level,
|  | int | optname,
|  | const void * | optval,
|  | socklen_t | optlen )

Set socket options.

Definition at line 167 of file XrdClSocket.cc.

{
if( pSocket == -1 )
return XRootDStatus( stError, errInvalidOp );
if( ::setsockopt( pSocket, level, optname, optval, optlen ) != 0 )
return XRootDStatus( stError, errSocketOptError, errno );
return XRootDStatus();
}

References XrdCl::errInvalidOp, XrdCl::errSocketOptError, pSocket, and XrdCl::stError.

Referenced by Initialize().

Here is the caller graph for this function:

## SetStatus()

void XrdCl::Socket::SetStatus | ( | SocketStatus | status | ) |

inline

Set socket status - do not use unless you know what you're doing.

Definition at line 133 of file XrdClSocket.hh.

{
pStatus = status;
}

References pStatus.

## TlsHandShake()

XRootDStatus XrdCl::Socket::TlsHandShake | ( | AsyncSocketHandler * | socketHandler,
|  | const std::string & | thehost = std::string() )

Definition at line 844 of file XrdClSocket.cc.

{
try
{
if( !pServerAddr ) return XRootDStatus( stError, errInvalidOp );
if( !pTls ) pTls.reset( new Tls( this, socketHandler ) );
return pTls->Connect( thehost, pServerAddr.get() );
}
catch( std::exception& ex )
{
// the exception has been thrown when we tried to create
// the TLS context
return XRootDStatus( stFatal, errTlsError, 0, ex.what() );
}
return XRootDStatus();
}
XrdCl::errTlsError
const uint16_t errTlsError
Definition XrdClStatus.hh:80

References XrdCl::errInvalidOp, XrdCl::errTlsError, pServerAddr, pTls, XrdCl::stError, and XrdCl::stFatal.

## Uncork()

XRootDStatus XrdCl::Socket::Uncork | ( |  | ) |

Definition at line 800 of file XrdClSocket.cc.

{
#if defined(TCP_CORK) && !defined(__GNU__)
// it's not defined on mac, we might want explore the possibility of using TCP_NOPUSH
if( !pCorked ) return XRootDStatus();
int state = 0;
int rc = setsockopt( pSocket, IPPROTO_TCP, TCP_CORK, &state, sizeof( state ) );
if( rc != 0 )
return XRootDStatus( stFatal, errSocketOptError, errno );
#endif
pCorked = false;
return XRootDStatus();
}

References XrdCl::errSocketOptError, IPPROTO_TCP, pCorked, pSocket, and XrdCl::stFatal.

Referenced by Flash().

Here is the caller graph for this function:

## WriteRaw()

XRootDStatus XrdCl::Socket::WriteRaw | ( | void * | buffer,
|  | uint32_t | size,
|  | int32_t | timeout,
|  | uint32_t & | bytesWritten )

Write raw bytes to the socket

Parameters

  buffer | data to be written
  size | size of the data buffer
  timeout | timeout value in seconds, -1 to wait indefinitely
  bytesWritten | the amount of data actually written

Definition at line 375 of file XrdClSocket.cc.

{
//--------------------------------------------------------------------------
// Check if we're connected
//--------------------------------------------------------------------------
if( pStatus != Connected )
return XRootDStatus( stError, errInvalidOp );
//--------------------------------------------------------------------------
// Some useful variables
//--------------------------------------------------------------------------
bytesWritten = 0;
char *current = (char *)buffer;
bool useTimeout = (timeout!=-1);
time_t now = 0;
time_t newNow = 0;
XRootDStatus sc;
if( useTimeout )
now = ::time(0);
//--------------------------------------------------------------------------
// Repeat the following until we have written everything
//--------------------------------------------------------------------------
while ( bytesWritten < size )
{
//------------------------------------------------------------------------
// Check if we can read something
//------------------------------------------------------------------------
sc = Poll( false, true, useTimeout ? timeout : -1 );
//------------------------------------------------------------------------
// Let's write
//------------------------------------------------------------------------
if( sc.status == stOK )
{
ssize_t n = ::write( pSocket, current, (size-bytesWritten) );
if( n > 0 )
{
bytesWritten += n;
current += n;
}
//----------------------------------------------------------------------
// Error
//----------------------------------------------------------------------
if( (n <= 0) && (errno != EAGAIN) && (errno != EWOULDBLOCK) )
{
Close();
return XRootDStatus( stError, errSocketError, errno );
}
}
else
{
Close();
return sc;
}
//------------------------------------------------------------------------
// Do we still have time to wait for data?
//------------------------------------------------------------------------
if( useTimeout )
{
newNow = ::time(0);
timeout -= (newNow-now);
now = newNow;
if( timeout < 0 )
break;
}
}
//--------------------------------------------------------------------------
// Have we managed to read everything?
//--------------------------------------------------------------------------
if( bytesWritten < size )
return XRootDStatus( stError, errSocketTimeout );
return XRootDStatus( stOK );
}

References Close(), Connected, XrdCl::errInvalidOp, XrdCl::errSocketError, XrdCl::errSocketTimeout, Poll(), pSocket, pStatus, XrdCl::Status::status, XrdCl::stError, XrdCl::stOK, and write.

Here is the call graph for this function:

## Member Data Documentation

## pChannelID

AnyObject* XrdCl::Socket::pChannelID

protected

Definition at line 334 of file XrdClSocket.hh.

Referenced by Socket(), GetChannelID(), and SetChannelID().

## pCorked

bool XrdCl::Socket::pCorked

protected

Definition at line 335 of file XrdClSocket.hh.

Referenced by Socket(), Cork(), IsCorked(), and Uncork().

## pName

std::string XrdCl::Socket::pName

mutable protected

Definition at line 332 of file XrdClSocket.hh.

Referenced by Close(), and GetName().

## pPeerName

std::string XrdCl::Socket::pPeerName

mutable protected

Definition at line 331 of file XrdClSocket.hh.

Referenced by Close(), and GetPeerName().

## pProtocolFamily

int XrdCl::Socket::pProtocolFamily

protected

Definition at line 333 of file XrdClSocket.hh.

Referenced by Socket(), Connect(), and Initialize().

## pServerAddr

std::unique_ptr<XrdNetAddr> XrdCl::Socket::pServerAddr

protected

Definition at line 329 of file XrdClSocket.hh.

Referenced by ConnectToAddress(), GetServerAddress(), and TlsHandShake().

## pSocket

int XrdCl::Socket::pSocket

protected

Definition at line 327 of file XrdClSocket.hh.

Referenced by Socket(), Close(), Connect(), ConnectToAddress(), Cork(), GetFD(), GetFlags(), GetPeerName(), GetSockName(), GetSockOpt(), Initialize(), Poll(), Read(), ReadRaw(), ReadV(), Send(), Send(), SetFlags(), SetSockOpt(), Uncork(), and WriteRaw().

## pSockName

std::string XrdCl::Socket::pSockName

mutable protected

Definition at line 330 of file XrdClSocket.hh.

Referenced by Close(), and GetSockName().

## pStatus

SocketStatus XrdCl::Socket::pStatus

protected

Definition at line 328 of file XrdClSocket.hh.

Referenced by Socket(), Close(), Connect(), ConnectToAddress(), GetName(), GetPeerName(), GetSockName(), GetStatus(), Poll(), ReadRaw(), SetStatus(), and WriteRaw().

## pTls

std::unique_ptr<Tls> XrdCl::Socket::pTls

protected

Definition at line 337 of file XrdClSocket.hh.

Referenced by Close(), ConnectToAddress(), IsEncrypted(), MapEvent(), Read(), ReadV(), Send(), Send(), and TlsHandShake().

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

- XrdClSocket.hh
- XrdClSocket.cc