]> git.tdb.fi Git - r2c2.git/blobdiff - source/network/server.cpp
Bugfixes for the network server
[r2c2.git] / source / network / server.cpp
index a892dedd25a85c79e9ba8edd1f3c60db767855a6..588734ec039d83965e6cae4a8fff7c7cc11cb45e 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/core/refptr.h>
 #include <msp/net/inet.h>
 #include <msp/net/resolve.h>
+#include "libr2c2/aicontrol.h"
 #include "libr2c2/catalogue.h"
 #include "libr2c2/driver.h"
 #include "libr2c2/route.h"
@@ -100,7 +101,7 @@ void Server::train_function_changed(const Train &train, unsigned, bool)
        send(pkt);
 }
 
-void Server::train_ai_event(const Train &train, TrainAI &, const TrainAI::Message &ev)
+void Server::train_ai_event(const Train &train, TrainAI &ai, const TrainAI::Message &ev)
 {
        if(ev.type=="route-changed")
        {
@@ -117,6 +118,18 @@ void Server::train_ai_event(const Train &train, TrainAI &, const TrainAI::Messag
                pkt.status = ev.value.value<std::string>();
                send(pkt);
        }
+       else if(ev.type=="target-speed-changed" || ev.type=="reverse-changed")
+       {
+               AIControl *control = dynamic_cast<AIControl *>(&ai);
+               if(control)
+               {
+                       TrainAIControlPacket pkt;
+                       pkt.address = train.get_address();
+                       pkt.target_speed = control->get_target_speed();
+                       pkt.reverse = control->get_reverse();
+                       send(pkt);
+               }
+       }
 }
 
 template<typename P>
@@ -124,26 +137,19 @@ void Server::send(const P &pkt)
 {
        for(vector<Connection *>::const_iterator i=connections.begin(); i!=connections.end(); ++i)
                if(!(*i)->stale && (*i)->comm.is_handshake_done())
-               {
-                       try
-                       {
-                               (*i)->comm.send(pkt);
-                       }
-                       catch(...)
-                       {
-                               (*i)->stale = true;
-                       }
-               }
+                       (*i)->comm.send(pkt);
 }
 
 
 Server::Connection::Connection(Server &s, Net::StreamSocket *o):
        server(s),
        socket(o),
-       comm(*socket, server.proto, *this)
+       comm(*socket, server.proto, *this),
+       stale(false)
 {
        socket->signal_end_of_file.connect(sigc::mem_fun(this, &Connection::end_of_file));
        comm.signal_handshake_done.connect(sigc::mem_fun(this, &Connection::handshake_done));
+       comm.signal_error.connect(sigc::mem_fun(this, &Connection::comm_error));
        comm.initiate_handshake();
 }
 
@@ -183,6 +189,14 @@ void Server::Connection::handshake_done()
                        pkt.name = train.get_name();
                        comm.send(pkt);
                }
+               if(AIControl *control = train.get_ai_of_type<AIControl>())
+               {
+                       TrainAIControlPacket pkt;
+                       pkt.address = train.get_address();
+                       pkt.target_speed = control->get_target_speed();
+                       pkt.reverse = control->get_reverse();
+                       comm.send(pkt);
+               }
                for(unsigned j=0;; ++j)
                {
                        const char *name = train.get_controller().enumerate_controls(j);
@@ -260,6 +274,20 @@ void Server::Connection::receive(const TrainFunctionPacket &pkt)
        }
 }
 
+void Server::Connection::receive(const TrainAIControlPacket &pkt)
+{
+       try
+       {
+               Train &train = server.layout.get_train(pkt.address);
+               train.ai_message(TrainAI::Message("set-target-speed", pkt.target_speed));
+               train.ai_message(TrainAI::Message("set-reverse", static_cast<bool>(pkt.reverse)));
+       }
+       catch(const exception &e)
+       {
+               error(e.what());
+       }
+}
+
 void Server::Connection::receive(const TrainRoutePacket &pkt)
 {
        try
@@ -279,6 +307,11 @@ void Server::Connection::receive(const TrainRoutePacket &pkt)
        }
 }
 
+void Server::Connection::comm_error(const exception &)
+{
+       stale = true;
+}
+
 void Server::Connection::error(const string &msg)
 {
        ErrorPacket pkt;