1*4cf62172SAdrien Destugues/* 2*4cf62172SAdrien Destugues * Copyright 2013 Haiku, Inc. All rights reserved. 3*4cf62172SAdrien Destugues * Distributed under the terms of the MIT License. 4*4cf62172SAdrien Destugues * 5*4cf62172SAdrien Destugues * Authors: 6*4cf62172SAdrien Destugues * Adrien Destugues, pulkomandy@pulkomandy.tk 7*4cf62172SAdrien Destugues * 8*4cf62172SAdrien Destugues * Corresponds to: 9*4cf62172SAdrien Destugues * headers/os/net/AbstractSocket.h rev 43302 10*4cf62172SAdrien Destugues * src/kits/network/libnetapi/AbstractSocket.cpp rev 43302 11*4cf62172SAdrien Destugues */ 12*4cf62172SAdrien Destugues 13*4cf62172SAdrien Destugues/*! 14*4cf62172SAdrien Destugues \file AbstractSocket.h 15*4cf62172SAdrien Destugues \ingroup network 16*4cf62172SAdrien Destugues \brief Provides the BAbstractSocket interface. 17*4cf62172SAdrien Destugues*/ 18*4cf62172SAdrien Destugues 19*4cf62172SAdrien Destugues/*! 20*4cf62172SAdrien Destugues \class BAbstractSocket 21*4cf62172SAdrien Destugues \ingroup network 22*4cf62172SAdrien Destugues \brief Abstract interface for all socket connections. 23*4cf62172SAdrien Destugues 24*4cf62172SAdrien Destugues BAbstractSocket provides a common interface for all socket-based 25*4cf62172SAdrien Destugues communication streams. These includes datagrams, TCP sockets and SSL 26*4cf62172SAdrien Destugues secure sockets. 27*4cf62172SAdrien Destugues 28*4cf62172SAdrien Destugues BAbstractSocket implements common behavior between these different socket 29*4cf62172SAdrien Destugues types. This includes management of a BSD socket integer handle, knowledge 30*4cf62172SAdrien Destugues of the local and remote network addresses, as well as the connection state. 31*4cf62172SAdrien Destugues 32*4cf62172SAdrien Destugues New subclasses of BAbstractSocket may be created to allow communication 33*4cf62172SAdrien Destugues using more protocols. 34*4cf62172SAdrien Destugues*/ 35*4cf62172SAdrien Destugues 36*4cf62172SAdrien Destugues/*! 37*4cf62172SAdrien Destugues \fn BAbstractSocket::BAbstractSocket() 38*4cf62172SAdrien Destugues \brief Default constructor. 39*4cf62172SAdrien Destugues 40*4cf62172SAdrien Destugues The socket is disconnected and unbound, and the status is B_NO_INIT. 41*4cf62172SAdrien Destugues Use Bind or Connect to initialize it. 42*4cf62172SAdrien Destugues*/ 43*4cf62172SAdrien Destugues 44*4cf62172SAdrien Destugues/*! 45*4cf62172SAdrien Destugues \fn BAbstractSocket::BAbstractSocket(const BAbstractSocket& other) 46*4cf62172SAdrien Destugues \brief Copy constructor 47*4cf62172SAdrien Destugues 48*4cf62172SAdrien Destugues There is no new connection to the server. Instead, data sent using several 49*4cf62172SAdrien Destugues copy of the class will be intermixed, and the first instance to read data 50*4cf62172SAdrien Destugues will steal it from the others. 51*4cf62172SAdrien Destugues 52*4cf62172SAdrien Destugues This is probably not what you want, unless you work with datagrams. In 53*4cf62172SAdrien Destugues that case, the messages read and written are atomic and can be safely sent 54*4cf62172SAdrien Destugues and received from different places. 55*4cf62172SAdrien Destugues*/ 56*4cf62172SAdrien Destugues 57*4cf62172SAdrien Destugues/*! 58*4cf62172SAdrien Destugues \fn BAbstractSocket::~BAbstractSocket() 59*4cf62172SAdrien Destugues \brief Destructor 60*4cf62172SAdrien Destugues 61*4cf62172SAdrien Destugues Disconnects the socket by calling Disconnect(). 62*4cf62172SAdrien Destugues*/ 63*4cf62172SAdrien Destugues 64*4cf62172SAdrien Destugues/*! 65*4cf62172SAdrien Destugues \fn status_t BAbstractSocket::InitCheck() const 66*4cf62172SAdrien Destugues \brief Check connection status 67*4cf62172SAdrien Destugues 68*4cf62172SAdrien Destugues \returns B_OK if the connection is working, or an error code if something 69*4cf62172SAdrien Destugues went wrong. 70*4cf62172SAdrien Destugues*/ 71*4cf62172SAdrien Destugues 72*4cf62172SAdrien Destugues/*! 73*4cf62172SAdrien Destugues \fn bool BAbstractSocket::IsBound() const 74*4cf62172SAdrien Destugues 75*4cf62172SAdrien Destugues A socket becomes bound when Bind succeeds, and stops being bound when 76*4cf62172SAdrien Destugues Disconnect is called. 77*4cf62172SAdrien Destugues 78*4cf62172SAdrien Destugues \returns wether the socket is currently bound 79*4cf62172SAdrien Destugues*/ 80*4cf62172SAdrien Destugues 81*4cf62172SAdrien Destugues/*! 82*4cf62172SAdrien Destugues \fn bool BAbstractSocket::IsConnected() const 83*4cf62172SAdrien Destugues 84*4cf62172SAdrien Destugues A socket becomes connected when Connect succeeds, and disconnected when 85*4cf62172SAdrien Destugues Disconnect is called. 86*4cf62172SAdrien Destugues 87*4cf62172SAdrien Destugues \returns wether the socket is currently connected 88*4cf62172SAdrien Destugues*/ 89*4cf62172SAdrien Destugues 90*4cf62172SAdrien Destugues/*! 91*4cf62172SAdrien Destugues \fn void BAbstractSocket::Disconnect() 92*4cf62172SAdrien Destugues \brief Close the connection 93*4cf62172SAdrien Destugues 94*4cf62172SAdrien Destugues The socket becomes disconnected and unbound. You can Connect or Bind it 95*4cf62172SAdrien Destugues again, either to the same or another peer. 96*4cf62172SAdrien Destugues*/ 97*4cf62172SAdrien Destugues 98*4cf62172SAdrien Destugues/*! 99*4cf62172SAdrien Destugues \fn status_t BAbstractSocket::SetTimeout(bigtime_t timeout) 100*4cf62172SAdrien Destugues \brief sets the read and write timeout 101*4cf62172SAdrien Destugues 102*4cf62172SAdrien Destugues A negative value disables timeouts, so the Read and Write calls will wait 103*4cf62172SAdrien Destugues until data is available or can be sent. 104*4cf62172SAdrien Destugues 105*4cf62172SAdrien Destugues \param timeout The timeout in microseconds, or B_INFINITE_TIMEOUT. 106*4cf62172SAdrien Destugues*/ 107*4cf62172SAdrien Destugues 108*4cf62172SAdrien Destugues/*! 109*4cf62172SAdrien Destugues \fn bigtime_t BAbstractSocket::Timeout() const 110*4cf62172SAdrien Destugues \brief gets the socket timeout 111*4cf62172SAdrien Destugues 112*4cf62172SAdrien Destugues \returns the timeout in microseconds, or B_INFINITE_TIMEOUT 113*4cf62172SAdrien Destugues*/ 114*4cf62172SAdrien Destugues 115*4cf62172SAdrien Destugues/*! 116*4cf62172SAdrien Destugues \fn const BNetworkAddress& BAbstractSocket::Local() const 117*4cf62172SAdrien Destugues \brief gets the local address for this socket 118*4cf62172SAdrien Destugues 119*4cf62172SAdrien Destugues This gives useful results only if the socket is either connected or bound. 120*4cf62172SAdrien Destugues Otherwise, an uninitialized address is returned. 121*4cf62172SAdrien Destugues*/ 122*4cf62172SAdrien Destugues 123*4cf62172SAdrien Destugues/*! 124*4cf62172SAdrien Destugues \fn const BNetworkAddress& BAbstractSocket::Peer() const 125*4cf62172SAdrien Destugues \brief gets the peer address 126*4cf62172SAdrien Destugues 127*4cf62172SAdrien Destugues This gives useful results only if the socket is either connected or bound. 128*4cf62172SAdrien Destugues Otherwise, an uninitialized address is returned. 129*4cf62172SAdrien Destugues*/ 130*4cf62172SAdrien Destugues 131*4cf62172SAdrien Destugues/*! 132*4cf62172SAdrien Destugues \fn size_t BAbstractSocket::MaxTransmissionSize() const 133*4cf62172SAdrien Destugues \brief Return the maximal size of a transmission on this socket. 134*4cf62172SAdrien Destugues 135*4cf62172SAdrien Destugues The default implementation always returns SSIZE_MAX, but subclasses may 136*4cf62172SAdrien Destugues restrict this to a smaller size. 137*4cf62172SAdrien Destugues*/ 138*4cf62172SAdrien Destugues 139*4cf62172SAdrien Destugues/*! 140*4cf62172SAdrien Destugues \fn status_t BAbstractSocket::WaitForReadable(bigtime_t timeout) const 141*4cf62172SAdrien Destugues \brief wait for incoming data 142*4cf62172SAdrien Destugues 143*4cf62172SAdrien Destugues Wait until data comes in, or the timeout expires. After this function 144*4cf62172SAdrien Destugues returns B_OK, Read can be called without blocking. 145*4cf62172SAdrien Destugues 146*4cf62172SAdrien Destugues \param timeout the timeout in microseconds, or B_INFINITE_TIMEOUT 147*4cf62172SAdrien Destugues \returns B_OK when data is available, B_TIMED_OUT when the timeout expires, 148*4cf62172SAdrien Destugues or B_WOULD_BLOCK when the wait was interrupted for other reasons. 149*4cf62172SAdrien Destugues*/ 150*4cf62172SAdrien Destugues 151*4cf62172SAdrien Destugues/*! 152*4cf62172SAdrien Destugues \fn status_t BAbstractSocket::WaitForWritable(bigtime_t timeout) const 153*4cf62172SAdrien Destugues \brief wait until writing is possible 154*4cf62172SAdrien Destugues 155*4cf62172SAdrien Destugues Wait until the socket becomes ready for writing, or the timeout expires. 156*4cf62172SAdrien Destugues After this function returns B_OK, Write can be called without blocking. 157*4cf62172SAdrien Destugues 158*4cf62172SAdrien Destugues \param timeout the timeout in microseconds, or B_INFINITE_TIMEOUT 159*4cf62172SAdrien Destugues \returns B_OK when the socket is ready to accept writes, B_TIMED_OUT when 160*4cf62172SAdrien Destugues the timeout expires, or B_WOULD_BLOCK when the wait was interrupted for 161*4cf62172SAdrien Destugues another reason. 162*4cf62172SAdrien Destugues*/ 163*4cf62172SAdrien Destugues 164*4cf62172SAdrien Destugues/*! 165*4cf62172SAdrien Destugues \fn int BAbstractSocket::Socket() const 166*4cf62172SAdrien Destugues \brief get the underlying socket descriptor 167*4cf62172SAdrien Destugues 168*4cf62172SAdrien Destugues The BSD socket descriptor can be used to modify advanced connection 169*4cf62172SAdrien Destugues paramters using the POSIX socket API. 170*4cf62172SAdrien Destugues 171*4cf62172SAdrien Destugues \returns the socket descriptor 172*4cf62172SAdrien Destugues*/ 173*4cf62172SAdrien Destugues 174*4cf62172SAdrien Destugues/*! 175*4cf62172SAdrien Destugues \fn status_t BAbstractSocket::Bind(const BNetworkAddress& local, int type) 176*4cf62172SAdrien Destugues \brief binds the socket to the given address 177*4cf62172SAdrien Destugues 178*4cf62172SAdrien Destugues If the socket was already bound, the previous binding is removed. 179*4cf62172SAdrien Destugues 180*4cf62172SAdrien Destugues \param local the local address to bind 181*4cf62172SAdrien Destugues \param type the socket type 182*4cf62172SAdrien Destugues \return B_OK on success, other error codes on error. 183*4cf62172SAdrien Destugues*/ 184*4cf62172SAdrien Destugues 185*4cf62172SAdrien Destugues/*! 186*4cf62172SAdrien Destugues \fn status_t BAbstractSocket::Connect(const BNetworkAddress& peer, 187*4cf62172SAdrien Destugues int type, bigtime_t timeout) 188*4cf62172SAdrien Destugues \brief Connect the socket to the given peer. 189*4cf62172SAdrien Destugues 190*4cf62172SAdrien Destugues The socket is disconnected from any previous connections. 191*4cf62172SAdrien Destugues 192*4cf62172SAdrien Destugues \param peer the peer to connect to 193*4cf62172SAdrien Destugues \param type the socket type 194*4cf62172SAdrien Destugues \param timeout The timeout in microseconds or B_INFINITE_TIMEOUT. This is 195*4cf62172SAdrien Destugues used for subsequent reads and writes as well. 196*4cf62172SAdrien Destugues*/ 197