2015-11-01 15:15:08 -05:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
"github.com/v2ray/v2ray-core/app/dispatcher"
|
2016-01-29 08:39:55 -05:00
|
|
|
v2io "github.com/v2ray/v2ray-core/common/io"
|
2015-11-01 15:15:08 -05:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InboundConnectionHandler struct {
|
2016-01-31 11:01:28 -05:00
|
|
|
port v2net.Port
|
|
|
|
PacketDispatcher dispatcher.PacketDispatcher
|
|
|
|
ConnInput io.Reader
|
|
|
|
ConnOutput io.Writer
|
2015-11-01 15:15:08 -05:00
|
|
|
}
|
|
|
|
|
2015-12-02 15:44:01 -05:00
|
|
|
func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
|
2016-01-19 17:41:40 -05:00
|
|
|
this.port = port
|
2015-11-01 15:15:08 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-19 17:41:40 -05:00
|
|
|
func (this *InboundConnectionHandler) Port() v2net.Port {
|
|
|
|
return this.port
|
|
|
|
}
|
|
|
|
|
2016-01-03 17:30:37 -05:00
|
|
|
func (this *InboundConnectionHandler) Close() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-01 15:15:08 -05:00
|
|
|
func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
|
2016-01-31 11:01:28 -05:00
|
|
|
ray := this.PacketDispatcher.DispatchToOutbound(packet)
|
2015-11-01 15:15:08 -05:00
|
|
|
|
|
|
|
input := ray.InboundInput()
|
|
|
|
output := ray.InboundOutput()
|
|
|
|
|
|
|
|
readFinish := &sync.Mutex{}
|
|
|
|
writeFinish := &sync.Mutex{}
|
|
|
|
|
|
|
|
readFinish.Lock()
|
|
|
|
writeFinish.Lock()
|
|
|
|
|
|
|
|
go func() {
|
2016-01-29 08:39:55 -05:00
|
|
|
v2io.RawReaderToChan(input, this.ConnInput)
|
2015-11-01 15:15:08 -05:00
|
|
|
close(input)
|
|
|
|
readFinish.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2016-02-01 06:22:29 -05:00
|
|
|
v2io.ChanToRawWriter(this.ConnOutput, output)
|
2015-11-01 15:15:08 -05:00
|
|
|
writeFinish.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
readFinish.Lock()
|
|
|
|
writeFinish.Lock()
|
|
|
|
return nil
|
|
|
|
}
|