blob: 5d9598c82c250a82693749dd310114a3c48178dd [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//
20// FdInStream streams from a file descriptor.
21//
22
23#ifndef __RDR_FDINSTREAM_H__
24#define __RDR_FDINSTREAM_H__
25
26#include <rdr/InStream.h>
27
28namespace rdr {
29
30 class FdInStreamBlockCallback {
31 public:
32 virtual void blockCallback() = 0;
33 };
34
35 class FdInStream : public InStream {
36
37 public:
38
39 FdInStream(int fd, int timeoutms=-1, int bufSize=0,
40 bool closeWhenDone_=false);
41 FdInStream(int fd, FdInStreamBlockCallback* blockCallback, int bufSize=0);
42 virtual ~FdInStream();
43
44 void setTimeout(int timeoutms);
45 void setBlockCallback(FdInStreamBlockCallback* blockCallback);
46 int getFd() { return fd; }
47 int pos();
48 void readBytes(void* data, int length);
49
50 void startTiming();
51 void stopTiming();
52 unsigned int kbitsPerSecond();
53 unsigned int timeWaited() { return timeWaitedIn100us; }
54
55 protected:
56 int overrun(int itemSize, int nItems, bool wait);
57
58 private:
59 int readWithTimeoutOrCallback(void* buf, int len, bool wait=true);
60
61 int fd;
62 bool closeWhenDone;
63 int timeoutms;
64 FdInStreamBlockCallback* blockCallback;
65
66 bool timing;
67 unsigned int timeWaitedIn100us;
68 unsigned int timedKbits;
69
70 int bufSize;
71 int offset;
72 U8* start;
73 };
74
75} // end of namespace rdr
76
77#endif