30#include "tdesocketbase.h"
31#include "tdesocketbuffer_p.h"
34using namespace KNetwork::Internal;
36TDESocketBuffer::TDESocketBuffer(TQ_LONG size)
37 : m_mutex(true), m_offset(0), m_size(size), m_length(0)
41TDESocketBuffer::TDESocketBuffer(
const TDESocketBuffer& other)
47TDESocketBuffer::~TDESocketBuffer()
52TDESocketBuffer& TDESocketBuffer::operator=(
const TDESocketBuffer& other)
54 TQMutexLocker locker1(&m_mutex);
55 TQMutexLocker locker2(&other.m_mutex);
59 m_list = other.m_list;
60 m_offset = other.m_offset;
61 m_size = other.m_size;
62 m_length = other.m_length;
67bool TDESocketBuffer::canReadLine()
const
69 TQMutexLocker locker(&m_mutex);
71 TQValueListConstIterator<TQByteArray> it = m_list.constBegin(),
72 end = m_list.constEnd();
73 TQIODevice::Offset offset = m_offset;
76 for ( ; it !=
end; ++it)
78 if ((*it).find(
'\n', offset) != -1)
80 if ((*it).find(
'\r', offset) != -1)
88TQCString TDESocketBuffer::readLine()
93 TQMutexLocker locker(&m_mutex);
97 TQValueListConstIterator<TQByteArray> it = m_list.constBegin(),
98 end = m_list.constEnd();
99 TQIODevice::Offset offset = m_offset;
102 for ( ; it !=
end; ++it)
104 int posnl = (*it).find(
'\n', offset);
108 newline += (*it).size();
118 TQCString result(newline + 2 - m_offset);
119 consumeBuffer(result.data(), newline + 1 - m_offset);
123TQ_LONG TDESocketBuffer::length()
const
128TQ_LONG TDESocketBuffer::size()
const
133bool TDESocketBuffer::setSize(TQ_LONG size)
136 if (size == -1 || m_length < m_size)
140 TQMutexLocker locker(&m_mutex);
143 if (m_length < m_size)
147 return (m_length - m_size) == consumeBuffer(0L, m_length - m_size,
true);
150TQ_LONG TDESocketBuffer::feedBuffer(
const char *data, TQ_LONG len)
152 if (data == 0L || len == 0)
157 TQMutexLocker locker(&m_mutex);
160 if (m_size != -1 && (m_size - m_length) < len)
161 len = m_size - m_length;
164 a.duplicate(data, len);
171TQ_LONG TDESocketBuffer::consumeBuffer(
char *destbuffer, TQ_LONG maxlen,
bool discard)
173 if (maxlen == 0 || isEmpty())
176 TQValueListIterator<TQByteArray> it = m_list.begin(),
178 TQIODevice::Offset offset = m_offset;
182 while (it != end && maxlen)
185 size_t to_copy = (*it).size() - offset;
186 if (to_copy > (
size_t)maxlen)
191 memcpy(destbuffer + copied, (*it).data() + offset, to_copy);
195 if ((*it).size() - offset > to_copy)
207 it = m_list.remove(it);
217 assert(m_length >= 0);
223void TDESocketBuffer::clear()
225 TQMutexLocker locker(&m_mutex);
233 if (len == 0 || isEmpty())
236 TQMutexLocker locker(&m_mutex);
238 TQValueListIterator<TQByteArray> it = m_list.begin(),
240 TQIODevice::Offset offset = m_offset;
244 while (it != end && (len || len == -1))
251 TQ_ULONG bufsize = 1460;
252 if ((len != -1) && ((TQ_ULONG)len < bufsize)) {
255 TQByteArray buf(bufsize);
258 while (it != end && count + ((*it).size() - offset) <= bufsize)
260 memcpy(buf.data() + count, (*it).data() + offset, (*it).size() - offset);
261 count += (*it).size() - offset;
267 if ((TQ_ULONG)count < bufsize && it != end)
271 memcpy(buf.data() + count, (*it).data() + offset, bufsize - count);
272 offset += bufsize - count;
292 consumeBuffer(0L, written);
299 if (len == 0 || isFull())
302 TQMutexLocker locker(&m_mutex);
311 if (m_size != -1 && len > (m_size - m_length))
312 len = m_size - m_length;
Abstract class for active sockets.
virtual TQ_LONG bytesAvailable() const =0
Returns the number of bytes available for reading without blocking.
virtual TQ_LONG readBlock(char *data, TQ_ULONG len)=0
Reads data from the socket.
virtual TQ_LONG writeBlock(const char *data, TQ_ULONG len)=0
Writes the given data to the socket.
base for I/O buffer implementation
TDEIOBufferBase & operator=(const TDEIOBufferBase &)
Assignment operator.
A namespace to store all networking-related (socket) classes.
const TDEShortcut & end()
Goto end of the document.