1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

fix tcp testing server

This commit is contained in:
Darien Raymond 2018-07-02 15:26:42 +02:00
parent 59819871a1
commit c4491d06d3
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -4,7 +4,10 @@ import (
"fmt" "fmt"
"io" "io"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/common/task"
"v2ray.com/core/transport/pipe"
) )
type Server struct { type Server struct {
@ -52,25 +55,40 @@ func (server *Server) handleConnection(conn net.Conn) {
if len(server.SendFirst) > 0 { if len(server.SendFirst) > 0 {
conn.Write(server.SendFirst) conn.Write(server.SendFirst)
} }
request := make([]byte, 4096)
for { pReader, pWriter := pipe.New(pipe.WithoutSizeLimit())
nBytes, err := conn.Read(request) err := task.Run(task.Parallel(func() error {
if err != nil { for {
if err != io.EOF { b := buf.New()
fmt.Println("Failed to read request:", err) 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]) }, func() error {
if _, err := conn.Write(response); err != nil { w := buf.NewWriter(conn)
fmt.Println("Failed to write response:", err) for {
break mb, err := pReader.ReadMultiBuffer()
} if err != nil {
if server.ShouldClose { return err
break }
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 { func (server *Server) Close() error {