File indexing completed on 2024-11-24 04:31:17
0001 /* 0002 SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #ifndef BTPACKET_H 0007 #define BTPACKET_H 0008 0009 #include <QSharedPointer> 0010 #include <util/constants.h> 0011 0012 namespace net 0013 { 0014 class SocketDevice; 0015 } 0016 0017 namespace bt 0018 { 0019 class BitSet; 0020 class Request; 0021 class Chunk; 0022 class Peer; 0023 0024 /** 0025 * @author Joris Guisson 0026 * 0027 * Packet off data, which gets sent to a Peer 0028 */ 0029 class Packet 0030 { 0031 public: 0032 Packet(Uint8 type); 0033 Packet(Uint16 port); 0034 Packet(Uint32 chunk, Uint8 type); 0035 Packet(const BitSet &bs); 0036 Packet(const Request &req, Uint8 type); 0037 Packet(Uint32 index, Uint32 begin, Uint32 len, Chunk *ch); 0038 Packet(Uint8 ext_id, const QByteArray &ext_data); // extension protocol packet 0039 ~Packet(); 0040 0041 /// Get the packet type 0042 Uint8 getType() const 0043 { 0044 return type; 0045 } 0046 0047 bool isOK() const; 0048 0049 const Uint8 *getData() const 0050 { 0051 return data; 0052 } 0053 Uint8 *getData() 0054 { 0055 return data; 0056 } 0057 Uint32 getDataLength() const 0058 { 0059 return size; 0060 } 0061 0062 /// Is the packet sent ? 0063 Uint32 isSent() const 0064 { 0065 return written == size; 0066 } 0067 0068 /** 0069 * If this packet is a piece, make a reject for it 0070 * @return The newly created Packet, 0 if this is not a piece 0071 */ 0072 Packet *makeRejectOfPiece(); 0073 0074 /// Are we sending this packet ? 0075 bool sending() const 0076 { 0077 return written > 0; 0078 } 0079 0080 /** 0081 * Is this a piece packet which matches a request 0082 * @param req The request 0083 * @return If this is a piece in response of this request 0084 */ 0085 bool isPiece(const Request &req) const; 0086 0087 /** 0088 * Send the packet over a SocketDevice 0089 * @param sock The socket 0090 * @param max_to_send Max bytes to send 0091 * @return int Return value of send call from SocketDevice 0092 **/ 0093 int send(net::SocketDevice *sock, Uint32 max_to_send); 0094 0095 typedef QSharedPointer<Packet> Ptr; 0096 0097 private: 0098 Uint8 *data; 0099 Uint32 size; 0100 Uint32 written; 0101 Uint8 type; 0102 }; 0103 0104 } 0105 0106 #endif