From 990569c02a6d6fde3202a464b3ccb3084c672a1a Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 26 Sep 2014 15:47:32 +0300 Subject: [PATCH] builtin/network, interface/packet_stream: Catch unknown packet exceptions and log them as warnings --- builtin/network/network.cpp | 16 ++++++++++------ src/interface/packet_stream.h | 11 ++++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/builtin/network/network.cpp b/builtin/network/network.cpp index 08263da..20adc17 100644 --- a/builtin/network/network.cpp +++ b/builtin/network/network.cpp @@ -213,12 +213,16 @@ struct Module: public interface::Module, public network::Interface log_i(MODULE, "Received %zu bytes", r); peer.socket_buffer.insert(peer.socket_buffer.end(), buf, buf + r); - peer.packet_stream.input(peer.socket_buffer, - [&](const ss_ & name, const ss_ & data){ - // Emit event - m_server->emit_event(ss_()+"network:packet_received/"+name, - new Packet(peer.id, name, data)); - }); + try { + peer.packet_stream.input(peer.socket_buffer, + [&](const ss_ & name, const ss_ & data){ + // Emit event + m_server->emit_event(ss_()+"network:packet_received/"+name, + new Packet(peer.id, name, data)); + }); + } catch(interface::UnknownPacketReceived &e){ + log_w(MODULE, "%s", e.what()); + } } void send_u(Peer &peer, const ss_ &name, const ss_ &data) diff --git a/src/interface/packet_stream.h b/src/interface/packet_stream.h index 8cb5ef0..acf7b06 100644 --- a/src/interface/packet_stream.h +++ b/src/interface/packet_stream.h @@ -9,6 +9,11 @@ namespace interface { typedef size_t PacketType; + struct UnknownPacketReceived: public Exception { + ss_ msg; + UnknownPacketReceived(const ss_ &msg): Exception(msg){} + }; + struct OutgoingPacketTypeRegistry { sm_ m_types; @@ -32,7 +37,7 @@ namespace interface auto it = m_names.find(type); if(it != m_names.end()) return it->second; - throw Exception(ss_()+"Packet type not known: "+itos(type)); + throw UnknownPacketReceived(ss_()+"Packet type not known: "+itos(type)); } }; @@ -49,13 +54,13 @@ namespace interface auto it = m_types.find(name); if(it != m_types.end()) return it->second; - throw Exception(ss_()+"Packet not known: "+name); + throw UnknownPacketReceived(ss_()+"Packet not known: "+name); } ss_ get_name(PacketType type){ auto it = m_names.find(type); if(it != m_names.end()) return it->second; - throw Exception(ss_()+"Packet not known: "+itos(type)); + throw UnknownPacketReceived(ss_()+"Packet not known: "+itos(type)); } };