| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 | 
							- package kcp
 
- import (
 
- 	"errors"
 
- 	"io"
 
- 	"net"
 
- 	"sync"
 
- 	"sync/atomic"
 
- 	"time"
 
- 	"v2ray.com/core/common/alloc"
 
- 	"v2ray.com/core/common/log"
 
- 	"v2ray.com/core/transport/internet"
 
- )
 
- var (
 
- 	ErrIOTimeout        = errors.New("Read/Write timeout")
 
- 	ErrClosedListener   = errors.New("Listener closed.")
 
- 	ErrClosedConnection = errors.New("Connection closed.")
 
- )
 
- type State int32
 
- func (this State) Is(states ...State) bool {
 
- 	for _, state := range states {
 
- 		if this == state {
 
- 			return true
 
- 		}
 
- 	}
 
- 	return false
 
- }
 
- const (
 
- 	StateActive          State = 0
 
- 	StateReadyToClose    State = 1
 
- 	StatePeerClosed      State = 2
 
- 	StateTerminating     State = 3
 
- 	StatePeerTerminating State = 4
 
- 	StateTerminated      State = 5
 
- )
 
- const (
 
- 	headerSize uint32 = 2
 
- )
 
- func nowMillisec() int64 {
 
- 	now := time.Now()
 
- 	return now.Unix()*1000 + int64(now.Nanosecond()/1000000)
 
- }
 
- type RoundTripInfo struct {
 
- 	sync.RWMutex
 
- 	variation        uint32
 
- 	srtt             uint32
 
- 	rto              uint32
 
- 	minRtt           uint32
 
- 	updatedTimestamp uint32
 
- }
 
- func (this *RoundTripInfo) UpdatePeerRTO(rto uint32, current uint32) {
 
- 	this.Lock()
 
- 	defer this.Unlock()
 
- 	if current-this.updatedTimestamp < 3000 {
 
- 		return
 
- 	}
 
- 	this.updatedTimestamp = current
 
- 	this.rto = rto
 
- }
 
- func (this *RoundTripInfo) Update(rtt uint32, current uint32) {
 
- 	if rtt > 0x7FFFFFFF {
 
- 		return
 
- 	}
 
- 	this.Lock()
 
- 	defer this.Unlock()
 
- 	// https://tools.ietf.org/html/rfc6298
 
- 	if this.srtt == 0 {
 
- 		this.srtt = rtt
 
- 		this.variation = rtt / 2
 
- 	} else {
 
- 		delta := rtt - this.srtt
 
- 		if this.srtt > rtt {
 
- 			delta = this.srtt - rtt
 
- 		}
 
- 		this.variation = (3*this.variation + delta) / 4
 
- 		this.srtt = (7*this.srtt + rtt) / 8
 
- 		if this.srtt < this.minRtt {
 
- 			this.srtt = this.minRtt
 
- 		}
 
- 	}
 
- 	var rto uint32
 
- 	if this.minRtt < 4*this.variation {
 
- 		rto = this.srtt + 4*this.variation
 
- 	} else {
 
- 		rto = this.srtt + this.variation
 
- 	}
 
- 	if rto > 10000 {
 
- 		rto = 10000
 
- 	}
 
- 	this.rto = rto * 3 / 2
 
- 	this.updatedTimestamp = current
 
- }
 
- func (this *RoundTripInfo) Timeout() uint32 {
 
- 	this.RLock()
 
- 	defer this.RUnlock()
 
- 	return this.rto
 
- }
 
- func (this *RoundTripInfo) SmoothedTime() uint32 {
 
- 	this.RLock()
 
- 	defer this.RUnlock()
 
- 	return this.srtt
 
- }
 
- // Connection is a KCP connection over UDP.
 
- type Connection struct {
 
- 	block          internet.Authenticator
 
- 	local, remote  net.Addr
 
- 	rd             time.Time
 
- 	wd             time.Time // write deadline
 
- 	writer         io.WriteCloser
 
- 	since          int64
 
- 	dataInputCond  *sync.Cond
 
- 	dataOutputCond *sync.Cond
 
- 	conv             uint16
 
- 	state            State
 
- 	stateBeginTime   uint32
 
- 	lastIncomingTime uint32
 
- 	lastPingTime     uint32
 
- 	mss       uint32
 
- 	roundTrip *RoundTripInfo
 
- 	interval  uint32
 
- 	receivingWorker *ReceivingWorker
 
- 	sendingWorker   *SendingWorker
 
- 	fastresend        uint32
 
- 	congestionControl bool
 
- 	output            *BufferedSegmentWriter
 
- }
 
- // NewConnection create a new KCP connection between local and remote.
 
- func NewConnection(conv uint16, writerCloser io.WriteCloser, local *net.UDPAddr, remote *net.UDPAddr, block internet.Authenticator) *Connection {
 
- 	log.Info("KCP|Connection: creating connection ", conv)
 
- 	conn := new(Connection)
 
- 	conn.local = local
 
- 	conn.remote = remote
 
- 	conn.block = block
 
- 	conn.writer = writerCloser
 
- 	conn.since = nowMillisec()
 
- 	conn.dataInputCond = sync.NewCond(new(sync.Mutex))
 
- 	conn.dataOutputCond = sync.NewCond(new(sync.Mutex))
 
- 	authWriter := &AuthenticationWriter{
 
- 		Authenticator: block,
 
- 		Writer:        writerCloser,
 
- 	}
 
- 	conn.conv = conv
 
- 	conn.output = NewSegmentWriter(authWriter)
 
- 	conn.mss = authWriter.Mtu() - DataSegmentOverhead
 
- 	conn.roundTrip = &RoundTripInfo{
 
- 		rto:    100,
 
- 		minRtt: effectiveConfig.Tti,
 
- 	}
 
- 	conn.interval = effectiveConfig.Tti
 
- 	conn.receivingWorker = NewReceivingWorker(conn)
 
- 	conn.fastresend = 2
 
- 	conn.congestionControl = effectiveConfig.Congestion
 
- 	conn.sendingWorker = NewSendingWorker(conn)
 
- 	go conn.updateTask()
 
- 	return conn
 
- }
 
- func (this *Connection) Elapsed() uint32 {
 
- 	return uint32(nowMillisec() - this.since)
 
- }
 
- // Read implements the Conn Read method.
 
- func (this *Connection) Read(b []byte) (int, error) {
 
- 	if this == nil {
 
- 		return 0, io.EOF
 
- 	}
 
- 	for {
 
- 		if this.State().Is(StateReadyToClose, StateTerminating, StateTerminated) {
 
- 			return 0, io.EOF
 
- 		}
 
- 		nBytes := this.receivingWorker.Read(b)
 
- 		if nBytes > 0 {
 
- 			return nBytes, nil
 
- 		}
 
- 		if this.State() == StatePeerTerminating {
 
- 			return 0, io.EOF
 
- 		}
 
- 		var timer *time.Timer
 
- 		if !this.rd.IsZero() {
 
- 			duration := this.rd.Sub(time.Now())
 
- 			if duration <= 0 {
 
- 				return 0, ErrIOTimeout
 
- 			}
 
- 			timer = time.AfterFunc(duration, this.dataInputCond.Signal)
 
- 		}
 
- 		this.dataInputCond.L.Lock()
 
- 		this.dataInputCond.Wait()
 
- 		this.dataInputCond.L.Unlock()
 
- 		if timer != nil {
 
- 			timer.Stop()
 
- 		}
 
- 		if !this.rd.IsZero() && this.rd.Before(time.Now()) {
 
- 			return 0, ErrIOTimeout
 
- 		}
 
- 	}
 
- }
 
- // Write implements the Conn Write method.
 
- func (this *Connection) Write(b []byte) (int, error) {
 
- 	totalWritten := 0
 
- 	for {
 
- 		if this == nil || this.State() != StateActive {
 
- 			return totalWritten, io.ErrClosedPipe
 
- 		}
 
- 		nBytes := this.sendingWorker.Push(b[totalWritten:])
 
- 		if nBytes > 0 {
 
- 			totalWritten += nBytes
 
- 			if totalWritten == len(b) {
 
- 				return totalWritten, nil
 
- 			}
 
- 		}
 
- 		var timer *time.Timer
 
- 		if !this.wd.IsZero() {
 
- 			duration := this.wd.Sub(time.Now())
 
- 			if duration <= 0 {
 
- 				return totalWritten, ErrIOTimeout
 
- 			}
 
- 			timer = time.AfterFunc(duration, this.dataOutputCond.Signal)
 
- 		}
 
- 		this.dataOutputCond.L.Lock()
 
- 		this.dataOutputCond.Wait()
 
- 		this.dataOutputCond.L.Unlock()
 
- 		if timer != nil {
 
- 			timer.Stop()
 
- 		}
 
- 		if !this.wd.IsZero() && this.wd.Before(time.Now()) {
 
- 			return totalWritten, ErrIOTimeout
 
- 		}
 
- 	}
 
- }
 
- func (this *Connection) SetState(state State) {
 
- 	current := this.Elapsed()
 
- 	atomic.StoreInt32((*int32)(&this.state), int32(state))
 
- 	atomic.StoreUint32(&this.stateBeginTime, current)
 
- 	log.Debug("KCP|Connection: #", this.conv, " entering state ", state, " at ", current)
 
- 	switch state {
 
- 	case StateReadyToClose:
 
- 		this.receivingWorker.CloseRead()
 
- 	case StatePeerClosed:
 
- 		this.sendingWorker.CloseWrite()
 
- 	case StateTerminating:
 
- 		this.receivingWorker.CloseRead()
 
- 		this.sendingWorker.CloseWrite()
 
- 	case StatePeerTerminating:
 
- 		this.sendingWorker.CloseWrite()
 
- 	case StateTerminated:
 
- 		this.receivingWorker.CloseRead()
 
- 		this.sendingWorker.CloseWrite()
 
- 	}
 
- }
 
- // Close closes the connection.
 
- func (this *Connection) Close() error {
 
- 	if this == nil {
 
- 		return ErrClosedConnection
 
- 	}
 
- 	this.dataInputCond.Broadcast()
 
- 	this.dataOutputCond.Broadcast()
 
- 	state := this.State()
 
- 	if state.Is(StateReadyToClose, StateTerminating, StateTerminated) {
 
- 		return ErrClosedConnection
 
- 	}
 
- 	log.Info("KCP|Connection: Closing connection to ", this.remote)
 
- 	if state == StateActive {
 
- 		this.SetState(StateReadyToClose)
 
- 	}
 
- 	if state == StatePeerClosed {
 
- 		this.SetState(StateTerminating)
 
- 	}
 
- 	if state == StatePeerTerminating {
 
- 		this.SetState(StateTerminated)
 
- 	}
 
- 	return nil
 
- }
 
- // LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.
 
- func (this *Connection) LocalAddr() net.Addr {
 
- 	if this == nil {
 
- 		return nil
 
- 	}
 
- 	return this.local
 
- }
 
- // RemoteAddr returns the remote network address. The Addr returned is shared by all invocations of RemoteAddr, so do not modify it.
 
- func (this *Connection) RemoteAddr() net.Addr {
 
- 	if this == nil {
 
- 		return nil
 
- 	}
 
- 	return this.remote
 
- }
 
- // SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.
 
- func (this *Connection) SetDeadline(t time.Time) error {
 
- 	if err := this.SetReadDeadline(t); err != nil {
 
- 		return err
 
- 	}
 
- 	if err := this.SetWriteDeadline(t); err != nil {
 
- 		return err
 
- 	}
 
- 	return nil
 
- }
 
- // SetReadDeadline implements the Conn SetReadDeadline method.
 
- func (this *Connection) SetReadDeadline(t time.Time) error {
 
- 	if this == nil || this.State() != StateActive {
 
- 		return ErrClosedConnection
 
- 	}
 
- 	this.rd = t
 
- 	return nil
 
- }
 
- // SetWriteDeadline implements the Conn SetWriteDeadline method.
 
- func (this *Connection) SetWriteDeadline(t time.Time) error {
 
- 	if this == nil || this.State() != StateActive {
 
- 		return ErrClosedConnection
 
- 	}
 
- 	this.wd = t
 
- 	return nil
 
- }
 
- // kcp update, input loop
 
- func (this *Connection) updateTask() {
 
- 	for this.State() != StateTerminated {
 
- 		this.flush()
 
- 		interval := time.Duration(effectiveConfig.Tti) * time.Millisecond
 
- 		if this.State() == StateTerminating {
 
- 			interval = time.Second
 
- 		}
 
- 		time.Sleep(interval)
 
- 	}
 
- 	this.Terminate()
 
- }
 
- func (this *Connection) FetchInputFrom(conn io.Reader) {
 
- 	go func() {
 
- 		payload := alloc.NewLocalBuffer(2048)
 
- 		defer payload.Release()
 
- 		for this.State() != StateTerminated {
 
- 			payload.Reset()
 
- 			nBytes, err := conn.Read(payload.Value)
 
- 			if err != nil {
 
- 				return
 
- 			}
 
- 			payload.Slice(0, nBytes)
 
- 			if this.block.Open(payload) {
 
- 				this.Input(payload.Value)
 
- 			}
 
- 		}
 
- 	}()
 
- }
 
- func (this *Connection) Reusable() bool {
 
- 	return false
 
- }
 
- func (this *Connection) SetReusable(b bool) {}
 
- func (this *Connection) Terminate() {
 
- 	if this == nil || this.writer == nil {
 
- 		return
 
- 	}
 
- 	log.Info("KCP|Connection: Terminating connection to ", this.RemoteAddr())
 
- 	this.SetState(StateTerminated)
 
- 	this.dataInputCond.Broadcast()
 
- 	this.dataOutputCond.Broadcast()
 
- 	this.writer.Close()
 
- }
 
- func (this *Connection) HandleOption(opt SegmentOption) {
 
- 	if (opt & SegmentOptionClose) == SegmentOptionClose {
 
- 		this.OnPeerClosed()
 
- 	}
 
- }
 
- func (this *Connection) OnPeerClosed() {
 
- 	state := this.State()
 
- 	if state == StateReadyToClose {
 
- 		this.SetState(StateTerminating)
 
- 	}
 
- 	if state == StateActive {
 
- 		this.SetState(StatePeerClosed)
 
- 	}
 
- }
 
- // Input when you received a low level packet (eg. UDP packet), call it
 
- func (this *Connection) Input(data []byte) int {
 
- 	current := this.Elapsed()
 
- 	atomic.StoreUint32(&this.lastIncomingTime, current)
 
- 	var seg Segment
 
- 	for {
 
- 		seg, data = ReadSegment(data)
 
- 		if seg == nil {
 
- 			break
 
- 		}
 
- 		switch seg := seg.(type) {
 
- 		case *DataSegment:
 
- 			this.HandleOption(seg.Option)
 
- 			this.receivingWorker.ProcessSegment(seg)
 
- 			this.dataInputCond.Signal()
 
- 		case *AckSegment:
 
- 			this.HandleOption(seg.Option)
 
- 			this.sendingWorker.ProcessSegment(current, seg)
 
- 			this.dataOutputCond.Signal()
 
- 		case *CmdOnlySegment:
 
- 			this.HandleOption(seg.Option)
 
- 			if seg.Command == CommandTerminate {
 
- 				state := this.State()
 
- 				if state == StateActive ||
 
- 					state == StatePeerClosed {
 
- 					this.SetState(StatePeerTerminating)
 
- 				} else if state == StateReadyToClose {
 
- 					this.SetState(StateTerminating)
 
- 				} else if state == StateTerminating {
 
- 					this.SetState(StateTerminated)
 
- 				}
 
- 			}
 
- 			this.sendingWorker.ProcessReceivingNext(seg.ReceivinNext)
 
- 			this.receivingWorker.ProcessSendingNext(seg.SendingNext)
 
- 			this.roundTrip.UpdatePeerRTO(seg.PeerRTO, current)
 
- 			seg.Release()
 
- 		default:
 
- 		}
 
- 	}
 
- 	return 0
 
- }
 
- func (this *Connection) flush() {
 
- 	current := this.Elapsed()
 
- 	if this.State() == StateTerminated {
 
- 		return
 
- 	}
 
- 	if this.State() == StateActive && current-atomic.LoadUint32(&this.lastIncomingTime) >= 30000 {
 
- 		this.Close()
 
- 	}
 
- 	if this.State() == StateReadyToClose && this.sendingWorker.IsEmpty() {
 
- 		this.SetState(StateTerminating)
 
- 	}
 
- 	if this.State() == StateTerminating {
 
- 		log.Debug("KCP|Connection: #", this.conv, " sending terminating cmd.")
 
- 		seg := NewCmdOnlySegment()
 
- 		defer seg.Release()
 
- 		seg.Conv = this.conv
 
- 		seg.Command = CommandTerminate
 
- 		this.output.Write(seg)
 
- 		this.output.Flush()
 
- 		if current-atomic.LoadUint32(&this.stateBeginTime) > 8000 {
 
- 			this.SetState(StateTerminated)
 
- 		}
 
- 		return
 
- 	}
 
- 	if this.State() == StatePeerTerminating && current-atomic.LoadUint32(&this.stateBeginTime) > 4000 {
 
- 		this.SetState(StateTerminating)
 
- 	}
 
- 	if this.State() == StateReadyToClose && current-atomic.LoadUint32(&this.stateBeginTime) > 15000 {
 
- 		this.SetState(StateTerminating)
 
- 	}
 
- 	// flush acknowledges
 
- 	this.receivingWorker.Flush(current)
 
- 	this.sendingWorker.Flush(current)
 
- 	if this.sendingWorker.PingNecessary() || this.receivingWorker.PingNecessary() || current-atomic.LoadUint32(&this.lastPingTime) >= 3000 {
 
- 		seg := NewCmdOnlySegment()
 
- 		seg.Conv = this.conv
 
- 		seg.Command = CommandPing
 
- 		seg.ReceivinNext = this.receivingWorker.nextNumber
 
- 		seg.SendingNext = this.sendingWorker.firstUnacknowledged
 
- 		seg.PeerRTO = this.roundTrip.Timeout()
 
- 		if this.State() == StateReadyToClose {
 
- 			seg.Option = SegmentOptionClose
 
- 		}
 
- 		this.output.Write(seg)
 
- 		this.lastPingTime = current
 
- 		this.sendingWorker.MarkPingNecessary(false)
 
- 		this.receivingWorker.MarkPingNecessary(false)
 
- 		seg.Release()
 
- 	}
 
- 	// flash remain segments
 
- 	this.output.Flush()
 
- }
 
- func (this *Connection) State() State {
 
- 	return State(atomic.LoadInt32((*int32)(&this.state)))
 
- }
 
 
  |