builtin/network, interface/packet_stream: Catch unknown packet exceptions and log them as warnings

This commit is contained in:
Perttu Ahola 2014-09-26 15:47:32 +03:00
parent 44596b6d13
commit 990569c02a
2 changed files with 18 additions and 9 deletions

View File

@ -213,12 +213,16 @@ struct Module: public interface::Module, public network::Interface
log_i(MODULE, "Received %zu bytes", r); log_i(MODULE, "Received %zu bytes", r);
peer.socket_buffer.insert(peer.socket_buffer.end(), buf, buf + r); peer.socket_buffer.insert(peer.socket_buffer.end(), buf, buf + r);
try {
peer.packet_stream.input(peer.socket_buffer, peer.packet_stream.input(peer.socket_buffer,
[&](const ss_ & name, const ss_ & data){ [&](const ss_ & name, const ss_ & data){
// Emit event // Emit event
m_server->emit_event(ss_()+"network:packet_received/"+name, m_server->emit_event(ss_()+"network:packet_received/"+name,
new Packet(peer.id, name, data)); 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) void send_u(Peer &peer, const ss_ &name, const ss_ &data)

View File

@ -9,6 +9,11 @@ namespace interface
{ {
typedef size_t PacketType; typedef size_t PacketType;
struct UnknownPacketReceived: public Exception {
ss_ msg;
UnknownPacketReceived(const ss_ &msg): Exception(msg){}
};
struct OutgoingPacketTypeRegistry struct OutgoingPacketTypeRegistry
{ {
sm_<ss_, PacketType> m_types; sm_<ss_, PacketType> m_types;
@ -32,7 +37,7 @@ namespace interface
auto it = m_names.find(type); auto it = m_names.find(type);
if(it != m_names.end()) if(it != m_names.end())
return it->second; 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); auto it = m_types.find(name);
if(it != m_types.end()) if(it != m_types.end())
return it->second; return it->second;
throw Exception(ss_()+"Packet not known: "+name); throw UnknownPacketReceived(ss_()+"Packet not known: "+name);
} }
ss_ get_name(PacketType type){ ss_ get_name(PacketType type){
auto it = m_names.find(type); auto it = m_names.find(type);
if(it != m_names.end()) if(it != m_names.end())
return it->second; return it->second;
throw Exception(ss_()+"Packet not known: "+itos(type)); throw UnknownPacketReceived(ss_()+"Packet not known: "+itos(type));
} }
}; };