Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
Adam Tkac | 8aee1a8 | 2009-09-04 12:08:56 +0000 | [diff] [blame] | 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
Adam Tkac | 49e5ce6 | 2008-11-14 12:25:34 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <string.h> |
Pierre Ossman | 500cb6e | 2015-05-29 16:54:21 +0200 | [diff] [blame] | 25 | #include <errno.h> |
Pierre Ossman | 9c181ba | 2015-11-09 15:27:54 +0100 | [diff] [blame] | 26 | #include <sys/time.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 27 | #ifdef _WIN32 |
| 28 | #include <winsock2.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 29 | #define read(s,b,l) recv(s,(char*)b,l,0) |
| 30 | #define close closesocket |
| 31 | #undef errno |
| 32 | #define errno WSAGetLastError() |
Peter Åstrand (astrand) | 11167e1 | 2015-02-05 11:10:32 +0100 | [diff] [blame] | 33 | #include <os/winerrno.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 34 | #else |
| 35 | #include <sys/types.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 36 | #include <unistd.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | #ifndef vncmin |
| 40 | #define vncmin(a,b) (((a) < (b)) ? (a) : (b)) |
| 41 | #endif |
| 42 | #ifndef vncmax |
| 43 | #define vncmax(a,b) (((a) > (b)) ? (a) : (b)) |
| 44 | #endif |
| 45 | |
Adam Tkac | 49e5ce6 | 2008-11-14 12:25:34 +0000 | [diff] [blame] | 46 | /* Old systems have select() in sys/time.h */ |
| 47 | #ifdef HAVE_SYS_SELECT_H |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 48 | #include <sys/select.h> |
| 49 | #endif |
| 50 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 51 | #include <rdr/FdInStream.h> |
| 52 | #include <rdr/Exception.h> |
| 53 | |
| 54 | using namespace rdr; |
| 55 | |
| 56 | enum { DEFAULT_BUF_SIZE = 8192, |
| 57 | MIN_BULK_SIZE = 1024 }; |
| 58 | |
| 59 | FdInStream::FdInStream(int fd_, int timeoutms_, int bufSize_, |
| 60 | bool closeWhenDone_) |
| 61 | : fd(fd_), closeWhenDone(closeWhenDone_), |
| 62 | timeoutms(timeoutms_), blockCallback(0), |
| 63 | timing(false), timeWaitedIn100us(5), timedKbits(0), |
| 64 | bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0) |
| 65 | { |
| 66 | ptr = end = start = new U8[bufSize]; |
| 67 | } |
| 68 | |
| 69 | FdInStream::FdInStream(int fd_, FdInStreamBlockCallback* blockCallback_, |
| 70 | int bufSize_) |
| 71 | : fd(fd_), timeoutms(0), blockCallback(blockCallback_), |
| 72 | timing(false), timeWaitedIn100us(5), timedKbits(0), |
| 73 | bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0) |
| 74 | { |
| 75 | ptr = end = start = new U8[bufSize]; |
| 76 | } |
| 77 | |
| 78 | FdInStream::~FdInStream() |
| 79 | { |
| 80 | delete [] start; |
| 81 | if (closeWhenDone) close(fd); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | void FdInStream::setTimeout(int timeoutms_) { |
| 86 | timeoutms = timeoutms_; |
| 87 | } |
| 88 | |
| 89 | void FdInStream::setBlockCallback(FdInStreamBlockCallback* blockCallback_) |
| 90 | { |
| 91 | blockCallback = blockCallback_; |
| 92 | timeoutms = 0; |
| 93 | } |
| 94 | |
| 95 | int FdInStream::pos() |
| 96 | { |
| 97 | return offset + ptr - start; |
| 98 | } |
| 99 | |
| 100 | void FdInStream::readBytes(void* data, int length) |
| 101 | { |
| 102 | if (length < MIN_BULK_SIZE) { |
| 103 | InStream::readBytes(data, length); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | U8* dataPtr = (U8*)data; |
| 108 | |
| 109 | int n = end - ptr; |
| 110 | if (n > length) n = length; |
| 111 | |
| 112 | memcpy(dataPtr, ptr, n); |
| 113 | dataPtr += n; |
| 114 | length -= n; |
| 115 | ptr += n; |
| 116 | |
| 117 | while (length > 0) { |
| 118 | n = readWithTimeoutOrCallback(dataPtr, length); |
| 119 | dataPtr += n; |
| 120 | length -= n; |
| 121 | offset += n; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | int FdInStream::overrun(int itemSize, int nItems, bool wait) |
| 127 | { |
| 128 | if (itemSize > bufSize) |
| 129 | throw Exception("FdInStream overrun: max itemSize exceeded"); |
| 130 | |
| 131 | if (end - ptr != 0) |
| 132 | memmove(start, ptr, end - ptr); |
| 133 | |
| 134 | offset += ptr - start; |
| 135 | end -= ptr - start; |
| 136 | ptr = start; |
| 137 | |
| 138 | int bytes_to_read; |
| 139 | while (end < start + itemSize) { |
| 140 | bytes_to_read = start + bufSize - end; |
| 141 | if (!timing) { |
| 142 | // When not timing, we must be careful not to read too much |
| 143 | // extra data into the buffer. Otherwise, the line speed |
| 144 | // estimation might stay at zero for a long time: All reads |
| 145 | // during timing=1 can be satisfied without calling |
| 146 | // readWithTimeoutOrCallback. However, reading only 1 or 2 bytes |
| 147 | // bytes is ineffecient. |
| 148 | bytes_to_read = vncmin(bytes_to_read, vncmax(itemSize*nItems, 8)); |
| 149 | } |
| 150 | int n = readWithTimeoutOrCallback((U8*)end, bytes_to_read, wait); |
| 151 | if (n == 0) return 0; |
| 152 | end += n; |
| 153 | } |
| 154 | |
| 155 | if (itemSize * nItems > end - ptr) |
| 156 | nItems = (end - ptr) / itemSize; |
| 157 | |
| 158 | return nItems; |
| 159 | } |
| 160 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 161 | // |
| 162 | // readWithTimeoutOrCallback() reads up to the given length in bytes from the |
| 163 | // file descriptor into a buffer. If the wait argument is false, then zero is |
| 164 | // returned if no bytes can be read without blocking. Otherwise if a |
| 165 | // blockCallback is set, it will be called (repeatedly) instead of blocking. |
| 166 | // If alternatively there is a timeout set and that timeout expires, it throws |
| 167 | // a TimedOut exception. Otherwise it returns the number of bytes read. It |
| 168 | // never attempts to read() unless select() indicates that the fd is readable - |
| 169 | // this means it can be used on an fd which has been set non-blocking. It also |
| 170 | // has to cope with the annoying possibility of both select() and read() |
| 171 | // returning EINTR. |
| 172 | // |
| 173 | |
| 174 | int FdInStream::readWithTimeoutOrCallback(void* buf, int len, bool wait) |
| 175 | { |
| 176 | struct timeval before, after; |
| 177 | if (timing) |
| 178 | gettimeofday(&before, 0); |
| 179 | |
| 180 | int n; |
| 181 | while (true) { |
| 182 | do { |
| 183 | fd_set fds; |
| 184 | struct timeval tv; |
| 185 | struct timeval* tvp = &tv; |
| 186 | |
| 187 | if (!wait) { |
| 188 | tv.tv_sec = tv.tv_usec = 0; |
| 189 | } else if (timeoutms != -1) { |
| 190 | tv.tv_sec = timeoutms / 1000; |
| 191 | tv.tv_usec = (timeoutms % 1000) * 1000; |
| 192 | } else { |
| 193 | tvp = 0; |
| 194 | } |
| 195 | |
| 196 | FD_ZERO(&fds); |
| 197 | FD_SET(fd, &fds); |
| 198 | n = select(fd+1, &fds, 0, 0, tvp); |
| 199 | } while (n < 0 && errno == EINTR); |
| 200 | |
| 201 | if (n > 0) break; |
| 202 | if (n < 0) throw SystemException("select",errno); |
| 203 | if (!wait) return 0; |
| 204 | if (!blockCallback) throw TimedOut(); |
| 205 | |
| 206 | blockCallback->blockCallback(); |
| 207 | } |
| 208 | |
| 209 | do { |
| 210 | n = ::read(fd, buf, len); |
| 211 | } while (n < 0 && errno == EINTR); |
| 212 | |
| 213 | if (n < 0) throw SystemException("read",errno); |
| 214 | if (n == 0) throw EndOfStream(); |
| 215 | |
| 216 | if (timing) { |
| 217 | gettimeofday(&after, 0); |
| 218 | // fprintf(stderr,"%d.%06d\n",(after.tv_sec - before.tv_sec), |
| 219 | // (after.tv_usec - before.tv_usec)); |
| 220 | int newTimeWaited = ((after.tv_sec - before.tv_sec) * 10000 + |
| 221 | (after.tv_usec - before.tv_usec) / 100); |
| 222 | int newKbits = n * 8 / 1000; |
| 223 | |
| 224 | // if (newTimeWaited == 0) { |
| 225 | // fprintf(stderr,"new kbps infinite t %d k %d\n", |
| 226 | // newTimeWaited, newKbits); |
| 227 | // } else { |
| 228 | // fprintf(stderr,"new kbps %d t %d k %d\n", |
| 229 | // newKbits * 10000 / newTimeWaited, newTimeWaited, newKbits); |
| 230 | // } |
| 231 | |
| 232 | // limit rate to between 10kbit/s and 40Mbit/s |
| 233 | |
| 234 | if (newTimeWaited > newKbits*1000) newTimeWaited = newKbits*1000; |
| 235 | if (newTimeWaited < newKbits/4) newTimeWaited = newKbits/4; |
| 236 | |
| 237 | timeWaitedIn100us += newTimeWaited; |
| 238 | timedKbits += newKbits; |
| 239 | } |
| 240 | |
| 241 | return n; |
| 242 | } |
| 243 | |
| 244 | void FdInStream::startTiming() |
| 245 | { |
| 246 | timing = true; |
| 247 | |
| 248 | // Carry over up to 1s worth of previous rate for smoothing. |
| 249 | |
| 250 | if (timeWaitedIn100us > 10000) { |
| 251 | timedKbits = timedKbits * 10000 / timeWaitedIn100us; |
| 252 | timeWaitedIn100us = 10000; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void FdInStream::stopTiming() |
| 257 | { |
| 258 | timing = false; |
| 259 | if (timeWaitedIn100us < timedKbits/2) |
| 260 | timeWaitedIn100us = timedKbits/2; // upper limit 20Mbit/s |
| 261 | } |
| 262 | |
| 263 | unsigned int FdInStream::kbitsPerSecond() |
| 264 | { |
| 265 | // The following calculation will overflow 32-bit arithmetic if we have |
| 266 | // received more than about 50Mbytes (400Mbits) since we started timing, so |
| 267 | // it should be OK for a single RFB update. |
| 268 | |
| 269 | return timedKbits * 10000 / timeWaitedIn100us; |
| 270 | } |