blob: c84c946fa8dcd9d24547ac488ee945325602a50d [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
Adam Tkac8aee1a82009-09-04 12:08:56 +000019#ifdef HAVE_CONFIG_H
20#include <config.h>
Adam Tkac49e5ce62008-11-14 12:25:34 +000021#endif
22
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000023#include <stdio.h>
24#include <string.h>
25#ifdef _WIN32
26#include <winsock2.h>
27#define write(s,b,l) send(s,(const char*)b,l,0)
28#define EWOULDBLOCK WSAEWOULDBLOCK
29#undef errno
30#define errno WSAGetLastError()
31#undef EINTR
32#define EINTR WSAEINTR
33#else
34#include <sys/types.h>
35#include <errno.h>
36#include <unistd.h>
37#include <sys/time.h>
38#endif
39
Adam Tkac49e5ce62008-11-14 12:25:34 +000040/* Old systems have select() in sys/time.h */
41#ifdef HAVE_SYS_SELECT_H
42#include <sys/select.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000043#endif
44
45#include <rdr/FdOutStream.h>
46#include <rdr/Exception.h>
47
48
49using namespace rdr;
50
51enum { DEFAULT_BUF_SIZE = 16384 };
52
53FdOutStream::FdOutStream(int fd_, int timeoutms_, int bufSize_)
54 : fd(fd_), timeoutms(timeoutms_),
55 bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
56{
57 ptr = start = new U8[bufSize];
58 end = start + bufSize;
59}
60
61FdOutStream::~FdOutStream()
62{
63 try {
64 flush();
65 } catch (Exception&) {
66 }
67 delete [] start;
68}
69
70void FdOutStream::setTimeout(int timeoutms_) {
71 timeoutms = timeoutms_;
72}
73
74int FdOutStream::length()
75{
76 return offset + ptr - start;
77}
78
79void FdOutStream::flush()
80{
81 U8* sentUpTo = start;
82 while (sentUpTo < ptr) {
83 int n = writeWithTimeout((const void*) sentUpTo, ptr - sentUpTo);
84 sentUpTo += n;
85 offset += n;
86 }
87
88 ptr = start;
89}
90
91
92int FdOutStream::overrun(int itemSize, int nItems)
93{
94 if (itemSize > bufSize)
95 throw Exception("FdOutStream overrun: max itemSize exceeded");
96
97 flush();
98
99 if (itemSize * nItems > end - ptr)
100 nItems = (end - ptr) / itemSize;
101
102 return nItems;
103}
104
105//
106// writeWithTimeout() writes up to the given length in bytes from the given
107// buffer to the file descriptor. If there is a timeout set and that timeout
108// expires, it throws a TimedOut exception. Otherwise it returns the number of
109// bytes written. It never attempts to write() unless select() indicates that
110// the fd is writable - this means it can be used on an fd which has been set
111// non-blocking. It also has to cope with the annoying possibility of both
112// select() and write() returning EINTR.
113//
114
115int FdOutStream::writeWithTimeout(const void* data, int length)
116{
117 int n;
118
119 do {
120
121 do {
122 fd_set fds;
123 struct timeval tv;
124 struct timeval* tvp = &tv;
125
126 if (timeoutms != -1) {
127 tv.tv_sec = timeoutms / 1000;
128 tv.tv_usec = (timeoutms % 1000) * 1000;
129 } else {
130 tvp = 0;
131 }
132
133 FD_ZERO(&fds);
134 FD_SET(fd, &fds);
135#ifdef _WIN32_WCE
136 // NB: This fixes a broken Winsock2 select() behaviour. select()
137 // never returns for non-blocking sockets, unless they're already
138 // ready to be written to...
139 u_long zero = 0; ioctlsocket(fd, FIONBIO, &zero);
140#endif
141 n = select(fd+1, 0, &fds, 0, tvp);
142#ifdef _WIN32_WCE
143 u_long one = 0; ioctlsocket(fd, FIONBIO, &one);
144#endif
145 } while (n < 0 && errno == EINTR);
146
147 if (n < 0) throw SystemException("select",errno);
148
149 if (n == 0) throw TimedOut();
150
151 do {
152 n = ::write(fd, data, length);
153 } while (n < 0 && (errno == EINTR));
154
155 // NB: This outer loop simply fixes a broken Winsock2 EWOULDBLOCK
156 // condition, found only under Win98 (first edition), with slow
157 // network connections. Should in fact never ever happen...
158 } while (n < 0 && (errno == EWOULDBLOCK));
159
160 if (n < 0) throw SystemException("write",errno);
161
162 return n;
163}