File indexing completed on 2025-01-05 04:37:19
0001 /* 0002 SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #ifndef NETBUFFEREDSOCKET_H 0007 #define NETBUFFEREDSOCKET_H 0008 0009 #include <QMutex> 0010 #include <deque> 0011 #include <download/packet.h> 0012 #include <download/request.h> 0013 #include <net/socket.h> 0014 #include <net/trafficshapedsocket.h> 0015 0016 namespace net 0017 { 0018 using bt::Uint32; 0019 using bt::Uint8; 0020 0021 /** 0022 * @author Joris Guisson <joris.guisson@gmail.com> 0023 * 0024 * Extends the TrafficShapedSocket with outbound bittorrent 0025 * packet queues. 0026 */ 0027 class PacketSocket : public TrafficShapedSocket 0028 { 0029 public: 0030 PacketSocket(SocketDevice *sock); 0031 PacketSocket(int fd, int ip_version); 0032 PacketSocket(bool tcp, int ip_version); 0033 ~PacketSocket() override; 0034 0035 /** 0036 * Add a packet to send 0037 * @param packet The Packet to send 0038 **/ 0039 void addPacket(bt::Packet::Ptr packet); 0040 0041 Uint32 write(Uint32 max, bt::TimeStamp now) override; 0042 bool bytesReadyToWrite() const override; 0043 0044 /// Get the number of data bytes uploaded 0045 Uint32 dataBytesUploaded(); 0046 0047 /** 0048 * Clear all pieces we are not in the progress of sending. 0049 * @param reject Whether or not to send a reject 0050 */ 0051 void clearPieces(bool reject); 0052 0053 /** 0054 * Do not send a piece which matches this request. 0055 * But only if we are not already sending the piece. 0056 * @param req The request 0057 * @param reject Whether we can send a reject instead 0058 */ 0059 void doNotSendPiece(const bt::Request &req, bool reject); 0060 0061 /// Get the number of pending piece uploads 0062 Uint32 numPendingPieceUploads() const; 0063 0064 /// Get the number of pending piece upload bytes (including message headers) 0065 Uint32 numPendingPieceUploadBytes() const; 0066 0067 protected: 0068 /** 0069 * Preprocess the packet data, before it is sent. Default implementation does nothing. 0070 * @param packet The packet 0071 **/ 0072 virtual void preProcess(bt::Packet::Ptr packet); 0073 0074 private: 0075 bt::Packet::Ptr selectPacket(); 0076 0077 protected: 0078 std::deque<bt::Packet::Ptr> control_packets; 0079 std::deque<bt::Packet::Ptr> data_packets; // NOTE: revert back to lists because of erase() calls? 0080 bt::Packet::Ptr curr_packet; 0081 Uint32 ctrl_packets_sent; 0082 0083 Uint32 pending_upload_data_bytes; 0084 Uint32 uploaded_data_bytes; 0085 }; 0086 0087 } 0088 0089 #endif