14cf62172SAdrien Destugues/* 24cf62172SAdrien Destugues * Copyright 2013 Haiku, Inc. All rights reserved. 34cf62172SAdrien Destugues * Distributed under the terms of the MIT License. 44cf62172SAdrien Destugues * 54cf62172SAdrien Destugues * Authors: 64cf62172SAdrien Destugues * Adrien Destugues, pulkomandy@pulkomandy.tk 74cf62172SAdrien Destugues * 84cf62172SAdrien Destugues * Corresponds to: 94cf62172SAdrien Destugues * headers/os/net/AbstractSocket.h rev 43302 104cf62172SAdrien Destugues * src/kits/network/libnetapi/AbstractSocket.cpp rev 43302 114cf62172SAdrien Destugues */ 124cf62172SAdrien Destugues 134cf62172SAdrien Destugues/*! 144cf62172SAdrien Destugues \file AbstractSocket.h 154cf62172SAdrien Destugues \ingroup network 164cf62172SAdrien Destugues \brief Provides the BAbstractSocket interface. 174cf62172SAdrien Destugues*/ 184cf62172SAdrien Destugues 194cf62172SAdrien Destugues/*! 204cf62172SAdrien Destugues \class BAbstractSocket 214cf62172SAdrien Destugues \ingroup network 224cf62172SAdrien Destugues \brief Abstract interface for all socket connections. 234cf62172SAdrien Destugues 244cf62172SAdrien Destugues BAbstractSocket provides a common interface for all socket-based 25*7ed015e4SAdrien Destugues communication streams. These include BDatagramSocket, BSocket, 26*7ed015e4SAdrien Destugues BSecureSocket and BServerSocket. 274cf62172SAdrien Destugues 284cf62172SAdrien Destugues BAbstractSocket implements common behavior between these different socket 294cf62172SAdrien Destugues types. This includes management of a BSD socket integer handle, knowledge 304cf62172SAdrien Destugues of the local and remote network addresses, as well as the connection state. 314cf62172SAdrien Destugues*/ 324cf62172SAdrien Destugues 334cf62172SAdrien Destugues/*! 344cf62172SAdrien Destugues \fn BAbstractSocket::BAbstractSocket() 354cf62172SAdrien Destugues \brief Default constructor. 364cf62172SAdrien Destugues 37*7ed015e4SAdrien Destugues Creates an uninitialized socket in disconnected and unbound state. 384cf62172SAdrien Destugues*/ 394cf62172SAdrien Destugues 404cf62172SAdrien Destugues/*! 414cf62172SAdrien Destugues \fn BAbstractSocket::BAbstractSocket(const BAbstractSocket& other) 424cf62172SAdrien Destugues \brief Copy constructor 434cf62172SAdrien Destugues 44*7ed015e4SAdrien Destugues The copied object accesses the same underlying socket. 454cf62172SAdrien Destugues*/ 464cf62172SAdrien Destugues 474cf62172SAdrien Destugues/*! 484cf62172SAdrien Destugues \fn BAbstractSocket::~BAbstractSocket() 494cf62172SAdrien Destugues \brief Destructor 504cf62172SAdrien Destugues 514cf62172SAdrien Destugues*/ 524cf62172SAdrien Destugues 534cf62172SAdrien Destugues/*! 544cf62172SAdrien Destugues \fn status_t BAbstractSocket::InitCheck() const 554cf62172SAdrien Destugues \brief Check connection status 564cf62172SAdrien Destugues 574cf62172SAdrien Destugues \returns B_OK if the connection is working, or an error code if something 584cf62172SAdrien Destugues went wrong. 594cf62172SAdrien Destugues*/ 604cf62172SAdrien Destugues 614cf62172SAdrien Destugues/*! 624cf62172SAdrien Destugues \fn bool BAbstractSocket::IsBound() const 634cf62172SAdrien Destugues 644cf62172SAdrien Destugues A socket becomes bound when Bind succeeds, and stops being bound when 654cf62172SAdrien Destugues Disconnect is called. 664cf62172SAdrien Destugues 674cf62172SAdrien Destugues \returns wether the socket is currently bound 684cf62172SAdrien Destugues*/ 694cf62172SAdrien Destugues 704cf62172SAdrien Destugues/*! 714cf62172SAdrien Destugues \fn bool BAbstractSocket::IsConnected() const 724cf62172SAdrien Destugues 734cf62172SAdrien Destugues A socket becomes connected when Connect succeeds, and disconnected when 744cf62172SAdrien Destugues Disconnect is called. 754cf62172SAdrien Destugues 764cf62172SAdrien Destugues \returns wether the socket is currently connected 774cf62172SAdrien Destugues*/ 784cf62172SAdrien Destugues 794cf62172SAdrien Destugues/*! 804cf62172SAdrien Destugues \fn void BAbstractSocket::Disconnect() 814cf62172SAdrien Destugues \brief Close the connection 824cf62172SAdrien Destugues 834cf62172SAdrien Destugues The socket becomes disconnected and unbound. You can Connect or Bind it 844cf62172SAdrien Destugues again, either to the same or another peer. 854cf62172SAdrien Destugues*/ 864cf62172SAdrien Destugues 874cf62172SAdrien Destugues/*! 884cf62172SAdrien Destugues \fn status_t BAbstractSocket::SetTimeout(bigtime_t timeout) 894cf62172SAdrien Destugues \brief sets the read and write timeout 904cf62172SAdrien Destugues 914cf62172SAdrien Destugues A negative value disables timeouts, so the Read and Write calls will wait 924cf62172SAdrien Destugues until data is available or can be sent. 934cf62172SAdrien Destugues 944cf62172SAdrien Destugues \param timeout The timeout in microseconds, or B_INFINITE_TIMEOUT. 954cf62172SAdrien Destugues*/ 964cf62172SAdrien Destugues 974cf62172SAdrien Destugues/*! 984cf62172SAdrien Destugues \fn bigtime_t BAbstractSocket::Timeout() const 994cf62172SAdrien Destugues \brief gets the socket timeout 1004cf62172SAdrien Destugues 1014cf62172SAdrien Destugues \returns the timeout in microseconds, or B_INFINITE_TIMEOUT 1024cf62172SAdrien Destugues*/ 1034cf62172SAdrien Destugues 1044cf62172SAdrien Destugues/*! 1054cf62172SAdrien Destugues \fn const BNetworkAddress& BAbstractSocket::Local() const 1064cf62172SAdrien Destugues \brief gets the local address for this socket 1074cf62172SAdrien Destugues 1084cf62172SAdrien Destugues This gives useful results only if the socket is either connected or bound. 1094cf62172SAdrien Destugues Otherwise, an uninitialized address is returned. 1104cf62172SAdrien Destugues*/ 1114cf62172SAdrien Destugues 1124cf62172SAdrien Destugues/*! 1134cf62172SAdrien Destugues \fn const BNetworkAddress& BAbstractSocket::Peer() const 1144cf62172SAdrien Destugues \brief gets the peer address 1154cf62172SAdrien Destugues 1164cf62172SAdrien Destugues This gives useful results only if the socket is either connected or bound. 1174cf62172SAdrien Destugues Otherwise, an uninitialized address is returned. 1184cf62172SAdrien Destugues*/ 1194cf62172SAdrien Destugues 1204cf62172SAdrien Destugues/*! 1214cf62172SAdrien Destugues \fn size_t BAbstractSocket::MaxTransmissionSize() const 1224cf62172SAdrien Destugues \brief Return the maximal size of a transmission on this socket. 1234cf62172SAdrien Destugues 1244cf62172SAdrien Destugues The default implementation always returns SSIZE_MAX, but subclasses may 1254cf62172SAdrien Destugues restrict this to a smaller size. 1264cf62172SAdrien Destugues*/ 1274cf62172SAdrien Destugues 1284cf62172SAdrien Destugues/*! 1294cf62172SAdrien Destugues \fn status_t BAbstractSocket::WaitForReadable(bigtime_t timeout) const 1304cf62172SAdrien Destugues \brief wait for incoming data 1314cf62172SAdrien Destugues 1324cf62172SAdrien Destugues Wait until data comes in, or the timeout expires. After this function 1334cf62172SAdrien Destugues returns B_OK, Read can be called without blocking. 1344cf62172SAdrien Destugues 1354cf62172SAdrien Destugues \param timeout the timeout in microseconds, or B_INFINITE_TIMEOUT 1364cf62172SAdrien Destugues \returns B_OK when data is available, B_TIMED_OUT when the timeout expires, 1374cf62172SAdrien Destugues or B_WOULD_BLOCK when the wait was interrupted for other reasons. 1384cf62172SAdrien Destugues*/ 1394cf62172SAdrien Destugues 1404cf62172SAdrien Destugues/*! 1414cf62172SAdrien Destugues \fn status_t BAbstractSocket::WaitForWritable(bigtime_t timeout) const 1424cf62172SAdrien Destugues \brief wait until writing is possible 1434cf62172SAdrien Destugues 1444cf62172SAdrien Destugues Wait until the socket becomes ready for writing, or the timeout expires. 1454cf62172SAdrien Destugues After this function returns B_OK, Write can be called without blocking. 1464cf62172SAdrien Destugues 1474cf62172SAdrien Destugues \param timeout the timeout in microseconds, or B_INFINITE_TIMEOUT 1484cf62172SAdrien Destugues \returns B_OK when the socket is ready to accept writes, B_TIMED_OUT when 1494cf62172SAdrien Destugues the timeout expires, or B_WOULD_BLOCK when the wait was interrupted for 1504cf62172SAdrien Destugues another reason. 1514cf62172SAdrien Destugues*/ 1524cf62172SAdrien Destugues 1534cf62172SAdrien Destugues/*! 1544cf62172SAdrien Destugues \fn int BAbstractSocket::Socket() const 1554cf62172SAdrien Destugues \brief get the underlying socket descriptor 1564cf62172SAdrien Destugues 1574cf62172SAdrien Destugues The BSD socket descriptor can be used to modify advanced connection 1584cf62172SAdrien Destugues paramters using the POSIX socket API. 1594cf62172SAdrien Destugues 1604cf62172SAdrien Destugues \returns the socket descriptor 1614cf62172SAdrien Destugues*/ 1624cf62172SAdrien Destugues 1634cf62172SAdrien Destugues/*! 1644cf62172SAdrien Destugues \fn status_t BAbstractSocket::Bind(const BNetworkAddress& local, int type) 1654cf62172SAdrien Destugues \brief binds the socket to the given address 1664cf62172SAdrien Destugues 1674cf62172SAdrien Destugues If the socket was already bound, the previous binding is removed. 1684cf62172SAdrien Destugues 1694cf62172SAdrien Destugues \param local the local address to bind 1704cf62172SAdrien Destugues \param type the socket type 1714cf62172SAdrien Destugues \return B_OK on success, other error codes on error. 1724cf62172SAdrien Destugues*/ 1734cf62172SAdrien Destugues 1744cf62172SAdrien Destugues/*! 1754cf62172SAdrien Destugues \fn status_t BAbstractSocket::Connect(const BNetworkAddress& peer, 1764cf62172SAdrien Destugues int type, bigtime_t timeout) 1774cf62172SAdrien Destugues \brief Connect the socket to the given peer. 1784cf62172SAdrien Destugues 1794cf62172SAdrien Destugues The socket is disconnected from any previous connections. 1804cf62172SAdrien Destugues 1814cf62172SAdrien Destugues \param peer the peer to connect to 1824cf62172SAdrien Destugues \param type the socket type 1834cf62172SAdrien Destugues \param timeout The timeout in microseconds or B_INFINITE_TIMEOUT. This is 1844cf62172SAdrien Destugues used for subsequent reads and writes as well. 1854cf62172SAdrien Destugues*/ 186