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 |
Peter Åstrand (astrand) | 3a1db16 | 2017-10-16 11:11:45 +0200 | [diff] [blame^] | 3 | * Copyright 2017 Peter Astrand <astrand@cendio.se> for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 4 | * |
| 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 18 | * USA. |
| 19 | */ |
| 20 | |
Adam Tkac | 8aee1a8 | 2009-09-04 12:08:56 +0000 | [diff] [blame] | 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
Adam Tkac | 49e5ce6 | 2008-11-14 12:25:34 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <string.h> |
Pierre Ossman | 500cb6e | 2015-05-29 16:54:21 +0200 | [diff] [blame] | 27 | #include <errno.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 28 | #ifdef _WIN32 |
| 29 | #include <winsock2.h> |
| 30 | #define write(s,b,l) send(s,(const char*)b,l,0) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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> |
| 37 | #include <sys/time.h> |
Peter Åstrand (astrand) | e2c3b60 | 2017-10-10 13:56:51 +0200 | [diff] [blame] | 38 | #include <sys/socket.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 100 | while (sentUpTo < ptr) { |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 101 | int n = writeWithTimeout((const void*) sentUpTo, |
Pierre Ossman | c6df31d | 2016-04-29 13:40:13 +0200 | [diff] [blame] | 102 | ptr - sentUpTo, |
| 103 | blocking? timeoutms : 0); |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 104 | |
| 105 | // Timeout? |
Pierre Ossman | b08b3d4 | 2016-10-10 16:05:46 +0200 | [diff] [blame] | 106 | if (n == 0) { |
| 107 | // If non-blocking then we're done here |
| 108 | if (!blocking) |
| 109 | break; |
| 110 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 111 | throw TimedOut(); |
Pierre Ossman | b08b3d4 | 2016-10-10 16:05:46 +0200 | [diff] [blame] | 112 | } |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 113 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 114 | sentUpTo += n; |
| 115 | offset += n; |
| 116 | } |
| 117 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 118 | // Managed to flush everything? |
| 119 | if (sentUpTo == ptr) |
| 120 | ptr = sentUpTo = start; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
| 124 | int FdOutStream::overrun(int itemSize, int nItems) |
| 125 | { |
| 126 | if (itemSize > bufSize) |
| 127 | throw Exception("FdOutStream overrun: max itemSize exceeded"); |
| 128 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 129 | // First try to get rid of the data we have |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 130 | flush(); |
| 131 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 132 | // Still not enough space? |
| 133 | if (itemSize > end - ptr) { |
| 134 | // Can we shuffle things around? |
| 135 | // (don't do this if it gains us less than 25%) |
| 136 | if ((sentUpTo - start > bufSize / 4) && |
| 137 | (itemSize < bufSize - (ptr - sentUpTo))) { |
| 138 | memmove(start, sentUpTo, ptr - sentUpTo); |
| 139 | ptr = start + (ptr - sentUpTo); |
| 140 | sentUpTo = start; |
| 141 | } else { |
| 142 | // Have to get rid of more data, so turn off non-blocking |
| 143 | // for a bit... |
| 144 | bool realBlocking; |
| 145 | |
| 146 | realBlocking = blocking; |
| 147 | blocking = true; |
| 148 | flush(); |
| 149 | blocking = realBlocking; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Can we fit all the items asked for? |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 154 | if (itemSize * nItems > end - ptr) |
| 155 | nItems = (end - ptr) / itemSize; |
| 156 | |
| 157 | return nItems; |
| 158 | } |
| 159 | |
| 160 | // |
| 161 | // writeWithTimeout() writes up to the given length in bytes from the given |
| 162 | // buffer to the file descriptor. If there is a timeout set and that timeout |
| 163 | // expires, it throws a TimedOut exception. Otherwise it returns the number of |
| 164 | // bytes written. It never attempts to write() unless select() indicates that |
| 165 | // the fd is writable - this means it can be used on an fd which has been set |
| 166 | // non-blocking. It also has to cope with the annoying possibility of both |
| 167 | // select() and write() returning EINTR. |
| 168 | // |
| 169 | |
Pierre Ossman | 4ce51ff | 2011-10-25 15:13:13 +0000 | [diff] [blame] | 170 | int FdOutStream::writeWithTimeout(const void* data, int length, int timeoutms) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 171 | { |
| 172 | int n; |
| 173 | |
| 174 | do { |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 175 | fd_set fds; |
| 176 | struct timeval tv; |
| 177 | struct timeval* tvp = &tv; |
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 | if (timeoutms != -1) { |
| 180 | tv.tv_sec = timeoutms / 1000; |
| 181 | tv.tv_usec = (timeoutms % 1000) * 1000; |
| 182 | } else { |
| 183 | tvp = NULL; |
| 184 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 185 | |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 186 | FD_ZERO(&fds); |
| 187 | FD_SET(fd, &fds); |
| 188 | n = select(fd+1, 0, &fds, 0, tvp); |
| 189 | } while (n < 0 && errno == EINTR); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 190 | |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 191 | if (n < 0) |
| 192 | throw SystemException("select", errno); |
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 | return 0; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 196 | |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 197 | do { |
Peter Åstrand (astrand) | e2c3b60 | 2017-10-10 13:56:51 +0200 | [diff] [blame] | 198 | // select only guarantees that you can write SO_SNDLOWAT without |
| 199 | // blocking, which is normally 1. Use MSG_DONTWAIT to avoid |
| 200 | // blocking, when possible. |
| 201 | #ifndef MSG_DONTWAIT |
| 202 | n = ::send(fd, (const char*)data, length, 0); |
| 203 | #else |
| 204 | n = ::send(fd, (const char*)data, length, MSG_DONTWAIT); |
| 205 | #endif |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 206 | } while (n < 0 && (errno == EINTR)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 207 | |
Pierre Ossman | 3b46a39 | 2016-04-29 13:37:55 +0200 | [diff] [blame] | 208 | if (n < 0) |
| 209 | throw SystemException("write", errno); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 210 | |
Pierre Ossman | 3c83713 | 2011-11-15 12:07:43 +0000 | [diff] [blame] | 211 | gettimeofday(&lastWrite, NULL); |
| 212 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 213 | return n; |
| 214 | } |