blob: 29e864fc314c03b1b94786912112601b88590b99 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman3c837132011-11-15 12:07:43 +00002 * Copyright 2011 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
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 Tkac8aee1a82009-09-04 12:08:56 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
Adam Tkac49e5ce62008-11-14 12:25:34 +000022#endif
23
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000024#include <stdio.h>
25#include <string.h>
Pierre Ossman500cb6e2015-05-29 16:54:21 +020026#include <errno.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000027#ifdef _WIN32
28#include <winsock2.h>
29#define write(s,b,l) send(s,(const char*)b,l,0)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030#undef errno
31#define errno WSAGetLastError()
Peter Åstrand (astrand)11167e12015-02-05 11:10:32 +010032#include <os/winerrno.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000033#else
34#include <sys/types.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000035#include <unistd.h>
36#include <sys/time.h>
37#endif
38
Adam Tkac49e5ce62008-11-14 12:25:34 +000039/* Old systems have select() in sys/time.h */
40#ifdef HAVE_SYS_SELECT_H
41#include <sys/select.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042#endif
43
44#include <rdr/FdOutStream.h>
45#include <rdr/Exception.h>
Pierre Ossman3c837132011-11-15 12:07:43 +000046#include <rfb/util.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000047
48
49using namespace rdr;
50
51enum { DEFAULT_BUF_SIZE = 16384 };
52
Pierre Ossman4ce51ff2011-10-25 15:13:13 +000053FdOutStream::FdOutStream(int fd_, bool blocking_, int timeoutms_, int bufSize_)
54 : fd(fd_), blocking(blocking_), timeoutms(timeoutms_),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000055 bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
56{
Pierre Ossman4ce51ff2011-10-25 15:13:13 +000057 ptr = start = sentUpTo = new U8[bufSize];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000058 end = start + bufSize;
Pierre Ossman3c837132011-11-15 12:07:43 +000059
60 gettimeofday(&lastWrite, NULL);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000061}
62
63FdOutStream::~FdOutStream()
64{
65 try {
Pierre Ossman4ce51ff2011-10-25 15:13:13 +000066 blocking = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067 flush();
68 } catch (Exception&) {
69 }
70 delete [] start;
71}
72
73void FdOutStream::setTimeout(int timeoutms_) {
74 timeoutms = timeoutms_;
75}
76
Pierre Ossman4ce51ff2011-10-25 15:13:13 +000077void FdOutStream::setBlocking(bool blocking_) {
78 blocking = blocking_;
79}
80
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081int FdOutStream::length()
82{
Pierre Ossman4ce51ff2011-10-25 15:13:13 +000083 return offset + ptr - sentUpTo;
84}
85
86int FdOutStream::bufferUsage()
87{
88 return ptr - sentUpTo;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089}
90
Pierre Ossman3c837132011-11-15 12:07:43 +000091unsigned FdOutStream::getIdleTime()
92{
93 return rfb::msSince(&lastWrite);
94}
95
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096void FdOutStream::flush()
97{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098 while (sentUpTo < ptr) {
Pierre Ossman4ce51ff2011-10-25 15:13:13 +000099 int n = writeWithTimeout((const void*) sentUpTo,
Pierre Ossmanc6df31d2016-04-29 13:40:13 +0200100 ptr - sentUpTo,
101 blocking? timeoutms : 0);
Pierre Ossman4ce51ff2011-10-25 15:13:13 +0000102
103 // Timeout?
Pierre Ossmanb08b3d42016-10-10 16:05:46 +0200104 if (n == 0) {
105 // If non-blocking then we're done here
106 if (!blocking)
107 break;
108
Pierre Ossman4ce51ff2011-10-25 15:13:13 +0000109 throw TimedOut();
Pierre Ossmanb08b3d42016-10-10 16:05:46 +0200110 }
Pierre Ossman4ce51ff2011-10-25 15:13:13 +0000111
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112 sentUpTo += n;
113 offset += n;
114 }
115
Pierre Ossman4ce51ff2011-10-25 15:13:13 +0000116 // Managed to flush everything?
117 if (sentUpTo == ptr)
118 ptr = sentUpTo = start;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119}
120
121
122int FdOutStream::overrun(int itemSize, int nItems)
123{
124 if (itemSize > bufSize)
125 throw Exception("FdOutStream overrun: max itemSize exceeded");
126
Pierre Ossman4ce51ff2011-10-25 15:13:13 +0000127 // First try to get rid of the data we have
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000128 flush();
129
Pierre Ossman4ce51ff2011-10-25 15:13:13 +0000130 // Still not enough space?
131 if (itemSize > end - ptr) {
132 // Can we shuffle things around?
133 // (don't do this if it gains us less than 25%)
134 if ((sentUpTo - start > bufSize / 4) &&
135 (itemSize < bufSize - (ptr - sentUpTo))) {
136 memmove(start, sentUpTo, ptr - sentUpTo);
137 ptr = start + (ptr - sentUpTo);
138 sentUpTo = start;
139 } else {
140 // Have to get rid of more data, so turn off non-blocking
141 // for a bit...
142 bool realBlocking;
143
144 realBlocking = blocking;
145 blocking = true;
146 flush();
147 blocking = realBlocking;
148 }
149 }
150
151 // Can we fit all the items asked for?
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000152 if (itemSize * nItems > end - ptr)
153 nItems = (end - ptr) / itemSize;
154
155 return nItems;
156}
157
158//
159// writeWithTimeout() writes up to the given length in bytes from the given
160// buffer to the file descriptor. If there is a timeout set and that timeout
161// expires, it throws a TimedOut exception. Otherwise it returns the number of
162// bytes written. It never attempts to write() unless select() indicates that
163// the fd is writable - this means it can be used on an fd which has been set
164// non-blocking. It also has to cope with the annoying possibility of both
165// select() and write() returning EINTR.
166//
167
Pierre Ossman4ce51ff2011-10-25 15:13:13 +0000168int FdOutStream::writeWithTimeout(const void* data, int length, int timeoutms)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169{
170 int n;
171
172 do {
Pierre Ossman3b46a392016-04-29 13:37:55 +0200173 fd_set fds;
174 struct timeval tv;
175 struct timeval* tvp = &tv;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000176
Pierre Ossman3b46a392016-04-29 13:37:55 +0200177 if (timeoutms != -1) {
178 tv.tv_sec = timeoutms / 1000;
179 tv.tv_usec = (timeoutms % 1000) * 1000;
180 } else {
181 tvp = NULL;
182 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000183
Pierre Ossman3b46a392016-04-29 13:37:55 +0200184 FD_ZERO(&fds);
185 FD_SET(fd, &fds);
186 n = select(fd+1, 0, &fds, 0, tvp);
187 } while (n < 0 && errno == EINTR);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188
Pierre Ossman3b46a392016-04-29 13:37:55 +0200189 if (n < 0)
190 throw SystemException("select", errno);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191
Pierre Ossman3b46a392016-04-29 13:37:55 +0200192 if (n == 0)
193 return 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194
Pierre Ossman3b46a392016-04-29 13:37:55 +0200195 do {
196 n = ::write(fd, data, length);
197 } while (n < 0 && (errno == EINTR));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000198
Pierre Ossman3b46a392016-04-29 13:37:55 +0200199 if (n < 0)
200 throw SystemException("write", errno);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201
Pierre Ossman3c837132011-11-15 12:07:43 +0000202 gettimeofday(&lastWrite, NULL);
203
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204 return n;
205}