From f6019623917bb45e5f17b7b868fecc89b30681b1 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Mon, 13 Jun 2016 06:16:44 +0800 Subject: [PATCH] KCP: discard useless conn in Accept --- transport/hub/kcp.go | 7 +++++++ transport/hub/tcp.go | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/transport/hub/kcp.go b/transport/hub/kcp.go index 1ceebc0c1..6bf88b100 100644 --- a/transport/hub/kcp.go +++ b/transport/hub/kcp.go @@ -19,6 +19,12 @@ type KCPVlistener struct { previousSocketid_mapid int } +/*Accept Accept a KCP connection +Since KCP is stateless, if package deliver after it was closed, +It could be reconized as a new connection and call accept. +If we can detect that the connection is of such a kind, +we will discard that conn. +*/ func (kvl *KCPVlistener) Accept() (net.Conn, error) { conn, err := kvl.lst.Accept() if err != nil { @@ -37,6 +43,7 @@ func (kvl *KCPVlistener) Accept() (net.Conn, error) { } } if badbit { + conn.Close() return nil, errors.New("KCP:ConnDup, Don't worry~") } else { kvl.previousSocketid_mapid++ diff --git a/transport/hub/tcp.go b/transport/hub/tcp.go index 8f63d3ab5..266f13d81 100644 --- a/transport/hub/tcp.go +++ b/transport/hub/tcp.go @@ -92,7 +92,7 @@ func (this *TCPHub) start() { if err != nil { if this.accepting { - log.Warning("Listener: Failed to accept new TCP connection: ", err) + log.info("Listener: Failed to accept new TCP connection: ", err) } continue }