AC_INIT(configure.in)
AM_CONFIG_HEADER(config.h)
+dnl AC_CONFIG_HEADERS([config.h:config.h.in])
AM_INIT_AUTOMAKE(libt2n, 0.1)
+AC_DEFINE(PROTOCOL_VERSION, 1, [protocol version used (integers, increase version if incompatible)])
+
AC_LANG_CPLUSPLUS
AC_PROG_CXX
AM_PROG_LIBTOOL
#include <string>
#include <sstream>
#include <stdexcept>
+#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
+#include <boost/bind.hpp>
+
#include "command_server.hxx"
#include "container.hxx"
#include "log.hxx"
namespace libt2n
{
+command_server::command_server(server& _s)
+ : s(_s)
+{
+ // register callback
+ s.add_callback(new_connection,bind(&command_server::new_connection_callback, boost::ref(*this), _1));
+}
+
+void command_server::new_connection_callback(server_connection* conn)
+{
+ cerr << "new connection callback: " << conn->get_id() << endl;
+}
+
/// handle a command including deserialization and answering
void command_server::handle_packet(const std::string& packet, server_connection* conn)
{
void handle_packet(const std::string& packet, server_connection* conn);
public:
- command_server(server& _s)
- : s(_s)
- { }
+ command_server(server& _s);
void handle(long long usec_timeout=-1);
+
+ void new_connection_callback(server_connection* conn);
};
}
}
server::server()
+ : callbacks(__events_end)
{
set_default_timeout(30);
set_logging(NULL,none);
delete i->second;
}
+/** @brief add a callback
+
+ @param event event the function will be called at
+ @param func functor (see boost function) that will be called
+*/
+void server::add_callback(callback_event_type event, const boost::function<void (server_connection*)>& func)
+{
+ callbacks[event].push_back(func);
+}
+
+void server::do_callbacks(callback_event_type event, server_connection* conn)
+{
+ std::list<boost::function<void (server_connection*)> >::iterator i,ie=callbacks[event].end();
+ for (i=callbacks[event].begin(); i != ie; i++)
+ (*i)(conn);
+}
+
int server::add_connection(server_connection* newconn)
{
unsigned int cid=next_id++;
LOGSTREAM(debug,"new connection accepted, id: " << cid);
+ do_callbacks(new_connection,newconn);
+
return cid;
}
#include <iostream>
#include <string>
#include <map>
+#include <vector>
+#include <list>
+
+#include <boost/function.hpp>
#include "connection.hxx"
#include "types.hxx"
log_level_values log_level;
std::ostream *logstream;
+ /// vector initialized for all callback-types, all elements in each list will be called
+ std::vector<std::list<boost::function<void (server_connection*)> > > callbacks;
+
unsigned int next_id;
protected:
int add_connection(server_connection* newconn);
+ void do_callbacks(callback_event_type event, server_connection* conn);
+
public:
virtual ~server();
server_connection* get_connection(unsigned int conn_id);
+ void add_callback(callback_event_type event, const boost::function<void (server_connection*)>& func);
+
/** @brief look for new data on all open connections, accept new connections
@param usec_timeout wait until new data is found, max timeout usecs.
-1: wait endless
/// possible types of a socket (tcp and unix)
enum socket_type_value { tcp_s, unix_s };
+/// possible events for callback, __events_end must be the last event and must not be used
+/// for anything else than marking the end
+enum callback_event_type { new_connection=0, connection_closed=1, connection_deleted=2, __events_end };
+
}
#endif