libshevek

- Main Page
- Classes
- Files

- File List

src/fd.hh

    00001 /* fd.hh - use file descriptors with Glib
    00002  * Copyright 2003-2005 Bas Wijnen <wijnen@debian.org>
    00003  *
    00004  * This program is free software: you can redistribute it and/or modify
    00005  * it under the terms of the GNU General Public License as published by
    00006  * the Free Software Foundation, either version 3 of the License, or
    00007  * (at your option) any later version.
    00008  *
    00009  * This program is distributed in the hope that it will be useful,
    00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  * GNU General Public License for more details.
    00013  *
    00014  * You should have received a copy of the GNU General Public License
    00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    00016  */
    00017
    00018 #ifndef SHEVEK_FD_HH
    00019 #define SHEVEK_FD_HH
    00020
    00021 #include <sigc++/sigc++.h>
    00022 #include <glibmm.h>
    00023 #include "refbase.hh"
    00024 #include "time.hh"
    00025
    00026 namespace shevek
    00027 {
    00029   class fd : virtual public refbase
    00030   {
    00031   public:
    00032     /* types */
    00034     typedef sigc::slot0 <void> read_custom_t;
    00036     typedef sigc::slot1 <bool, std::string &> read_t;
    00038     typedef sigc::slot1 <void, std::string const &> read_lines_t;
    00040     typedef sigc::slot0 <void> error_t;
    00042     typedef sigc::slot0 <void> write_done_t;
    00044     typedef sigc::slot1 <void, std::string &> filter_t;
    00046     typedef sigc::slot0 <void> flush_t;
    00047
    00048     /* member functions */
    00050
    00052     void read_custom (read_custom_t cb);
    00054     void read_priority_custom (read_custom_t cb);
    00056
    00058     void read (read_t cb);
    00060     void read_priority (read_t cb);
    00062     void read_lines (read_lines_t cb);
    00064     void unread (bool flush_buffer = false, flush_t cb = flush_t () );
    00066     void write (std::string const &data, write_done_t cb = write_done_t () );
    00068     void write_raw (std::string const &data, write_done_t cb = write_done_t () );
    00070
    00072     bool write_block (relative_time timeout = relative_time (-1, 0) );
    00074
    00080     std::string &read_block (relative_time timeout = relative_time (-1, 0) );
    00082     std::string read_line_block (relative_time timeout = relative_time (-1, 0) );
    00084     void set_fd (int fd);
    00086     void in_filter (filter_t cb);
    00088     void out_filter (filter_t cb);
    00090     static Glib::RefPtr <fd>
    00091     create (int value = -1, Glib::RefPtr <Glib::MainContext> main
    00092             = Glib::MainContext::get_default () );
    00094
    00096     void set_error (error_t cb);
    00098     void set_poll_error (error_t cb);
    00100     void set_read_error (error_t cb);
    00102     void set_write_error (error_t cb);
    00104     void set_eof (error_t cb);
    00106     void read_reset ();
    00108     void write_reset ();
    00110     void reset ();
    00112     int get_fd () const;
    00114     Glib::RefPtr <Glib::MainContext> get_main_context ();
    00115   protected:
    00117     fd (int value, Glib::RefPtr <Glib::MainContext> main);
    00119     ~fd ();
    00120   private:
    00121     // not copyable
    00122     fd (fd const &that); // NI
    00123     fd &operator= (fd const &that); // NI
    00124     /* internal functions */
    00125     // callback, called when poll returns any event on the fd
    00126     bool l_check (Glib::IOCondition result);
    00127     // helper function for l_check and blocking functions
    00128     void l_write ();
    00129     // helper function for l_check and blocking functions
    00130     void l_read (bool force_fill);
    00131     void l_read_priority (bool force_fill);
    00132     // callback for idle function
    00133     bool l_idle ();
    00134     bool l_idle_priority ();
    00135     // finish up unread after read buffer is flushed
    00136     void l_unread ();
    00137     // disconnect if neccesary, and reconnect (with new fd and/or iocondition)
    00138     void l_connect (Glib::IOCondition
    00139                     io = Glib::IO_HUP | Glib::IO_ERR | Glib::IO_NVAL);
    00140     // read lines at a time
    00141     bool l_read_lines (std::string &data);
    00142     /* types */
    00143     // element for the write queue
    00144     struct write_t
    00145     {
    00146       std::string data;
    00147       write_done_t done;
    00148     };
    00149     /* data */
    00150     // write queue
    00151     std::list <write_t> m_writebuffer;
    00152     // read buffer
    00153     std::string m_readbuffer;
    00154     std::string m_priority_readbuffer;
    00155     // read callback
    00156     read_t m_read;
    00157     read_t m_read_priority;
    00158     // read lines callback
    00159     read_lines_t m_read_lines;
    00160     // callback for custom reader
    00161     read_custom_t m_read_custom;
    00162     read_custom_t m_read_priority_custom;
    00163     // flush callback
    00164     bool m_flushing;
    00165     flush_t m_flush;
    00166     // file descriptor
    00167     int m_fd;
    00168     // data filters
    00169     filter_t m_in_filter, m_out_filter;
    00170     // read and idle callback
    00171     sigc::connection m_handle, m_idle, m_idle_priority;
    00172     // error callbacks
    00173     error_t m_error, m_poll_error, m_rerror, m_werror, m_eof;
    00174     // main context
    00175     Glib::RefPtr <Glib::MainContext> m_main;
    00176     // current io condition
    00177     Glib::IOCondition m_iocondition;
    00178     // objects to keep the object alive as long as it can be called.
    00179     Glib::RefPtr <fd> m_keepalive_helper;
    00180     Glib::RefPtr <fd> m_keepalive_helper_idle;
    00181     Glib::RefPtr <fd> m_keepalive_helper_idle_priority;
    00182
    00183     /* static members */
    00184     // buffer to return from blocking functions when object is destroyed.
    00185     static std::string s_junkbuffer;
    00186   };
    00187 }
    00188
    00189 #endif