1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Monitoring I/O</title><meta name="generator" content="DocBook XSL Stylesheets V1.64.1"><link rel="home" href="index.html" title="Programming with gtkmm2"><link rel="up" href="ch17.html" title="Chapter17.Timeouts, I/O and Idle Functions "><link rel="previous" href="ch17.html" title="Chapter17.Timeouts, I/O and Idle Functions "><link rel="next" href="ch17s03.html" title="Idle Functions"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Monitoring I/O</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch17.html">Prev</a></td><th width="60%" align="center">Chapter17.Timeouts, I/O and Idle Functions </th><td width="20%" align="right"><a accesskey="n" href="ch17s03.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2518291"></a>Monitoring I/O</h2></div></div><div></div></div><p>
A nifty feature of GDK (one of the libraries that underlying
gtkmm) is the ability to have it check for data on a file descriptor
for you. This is especially useful for networking applications. The
following method is used to do this:
</p><p>
</p><pre class="programlisting">
Connection Gtk::Main::input.connect(const SlotType& sd, int source,
GdkInputCondition condition);
</pre><p>
</p><p>
The first argument is a slot you wish to have called when then the
specified event (see argument 3) occurs on the file descriptor you
specify using argument two. Argument three may be one or a combination
(using <tt class="literal">|</tt>) of:
</p><p>
</p><div class="itemizedlist"><ul type="disc"><li><p>
GDK_INPUT_READ - Call your method when there is data ready for
reading on your file descriptor.
</p></li><li><p>
GDK_INPUT_WRITE - Call your method when the file descriptor is
ready for writing.
</p></li><li><p>
GDK_INPUT_EXCEPTION - Call your method when an exception happened
on the file descriptor.
</p></li></ul></div><p>
</p><p>
The return value is a Connection that may be used to stop monitoring
this file descriptor using the <tt class="literal">disconnect</tt> following method.
The signal handler should be declared as follows:
</p><p>
</p><pre class="programlisting">
void input_callback(int source, GdkInputCondition condition);
</pre><p>
</p><p>
where <tt class="literal">source</tt> and <tt class="literal">condition</tt> are as specified above. As usual
the slot is created with <tt class="literal">slot()</tt> and can be a member method
of an object.
</p><p>
A little (and somewhat dirty) example follows as usual. To use
the example just execute it from a terminal; it doesn't create a
window. It will create a pipe named <tt class="literal">testpipe</tt> in the current
directory. Then start another shell and execute <tt class="literal">cat
>testpipe</tt>. The example will print each line you enter until you
type <tt class="literal">quit</tt>.
</p><p>
<span class="emphasis"><em>Source location: examples/input/input.cc</em></span>
</p><pre class="programlisting">
#include <gtkmm/main.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fstream>
#include <iostream>
#include <memory>
using std::istream;
using std::auto_ptr;
using SigC::slot;
auto_ptr<istream> input;
// this will be our signal handler for read operations
// there is not much to say. just read a string,
// print it and quit the application if the string was quit
void MyCallback(int, GdkInputCondition) {
Gtk::string dummy;
do {
(*input) >> dummy;
cout << dummy << endl;
if(dummy == "quit") Gtk::Main::quit();
} while(input->fail());
}
int main (int argc, char *argv[])
{
// the usual Gtk::Main object
Gtk::Main app(argc, argv);
// create a fifo for testing purposes
if (mkfifo("testfifo",0666) != 0) {
cerr << "error creating fifo" << endl;
return -1;
}
// open the fifo
input=new ifstream("testfifo");
// int fd = open("testfifo", 0);
// if (fd == -1) {
// cerr << "error opening fifo" << endl;
// return -1;
// }
// assign the fifo's filedescriptor to our ifstream object
//This sucks; it will only ever work with libstdc++-v3, as
// both istream::__filebuf_type and the basic_filebuf contructor
// that takes an fd are libstdc++-v3 specific.
//input=new istream(new ifstream::__filebuf_type(fd,"testfifo"));
// connect the signal handler
app.input.connect(slot(MyCallback), fd, GDK_INPUT_READ);
// and last but not least - run the application main loop
app.run();
// now remove the temporary fifo
if(unlink("testfifo"))
cerr << "error removing fifo" << endl;
return 0;
}
</pre><p>
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch17.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="ch17.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="ch17s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter17.Timeouts, I/O and Idle Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">Idle Functions</td></tr></table></div></body></html>
|