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

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef NETSOCKET_H
0007 #define NETSOCKET_H
0008 
0009 #include "address.h"
0010 #include <QSharedPointer>
0011 #include <ktorrent_export.h>
0012 #include <net/socketdevice.h>
0013 
0014 namespace net
0015 {
0016 const int SEND_FAILURE = 0;
0017 const int SEND_WOULD_BLOCK = -1;
0018 
0019 /**
0020     @author Joris Guisson <joris.guisson@gmail.com>
0021 */
0022 class KTORRENT_EXPORT Socket : public SocketDevice
0023 {
0024 public:
0025     explicit Socket(int fd, int ip_version);
0026     explicit Socket(bool tcp, int ip_version);
0027     ~Socket() override;
0028 
0029     void setBlocking(bool on) override;
0030     bool connectTo(const Address &addr) override;
0031     bool connectSuccesFull() override;
0032     void close() override;
0033     Uint32 bytesAvailable() const override;
0034     int send(const bt::Uint8 *buf, int len) override;
0035     int recv(bt::Uint8 *buf, int max_len) override;
0036     bool ok() const override
0037     {
0038         return m_fd >= 0;
0039     }
0040     int fd() const override
0041     {
0042         return m_fd;
0043     }
0044     bool setTOS(unsigned char type_of_service) override;
0045     const Address &getPeerName() const override
0046     {
0047         return addr;
0048     }
0049     Address getSockName() const override;
0050 
0051     void reset() override;
0052     void prepare(Poll *p, Poll::Mode mode) override;
0053     bool ready(const Poll *p, Poll::Mode mode) const override;
0054 
0055     bool bind(const QString &ip, Uint16 port, bool also_listen);
0056     bool bind(const Address &addr, bool also_listen);
0057     int accept(Address &a);
0058 
0059     int sendTo(const bt::Uint8 *buf, int size, const Address &addr);
0060     int recvFrom(bt::Uint8 *buf, int max_size, Address &addr);
0061 
0062     bool isIPv4() const
0063     {
0064         return m_ip_version == 4;
0065     }
0066     bool isIPv6() const
0067     {
0068         return m_ip_version == 6;
0069     }
0070 
0071     /// Take the filedescriptor from the socket
0072     int take();
0073 
0074     typedef QSharedPointer<Socket> Ptr;
0075 
0076 private:
0077     void cacheAddress();
0078 
0079 private:
0080     int m_fd;
0081     int m_ip_version;
0082     int r_poll_index;
0083     int w_poll_index;
0084 };
0085 
0086 }
0087 
0088 #endif