blob: b4c87653837ee04f4245f6bb70505e0e186cc2ca [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;
Steve Kondika6424622017-07-08 01:49:14 -070033 virtual ~FdInStreamBlockCallback() {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000034 };
35
36 class FdInStream : public InStream {
37
38 public:
39
40 FdInStream(int fd, int timeoutms=-1, int bufSize=0,
41 bool closeWhenDone_=false);
42 FdInStream(int fd, FdInStreamBlockCallback* blockCallback, int bufSize=0);
43 virtual ~FdInStream();
44
45 void setTimeout(int timeoutms);
46 void setBlockCallback(FdInStreamBlockCallback* blockCallback);
47 int getFd() { return fd; }
48 int pos();
49 void readBytes(void* data, int length);
50
51 void startTiming();
52 void stopTiming();
53 unsigned int kbitsPerSecond();
54 unsigned int timeWaited() { return timeWaitedIn100us; }
55
56 protected:
57 int overrun(int itemSize, int nItems, bool wait);
58
59 private:
60 int readWithTimeoutOrCallback(void* buf, int len, bool wait=true);
61
62 int fd;
63 bool closeWhenDone;
64 int timeoutms;
65 FdInStreamBlockCallback* blockCallback;
66
67 bool timing;
68 unsigned int timeWaitedIn100us;
69 unsigned int timedKbits;
70
71 int bufSize;
72 int offset;
73 U8* start;
74 };
75
76} // end of namespace rdr
77
78#endif