File indexing completed on 2025-01-05 04:37:33
0001 /* 0002 SPDX-FileCopyrightText: 2011 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "packetbuffer.h" 0008 #include "utpprotocol.h" 0009 #include <QMutex> 0010 #include <QMutexLocker> 0011 #include <list> 0012 0013 namespace utp 0014 { 0015 bt::BufferPool::Ptr PacketBuffer::pool; 0016 0017 PacketBuffer::PacketBuffer() 0018 : header(nullptr) 0019 , extension(nullptr) 0020 , payload(nullptr) 0021 , size(0) 0022 { 0023 if (!pool) { 0024 pool = bt::BufferPool::Ptr(new bt::BufferPool()); 0025 pool->setWeakPointer(pool.toWeakRef()); 0026 } 0027 buffer = pool->get(MAX_SIZE); 0028 } 0029 0030 PacketBuffer::~PacketBuffer() 0031 { 0032 } 0033 0034 void PacketBuffer::clearPool() 0035 { 0036 pool.clear(); 0037 } 0038 0039 bool PacketBuffer::setHeader(const Header &hdr, bt::Uint32 extension_length) 0040 { 0041 if (Header::size() - extension_length > headRoom()) 0042 return false; 0043 0044 if (payload) 0045 header = payload - Header::size() - extension_length; 0046 else 0047 header = buffer->get(); 0048 0049 hdr.write(header); 0050 extension = header + Header::size(); 0051 if (payload) 0052 size = (buffer->get() + MAX_SIZE) - header; 0053 else 0054 size = Header::size() + extension_length; 0055 0056 return true; 0057 } 0058 0059 bt::Uint32 PacketBuffer::fillData(bt::CircularBuffer &cbuf, bt::Uint32 to_read) 0060 { 0061 // Make sure we leave enough room for a header 0062 if (to_read > MAX_SIZE - Header::size()) 0063 to_read = MAX_SIZE - Header::size(); 0064 0065 // Data is put at the end of the buffer, so we can put headers easily in front of it 0066 payload = (buffer->get() + MAX_SIZE) - to_read; 0067 cbuf.read(payload, to_read); 0068 size = to_read; 0069 0070 header = extension = payload; 0071 return to_read; 0072 } 0073 0074 bt::Uint32 PacketBuffer::fillData(const bt::Uint8 *data, bt::Uint32 data_size) 0075 { 0076 if (data_size > MAX_SIZE) 0077 data_size = MAX_SIZE; 0078 0079 payload = (buffer->get() + MAX_SIZE) - data_size; 0080 memcpy(payload, data, data_size); 0081 header = extension = payload; 0082 size = data_size; 0083 return data_size; 0084 } 0085 0086 void PacketBuffer::fillDummyData(bt::Uint32 amount) 0087 { 0088 header = extension = payload = (buffer->get() + MAX_SIZE) - amount; 0089 size += amount; 0090 } 0091 0092 } /* namespace utp */