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);
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