summaryrefslogtreecommitdiffstats
path: root/tdecore/network/ksocketbuffer.cpp
blob: 0ba18b77b0e356de449b75f0390620cc13253974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*  -*- C++ -*-
 *  Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
 *
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included 
 *  in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <config.h>

#include <assert.h>
#include <string.h>

#include "tdesocketbase.h"
#include "tdesocketbuffer_p.h"

using namespace KNetwork;
using namespace KNetwork::Internal;

KSocketBuffer::KSocketBuffer(TQ_LONG size)
  : m_mutex(true), m_offset(0), m_size(size), m_length(0)
{
}

KSocketBuffer::KSocketBuffer(const KSocketBuffer& other)
  : KIOBufferBase(other), m_mutex(true)
{
  *this = other;
}

KSocketBuffer::~KSocketBuffer()
{
  // TQValueList takes care of deallocating memory
}

KSocketBuffer& KSocketBuffer::operator=(const KSocketBuffer& other)
{
  TQMutexLocker locker1(&m_mutex);
  TQMutexLocker locker2(&other.m_mutex);

  KIOBufferBase::operator=(other);

  m_list = other.m_list;	// copy-on-write
  m_offset = other.m_offset;
  m_size = other.m_size;
  m_length = other.m_length;

  return *this;
}

bool KSocketBuffer::canReadLine() const
{
  TQMutexLocker locker(&m_mutex);

  TQValueListConstIterator<TQByteArray> it = m_list.constBegin(),
    end = m_list.constEnd();
  TQIODevice::Offset offset = m_offset;

  // walk the buffer
  for ( ; it != end; ++it)
    {
      if ((*it).find('\n', offset) != -1)
	return true;
      if ((*it).find('\r', offset) != -1)
	return true;
      offset = 0;
    }

  return false;			// not found
}

TQCString KSocketBuffer::readLine()
{
  if (!canReadLine())
    return TQCString();		// empty

  TQMutexLocker locker(&m_mutex);

  // find the offset of the newline in the buffer
  int newline = 0;
  TQValueListConstIterator<TQByteArray> it = m_list.constBegin(),
    end = m_list.constEnd();
  TQIODevice::Offset offset = m_offset;

  // walk the buffer
  for ( ; it != end; ++it)
    {
      int posnl = (*it).find('\n', offset);
      if (posnl == -1)
	{
	  // not found in this one
	  newline += (*it).size();
	  offset = 0;
	  continue;
	}

      // we found it
      newline += posnl;
      break;
    }

  TQCString result(newline + 2 - m_offset);
  consumeBuffer(result.data(), newline + 1 - m_offset);
  return result;
}

TQ_LONG KSocketBuffer::length() const
{
  return m_length;
}

TQ_LONG KSocketBuffer::size() const
{
  return m_size;
}

bool KSocketBuffer::setSize(TQ_LONG size)
{
  m_size = size;
  if (size == -1 || m_length < m_size)
    return true;

  // size is now smaller than length
  TQMutexLocker locker(&m_mutex);

  // repeat the test
  if (m_length < m_size)
    return true;

  // discard from the beginning
  return (m_length - m_size) == consumeBuffer(0L, m_length - m_size, true);
}

TQ_LONG KSocketBuffer::feedBuffer(const char *data, TQ_LONG len)
{
  if (data == 0L || len == 0)
    return 0;			// nothing to write
  if (isFull())
    return -1;			// can't write

  TQMutexLocker locker(&m_mutex);

  // verify if we can add len bytes
  if (m_size != -1 && (m_size - m_length) < len)
    len = m_size - m_length;

  TQByteArray a(len);
  a.duplicate(data, len);
  m_list.append(a);

  m_length += len;
  return len;
}

TQ_LONG KSocketBuffer::consumeBuffer(char *destbuffer, TQ_LONG maxlen, bool discard)
{
  if (maxlen == 0 || isEmpty())
    return 0;

  TQValueListIterator<TQByteArray> it = m_list.begin(),
    end = m_list.end();
  TQIODevice::Offset offset = m_offset;
  TQ_LONG copied = 0;

  // walk the buffer
  while (it != end && maxlen)
    {
      // calculate how much we'll copy
      size_t to_copy = (*it).size() - offset;
      if (to_copy > maxlen)
	to_copy = maxlen;

      // do the copying
      if (destbuffer)
	memcpy(destbuffer + copied, (*it).data() + offset, to_copy);
      maxlen -= to_copy;
      copied += to_copy;

      if ((*it).size() - offset > to_copy)
	{
	  // we did not copy everything
	  offset += to_copy;
	  break;
	}
      else
	{
	  // we copied everything
	  // discard this element;
	  offset = 0;
	  if (discard)
	    it = m_list.remove(it);
	  else
	    ++it;
	}
    }

  if (discard)
    {
      m_offset = offset;
      m_length -= copied;
      assert(m_length >= 0);
    }

  return copied;
}

void KSocketBuffer::clear()
{
  TQMutexLocker locker(&m_mutex);
  m_list.clear();
  m_offset = 0;
  m_length = 0;
}

TQ_LONG KSocketBuffer::sendTo(KActiveSocketBase* dev, TQ_LONG len)
{
  if (len == 0 || isEmpty())
    return 0;

  TQMutexLocker locker(&m_mutex);
  
  TQValueListIterator<TQByteArray> it = m_list.begin(),
    end = m_list.end();
  TQIODevice::Offset offset = m_offset;
  TQ_LONG written = 0;
  
  // walk the buffer
  while (it != end && (len || len == -1))
    {
      // we have to write each element up to len bytes
      // but since we can have several very small buffers, we can make things
      // better by concatenating a few of them into a big buffer
      // question is: how big should that buffer be? 2 kB should be enough

      TQ_ULONG bufsize = 1460;
      if (len != -1 && len < bufsize)
	bufsize = len;
      TQByteArray buf(bufsize);
      TQ_LONG count = 0;

      while (it != end && count + ((*it).size() - offset) <= bufsize)
	{
	  memcpy(buf.data() + count, (*it).data() + offset, (*it).size() - offset);
	  count += (*it).size() - offset;
	  offset = 0;
	  ++it;
	}

      // see if we can still fit more
      if (count < bufsize && it != end)
	{
	  // getting here means this buffer (*it) is larger than
	  // (bufsize - count) (even for count == 0).
	  memcpy(buf.data() + count, (*it).data() + offset, bufsize - count);
	  offset += bufsize - count;
	  count = bufsize;
	}

      // now try to write those bytes
      TQ_LONG wrote = dev->tqwriteBlock(buf, count);

      if (wrote == -1)
	// error?
	break;

      written += wrote;
      if (wrote != count)
	// can't fit more?
	break;
    }

  // discard data that has been written
  // this updates m_length too
  if (written)
    consumeBuffer(0L, written);

  return written;
}

TQ_LONG KSocketBuffer::receiveFrom(KActiveSocketBase* dev, TQ_LONG len)
{
  if (len == 0 || isFull())
    return 0;

  TQMutexLocker locker(&m_mutex);

  if (len == -1)
    len = dev->bytesAvailable();
  if (len <= 0)
    // error or closing socket
    return len;

  // see if we can read that much
  if (m_size != -1 && len > (m_size - m_length))
    len = m_size - m_length;

  // here, len contains just as many bytes as we're supposed to read

  // now do the reading
  TQByteArray a(len);
  len = dev->tqreadBlock(a.data(), len);

  if (len == -1)
    // error?
    return -1;

  // success
  // resize the buffer and add it
  a.truncate(len);
  m_list.append(a);
  m_length += len;
  return len;
}