File indexing completed on 2025-01-05 04:37:20

0001 /*
0002     SPDX-FileCopyrightText: 2009 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef NET_SOCKETDEVICE_H
0008 #define NET_SOCKETDEVICE_H
0009 
0010 #include <ktorrent_export.h>
0011 #include <net/address.h>
0012 #include <net/poll.h>
0013 #include <util/constants.h>
0014 
0015 namespace net
0016 {
0017 class SocketDevice
0018 {
0019 public:
0020     SocketDevice(bt::TransportProtocol proto);
0021     virtual ~SocketDevice();
0022 
0023     enum State {
0024         IDLE,
0025         CONNECTING,
0026         CONNECTED,
0027         BOUND,
0028         CLOSED,
0029     };
0030 
0031     virtual int fd() const = 0;
0032     virtual bool ok() const = 0;
0033     virtual int send(const bt::Uint8 *buf, int len) = 0;
0034     virtual int recv(bt::Uint8 *buf, int max_len) = 0;
0035     virtual void close() = 0;
0036     virtual void setBlocking(bool on) = 0;
0037     virtual Uint32 bytesAvailable() const = 0;
0038     virtual bool setTOS(unsigned char type_of_service) = 0;
0039     virtual bool connectTo(const Address &addr) = 0;
0040     /// See if a connectTo was succesfull in non blocking mode
0041     virtual bool connectSuccesFull() = 0;
0042     virtual const Address &getPeerName() const = 0;
0043     virtual Address getSockName() const = 0;
0044 
0045     /// Get the used transport protocol for this SocketDevice
0046     bt::TransportProtocol transportProtocol() const
0047     {
0048         return transport_protocol;
0049     }
0050 
0051     /**
0052      * Set the remote address, used by Socks to set the actual address.
0053      * @param addr The address
0054      */
0055     void setRemoteAddress(const Address &a)
0056     {
0057         addr = a;
0058         remote_addr_override = true;
0059     }
0060 
0061     /// reset the socket (i.e. close it and create a new one)
0062     virtual void reset() = 0;
0063 
0064     State state() const
0065     {
0066         return m_state;
0067     }
0068 
0069     /// Prepare for polling
0070     virtual void prepare(Poll *p, Poll::Mode mode) = 0;
0071 
0072     /// Check if the socket is ready according to the poll
0073     virtual bool ready(const Poll *p, Poll::Mode mode) const = 0;
0074 
0075 protected:
0076     State m_state;
0077     Address addr;
0078     bool remote_addr_override;
0079     bt::TransportProtocol transport_protocol;
0080 };
0081 
0082 }
0083 
0084 #endif // NET_SOCKETDEVICE_H