X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fnetwork%2Fserver.cpp;h=d4e2049472c94caf42da31f7ac2042ecc0246e8d;hb=5c45baac3643a3c7ade8ab0dbaa97dfa55b5117d;hp=43d22ffef9016f26b376e691d840840bea13a940;hpb=fadc35133b1ebbd98af948c1fa529dcde6fddcfd;p=r2c2.git diff --git a/source/network/server.cpp b/source/network/server.cpp index 43d22ff..d4e2049 100644 --- a/source/network/server.cpp +++ b/source/network/server.cpp @@ -1,11 +1,6 @@ -/* $Id$ - -This file is part of R²C² -Copyright © 2009-2010 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - +#include #include +#include #include "libr2c2/driver.h" #include "libr2c2/route.h" #include "libr2c2/train.h" @@ -32,7 +27,8 @@ Server::Server(Layout &l): for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) train_added(*i->second); - listen_sock.listen(Net::InetAddr(0, 8315), 4); + RefPtr addr = Net::resolve("*", "8315", Net::INET); + listen_sock.listen(*addr, 4); listen_sock.signal_data_available.connect(sigc::mem_fun(this, &Server::incoming_connection)); } @@ -77,6 +73,10 @@ void Server::train_added(Train &train) pkt.loco_type = train.get_locomotive_type().get_article_number().str(); pkt.name = train.get_name(); send(pkt); + + TrainStatus *status = new TrainStatus(train); + status->set_tag("server:status"); + status->signal_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_status_changed), sigc::ref(train))); } void Server::train_control_changed(const Train &train, const string &control, float value) @@ -117,8 +117,17 @@ template void Server::send(const P &pkt) { for(vector::const_iterator i=connections.begin(); i!=connections.end(); ++i) - if(!(*i)->stale) - (*i)->comm.send(pkt); + if(!(*i)->stale && (*i)->comm.is_handshake_done()) + { + try + { + (*i)->comm.send(pkt); + } + catch(...) + { + (*i)->stale = true; + } + } } @@ -185,10 +194,11 @@ void Server::Connection::handshake_done() pkt.functions = train.get_functions(); comm.send(pkt); } + if(TrainStatus *status = dynamic_cast(train.get_tagged_ai("server:status"))) { TrainStatusPacket pkt; pkt.address = train.get_address(); - pkt.status = train.get_status(); + pkt.status = status->get_status(); comm.send(pkt); } if(train.get_route()) @@ -203,7 +213,6 @@ void Server::Connection::handshake_done() void Server::Connection::end_of_file() { - socket->close(); stale = true; } @@ -223,7 +232,7 @@ void Server::Connection::receive(const TrainControlPacket &pkt) Train &train = server.layout.get_train(pkt.address); train.set_control(pkt.control, pkt.value); } - catch(const Exception &e) + catch(const exception &e) { error(e.what()); } @@ -238,7 +247,7 @@ void Server::Connection::receive(const TrainFunctionPacket &pkt) if(((pkt.functions^train.get_functions())>>i)&1) train.set_function(i, (pkt.functions>>i)&1); } - catch(const Exception &e) + catch(const exception &e) { error(e.what()); } @@ -257,7 +266,7 @@ void Server::Connection::receive(const TrainRoutePacket &pkt) train.set_route(&route); } } - catch(const Exception &e) + catch(const exception &e) { error(e.what()); }