FdInStream.cxx
9.32 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright (C) 2002 Ultr@VNC Team Members. All Rights Reserved.
// Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this software; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#ifdef _WIN32
#include <winsock.h>
#include <sys/timeb.h>
#define read(s,b,l) recv(s,(char*)b,l,0)
#undef errno
#define errno WSAGetLastError()
#else
#include <unistd.h>
#include <sys/time.h>
#endif
// XXX should use autoconf HAVE_SYS_SELECT_H
#ifdef _AIX
#include <sys/select.h>
#endif
#include "FdInStream.h"
#include "Exception.h"
using namespace rdr;
enum { DEFAULT_BUF_SIZE = 8192,
MIN_BULK_SIZE = 1024 };
FdInStream::FdInStream(int fd_, int timeout_, int bufSize_)
: fd(fd_), timeout(timeout_), blockCallback(0), blockCallbackArg(0),
timing(false), timeWaitedIn100us(5), timedKbits(0),
bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
{
ptr = end = start = new U8[bufSize];
// sf@2002
m_fDSMMode = false;
m_fReadFromNetRectBuf = false;
m_nNetRectBufOffset = 0;
m_nReadSize = 0;
m_nBytesRead = 0; // For stats
}
FdInStream::FdInStream(int fd_, void (*blockCallback_)(void*),
void* blockCallbackArg_, int bufSize_)
: fd(fd_), timeout(0), blockCallback(blockCallback_),
blockCallbackArg(blockCallbackArg_),
timing(false), timeWaitedIn100us(5), timedKbits(0),
bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
{
ptr = end = start = new U8[bufSize];
// sf@2002
m_fDSMMode = false;
m_fReadFromNetRectBuf = false;
m_nNetRectBufOffset = 0;
m_nReadSize = 0;
}
FdInStream::~FdInStream()
{
delete [] start;
}
int FdInStream::pos()
{
return offset + ptr - start;
}
void FdInStream::readBytes(void* data, int length)
{
// sf@2003 - Seems to fix the ZRLE+DSM bug...
if (!m_fDSMMode)
{
if (length < MIN_BULK_SIZE)
{
InStream::readBytes(data, length);
return;
}
}
U8* dataPtr = (U8*)data;
int n = end - ptr;
if (n > length) n = length;
memcpy(dataPtr, ptr, n);
dataPtr += n;
length -= n;
ptr += n;
while (length > 0) {
n = readWithTimeoutOrCallback(dataPtr, length);
dataPtr += n;
length -= n;
offset += n;
}
}
int FdInStream::overrun(int itemSize, int nItems)
{
if (itemSize > bufSize)
throw Exception("FdInStream overrun: max itemSize exceeded");
if (end - ptr != 0)
memmove(start, ptr, end - ptr);
offset += ptr - start;
end -= ptr - start;
ptr = start;
while (end < start + itemSize) {
int n = readWithTimeoutOrCallback((U8*)end, start + bufSize - end);
end += n;
}
if (itemSize * nItems > end - ptr)
nItems = (end - ptr) / itemSize;
return nItems;
}
int FdInStream::Check_if_buffer_has_data()
{
InStream::setptr(InStream::getend());
return checkReadable(fd, 500);
}
int FdInStream::checkReadable(int fd, int timeout)
{
while (true) {
fd_set rfds;
struct timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
int n = select(fd+1, &rfds, 0, 0, &tv);
if (n != -1 || errno != EINTR)
return n;
fprintf(stderr,"select returned EINTR\n");
}
}
/*#ifdef _WIN32
static void gettimeofday_(struct timeval* tv, void*)
{
LARGE_INTEGER counts, countsPerSec;
static double usecPerCount = 0.0;
counts.QuadPart=0;
if (QueryPerformanceCounter(&counts)) {
if (usecPerCount == 0.0) {
QueryPerformanceFrequency(&countsPerSec);
if (countsPerSec.QuadPart!=0)
usecPerCount = 1000000.0 / countsPerSec.QuadPart;
else usecPerCount = 200;
}
LONGLONG usecs = (LONGLONG)(counts.QuadPart * usecPerCount);
tv->tv_usec = (long)((LONGLONG)usecs % (LONGLONG)1000000);
tv->tv_sec = (long)(usecs / 1000000);
} else {
struct timeb tb;
ftime(&tb);
tv->tv_sec = (long)tb.time;
tv->tv_usec = tb.millitm * 1000;
}
}
#endif*/
#ifdef _WIN32
LONGLONG
Passedusecs()
{
LARGE_INTEGER counts, countsPerSec;
static LONGLONG usecPerCount = 0;
LONGLONG usecs=0;
if (QueryPerformanceCounter(&counts)) {
if (usecPerCount == 0) {
QueryPerformanceFrequency(&countsPerSec);
usecPerCount = 1000000000000000000 / countsPerSec.QuadPart;
}
usecs = (LONGLONG)(counts.QuadPart * usecPerCount /1000000000000);
} else {
struct timeb tb;
ftime(&tb);
usecs=tb.time*1000000+tb.millitm * 1000;
}
return usecs;
}
#endif
int FdInStream::readWithTimeoutOrCallback(void* buf, int len)
{
/*struct timeval before = {0, 0}, after; // before will not get initialized if the condition is false
if (timing)
gettimeofday_(&before, NULL);*/
LONGLONG before =0, after; // before will not get initialized if the condition is false
if (timing)
before=Passedusecs();
int n=0;
if (!m_fReadFromNetRectBuf)
{
n = checkReadable(fd, timeout);
if (n < 0) throw SystemException("select",errno);
if (n == 0)
{
if (timeout) throw TimedOut();
if (blockCallback) (*blockCallback)(blockCallbackArg);
}
}
bool fAlreadyCounted = false; // sf@2004 - Avoid to count the plugin processed bytes twice...
while (true)
{
// sf@2002 - DSM Plugin hack - Only necessary for ZRLE encoding
// If we must read already restored data from DSMPLugin memory
if (m_fReadFromNetRectBuf)
{
int nRem = m_nReadSize - m_nNetRectBufOffset;
int nRead = (len <= nRem) ? len : nRem;
memcpy(buf, m_pNetRectBuf + m_nNetRectBufOffset, nRead);
m_nNetRectBufOffset += nRead;
if (m_nNetRectBufOffset == m_nReadSize)
{
// Next read calls should read the socket
m_fReadFromNetRectBuf = false;
m_nNetRectBufOffset = 0;
m_nReadSize = 0;
}
n = nRead;
fAlreadyCounted = true;
}
else
{
n = ::read(fd, buf, len);
}
if (n != -1 || errno != EINTR)
break;
fprintf(stderr,"read returned EINTR\n");
}
if (n < 0) throw SystemException("read",errno);
if (n == 0) throw EndOfStream();
if (fAlreadyCounted)
return n;
// sf@2002 - stats
m_nBytesRead += n;
if (timing)
{
after=Passedusecs();
LONGLONG newTimeWaited = (after- before)/100;
int newKbits = n * 8 / 1000;
if (newTimeWaited > newKbits*1000) newTimeWaited = newKbits*1000;
if (newTimeWaited < newKbits/4) newTimeWaited = newKbits/4;
timeWaitedIn100us += (unsigned int)newTimeWaited;
timedKbits += newKbits;
}
/*if (timing)
{
gettimeofday_(&after, 0);
// fprintf(stderr,"%d.%06d\n",(after.tv_sec - before.tv_sec),
// (after.tv_usec - before.tv_usec));
int newTimeWaited = ((after.tv_sec - before.tv_sec) * 10000 +
(after.tv_usec - before.tv_usec) / 100);
int newKbits = n * 8 / 1000;
// if (newTimeWaited == 0) {
// fprintf(stderr,"new kbps infinite t %d k %d\n",
// newTimeWaited, newKbits);
// } else {
// fprintf(stderr,"new kbps %d t %d k %d\n",
// newKbits * 10000 / newTimeWaited, newTimeWaited, newKbits);
// }
// limit rate to between 10kbit/s and 40Mbit/s
if (newTimeWaited > newKbits*1000) newTimeWaited = newKbits*1000;
if (newTimeWaited < newKbits/4) newTimeWaited = newKbits/4;
timeWaitedIn100us += newTimeWaited;
timedKbits += newKbits;
}*/
return n;
}
void FdInStream::startTiming()
{
timing = true;
// Carry over up to 1s worth of previous rate for smoothing.
if (timeWaitedIn100us > 10000) {
timedKbits = timedKbits * 10000 / timeWaitedIn100us;
timeWaitedIn100us = 10000;
}
}
void FdInStream::stopTiming()
{
timing = false;
if (timeWaitedIn100us < timedKbits/2)
timeWaitedIn100us = timedKbits/2; // upper limit 20Mbit/s
}
unsigned int FdInStream::kbitsPerSecond()
{
// The following calculation will overflow 32-bit arithmetic if we have
// received more than about 50Mbytes (400Mbits) since we started timing, so
// it should be OK for a single RFB update.
return timedKbits * 10000 / timeWaitedIn100us;
}
//
// sf@2002 - DSMPlugin - This hack is necessary because
// the ZRLE encoder/decoder does not use the SendExact/ReadExact functions
// like ALL the others encoders...
//
void FdInStream::SetReadFromMemoryBuffer(int nReadSize, char* pMemBuffer)
{
m_nReadSize = nReadSize; // Nb of bytes to read from buffer instead of socket
m_pNetRectBuf = pMemBuffer; // The memory buffer containing restored data
m_nNetRectBufOffset = 0; // Initial offset
m_fReadFromNetRectBuf = true; // Order to read from the buffer
}