BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
FdStream.h
1 #pragma once
2 
3 #include <iostream>
4 
5 #include <Poco/BufferedStreamBuf.h>
6 #include <Poco/Timespan.h>
7 
8 struct pollfd;
9 
10 namespace BeeeOn {
11 
12 class FdStreamBuf : public Poco::BufferedStreamBuf {
13 public:
14  FdStreamBuf(std::streamsize size, openmode mode);
15  FdStreamBuf(int fd, std::streamsize size, openmode mode);
16  ~FdStreamBuf();
17 
18  void setFd(int fd);
19  int fd() const;
20 
21  int writeToDevice(const char *buffer, std::streamsize size) override;
22  int readFromDevice(char *buffer, std::streamsize capacity) override;
23 
24  void close();
25 
26 private:
27  int m_fd;
28 };
29 
30 class FdStreamIOS : public virtual std::ios {
31 public:
32  enum {
33  POLL_READABLE = 0x01,
34  POLL_WRITABLE = 0x02,
35  POLL_ERROR = 0x04,
36  POLL_HANGUP = 0x08,
37  POLL_INVALID = 0x10,
38  POLL_TIMEOUT = 0x20,
39  };
40 
41  FdStreamIOS(std::streamsize size, openmode mode);
42  FdStreamIOS(int fd, std::streamsize size, openmode mode);
43 
44  int fd() const;
45  void setBlocking(bool blocking);
46  bool blocking() const;
47  void assign(int fd, bool closeOld = true);
48  void close();
49 
50 protected:
51  bool poll(struct pollfd &fd, const Poco::Timespan &timeout) const;
52 
53 protected:
54  FdStreamBuf m_buf;
55 };
56 
57 class FdInputStream : public virtual FdStreamIOS, public std::istream {
58 public:
59  FdInputStream(std::size_t size = 512);
60  FdInputStream(int fd, std::size_t size = 512);
61 
62  bool poll(const Poco::Timespan &timeout) const;
63 };
64 
65 class FdOutputStream : public virtual FdStreamIOS, public std::ostream {
66 public:
67  FdOutputStream(std::size_t size = 512);
68  FdOutputStream(int fd, std::size_t size = 512);
69 
70  bool poll(const Poco::Timespan &timeout) const;
71 };
72 
73 }
Definition: FdStream.h:30
Definition: FdStream.h:12
Definition: FdStream.h:57
Definition: FdStream.h:65