Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 3c83713 | 2011-11-15 12:07:43 +0000 | [diff] [blame^] | 2 | * Copyright 2011 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
Adam Tkac | 8aee1a8 | 2009-09-04 12:08:56 +0000 | [diff] [blame] | 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
Adam Tkac | 49e5ce6 | 2008-11-14 12:25:34 +0000 | [diff] [blame] | 22 | #endif |
| 23 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #ifdef _WIN32 |
| 27 | #include <winsock2.h> |
| 28 | #define write(s,b,l) send(s,(const char*)b,l,0) |
| 29 | #define EWOULDBLOCK WSAEWOULDBLOCK |
| 30 | #undef errno |
| 31 | #define errno WSAGetLastError() |
| 32 | #undef EINTR |
| 33 | #define EINTR WSAEINTR |
| 34 | #else |
| 35 | #include <sys/types.h> |
| 36 | #include <errno.h> |
| 37 | #include <unistd.h> |
| 38 | #include <sys/time.h> |
| 39 | #endif |
| 40 | |
Adam Tkac | 49e5ce6 | 2008-11-14 12:25:34 +0000 | [diff] [blame] | 41 | /* Old systems have select() in sys/time.h */ |
| 42 | #ifdef HAVE_SYS_SELECT_H |
| 43 | #include <sys/select.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | #include <rdr/FdOutStream.h> |
| 47 | #include <rdr/Exception.h> |
Pierre Ossman | 3c83713 | 2011-11-15 12:07:43 +0000 | [diff] [blame^] | 48 | #include <rfb/util.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | using namespace rdr; |
| 52 | |
| 53 | enum { DEFAULT_BUF_SIZE = 16384 }; |
| 54 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 55 | FdOutStream::FdOutStream(int fd_, bool blocking_, int timeoutms_, int bufSize_) |
| 56 | : fd(fd_), blocking(blocking_), timeoutms(timeoutms_), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 57 | bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0) |
| 58 | { |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 59 | ptr = start = sentUpTo = new U8[bufSize]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 60 | end = start + bufSize; |
Pierre Ossman | 3c83713 | 2011-11-15 12:07:43 +0000 | [diff] [blame^] | 61 | |
| 62 | gettimeofday(&lastWrite, NULL); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | FdOutStream::~FdOutStream() |
| 66 | { |
| 67 | try { |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 68 | blocking = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 69 | flush(); |
| 70 | } catch (Exception&) { |
| 71 | } |
| 72 | delete [] start; |
| 73 | } |
| 74 | |
| 75 | void FdOutStream::setTimeout(int timeoutms_) { |
| 76 | timeoutms = timeoutms_; |
| 77 | } |
| 78 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 79 | void FdOutStream::setBlocking(bool blocking_) { |
| 80 | blocking = blocking_; |
| 81 | } |
| 82 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 83 | int FdOutStream::length() |
| 84 | { |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 85 | return offset + ptr - sentUpTo; |
| 86 | } |
| 87 | |
| 88 | int FdOutStream::bufferUsage() |
| 89 | { |
| 90 | return ptr - sentUpTo; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Pierre Ossman | 3c83713 | 2011-11-15 12:07:43 +0000 | [diff] [blame^] | 93 | unsigned FdOutStream::getIdleTime() |
| 94 | { |
| 95 | return rfb::msSince(&lastWrite); |
| 96 | } |
| 97 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 98 | void FdOutStream::flush() |
| 99 | { |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 100 | int timeoutms_; |
| 101 | |
| 102 | if (blocking) |
| 103 | timeoutms_ = timeoutms; |
| 104 | else |
| 105 | timeoutms_ = 0; |
| 106 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | while (sentUpTo < ptr) { |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 108 | int n = writeWithTimeout((const void*) sentUpTo, |
| 109 | ptr - sentUpTo, timeoutms_); |
| 110 | |
| 111 | // Timeout? |
| 112 | if (n == 0) { |
| 113 | // If non-blocking then we're done here |
| 114 | if (!blocking) |
| 115 | break; |
| 116 | |
| 117 | // Otherwise try blocking (with possible timeout) |
| 118 | if ((timeoutms_ == 0) && (timeoutms != 0)) { |
| 119 | timeoutms_ = timeoutms; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | // Proper timeout |
| 124 | throw TimedOut(); |
| 125 | } |
| 126 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 127 | sentUpTo += n; |
| 128 | offset += n; |
| 129 | } |
| 130 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 131 | // Managed to flush everything? |
| 132 | if (sentUpTo == ptr) |
| 133 | ptr = sentUpTo = start; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
| 137 | int FdOutStream::overrun(int itemSize, int nItems) |
| 138 | { |
| 139 | if (itemSize > bufSize) |
| 140 | throw Exception("FdOutStream overrun: max itemSize exceeded"); |
| 141 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 142 | // First try to get rid of the data we have |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 143 | flush(); |
| 144 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 145 | // Still not enough space? |
| 146 | if (itemSize > end - ptr) { |
| 147 | // Can we shuffle things around? |
| 148 | // (don't do this if it gains us less than 25%) |
| 149 | if ((sentUpTo - start > bufSize / 4) && |
| 150 | (itemSize < bufSize - (ptr - sentUpTo))) { |
| 151 | memmove(start, sentUpTo, ptr - sentUpTo); |
| 152 | ptr = start + (ptr - sentUpTo); |
| 153 | sentUpTo = start; |
| 154 | } else { |
| 155 | // Have to get rid of more data, so turn off non-blocking |
| 156 | // for a bit... |
| 157 | bool realBlocking; |
| 158 | |
| 159 | realBlocking = blocking; |
| 160 | blocking = true; |
| 161 | flush(); |
| 162 | blocking = realBlocking; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Can we fit all the items asked for? |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 167 | if (itemSize * nItems > end - ptr) |
| 168 | nItems = (end - ptr) / itemSize; |
| 169 | |
| 170 | return nItems; |
| 171 | } |
| 172 | |
| 173 | // |
| 174 | // writeWithTimeout() writes up to the given length in bytes from the given |
| 175 | // buffer to the file descriptor. If there is a timeout set and that timeout |
| 176 | // expires, it throws a TimedOut exception. Otherwise it returns the number of |
| 177 | // bytes written. It never attempts to write() unless select() indicates that |
| 178 | // the fd is writable - this means it can be used on an fd which has been set |
| 179 | // non-blocking. It also has to cope with the annoying possibility of both |
| 180 | // select() and write() returning EINTR. |
| 181 | // |
| 182 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 183 | int FdOutStream::writeWithTimeout(const void* data, int length, int timeoutms) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 184 | { |
| 185 | int n; |
| 186 | |
| 187 | do { |
| 188 | |
| 189 | do { |
| 190 | fd_set fds; |
| 191 | struct timeval tv; |
| 192 | struct timeval* tvp = &tv; |
| 193 | |
| 194 | if (timeoutms != -1) { |
| 195 | tv.tv_sec = timeoutms / 1000; |
| 196 | tv.tv_usec = (timeoutms % 1000) * 1000; |
| 197 | } else { |
| 198 | tvp = 0; |
| 199 | } |
| 200 | |
| 201 | FD_ZERO(&fds); |
| 202 | FD_SET(fd, &fds); |
| 203 | #ifdef _WIN32_WCE |
| 204 | // NB: This fixes a broken Winsock2 select() behaviour. select() |
| 205 | // never returns for non-blocking sockets, unless they're already |
| 206 | // ready to be written to... |
| 207 | u_long zero = 0; ioctlsocket(fd, FIONBIO, &zero); |
| 208 | #endif |
| 209 | n = select(fd+1, 0, &fds, 0, tvp); |
| 210 | #ifdef _WIN32_WCE |
| 211 | u_long one = 0; ioctlsocket(fd, FIONBIO, &one); |
| 212 | #endif |
| 213 | } while (n < 0 && errno == EINTR); |
| 214 | |
| 215 | if (n < 0) throw SystemException("select",errno); |
| 216 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 217 | if (n == 0) return 0; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 218 | |
| 219 | do { |
| 220 | n = ::write(fd, data, length); |
| 221 | } while (n < 0 && (errno == EINTR)); |
| 222 | |
| 223 | // NB: This outer loop simply fixes a broken Winsock2 EWOULDBLOCK |
| 224 | // condition, found only under Win98 (first edition), with slow |
| 225 | // network connections. Should in fact never ever happen... |
| 226 | } while (n < 0 && (errno == EWOULDBLOCK)); |
| 227 | |
| 228 | if (n < 0) throw SystemException("write",errno); |
| 229 | |
Pierre Ossman | 3c83713 | 2011-11-15 12:07:43 +0000 | [diff] [blame^] | 230 | gettimeofday(&lastWrite, NULL); |
| 231 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 232 | return n; |
| 233 | } |