]> git.tdb.fi Git - r2c2.git/blobdiff - source/network/server.h
Add networking library and a remote control program
[r2c2.git] / source / network / server.h
diff --git a/source/network/server.h b/source/network/server.h
new file mode 100644 (file)
index 0000000..6ce965e
--- /dev/null
@@ -0,0 +1,64 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2009  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#ifndef MARKLINNET_SERVER_H_
+#define MARKLINNET_SERVER_H_
+
+#include <msp/io/eventdispatcher.h>
+#include <msp/net/communicator.h>
+#include <msp/net/streamsocket.h>
+#include <msp/net/streamlistensocket.h>
+#include "libmarklin/trafficmanager.h"
+#include "packets.h"
+#include "protocol.h"
+
+namespace Marklin {
+
+class Server
+{
+private:
+       struct Connection: private Msp::Net::PacketReceiver<TrainSpeedPacket>,
+               private Msp::Net::PacketReceiver<TrainFunctionPacket>
+       {
+               Server &server;
+               Msp::Net::StreamSocket *socket;
+               Msp::Net::Communicator comm;
+               bool stale;
+
+               Connection(Server &, Msp::Net::StreamSocket *);
+               ~Connection();
+
+               void handshake_done();
+               void end_of_file();
+               virtual void receive(const TrainSpeedPacket &);
+               virtual void receive(const TrainFunctionPacket &);
+       };
+
+       Protocol proto;
+       TrafficManager &trfc_mgr;
+       Msp::Net::StreamListenSocket listen_sock;
+       Msp::IO::EventDispatcher *event_disp;
+       std::vector<Connection *> connections;
+
+public:
+       Server(TrafficManager &);
+       void use_event_dispatcher(Msp::IO::EventDispatcher &);
+private:
+       void incoming_connection();
+
+       void train_added(Train &);
+       void train_speed_changed(const Train &, unsigned);
+       void train_function_changed(const Train &, unsigned, bool);
+       void train_status_changed(const Train &, const std::string &);
+
+       template<typename P>
+       void send(const P &);
+};
+
+} // namespace Marklin
+
+#endif