libshevek
|
Set up a network server using shevek::telnet. More...
#include <server.hh>
Classes | |
struct | connection |
Base of the client class which is implemented by the calling program. More... | |
Public Types | |
typedef std::list< Glib::RefPtr< client > >::iterator | iterator |
Iterator for looping over all current connections. | |
typedef std::list< Glib::RefPtr< client > >::const_iterator | const_iterator |
Iterator for looping over all current connections. | |
Public Member Functions | |
void | open (std::string const &port, bool use_stdio=true) |
Open a port and start running. More... | |
serverdata & | data () |
Get the corresponding serverdata structure. | |
serverdata const & | data () const |
Get the corresponding serverdata structure. | |
iterator | begin () |
Loop over all current connections. | |
iterator | end () |
Loop over all current connections. | |
const_iterator | begin () const |
Loop over all current connections. | |
const_iterator | end () const |
Loop over all current connections. | |
void | shutdown () |
Stop running this server, closing all current connections. | |
~server () | |
The destructor shuts down the server. | |
![]() | |
template<typename _T > | |
Glib::RefPtr< _T > | cast_dynamic () |
Identical to GLib::RefPtr <>::cast_dynamic, but nicer to type. | |
Static Public Member Functions | |
static Glib::RefPtr< server > | create () |
Create a new server object. | |
Additional Inherited Members | |
![]() | |
refbase () | |
Constructor, increments reference count. | |
virtual | ~refbase () |
Destructor, decrements reference count and destroys the object if it reaches 0. | |
template<typename T > | |
Glib::RefPtr< T > | refptr_this () |
Get a RefPtr to this, protected because only members should need it. More... | |
Set up a network server using shevek::telnet.
New connections are accepted, and callbacks to the program are made when a line of data is received.
An example file showing how to use the server:
#include <shevek/server.hh>
#include <shevek/mainloop.hh>
// per-server data structure
struct serverdata
{
// Put in here whatever you like, possibly nothing. There is one
// instantiation of this class per server.
// In most cases, global variables can be used instead of this class, but
// this way allows programs to run more than one server of the same kind,
// each with its own settings. That is a Good Thing.
// It is available from server.data (), and therefore from
// client->get_server ()->data ()
};
// Each object of this class is a connection. Add any members that are
// needed on a per-connection basis in this class. Below is a very minimal
// example, which implements an echo server (everything you write to it is
// echoed back).
class client : public shevek::server <client, serverdata>::connection<BR> {
// Allow the server to call pickup and read.
friend class shevek::server <client, serverdata>;
void pickup (bool is_stdio)
{
out->write ("Welcome to the echo server.\\n");
}
void read (std::string const &line)
{
// An echo server should echo.
out->write (line + '\n');
}
static Glib::RefPtr <client> create ()
{ return Glib::RefPtr <client> (new client () ); }
// Make sure it cannot be constructed other than with create.
// Don't use the constructor for initialising, use pickup () instead.
// The connection is not initialised when the constructor is called.
client () {}
};
int main ()
{
Glib::RefPtr <shevek::server <client, serverdata> >
s = shevek::server <client, serverdata>::create ();
// "1234" is the port to listen on. It may be a name from
// /etc/services, such as "telnet" (although you need to be root to claim
// that one). It can also be a filename, which will be created as a unix
// domain socket. Debugging is a little harder then, as netcat cannot
// connect to such sockets.
s->open ("1234");
shevek::loop ();
return 0;
}
void shevek::server< client, serverdata >::open | ( | std::string const & | port, |
bool | use_stdio = true |
||
) |
Open a port and start running.
If use_stdio is true, there will be an initial connection from standard input/standard output.