2016-06-17 10:51:41 -04:00
|
|
|
package kcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2016-06-18 10:52:02 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/signal"
|
2016-06-17 10:51:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-06-17 17:50:08 -04:00
|
|
|
errTimeout = errors.New("i/o timeout")
|
|
|
|
errBrokenPipe = errors.New("broken pipe")
|
|
|
|
errClosedListener = errors.New("Listener closed.")
|
|
|
|
errClosedConnection = errors.New("Connection closed.")
|
2016-06-17 10:51:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-06-25 15:35:18 -04:00
|
|
|
headerSize uint32 = 2
|
2016-06-17 10:51:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type ConnState byte
|
|
|
|
|
|
|
|
var (
|
|
|
|
ConnStateActive ConnState = 0
|
|
|
|
ConnStateReadyToClose ConnState = 1
|
|
|
|
ConnStatePeerClosed ConnState = 2
|
|
|
|
ConnStateClosed ConnState = 4
|
|
|
|
)
|
|
|
|
|
|
|
|
func nowMillisec() int64 {
|
|
|
|
now := time.Now()
|
|
|
|
return now.Unix()*1000 + int64(now.Nanosecond()/1000000)
|
|
|
|
}
|
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
// Connection is a KCP connection over UDP.
|
|
|
|
type Connection struct {
|
2016-06-17 17:50:08 -04:00
|
|
|
sync.RWMutex
|
2016-07-02 05:19:32 -04:00
|
|
|
state ConnState
|
|
|
|
kcp *KCP // the core ARQ
|
|
|
|
kcpAccess sync.Mutex
|
|
|
|
block Authenticator
|
|
|
|
needUpdate bool
|
|
|
|
local, remote net.Addr
|
|
|
|
wd time.Time // write deadline
|
|
|
|
chReadEvent chan struct{}
|
|
|
|
writer io.WriteCloser
|
|
|
|
since int64
|
|
|
|
terminateOnce signal.Once
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
// NewConnection create a new KCP connection between local and remote.
|
2016-06-29 04:34:34 -04:00
|
|
|
func NewConnection(conv uint16, writerCloser io.WriteCloser, local *net.UDPAddr, remote *net.UDPAddr, block Authenticator) *Connection {
|
2016-06-18 10:34:04 -04:00
|
|
|
conn := new(Connection)
|
|
|
|
conn.local = local
|
|
|
|
conn.chReadEvent = make(chan struct{}, 1)
|
|
|
|
conn.remote = remote
|
|
|
|
conn.block = block
|
|
|
|
conn.writer = writerCloser
|
|
|
|
conn.since = nowMillisec()
|
2016-06-17 10:51:41 -04:00
|
|
|
|
2016-06-29 04:34:34 -04:00
|
|
|
authWriter := &AuthenticationWriter{
|
|
|
|
Authenticator: block,
|
|
|
|
Writer: writerCloser,
|
|
|
|
}
|
2016-07-02 05:19:32 -04:00
|
|
|
conn.kcp = NewKCP(conv, authWriter)
|
2016-06-25 11:32:09 -04:00
|
|
|
conn.kcp.NoDelay(effectiveConfig.Tti, 2, effectiveConfig.Congestion)
|
2016-06-18 10:34:04 -04:00
|
|
|
conn.kcp.current = conn.Elapsed()
|
2016-06-17 10:51:41 -04:00
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
go conn.updateTask()
|
2016-06-17 10:51:41 -04:00
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
return conn
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) Elapsed() uint32 {
|
2016-06-17 10:51:41 -04:00
|
|
|
return uint32(nowMillisec() - this.since)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements the Conn Read method.
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) Read(b []byte) (int, error) {
|
2016-06-30 08:51:49 -04:00
|
|
|
if this == nil || this.kcp.state == StateTerminating || this.kcp.state == StateTerminated {
|
2016-06-17 10:51:41 -04:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2016-07-02 15:26:50 -04:00
|
|
|
return this.kcp.receivingWorker.Read(b)
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements the Conn Write method.
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) Write(b []byte) (int, error) {
|
2016-06-29 04:34:34 -04:00
|
|
|
if this == nil || this.kcp.state != StateActive {
|
2016-06-17 10:51:41 -04:00
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
2016-06-26 17:51:17 -04:00
|
|
|
totalWritten := 0
|
2016-06-17 10:51:41 -04:00
|
|
|
|
|
|
|
for {
|
2016-06-18 10:34:04 -04:00
|
|
|
this.RLock()
|
2016-06-29 04:34:34 -04:00
|
|
|
if this == nil || this.kcp.state != StateActive {
|
2016-06-18 10:34:04 -04:00
|
|
|
this.RUnlock()
|
2016-06-26 17:51:17 -04:00
|
|
|
return totalWritten, io.ErrClosedPipe
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
2016-06-18 10:34:04 -04:00
|
|
|
this.RUnlock()
|
2016-06-17 10:51:41 -04:00
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
this.kcpAccess.Lock()
|
2016-06-26 17:51:17 -04:00
|
|
|
nBytes := this.kcp.Send(b[totalWritten:])
|
|
|
|
if nBytes > 0 {
|
|
|
|
totalWritten += nBytes
|
|
|
|
if totalWritten == len(b) {
|
|
|
|
this.kcpAccess.Unlock()
|
|
|
|
return totalWritten, nil
|
|
|
|
}
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
2016-06-26 17:51:17 -04:00
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
this.kcpAccess.Unlock()
|
2016-06-17 10:51:41 -04:00
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
if !this.wd.IsZero() && this.wd.Before(time.Now()) {
|
2016-06-26 17:51:17 -04:00
|
|
|
return totalWritten, errTimeout
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
2016-06-17 11:38:42 -04:00
|
|
|
|
2016-06-17 17:09:26 -04:00
|
|
|
// Sending windows is 1024 for the moment. This amount is not gonna sent in 1 sec.
|
|
|
|
time.Sleep(time.Second)
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the connection.
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) Close() error {
|
2016-06-29 04:34:34 -04:00
|
|
|
if this == nil ||
|
|
|
|
this.kcp.state == StateReadyToClose ||
|
|
|
|
this.kcp.state == StateTerminating ||
|
|
|
|
this.kcp.state == StateTerminated {
|
2016-06-17 17:50:08 -04:00
|
|
|
return errClosedConnection
|
|
|
|
}
|
2016-06-18 10:34:04 -04:00
|
|
|
log.Debug("KCP|Connection: Closing connection to ", this.remote)
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
|
2016-06-29 04:34:34 -04:00
|
|
|
this.kcpAccess.Lock()
|
|
|
|
this.kcp.OnClose()
|
|
|
|
this.kcpAccess.Unlock()
|
2016-06-17 10:51:41 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) LocalAddr() net.Addr {
|
|
|
|
if this == nil {
|
2016-06-17 17:50:08 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-06-18 10:34:04 -04:00
|
|
|
return this.local
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address. The Addr returned is shared by all invocations of RemoteAddr, so do not modify it.
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) RemoteAddr() net.Addr {
|
|
|
|
if this == nil {
|
2016-06-17 17:50:08 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-06-18 10:34:04 -04:00
|
|
|
return this.remote
|
2016-06-17 17:50:08 -04:00
|
|
|
}
|
2016-06-17 10:51:41 -04:00
|
|
|
|
|
|
|
// SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) SetDeadline(t time.Time) error {
|
2016-06-30 08:51:49 -04:00
|
|
|
if err := this.SetReadDeadline(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := this.SetWriteDeadline(t); err != nil {
|
|
|
|
return err
|
2016-06-17 17:50:08 -04:00
|
|
|
}
|
2016-06-17 10:51:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadDeadline implements the Conn SetReadDeadline method.
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) SetReadDeadline(t time.Time) error {
|
2016-06-29 04:34:34 -04:00
|
|
|
if this == nil || this.kcp.state != StateActive {
|
2016-06-17 17:50:08 -04:00
|
|
|
return errClosedConnection
|
|
|
|
}
|
2016-06-30 08:51:49 -04:00
|
|
|
this.kcpAccess.Lock()
|
|
|
|
defer this.kcpAccess.Unlock()
|
2016-07-02 15:26:50 -04:00
|
|
|
this.kcp.receivingWorker.SetReadDeadline(t)
|
2016-06-17 10:51:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteDeadline implements the Conn SetWriteDeadline method.
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) SetWriteDeadline(t time.Time) error {
|
2016-06-29 04:34:34 -04:00
|
|
|
if this == nil || this.kcp.state != StateActive {
|
2016-06-17 17:50:08 -04:00
|
|
|
return errClosedConnection
|
|
|
|
}
|
2016-06-18 10:34:04 -04:00
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
this.wd = t
|
2016-06-17 10:51:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// kcp update, input loop
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) updateTask() {
|
2016-06-29 04:34:34 -04:00
|
|
|
for this.kcp.state != StateTerminated {
|
2016-06-18 10:34:04 -04:00
|
|
|
current := this.Elapsed()
|
|
|
|
this.kcpAccess.Lock()
|
|
|
|
this.kcp.Update(current)
|
|
|
|
this.kcpAccess.Unlock()
|
2016-06-29 04:34:34 -04:00
|
|
|
|
|
|
|
interval := time.Duration(effectiveConfig.Tti) * time.Millisecond
|
|
|
|
if this.kcp.state == StateTerminating {
|
|
|
|
interval = time.Second
|
|
|
|
}
|
|
|
|
time.Sleep(interval)
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
2016-06-29 04:34:34 -04:00
|
|
|
this.Terminate()
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) notifyReadEvent() {
|
2016-06-17 10:51:41 -04:00
|
|
|
select {
|
2016-06-18 10:34:04 -04:00
|
|
|
case this.chReadEvent <- struct{}{}:
|
2016-06-17 10:51:41 -04:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) kcpInput(data []byte) {
|
|
|
|
this.kcpAccess.Lock()
|
|
|
|
this.kcp.current = this.Elapsed()
|
2016-06-29 04:34:34 -04:00
|
|
|
this.kcp.Input(data)
|
2016-06-17 11:38:42 -04:00
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
this.kcpAccess.Unlock()
|
|
|
|
this.notifyReadEvent()
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) FetchInputFrom(conn net.Conn) {
|
2016-06-17 10:51:41 -04:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
payload := alloc.NewBuffer()
|
|
|
|
nBytes, err := conn.Read(payload.Value)
|
|
|
|
if err != nil {
|
2016-06-29 04:34:34 -04:00
|
|
|
payload.Release()
|
2016-06-17 10:51:41 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
payload.Slice(0, nBytes)
|
|
|
|
if this.block.Open(payload) {
|
|
|
|
this.kcpInput(payload.Value)
|
2016-06-17 11:38:42 -04:00
|
|
|
} else {
|
|
|
|
log.Info("KCP|Connection: Invalid response from ", conn.RemoteAddr())
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
payload.Release()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) Reusable() bool {
|
2016-06-17 10:51:41 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-06-18 10:34:04 -04:00
|
|
|
func (this *Connection) SetReusable(b bool) {}
|
2016-06-29 04:34:34 -04:00
|
|
|
|
|
|
|
func (this *Connection) Terminate() {
|
|
|
|
if this == nil || this.writer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Info("Terminating connection to ", this.RemoteAddr())
|
|
|
|
|
|
|
|
this.writer.Close()
|
|
|
|
}
|