File indexing completed on 2025-01-05 04:37:20
0001 /* 0002 SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef NET_SERVERSOCKET_H 0008 #define NET_SERVERSOCKET_H 0009 0010 #include <QObject> 0011 #include <QSharedPointer> 0012 #include <ktorrent_export.h> 0013 #include <util/bufferpool.h> 0014 #include <util/constants.h> 0015 0016 namespace net 0017 { 0018 class Address; 0019 0020 /** 0021 Convenience class to create and bind a server socket. 0022 Internally it combines a QSocketNotifier and a net::Socket. 0023 */ 0024 class KTORRENT_EXPORT ServerSocket : public QObject 0025 { 0026 Q_OBJECT 0027 public: 0028 typedef QSharedPointer<ServerSocket> Ptr; 0029 0030 /** 0031 Interface class to handle new connections 0032 from a ServerSocket. 0033 */ 0034 class KTORRENT_EXPORT ConnectionHandler 0035 { 0036 public: 0037 virtual ~ConnectionHandler() 0038 { 0039 } 0040 0041 /** 0042 A new connection has been accepted 0043 @param fd The filedescriptor of the connection 0044 @param addr The address of the connection 0045 */ 0046 virtual void newConnection(int fd, const net::Address &addr) = 0; 0047 }; 0048 0049 /** 0050 Interface class to handle data from a ServerSocket 0051 */ 0052 class KTORRENT_EXPORT DataHandler 0053 { 0054 public: 0055 virtual ~DataHandler() 0056 { 0057 } 0058 0059 /** 0060 An UDP packet was received 0061 @param data The packet 0062 @param addr The address from which it was received 0063 */ 0064 virtual void dataReceived(bt::Buffer::Ptr buffer, const net::Address &addr) = 0; 0065 0066 /** 0067 Socket has become writeable 0068 @param sock The socket 0069 */ 0070 virtual void readyToWrite(net::ServerSocket *sock) = 0; 0071 }; 0072 0073 /** 0074 Create a TCP server socket 0075 @param chandler The connection handler 0076 */ 0077 ServerSocket(ConnectionHandler *chandler); 0078 0079 /** 0080 Create an UDP server socket 0081 @param dhandler The data handler 0082 */ 0083 ServerSocket(DataHandler *dhandler); 0084 0085 ~ServerSocket() override; 0086 0087 /** 0088 Bind the socket to an IP and port 0089 @param ip The IP address 0090 @param port The port number 0091 @return true upon success, false otherwise 0092 */ 0093 bool bind(const QString &ip, bt::Uint16 port); 0094 0095 /** 0096 Bind the socket to an address 0097 @param addr The address 0098 @return true upon success, false otherwise 0099 */ 0100 bool bind(const net::Address &addr); 0101 0102 /** 0103 Method to send data with the socket. Only use this when 0104 the socket is a UDP socket. It will fail for TCP server sockets. 0105 @param data The data to send 0106 @param addr The address to send to 0107 @return The number of bytes sent 0108 */ 0109 int sendTo(const QByteArray &data, const net::Address &addr); 0110 0111 /** 0112 Method to send data with the socket. Only use this when 0113 the socket is a UDP socket. It will fail for TCP server sockets. 0114 @param buf The data to send 0115 @param size The size of the data 0116 @param addr The address to send to 0117 @return The number of bytes sent 0118 */ 0119 int sendTo(const bt::Uint8 *buf, int size, const net::Address &addr); 0120 0121 /** 0122 Enable write notifications. 0123 @param on On or not 0124 */ 0125 void setWriteNotificationsEnabled(bool on); 0126 0127 /** 0128 Enable read notifications. 0129 @param on On or not 0130 */ 0131 void setReadNotificationsEnabled(bool on); 0132 0133 /** 0134 Set the TOS byte of the socket 0135 @param type_of_service Value to set 0136 @return true upon success, false otherwise 0137 */ 0138 bool setTOS(unsigned char type_of_service); 0139 0140 private Q_SLOTS: 0141 void readyToAccept(int fd); 0142 void readyToRead(int fd); 0143 void readyToWrite(int fd); 0144 0145 private: 0146 class Private; 0147 Private *d; 0148 }; 0149 0150 } 0151 0152 #endif // NET_SERVERSOCKET_H