A stream buffer class to stream characters to a PTY.
- Author
- Tobias Blomberg / SM0SVX
- Date
- 2014-12-20
This class can be used to write data to a UNIX 98 PTY using the standard streaming interface. The typical usage pattern is to first create an instance of a Pty. Then an instance of this class is created, the stream buffer. Lastly a std::ostream can be created with the stream buffer as an argument to the constructor. The std::ostream can then be used as any other output stream.
#include <sigc++/bind.h>
#include <string>
#include <cstdlib>
#include <ostream>
#include <iostream>
using namespace std;
void write_to_stream(ostream *os)
{
*os << "Hello, PTY\n";
}
void data_received(const void *buf, size_t len)
{
cout << "Received: ";
cout.write(static_cast<const char *>(buf), len);
}
int main(void)
{
string pty_slave_path("/tmp/testpty");
{
cout << "Failed to open PTY " << pty_slave_path << endl;
exit(1);
}
ostream os(&psb);
os.setf(ios::unitbuf);
cout << "\nThis demo app will write a line of text every second to a PTY.\n";
cout << "To see the printouts, in another console do:\n";
cout << "\n\tcat /tmp/testpty\n\n";
cout << "Or to also echo back the received text to the PTY:\n";
cout << "\n\ttee /tmp/testpty </tmp/testpty\n\n";
sigc::slot<void, ostream*> timer_handler = sigc::ptr_fun(&write_to_stream);
t.expired.connect(sigc::hide(sigc::bind(timer_handler, &os)));
}
The core class for writing asyncronous cpp applications.
A stream buffer for writing to a PTY.
A class that wrap up some functionality to use a PTY.
Contains a single shot or periodic timer that emits a signal on timeout.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
A stream buffer class to stream characters to a PTY.
Pty * pty(void) const
Return the PTY this stream is attached to.
A wrapper class for using a PTY.
bool open(void)
Open the PTY.
sigc::signal< void, const void *, size_t > dataReceived
Signal that is emitted when data has been received.
A class that produces timer events.
@ TYPE_PERIODIC
A timer that restarts itself every time it expires.
- Examples
- AsyncPtyStreamBuf_demo.cpp.
Definition at line 124 of file AsyncPtyStreamBuf.h.