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> |
Pierre Ossman | 500cb6e | 2015-05-29 16:54:21 +0200 | [diff] [blame] | 26 | #include <errno.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 27 | #ifdef _WIN32 |
| 28 | #include <winsock2.h> |
| 29 | #define write(s,b,l) send(s,(const char*)b,l,0) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 30 | #undef errno |
| 31 | #define errno WSAGetLastError() |
Peter Åstrand (astrand) | 11167e1 | 2015-02-05 11:10:32 +0100 | [diff] [blame] | 32 | #include <os/winerrno.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 33 | #else |
| 34 | #include <sys/types.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 35 | #include <unistd.h> |
| 36 | #include <sys/time.h> |
| 37 | #endif |
| 38 | |
Adam Tkac | 49e5ce6 | 2008-11-14 12:25:34 +0000 | [diff] [blame] | 39 | /* Old systems have select() in sys/time.h */ |
| 40 | #ifdef HAVE_SYS_SELECT_H |
| 41 | #include <sys/select.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | #include <rdr/FdOutStream.h> |
| 45 | #include <rdr/Exception.h> |
Pierre Ossman | 3c83713 | 2011-11-15 12:07:43 +0000 | [diff] [blame] | 46 | #include <rfb/util.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 47 | |
| 48 | |
| 49 | using namespace rdr; |
| 50 | |
| 51 | enum { DEFAULT_BUF_SIZE = 16384 }; |
| 52 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 53 | FdOutStream::FdOutStream(int fd_, bool blocking_, int timeoutms_, int bufSize_) |
| 54 | : fd(fd_), blocking(blocking_), timeoutms(timeoutms_), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 55 | bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0) |
| 56 | { |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 57 | ptr = start = sentUpTo = new U8[bufSize]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 58 | end = start + bufSize; |
Pierre Ossman | 3c83713 | 2011-11-15 12:07:43 +0000 | [diff] [blame] | 59 | |
| 60 | gettimeofday(&lastWrite, NULL); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | FdOutStream::~FdOutStream() |
| 64 | { |
| 65 | try { |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 66 | blocking = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 67 | flush(); |
| 68 | } catch (Exception&) { |
| 69 | } |
| 70 | delete [] start; |
| 71 | } |
| 72 | |
| 73 | void FdOutStream::setTimeout(int timeoutms_) { |
| 74 | timeoutms = timeoutms_; |
| 75 | } |
| 76 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 77 | void FdOutStream::setBlocking(bool blocking_) { |
| 78 | blocking = blocking_; |
| 79 | } |
| 80 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 81 | int FdOutStream::length() |
| 82 | { |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 83 | return offset + ptr - sentUpTo; |
| 84 | } |
| 85 | |
| 86 | int FdOutStream::bufferUsage() |
| 87 | { |
| 88 | return ptr - sentUpTo; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Pierre Ossman | 3c83713 | 2011-11-15 12:07:43 +0000 | [diff] [blame] | 91 | unsigned FdOutStream::getIdleTime() |
| 92 | { |
| 93 | return rfb::msSince(&lastWrite); |
| 94 | } |
| 95 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 96 | void FdOutStream::flush() |
| 97 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 98 | while (sentUpTo < ptr) { |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 99 | int n = writeWithTimeout((const void*) sentUpTo, |
Pierre Ossman | c6df31d | 2016-04-29 13:40:13 +0200 | [diff] [blame] | 100 | ptr - sentUpTo, |
| 101 | blocking? timeoutms : 0); |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 102 | |
| 103 | // Timeout? |
Pierre Ossman | c6df31d | 2016-04-29 13:40:13 +0200 | [diff] [blame] | 104 | if ((n == 0) && blocking) |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 105 | throw TimedOut(); |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 106 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | sentUpTo += n; |
| 108 | offset += n; |
| 109 | } |
| 110 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 111 | // Managed to flush everything? |
| 112 | if (sentUpTo == ptr) |
| 113 | ptr = sentUpTo = start; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
| 117 | int FdOutStream::overrun(int itemSize, int nItems) |
| 118 | { |
| 119 | if (itemSize > bufSize) |
| 120 | throw Exception("FdOutStream overrun: max itemSize exceeded"); |
| 121 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 122 | // First try to get rid of the data we have |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 123 | flush(); |
| 124 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 125 | // Still not enough space? |
| 126 | if (itemSize > end - ptr) { |
| 127 | // Can we shuffle things around? |
| 128 | // (don't do this if it gains us less than 25%) |
| 129 | if ((sentUpTo - start > bufSize / 4) && |
| 130 | (itemSize < bufSize - (ptr - sentUpTo))) { |
| 131 | memmove(start, sentUpTo, ptr - sentUpTo); |
| 132 | ptr = start + (ptr - sentUpTo); |
| 133 | sentUpTo = start; |
| 134 | } else { |
| 135 | // Have to get rid of more data, so turn off non-blocking |
| 136 | // for a bit... |
| 137 | bool realBlocking; |
| 138 | |
| 139 | realBlocking = blocking; |
| 140 | blocking = true; |
| 141 | flush(); |
| 142 | blocking = realBlocking; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Can we fit all the items asked for? |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 147 | if (itemSize * nItems > end - ptr) |
| 148 | nItems = (end - ptr) / itemSize; |
| 149 | |
| 150 | return nItems; |
| 151 | } |
| 152 | |
| 153 | // |
| 154 | // writeWithTimeout() writes up to the given length in bytes from the given |
| 155 | // buffer to the file descriptor. If there is a timeout set and that timeout |
| 156 | // expires, it throws a TimedOut exception. Otherwise it returns the number of |
| 157 | // bytes written. It never attempts to write() unless select() indicates that |
| 158 | // the fd is writable - this means it can be used on an fd which has been set |
| 159 | // non-blocking. It also has to cope with the annoying possibility of both |
| 160 | // select() and write() returning EINTR. |
| 161 | // |
| 162 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 163 | int FdOutStream::writeWithTimeout(const void* data, int length, int timeoutms) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 164 | { |
| 165 | int n; |
| 166 | |
| 167 | do { |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 168 | fd_set fds; |
| 169 | struct timeval tv; |
| 170 | struct timeval* tvp = &tv; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 171 | |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 172 | if (timeoutms != -1) { |
| 173 | tv.tv_sec = timeoutms / 1000; |
| 174 | tv.tv_usec = (timeoutms % 1000) * 1000; |
| 175 | } else { |
| 176 | tvp = NULL; |
| 177 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 178 | |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 179 | FD_ZERO(&fds); |
| 180 | FD_SET(fd, &fds); |
| 181 | n = select(fd+1, 0, &fds, 0, tvp); |
| 182 | } while (n < 0 && errno == EINTR); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 183 | |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 184 | if (n < 0) |
| 185 | throw SystemException("select", errno); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 186 | |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 187 | if (n == 0) |
| 188 | return 0; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 189 | |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 190 | do { |
| 191 | n = ::write(fd, data, length); |
| 192 | } while (n < 0 && (errno == EINTR)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 193 | |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 194 | if (n < 0) |
| 195 | throw SystemException("write", errno); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 196 | |
Pierre Ossman | 3c83713 | 2011-11-15 12:07:43 +0000 | [diff] [blame] | 197 | gettimeofday(&lastWrite, NULL); |
| 198 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 199 | return n; |
| 200 | } |