libshevek
fd.hh
1 /* fd.hh - use file descriptors with Glib
2  * Copyright 2003-2005 Bas Wijnen <wijnen@debian.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SHEVEK_FD_HH
19 #define SHEVEK_FD_HH
20 
21 #include <sigc++/sigc++.h>
22 #include <glibmm.h>
23 #include "refbase.hh"
24 #include "time.hh"
25 
26 namespace shevek
27 {
29  class fd : virtual public refbase
30  {
31  public:
32  /* types */
34  typedef sigc::slot0 <void> read_custom_t;
36  typedef sigc::slot1 <bool, std::string &> read_t;
38  typedef sigc::slot1 <void, std::string const &> read_lines_t;
40  typedef sigc::slot0 <void> error_t;
42  typedef sigc::slot0 <void> write_done_t;
44  typedef sigc::slot1 <void, std::string &> filter_t;
46  typedef sigc::slot0 <void> flush_t;
47 
48  /* member functions */
50 
52  void read_custom (read_custom_t cb);
54  void read_priority_custom (read_custom_t cb);
56 
58  void read (read_t cb);
60  void read_priority (read_t cb);
62  void read_lines (read_lines_t cb);
64  void unread (bool flush_buffer = false, flush_t cb = flush_t () );
66  void write (std::string const &data, write_done_t cb = write_done_t () );
68  void write_raw (std::string const &data, write_done_t cb = write_done_t () );
70 
72  bool write_block (relative_time timeout = relative_time (-1, 0) );
74 
80  std::string &read_block (relative_time timeout = relative_time (-1, 0) );
82  std::string read_line_block (relative_time timeout = relative_time (-1, 0) );
84  void set_fd (int fd);
86  void in_filter (filter_t cb);
88  void out_filter (filter_t cb);
90  static Glib::RefPtr <fd>
91  create (int value = -1, Glib::RefPtr <Glib::MainContext> main
92  = Glib::MainContext::get_default () );
94 
96  void set_error (error_t cb);
98  void set_poll_error (error_t cb);
100  void set_read_error (error_t cb);
102  void set_write_error (error_t cb);
104  void set_eof (error_t cb);
106  void read_reset ();
108  void write_reset ();
110  void reset ();
112  int get_fd () const;
114  Glib::RefPtr <Glib::MainContext> get_main_context ();
115  protected:
117  fd (int value, Glib::RefPtr <Glib::MainContext> main);
119  ~fd ();
120  private:
121  // not copyable
122  fd (fd const &that); // NI
123  fd &operator= (fd const &that); // NI
124  /* internal functions */
125  // callback, called when poll returns any event on the fd
126  bool l_check (Glib::IOCondition result);
127  // helper function for l_check and blocking functions
128  void l_write ();
129  // helper function for l_check and blocking functions
130  void l_read (bool force_fill);
131  void l_read_priority (bool force_fill);
132  // callback for idle function
133  bool l_idle ();
134  bool l_idle_priority ();
135  // finish up unread after read buffer is flushed
136  void l_unread ();
137  // disconnect if neccesary, and reconnect (with new fd and/or iocondition)
138  void l_connect (Glib::IOCondition
139  io = Glib::IO_HUP | Glib::IO_ERR | Glib::IO_NVAL);
140  // read lines at a time
141  bool l_read_lines (std::string &data);
142  /* types */
143  // element for the write queue
144  struct write_t
145  {
146  std::string data;
147  write_done_t done;
148  };
149  /* data */
150  // write queue
151  std::list <write_t> m_writebuffer;
152  // read buffer
153  std::string m_readbuffer;
154  std::string m_priority_readbuffer;
155  // read callback
156  read_t m_read;
157  read_t m_read_priority;
158  // read lines callback
159  read_lines_t m_read_lines;
160  // callback for custom reader
161  read_custom_t m_read_custom;
162  read_custom_t m_read_priority_custom;
163  // flush callback
164  bool m_flushing;
165  flush_t m_flush;
166  // file descriptor
167  int m_fd;
168  // data filters
169  filter_t m_in_filter, m_out_filter;
170  // read and idle callback
171  sigc::connection m_handle, m_idle, m_idle_priority;
172  // error callbacks
173  error_t m_error, m_poll_error, m_rerror, m_werror, m_eof;
174  // main context
175  Glib::RefPtr <Glib::MainContext> m_main;
176  // current io condition
177  Glib::IOCondition m_iocondition;
178  // objects to keep the object alive as long as it can be called.
179  Glib::RefPtr <fd> m_keepalive_helper;
180  Glib::RefPtr <fd> m_keepalive_helper_idle;
181  Glib::RefPtr <fd> m_keepalive_helper_idle_priority;
182 
183  /* static members */
184  // buffer to return from blocking functions when object is destroyed.
185  static std::string s_junkbuffer;
186  };
187 }
188 
189 #endif
void set_poll_error(error_t cb)
Callback for errors from poll.
sigc::slot0< void > write_done_t
Function pointer to call when data has been written.
Definition: fd.hh:42
Base class for classes which want reference counting through Glib::RefPtr.
Definition: refbase.hh:27
~fd()
Destructor.
sigc::slot1< bool, std::string & > read_t
Function pointer to call when fd is ready for reading.
Definition: fd.hh:36
void read_custom(read_custom_t cb)
Poll for read with a custom callback to poll.
sigc::slot1< void, std::string const & > read_lines_t
Function pointer to call when a complete line has arrived.
Definition: fd.hh:38
Time interval.
Definition: time.hh:143
fd(int value, Glib::RefPtr< Glib::MainContext > main)
Constructor.
void unread(bool flush_buffer=false, flush_t cb=flush_t())
Stop polling for read (including priority read).
void set_fd(int fd)
Change file descriptor.
Definition: args.hh:52
int get_fd() const
Get the fd. This function should mostly be used by derived classes.
sigc::slot0< void > flush_t
Function pointer to signal that all data is flushed after unread ()
Definition: fd.hh:46
void write(std::string const &data, write_done_t cb=write_done_t())
Write data and set a callback (defaults to none).
sigc::slot0< void > read_custom_t
Function pointer to call when data is read from fd.
Definition: fd.hh:34
void in_filter(filter_t cb)
If set, incoming data is filtered through this callback before it is put into the buffer...
void read_lines(read_lines_t cb)
Poll for read and set read lines callback (resets custom and read callback). Polls for priority read ...
static Glib::RefPtr< fd > create(int value=-1, Glib::RefPtr< Glib::MainContext > main=Glib::MainContext::get_default())
Create a new fd.
sigc::slot1< void, std::string & > filter_t
Function pointer to filter in and outgoing data.
Definition: fd.hh:44
sigc::slot0< void > error_t
Function pointer to call when an error occurs.
Definition: fd.hh:40
void read_reset()
Stop reading, delete the buffer.
void out_filter(filter_t cb)
If set, outgoing data is filtered through this callback before it is sent to the file descriptor...
The fd class is a generic wrapper for a file descriptor to use it in the Glib event loop...
Definition: fd.hh:29
void reset()
Stop reading and writing, delete the buffers.
void read_priority_custom(read_custom_t cb)
Poll for priority read with a custom callback to poll.
void read(read_t cb)
Poll for read and set read callback (resets custom callback)
void write_raw(std::string const &data, write_done_t cb=write_done_t())
Write data, ignoring the filter, and set a callback (defaults to none).
void set_write_error(error_t cb)
Callback for errors from write.
void set_read_error(error_t cb)
Callback for errors from read.
void read_priority(read_t cb)
Poll for priority read and set read callback (resets custom callback)
Glib::RefPtr< Glib::MainContext > get_main_context()
Get the main context. Also mostly used by derived classes.
void set_eof(error_t cb)
Callback for end of file.
void write_reset()
Stop writing, delete the buffer.
void set_error(error_t cb)
Set a callback for all error types at once.
std::string & read_block(relative_time timeout=relative_time(-1, 0))
Block until data is read, try writing if there is a write buffer.
bool write_block(relative_time timeout=relative_time(-1, 0))
Block until write buffer is empty.
std::string read_line_block(relative_time timeout=relative_time(-1, 0))
Call read_block until a line has been read, or the timeout expires.