From c4491d06d379d5a5a33bb628b8dfd085c915028e Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 2 Jul 2018 15:26:42 +0200 Subject: [PATCH] fix tcp testing server --- testing/servers/tcp/tcp.go | 48 ++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/testing/servers/tcp/tcp.go b/testing/servers/tcp/tcp.go index 9ca666f63..4f970c973 100644 --- a/testing/servers/tcp/tcp.go +++ b/testing/servers/tcp/tcp.go @@ -4,7 +4,10 @@ import ( "fmt" "io" + "v2ray.com/core/common/buf" "v2ray.com/core/common/net" + "v2ray.com/core/common/task" + "v2ray.com/core/transport/pipe" ) type Server struct { @@ -52,25 +55,40 @@ func (server *Server) handleConnection(conn net.Conn) { if len(server.SendFirst) > 0 { conn.Write(server.SendFirst) } - request := make([]byte, 4096) - for { - nBytes, err := conn.Read(request) - if err != nil { - if err != io.EOF { - fmt.Println("Failed to read request:", err) + + pReader, pWriter := pipe.New(pipe.WithoutSizeLimit()) + err := task.Run(task.Parallel(func() error { + for { + b := buf.New() + if err := b.AppendSupplier(buf.ReadFrom(conn)); err != nil { + if err == io.EOF { + return nil + } + return err + } + copy(b.Bytes(), server.MsgProcessor(b.Bytes())) + if err := pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)); err != nil { + return err } - break } - response := server.MsgProcessor(request[:nBytes]) - if _, err := conn.Write(response); err != nil { - fmt.Println("Failed to write response:", err) - break - } - if server.ShouldClose { - break + }, func() error { + w := buf.NewWriter(conn) + for { + mb, err := pReader.ReadMultiBuffer() + if err != nil { + return err + } + if err := w.WriteMultiBuffer(mb); err != nil { + return err + } } + }))() + + if err != nil { + fmt.Println("failed to transfer data: ", err.Error()) } - conn.Close() + + conn.Close() // nolint: errcheck } func (server *Server) Close() error {