]> git.tdb.fi Git - r2c2.git/blob - source/network/server.h
Rename the project to R²C²
[r2c2.git] / source / network / server.h
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2009-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef R2C2_NET_SERVER_H_
9 #define R2C2_NET_SERVER_H_
10
11 #include <msp/io/eventdispatcher.h>
12 #include <msp/net/communicator.h>
13 #include <msp/net/streamsocket.h>
14 #include <msp/net/streamlistensocket.h>
15 #include "libr2c2/layout.h"
16 #include "packets.h"
17 #include "protocol.h"
18
19 namespace R2C2 {
20
21 class Server
22 {
23 private:
24         struct Connection: private Msp::Net::PacketReceiver<TrainControlPacket>,
25                 private Msp::Net::PacketReceiver<TrainFunctionPacket>,
26                 private Msp::Net::PacketReceiver<TrainRoutePacket>
27         {
28                 Server &server;
29                 Msp::Net::StreamSocket *socket;
30                 Msp::Net::Communicator comm;
31                 bool stale;
32
33                 Connection(Server &, Msp::Net::StreamSocket *);
34                 ~Connection();
35
36                 void handshake_done();
37                 void end_of_file();
38                 virtual void receive(const TrainControlPacket &);
39                 virtual void receive(const TrainFunctionPacket &);
40                 virtual void receive(const TrainRoutePacket &);
41                 void error(const std::string &);
42         };
43
44         Protocol proto;
45         Layout &layout;
46         Msp::Net::StreamListenSocket listen_sock;
47         Msp::IO::EventDispatcher *event_disp;
48         std::vector<Connection *> connections;
49
50 public:
51         Server(Layout &);
52         void use_event_dispatcher(Msp::IO::EventDispatcher &);
53 private:
54         void incoming_connection();
55
56         void train_added(Train &);
57         void train_control_changed(const Train &, const std::string &, float);
58         void train_function_changed(const Train &, unsigned, bool);
59         void train_route_changed(const Train &, const Route *);
60         void train_status_changed(const Train &, const std::string &);
61
62         template<typename P>
63         void send(const P &);
64 };
65
66 } // namespace R2C2
67
68 #endif