All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Public Member Functions | Protected Member Functions | List of all members
BAbstractSocket Class Referenceabstract

Abstract interface for all socket connections. More...

Inherits BDataIO.

Inherited by BDatagramSocket, and BSocket.

Public Member Functions

 BAbstractSocket ()
 Default constructor.
 
 BAbstractSocket (const BAbstractSocket &other)
 Copy constructor.
 
virtual ~BAbstractSocket ()
 Destructor.
 
virtual void Disconnect ()
 Close the connection.
 
status_t InitCheck () const
 Check connection status.
 
virtual bool IsBound () const
 
virtual bool IsConnected () const
 
virtual const BNetworkAddress & Local () const
 gets the local address for this socket
 
virtual size_t MaxTransmissionSize () const
 Return the maximal size of a transmission on this socket.
 
virtual const BNetworkAddress & Peer () const
 gets the peer address
 
virtual status_t SetTimeout (bigtime_t timeout)
 sets the read and write timeout
 
int Socket () const
 get the underlying socket descriptor
 
virtual bigtime_t Timeout () const
 gets the socket timeout
 
virtual status_t WaitForReadable (bigtime_t timeout=B_INFINITE_TIMEOUT) const
 wait for incoming data
 
virtual status_t WaitForWritable (bigtime_t timeout=B_INFINITE_TIMEOUT) const
 wait until writing is possible
 
- Public Member Functions inherited from BDataIO
 BDataIO ()
 This constructor does nothing.
 
virtual ~BDataIO ()
 This destructor does nothing.
 
virtual status_t Flush ()
 Writes pending data to underlying storage.
 
virtual ssize_t Read (void *buffer, size_t size)
 Reads data from the object into a buffer.
 
status_t ReadExactly (void *buffer, size_t size, size_t *_bytesRead=NULL)
 Reads an exact amount of data from the object into a buffer.
 
virtual ssize_t Write (const void *buffer, size_t size)
 Writes data from a buffer to the object.
 
status_t WriteExactly (const void *buffer, size_t size, size_t *_bytesWritten=NULL)
 Writes an exact amount of data from a buffer to the object.
 

Protected Member Functions

status_t Bind (const BNetworkAddress &local, bool reuseAddr, int type)
 binds the socket to the given address
 
status_t Connect (const BNetworkAddress &peer, int type, bigtime_t timeout=B_INFINITE_TIMEOUT)
 Connect the socket to the given peer.
 

Detailed Description

Abstract interface for all socket connections.

BAbstractSocket provides a common interface for all socket-based communication streams. These include BDatagramSocket, BSocket, BSecureSocket and BServerSocket.

BAbstractSocket implements common behavior between these different socket types. This includes management of a BSD socket integer handle, knowledge of the local and remote network addresses, as well as the connection state.

Constructor & Destructor Documentation

◆ BAbstractSocket() [1/2]

BAbstractSocket::BAbstractSocket ( )

Default constructor.

Creates an uninitialized socket in disconnected and unbound state.

◆ BAbstractSocket() [2/2]

BAbstractSocket::BAbstractSocket ( const BAbstractSocket other)

Copy constructor.

The copied object accesses the same underlying socket.

◆ ~BAbstractSocket()

BAbstractSocket::~BAbstractSocket ( )
virtual

Destructor.

Member Function Documentation

◆ Bind()

status_t BAbstractSocket::Bind ( const BNetworkAddress &  local,
bool  reuseAddr,
int  type 
)
protected

binds the socket to the given address

If the socket was already bound, the previous binding is removed.

Parameters
localthe local address to bind
reuseAddrif true, non-zero requests reuse local address
typethe socket type
Returns
B_OK on success, other error codes on error.

◆ Connect()

status_t BAbstractSocket::Connect ( const BNetworkAddress &  peer,
int  type,
bigtime_t  timeout = B_INFINITE_TIMEOUT 
)
protected

Connect the socket to the given peer.

The socket is disconnected from any previous connections.

Parameters
peerthe peer to connect to
typethe socket type
timeoutThe timeout in microseconds or B_INFINITE_TIMEOUT. This is used for subsequent reads and writes as well.

◆ Disconnect()

void BAbstractSocket::Disconnect ( )
virtual

Close the connection.

The socket becomes disconnected and unbound. You can Connect or Bind it again, either to the same or another peer.

◆ InitCheck()

status_t BAbstractSocket::InitCheck ( ) const

Check connection status.

Returns
B_OK if the connection is working, or an error code if something went wrong.

◆ IsBound()

bool BAbstractSocket::IsBound ( ) const
virtual

A socket becomes bound when Bind succeeds, and stops being bound when Disconnect is called.

Returns
wether the socket is currently bound

◆ IsConnected()

bool BAbstractSocket::IsConnected ( ) const
virtual

A socket becomes connected when Connect succeeds, and disconnected when Disconnect is called.

Returns
wether the socket is currently connected

◆ Local()

const BNetworkAddress & BAbstractSocket::Local ( ) const
virtual

gets the local address for this socket

This gives useful results only if the socket is either connected or bound. Otherwise, an uninitialized address is returned.

◆ MaxTransmissionSize()

size_t BAbstractSocket::MaxTransmissionSize ( ) const
virtual

Return the maximal size of a transmission on this socket.

The default implementation always returns SSIZE_MAX, but subclasses may restrict this to a smaller size.

Reimplemented in BDatagramSocket.

◆ Peer()

const BNetworkAddress & BAbstractSocket::Peer ( ) const
virtual

gets the peer address

This gives useful results only if the socket is either connected or bound. Otherwise, an uninitialized address is returned.

◆ SetTimeout()

status_t BAbstractSocket::SetTimeout ( bigtime_t  timeout)
virtual

sets the read and write timeout

A negative value disables timeouts, so the Read and Write calls will wait until data is available or can be sent.

Parameters
timeoutThe timeout in microseconds, or B_INFINITE_TIMEOUT.

◆ Socket()

int BAbstractSocket::Socket ( ) const

get the underlying socket descriptor

The BSD socket descriptor can be used to modify advanced connection paramters using the POSIX socket API.

Returns
the socket descriptor

◆ Timeout()

bigtime_t BAbstractSocket::Timeout ( ) const
virtual

gets the socket timeout

Returns
the timeout in microseconds, or B_INFINITE_TIMEOUT

◆ WaitForReadable()

status_t BAbstractSocket::WaitForReadable ( bigtime_t  timeout = B_INFINITE_TIMEOUT) const
virtual

wait for incoming data

Wait until data comes in, or the timeout expires. After this function returns B_OK, Read can be called without blocking.

Parameters
timeoutthe timeout in microseconds, or B_INFINITE_TIMEOUT
Returns
B_OK when data is available, B_TIMED_OUT when the timeout expires, or B_WOULD_BLOCK when the wait was interrupted for other reasons.

◆ WaitForWritable()

status_t BAbstractSocket::WaitForWritable ( bigtime_t  timeout = B_INFINITE_TIMEOUT) const
virtual

wait until writing is possible

Wait until the socket becomes ready for writing, or the timeout expires. After this function returns B_OK, Write can be called without blocking.

Parameters
timeoutthe timeout in microseconds, or B_INFINITE_TIMEOUT
Returns
B_OK when the socket is ready to accept writes, B_TIMED_OUT when the timeout expires, or B_WOULD_BLOCK when the wait was interrupted for another reason.