blob: 07ac04c206ed01dd1ceb429a8c7875ead59b7a9d [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
19#include <stdio.h>
20#include <string.h>
21#ifdef _WIN32
22#include <winsock2.h>
23#define write(s,b,l) send(s,(const char*)b,l,0)
24#define EWOULDBLOCK WSAEWOULDBLOCK
25#undef errno
26#define errno WSAGetLastError()
27#undef EINTR
28#define EINTR WSAEINTR
29#else
30#include <sys/types.h>
31#include <errno.h>
32#include <unistd.h>
33#include <sys/time.h>
34#endif
35
36// XXX Lynx/OS 2.3: protos for select(), bzero()
37#ifdef Lynx
38#include <sys/proto.h>
39#endif
40
41#include <rdr/FdOutStream.h>
42#include <rdr/Exception.h>
43
44
45using namespace rdr;
46
47enum { DEFAULT_BUF_SIZE = 16384 };
48
49FdOutStream::FdOutStream(int fd_, int timeoutms_, int bufSize_)
50 : fd(fd_), timeoutms(timeoutms_),
51 bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
52{
53 ptr = start = new U8[bufSize];
54 end = start + bufSize;
55}
56
57FdOutStream::~FdOutStream()
58{
59 try {
60 flush();
61 } catch (Exception&) {
62 }
63 delete [] start;
64}
65
66void FdOutStream::setTimeout(int timeoutms_) {
67 timeoutms = timeoutms_;
68}
69
70int FdOutStream::length()
71{
72 return offset + ptr - start;
73}
74
75void FdOutStream::flush()
76{
77 U8* sentUpTo = start;
78 while (sentUpTo < ptr) {
79 int n = writeWithTimeout((const void*) sentUpTo, ptr - sentUpTo);
80 sentUpTo += n;
81 offset += n;
82 }
83
84 ptr = start;
85}
86
87
88int FdOutStream::overrun(int itemSize, int nItems)
89{
90 if (itemSize > bufSize)
91 throw Exception("FdOutStream overrun: max itemSize exceeded");
92
93 flush();
94
95 if (itemSize * nItems > end - ptr)
96 nItems = (end - ptr) / itemSize;
97
98 return nItems;
99}
100
101//
102// writeWithTimeout() writes up to the given length in bytes from the given
103// buffer to the file descriptor. If there is a timeout set and that timeout
104// expires, it throws a TimedOut exception. Otherwise it returns the number of
105// bytes written. It never attempts to write() unless select() indicates that
106// the fd is writable - this means it can be used on an fd which has been set
107// non-blocking. It also has to cope with the annoying possibility of both
108// select() and write() returning EINTR.
109//
110
111int FdOutStream::writeWithTimeout(const void* data, int length)
112{
113 int n;
114
115 do {
116
117 do {
118 fd_set fds;
119 struct timeval tv;
120 struct timeval* tvp = &tv;
121
122 if (timeoutms != -1) {
123 tv.tv_sec = timeoutms / 1000;
124 tv.tv_usec = (timeoutms % 1000) * 1000;
125 } else {
126 tvp = 0;
127 }
128
129 FD_ZERO(&fds);
130 FD_SET(fd, &fds);
131#ifdef _WIN32_WCE
132 // NB: This fixes a broken Winsock2 select() behaviour. select()
133 // never returns for non-blocking sockets, unless they're already
134 // ready to be written to...
135 u_long zero = 0; ioctlsocket(fd, FIONBIO, &zero);
136#endif
137 n = select(fd+1, 0, &fds, 0, tvp);
138#ifdef _WIN32_WCE
139 u_long one = 0; ioctlsocket(fd, FIONBIO, &one);
140#endif
141 } while (n < 0 && errno == EINTR);
142
143 if (n < 0) throw SystemException("select",errno);
144
145 if (n == 0) throw TimedOut();
146
147 do {
148 n = ::write(fd, data, length);
149 } while (n < 0 && (errno == EINTR));
150
151 // NB: This outer loop simply fixes a broken Winsock2 EWOULDBLOCK
152 // condition, found only under Win98 (first edition), with slow
153 // network connections. Should in fact never ever happen...
154 } while (n < 0 && (errno == EWOULDBLOCK));
155
156 if (n < 0) throw SystemException("write",errno);
157
158 return n;
159}