10e478f5aSAxel Dörfler /* 2*c9dd7d0dSRene Gollent * Copyright 2011-2016, Haiku, Inc. All Rights Reserved. 30e478f5aSAxel Dörfler * Distributed under the terms of the MIT License. 40e478f5aSAxel Dörfler */ 50e478f5aSAxel Dörfler #ifndef _ABSTRACT_SOCKET_H 60e478f5aSAxel Dörfler #define _ABSTRACT_SOCKET_H 70e478f5aSAxel Dörfler 80e478f5aSAxel Dörfler 90e478f5aSAxel Dörfler #include <DataIO.h> 100e478f5aSAxel Dörfler #include <NetworkAddress.h> 110e478f5aSAxel Dörfler 120e478f5aSAxel Dörfler #include <sys/socket.h> 130e478f5aSAxel Dörfler 140e478f5aSAxel Dörfler 150e478f5aSAxel Dörfler class BAbstractSocket : public BDataIO { 160e478f5aSAxel Dörfler public: 170e478f5aSAxel Dörfler BAbstractSocket(); 180e478f5aSAxel Dörfler BAbstractSocket(const BAbstractSocket& other); 190e478f5aSAxel Dörfler virtual ~BAbstractSocket(); 200e478f5aSAxel Dörfler 210e478f5aSAxel Dörfler status_t InitCheck() const; 220e478f5aSAxel Dörfler 23*c9dd7d0dSRene Gollent virtual status_t Bind(const BNetworkAddress& local, bool reuseAddr) = 0; 240e478f5aSAxel Dörfler virtual bool IsBound() const; 25*c9dd7d0dSRene Gollent virtual bool IsListening() const; 26*c9dd7d0dSRene Gollent 27*c9dd7d0dSRene Gollent virtual status_t Listen(int backlog = 10); 28*c9dd7d0dSRene Gollent virtual status_t Accept(BAbstractSocket*& _socket) = 0; 290e478f5aSAxel Dörfler 300e478f5aSAxel Dörfler virtual status_t Connect(const BNetworkAddress& peer, 310e478f5aSAxel Dörfler bigtime_t timeout = B_INFINITE_TIMEOUT) = 0; 320e478f5aSAxel Dörfler virtual bool IsConnected() const; 330e478f5aSAxel Dörfler virtual void Disconnect(); 340e478f5aSAxel Dörfler 350e478f5aSAxel Dörfler virtual status_t SetTimeout(bigtime_t timeout); 360e478f5aSAxel Dörfler virtual bigtime_t Timeout() const; 370e478f5aSAxel Dörfler 380e478f5aSAxel Dörfler virtual const BNetworkAddress& Local() const; 390e478f5aSAxel Dörfler virtual const BNetworkAddress& Peer() const; 400e478f5aSAxel Dörfler 410e478f5aSAxel Dörfler virtual size_t MaxTransmissionSize() const; 420e478f5aSAxel Dörfler 430e478f5aSAxel Dörfler virtual status_t WaitForReadable(bigtime_t timeout 440e478f5aSAxel Dörfler = B_INFINITE_TIMEOUT) const; 450e478f5aSAxel Dörfler virtual status_t WaitForWritable(bigtime_t timeout 460e478f5aSAxel Dörfler = B_INFINITE_TIMEOUT) const; 470e478f5aSAxel Dörfler 480e478f5aSAxel Dörfler int Socket() const; 490e478f5aSAxel Dörfler 500e478f5aSAxel Dörfler protected: 51*c9dd7d0dSRene Gollent status_t Bind(const BNetworkAddress& local, 52*c9dd7d0dSRene Gollent bool reuseAddr, int type); 530e478f5aSAxel Dörfler status_t Connect(const BNetworkAddress& peer, int type, 540e478f5aSAxel Dörfler bigtime_t timeout = B_INFINITE_TIMEOUT); 55*c9dd7d0dSRene Gollent status_t AcceptNext(int& _acceptedSocket, 56*c9dd7d0dSRene Gollent BNetworkAddress& _peer); 570e478f5aSAxel Dörfler 580e478f5aSAxel Dörfler private: 590e478f5aSAxel Dörfler status_t _OpenIfNeeded(int family, int type); 600e478f5aSAxel Dörfler status_t _UpdateLocalAddress(); 610e478f5aSAxel Dörfler status_t _WaitFor(int flags, bigtime_t timeout) const; 620e478f5aSAxel Dörfler 630e478f5aSAxel Dörfler protected: 640e478f5aSAxel Dörfler status_t fInitStatus; 650e478f5aSAxel Dörfler int fSocket; 660e478f5aSAxel Dörfler BNetworkAddress fLocal; 670e478f5aSAxel Dörfler BNetworkAddress fPeer; 680e478f5aSAxel Dörfler bool fIsBound; 690e478f5aSAxel Dörfler bool fIsConnected; 70*c9dd7d0dSRene Gollent bool fIsListening; 710e478f5aSAxel Dörfler }; 720e478f5aSAxel Dörfler 730e478f5aSAxel Dörfler 740e478f5aSAxel Dörfler #endif // _ABSTRACT_SOCKET_H 75