/usr/include/shevek/server.hh is in libshevek-dev 1.4-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | /* server.hh - a line-protocol network server
* Copyright 2003-2005 Bas Wijnen <wijnen@debian.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHEVEK_SERVER_HH
#define SHEVEK_SERVER_HH
#include "refbase.hh"
#include "telnet.hh"
#include <list>
#include <signal.h>
namespace
{
sighandler_t ignore_broken_pipes = ::signal (SIGPIPE, SIG_IGN);
}
namespace shevek
{
/// 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:
<CODE><BR>
// \#include <shevek/server.hh><BR>
// \#include <shevek/mainloop.hh><BR>
// <BR>
// // per-server data structure<BR>
// struct serverdata<BR>
// {<BR>
// // Put in here whatever you like, possibly nothing. There is one<BR>
// // instantiation of this class per server.<BR>
// // In most cases, global variables can be used instead of this class, but<BR>
// // this way allows programs to run more than one server of the same kind,<BR>
// // each with its own settings. That is a Good Thing.<BR>
// // It is available from server.data (), and therefore from<BR>
// // client->get_server ()->data ()<BR>
// };<BR>
// <BR>
// // Each object of this class is a connection. Add any members that are<BR>
// // needed on a per-connection basis in this class. Below is a very minimal<BR>
// // example, which implements an echo server (everything you write to it is<BR>
// // echoed back).<BR>
// class client : public shevek::server <client, serverdata>::connection<BR>
// {<BR>
// // Allow the server to call pickup and read.<BR>
// friend class shevek::server <client, serverdata>;<BR>
// void pickup (bool is_stdio)<BR>
// {<BR>
// out->write ("Welcome to the echo server.\\n");<BR>
// }<BR>
// void read (std::string const &line)<BR>
// {<BR>
// // An echo server should echo.<BR>
// out->write (line + '\\n');<BR>
// }<BR>
// static Glib::RefPtr <client> create ()<BR>
// { return Glib::RefPtr <client> (new client () ); }<BR>
// // Make sure it cannot be constructed other than with create.<BR>
// // Don't use the constructor for initialising, use pickup () instead.<BR>
// // The connection is not initialised when the constructor is called.<BR>
// client () {}<BR>
// };<BR>
// <BR>
// int main ()<BR>
// {<BR>
// Glib::RefPtr <shevek::server <client, serverdata> ><BR>
// s = shevek::server <client, serverdata>::create ();<BR>
// // "1234" is the port to listen on. It may be a name from<BR>
// // /etc/services, such as "telnet" (although you need to be root to claim<BR>
// // that one). It can also be a filename, which will be created as a unix<BR>
// // domain socket. Debugging is a little harder then, as netcat cannot<BR>
// // connect to such sockets.<BR>
// s->open ("1234");<BR>
// shevek::loop ();<BR>
// return 0;<BR>
// }<BR>
</CODE>
*/
template <typename client, typename serverdata>
class server : virtual public shevek::refbase
{
static inline fd::read_lines_t get_connection_cb_from_friend (client *who)
{ return sigc::mem_fun (who, &client::read); }
public:
/// Iterator for looping over all current connections.
typedef typename std::list <Glib::RefPtr <client> >::iterator iterator;
/// Iterator for looping over all current connections.
typedef typename std::list <Glib::RefPtr <client> >::const_iterator
const_iterator;
/// Base of the client class which is implemented by the calling program.
/** A client object is created for every connection which is accepted.
* This class handles server administration and provides access to members from the client class.
*/
struct connection : virtual public refbase
{
/// The input socket. The client can stop reading from this connection by calling in->unread ().
Glib::RefPtr <shevek::fd> in;
/// The output socket. This is used to send data to the connection.
Glib::RefPtr <shevek::fd> out;
/// This is called after in->unread (), to resume accepting data from this connection.
void continue_reading ()
{
in->read_lines (server <client, serverdata>
::get_connection_cb_from_friend
(dynamic_cast <client *> (this) ) );
}
/// Destructor.
~connection () {}
protected:
/// The client class can construct this object with its create function.
connection () {}
/// Access to the server object which hosts this client.
Glib::RefPtr <server <client, serverdata> > get_server () { return m_server; }
/// This can be called by the client object to close this connection.
void disconnect () { m_server->l_error (m_self); }
private:
friend class server <client, serverdata>;
Glib::RefPtr <server <client, serverdata> > m_server;
iterator m_self;
};
private:
std::list <Glib::RefPtr <client> > m_connections;
Glib::RefPtr <socket> m_listener;
serverdata m_data;
server ();
void l_read (std::string const &line, Glib::RefPtr <client> conn);
void l_connect (Glib::RefPtr <fd> in, Glib::RefPtr <fd> out,
bool is_stdio);
void l_pickup ();
void l_delayed_disconnect (typename std::list <Glib::RefPtr <client> >::iterator conn);
void l_error (typename std::list <Glib::RefPtr <client> >::iterator conn);
public:
/// Create a new server object.
static Glib::RefPtr <server> create ();
/// Open a port and start running.
/** If use_stdio is true, there will be an initial connection from standard input/standard output.
*/
void open (std::string const &port, bool use_stdio = true);
/// Get the corresponding serverdata structure.
serverdata &data () { return m_data; }
/// Get the corresponding serverdata structure.
serverdata const &data () const { return m_data;}
/// Loop over all current connections.
iterator begin () { return m_connections.begin (); }
/// Loop over all current connections.
iterator end () { return m_connections.end (); }
/// Loop over all current connections.
const_iterator begin () const { return m_connections.begin (); }
/// Loop over all current connections.
const_iterator end () const { return m_connections.end (); }
/// Stop running this server, closing all current connections.
void shutdown ();
/// The destructor shuts down the server.
~server () { shutdown (); }
};
template <typename client, typename serverdata>
void server <client, serverdata>::shutdown ()
{
if (m_listener)
{
m_listener->disconnect ();
m_listener = Glib::RefPtr <socket> ();
}
while (!m_connections.empty () )
m_connections.front ()->disconnect ();
}
template <typename client, typename serverdata>
void server <client, serverdata>::l_read (std::string const &line,
Glib::RefPtr <client> conn)
{
conn->read (line);
}
template <typename client, typename serverdata>
void server <client, serverdata>::l_delayed_disconnect (typename std::list <Glib::RefPtr <client> >::iterator conn)
{
Glib::RefPtr <telnet> s = Glib::RefPtr <telnet>::cast_dynamic ( (*conn)->in);
if (s)
s->disconnect ();
m_connections.erase (conn);
}
template <typename client, typename serverdata>
void server <client, serverdata>::l_error (typename std::list <Glib::RefPtr <client> >::iterator conn)
{
Glib::RefPtr <telnet> s = Glib::RefPtr <telnet>::cast_dynamic ( (*conn)->in);
if (s)
{
s->signal_disconnect ().connect (sigc::bind (sigc::mem_fun (*this, &server <client, serverdata>::l_delayed_disconnect), conn) );
s->disconnect ();
}
else
{
(*conn)->in->unread ();
m_connections.erase (conn);
}
}
template <typename client, typename serverdata>
void server <client, serverdata>::l_connect (Glib::RefPtr <fd> in,
Glib::RefPtr <fd> out,
bool is_stdio)
{
m_connections.push_back (client::create () );
m_connections.back ()->in = in;
m_connections.back ()->out = out;
m_connections.back ()->m_server = refptr_this <server <client, serverdata> > ();
m_connections.back ()->m_self = --m_connections.end ();
in->set_error (sigc::bind (sigc::mem_fun (*this, &server <client, serverdata>::l_error),
--m_connections.end () ) );
in->set_eof (sigc::bind (sigc::mem_fun (*this, &server <client, serverdata>::l_error),
--m_connections.end () ) );
m_connections.back ()->continue_reading ();
out->set_error (sigc::bind (sigc::mem_fun (*this, &server <client, serverdata>::l_error),
--m_connections.end () ) );
m_connections.back ()->pickup (is_stdio);
}
template <typename client, typename serverdata>
void server <client, serverdata>::l_pickup ()
{
Glib::RefPtr <telnet> newfd = telnet::create ();
m_listener->accept (newfd);
l_connect (newfd, newfd, false);
}
template <typename client, typename serverdata>
server <client, serverdata>::server ()
{
}
template <typename client, typename serverdata>
Glib::RefPtr <server <client, serverdata> > server <client, serverdata>::create ()
{
return Glib::RefPtr <server <client, serverdata> > (new server <client, serverdata> () );
}
template <typename client, typename serverdata>
void server <client, serverdata>::open (std::string const &port,
bool use_stdio)
{
if (m_listener)
m_listener->disconnect ();
if (!port.empty () )
{
m_listener = socket::create ();
m_listener->listen (port, sigc::mem_fun (*this, &server <client, serverdata>::l_pickup) );
}
if (use_stdio)
{
l_connect
(fd::create (STDIN_FILENO), fd::create (STDOUT_FILENO), true);
}
}
}
#endif
|