1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/testing/servers/tcp/tcp.go
2015-12-16 23:53:38 +01:00

59 lines
1.2 KiB
Go

package tcp
import (
"fmt"
"net"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type Server struct {
Port v2net.Port
MsgProcessor func(msg []byte) []byte
accepting bool
}
func (server *Server) Start() (v2net.Destination, error) {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: int(server.Port),
Zone: "",
})
if err != nil {
return nil, err
}
go server.acceptConnections(listener)
localAddr := listener.Addr().(*net.TCPAddr)
return v2net.TCPDestination(v2net.IPAddress(localAddr.IP), v2net.Port(localAddr.Port)), nil
}
func (server *Server) acceptConnections(listener *net.TCPListener) {
server.accepting = true
defer listener.Close()
for server.accepting {
conn, err := listener.Accept()
if err != nil {
fmt.Printf("Failed accept TCP connection: %v", err)
continue
}
go server.handleConnection(conn)
}
}
func (server *Server) handleConnection(conn net.Conn) {
for true {
request, err := v2net.ReadFrom(conn, nil)
if err != nil {
break
}
response := server.MsgProcessor(request.Value)
conn.Write(response)
}
conn.Close()
}
func (this *Server) Close() {
this.accepting = true
}