forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 29
/
inboundhandler.go
55 lines (43 loc) · 1.12 KB
/
inboundhandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package mocks
import (
"io"
"sync"
"github.com/v2ray/v2ray-core/app"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
)
type InboundConnectionHandler struct {
Port uint16
Dispatcher app.PacketDispatcher
ConnInput io.Reader
ConnOutput io.Writer
}
func (this *InboundConnectionHandler) Listen(port uint16) error {
this.Port = port
return nil
}
func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
ray := this.Dispatcher.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
}
func (this *InboundConnectionHandler) Create(dispatcher app.PacketDispatcher, config interface{}) (connhandler.InboundConnectionHandler, error) {
this.Dispatcher = dispatcher
return this, nil
}