mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-18 10:26:01 -05:00
58 lines
1.0 KiB
Go
58 lines
1.0 KiB
Go
package mocks
|
|
|
|
import (
|
|
"io"
|
|
"sync"
|
|
|
|
"github.com/v2ray/v2ray-core/app"
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
)
|
|
|
|
type InboundConnectionHandler struct {
|
|
port v2net.Port
|
|
Space app.Space
|
|
ConnInput io.Reader
|
|
ConnOutput io.Writer
|
|
}
|
|
|
|
func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
|
|
this.port = port
|
|
return nil
|
|
}
|
|
|
|
func (this *InboundConnectionHandler) Port() v2net.Port {
|
|
return this.port
|
|
}
|
|
|
|
func (this *InboundConnectionHandler) Close() {
|
|
|
|
}
|
|
|
|
func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
|
|
ray := this.Space.PacketDispatcher().DispatchToOutbound(packet)
|
|
|
|
input := ray.InboundInput()
|
|
output := ray.InboundOutput()
|
|
|
|
readFinish := &sync.Mutex{}
|
|
writeFinish := &sync.Mutex{}
|
|
|
|
readFinish.Lock()
|
|
writeFinish.Lock()
|
|
|
|
go func() {
|
|
v2net.ReaderToChan(input, this.ConnInput)
|
|
close(input)
|
|
readFinish.Unlock()
|
|
}()
|
|
|
|
go func() {
|
|
v2net.ChanToWriter(this.ConnOutput, output)
|
|
writeFinish.Unlock()
|
|
}()
|
|
|
|
readFinish.Lock()
|
|
writeFinish.Lock()
|
|
return nil
|
|
}
|