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