1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-01 16:26:02 -04:00
v2fly/app/commander/outbound.go

108 lines
1.9 KiB
Go
Raw Normal View History

2018-02-05 17:38:24 -05:00
package commander
import (
"context"
"net"
2018-02-08 09:39:46 -05:00
"sync"
2018-02-05 17:38:24 -05:00
2018-04-04 15:32:40 -04:00
"v2ray.com/core/common"
2018-02-05 17:38:24 -05:00
"v2ray.com/core/common/signal"
"v2ray.com/core/transport/ray"
)
type OutboundListener struct {
buffer chan net.Conn
2018-02-08 17:37:47 -05:00
done *signal.Done
2018-02-05 17:38:24 -05:00
}
func (l *OutboundListener) add(conn net.Conn) {
select {
case l.buffer <- conn:
2018-02-08 17:37:47 -05:00
case <-l.done.C():
conn.Close()
2018-02-05 17:38:24 -05:00
default:
conn.Close()
}
}
2018-04-04 15:32:40 -04:00
// Accept implements net.Listener.
2018-02-05 17:38:24 -05:00
func (l *OutboundListener) Accept() (net.Conn, error) {
2018-02-08 17:37:47 -05:00
select {
case <-l.done.C():
2018-03-14 22:32:10 -04:00
return nil, newError("listen closed")
2018-02-08 17:37:47 -05:00
case c := <-l.buffer:
return c, nil
2018-02-05 17:38:24 -05:00
}
}
2018-04-04 15:32:40 -04:00
// Close implement net.Listener.
2018-02-05 17:38:24 -05:00
func (l *OutboundListener) Close() error {
2018-04-04 15:32:40 -04:00
common.Must(l.done.Close())
2018-02-08 17:37:47 -05:00
L:
for {
select {
case c := <-l.buffer:
c.Close()
default:
break L
}
}
2018-02-05 17:38:24 -05:00
return nil
}
2018-04-04 15:32:40 -04:00
// Addr implements net.Listener.
2018-02-05 17:38:24 -05:00
func (l *OutboundListener) Addr() net.Addr {
return &net.TCPAddr{
IP: net.IP{0, 0, 0, 0},
Port: 0,
}
}
2018-04-04 15:32:40 -04:00
// Outbound is a core.OutboundHandler that handles gRPC connections.
type Outbound struct {
2018-02-05 17:38:24 -05:00
tag string
listener *OutboundListener
2018-02-08 09:39:46 -05:00
access sync.RWMutex
closed bool
2018-02-05 17:38:24 -05:00
}
2018-04-03 18:57:44 -04:00
// Dispatch implements core.OutboundHandler.
2018-04-04 15:32:40 -04:00
func (co *Outbound) Dispatch(ctx context.Context, r ray.OutboundRay) {
2018-02-08 09:39:46 -05:00
co.access.RLock()
if co.closed {
r.OutboundInput().CloseError()
r.OutboundOutput().CloseError()
co.access.RUnlock()
return
}
2018-02-05 17:38:24 -05:00
closeSignal := signal.NewNotifier()
c := ray.NewConnection(r.OutboundInput(), r.OutboundOutput(), ray.ConnCloseSignal(closeSignal))
co.listener.add(c)
2018-02-08 09:39:46 -05:00
co.access.RUnlock()
2018-02-05 17:38:24 -05:00
<-closeSignal.Wait()
}
2018-04-03 18:57:44 -04:00
// Tag implements core.OutboundHandler.
2018-04-04 15:32:40 -04:00
func (co *Outbound) Tag() string {
2018-02-05 17:38:24 -05:00
return co.tag
}
2018-04-03 18:57:44 -04:00
// Start implements common.Runnable.
2018-04-04 15:32:40 -04:00
func (co *Outbound) Start() error {
2018-02-08 09:39:46 -05:00
co.access.Lock()
co.closed = false
co.access.Unlock()
2018-02-05 17:38:24 -05:00
return nil
}
2018-04-03 18:57:44 -04:00
// Close implements common.Closable.
2018-04-04 15:32:40 -04:00
func (co *Outbound) Close() error {
2018-02-08 09:39:46 -05:00
co.access.Lock()
2018-04-04 15:32:40 -04:00
defer co.access.Unlock()
2018-02-08 09:39:46 -05:00
2018-04-04 15:32:40 -04:00
co.closed = true
return co.listener.Close()
2018-02-08 09:39:46 -05:00
}