1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-30 21:26:33 -05:00

normalize naming

This commit is contained in:
v2ray 2016-06-18 16:34:04 +02:00
parent cd4416e7e5
commit 5334d25e16
3 changed files with 124 additions and 133 deletions

View File

@ -19,16 +19,7 @@ var (
) )
const ( const (
basePort = 20000 // minimum port for listening
maxPort = 65535 // maximum port for listening
defaultWndSize = 128 // default window size, in packet
mtuLimit = 4096
rxQueueLimit = 8192
rxFecLimit = 2048
headerSize = 2 headerSize = 2
cmdData uint16 = 0
cmdClose uint16 = 1
) )
type Command byte type Command byte
@ -58,8 +49,8 @@ func nowMillisec() int64 {
return now.Unix()*1000 + int64(now.Nanosecond()/1000000) return now.Unix()*1000 + int64(now.Nanosecond()/1000000)
} }
// UDPSession defines a KCP session implemented by UDP // Connection is a KCP connection over UDP.
type UDPSession struct { type Connection struct {
sync.RWMutex sync.RWMutex
state ConnState state ConnState
kcp *KCP // the core ARQ kcp *KCP // the core ARQ
@ -74,108 +65,108 @@ type UDPSession struct {
since int64 since int64
} }
// newUDPSession create a new udp session for client or server // NewConnection create a new KCP connection between local and remote.
func newUDPSession(conv uint32, writerCloser io.WriteCloser, local *net.UDPAddr, remote *net.UDPAddr, block Authenticator) *UDPSession { func NewConnection(conv uint32, writerCloser io.WriteCloser, local *net.UDPAddr, remote *net.UDPAddr, block Authenticator) *Connection {
sess := new(UDPSession) conn := new(Connection)
sess.local = local conn.local = local
sess.chReadEvent = make(chan struct{}, 1) conn.chReadEvent = make(chan struct{}, 1)
sess.remote = remote conn.remote = remote
sess.block = block conn.block = block
sess.writer = writerCloser conn.writer = writerCloser
sess.since = nowMillisec() conn.since = nowMillisec()
mtu := uint32(effectiveConfig.Mtu - block.HeaderSize() - headerSize) mtu := uint32(effectiveConfig.Mtu - block.HeaderSize() - headerSize)
sess.kcp = NewKCP(conv, mtu, func(buf []byte, size int) { conn.kcp = NewKCP(conv, mtu, func(buf []byte, size int) {
if size >= IKCP_OVERHEAD { if size >= IKCP_OVERHEAD {
ext := alloc.NewBuffer().Clear().Append(buf[:size]) ext := alloc.NewBuffer().Clear().Append(buf[:size])
cmd := cmdData cmd := CommandData
opt := Option(0) opt := Option(0)
if sess.state == ConnStateReadyToClose { if conn.state == ConnStateReadyToClose {
opt = OptionClose opt = OptionClose
} }
ext.Prepend([]byte{byte(cmd), byte(opt)}) ext.Prepend([]byte{byte(cmd), byte(opt)})
go sess.output(ext) go conn.output(ext)
} }
if sess.state == ConnStateReadyToClose && sess.kcp.WaitSnd() == 0 { if conn.state == ConnStateReadyToClose && conn.kcp.WaitSnd() == 0 {
go sess.NotifyTermination() go conn.NotifyTermination()
} }
}) })
sess.kcp.WndSize(effectiveConfig.Sndwnd, effectiveConfig.Rcvwnd) conn.kcp.WndSize(effectiveConfig.Sndwnd, effectiveConfig.Rcvwnd)
sess.kcp.NoDelay(1, 20, 2, 1) conn.kcp.NoDelay(1, 20, 2, 1)
sess.kcp.current = sess.Elapsed() conn.kcp.current = conn.Elapsed()
go sess.updateTask() go conn.updateTask()
return sess return conn
} }
func (this *UDPSession) Elapsed() uint32 { func (this *Connection) Elapsed() uint32 {
return uint32(nowMillisec() - this.since) return uint32(nowMillisec() - this.since)
} }
// Read implements the Conn Read method. // Read implements the Conn Read method.
func (s *UDPSession) Read(b []byte) (int, error) { func (this *Connection) Read(b []byte) (int, error) {
if s == nil || s.state == ConnStateReadyToClose || s.state == ConnStateClosed { if this == nil || this.state == ConnStateReadyToClose || this.state == ConnStateClosed {
return 0, io.EOF return 0, io.EOF
} }
for { for {
s.RLock() this.RLock()
if s.state == ConnStateReadyToClose || s.state == ConnStateClosed { if this.state == ConnStateReadyToClose || this.state == ConnStateClosed {
s.RUnlock() this.RUnlock()
return 0, io.EOF return 0, io.EOF
} }
if !s.rd.IsZero() && s.rd.Before(time.Now()) { if !this.rd.IsZero() && this.rd.Before(time.Now()) {
s.RUnlock() this.RUnlock()
return 0, errTimeout return 0, errTimeout
} }
s.RUnlock() this.RUnlock()
s.kcpAccess.Lock() this.kcpAccess.Lock()
nBytes := s.kcp.Recv(b) nBytes := this.kcp.Recv(b)
s.kcpAccess.Unlock() this.kcpAccess.Unlock()
if nBytes > 0 { if nBytes > 0 {
return nBytes, nil return nBytes, nil
} }
select { select {
case <-s.chReadEvent: case <-this.chReadEvent:
case <-time.After(time.Second): case <-time.After(time.Second):
} }
} }
} }
// Write implements the Conn Write method. // Write implements the Conn Write method.
func (s *UDPSession) Write(b []byte) (int, error) { func (this *Connection) Write(b []byte) (int, error) {
if s == nil || if this == nil ||
s.state == ConnStateReadyToClose || this.state == ConnStateReadyToClose ||
s.state == ConnStatePeerClosed || this.state == ConnStatePeerClosed ||
s.state == ConnStateClosed { this.state == ConnStateClosed {
return 0, io.ErrClosedPipe return 0, io.ErrClosedPipe
} }
for { for {
s.RLock() this.RLock()
if s.state == ConnStateReadyToClose || if this.state == ConnStateReadyToClose ||
s.state == ConnStatePeerClosed || this.state == ConnStatePeerClosed ||
s.state == ConnStateClosed { this.state == ConnStateClosed {
s.RUnlock() this.RUnlock()
return 0, io.ErrClosedPipe return 0, io.ErrClosedPipe
} }
s.RUnlock() this.RUnlock()
s.kcpAccess.Lock() this.kcpAccess.Lock()
if s.kcp.WaitSnd() < int(s.kcp.snd_wnd) { if this.kcp.WaitSnd() < int(this.kcp.snd_wnd) {
nBytes := len(b) nBytes := len(b)
s.kcp.Send(b) this.kcp.Send(b)
s.kcp.current = s.Elapsed() this.kcp.current = this.Elapsed()
s.kcp.flush() this.kcp.flush()
s.kcpAccess.Unlock() this.kcpAccess.Unlock()
return nBytes, nil return nBytes, nil
} }
s.kcpAccess.Unlock() this.kcpAccess.Unlock()
if !s.wd.IsZero() && s.wd.Before(time.Now()) { if !this.wd.IsZero() && this.wd.Before(time.Now()) {
return 0, errTimeout return 0, errTimeout
} }
@ -184,7 +175,7 @@ func (s *UDPSession) Write(b []byte) (int, error) {
} }
} }
func (this *UDPSession) Terminate() { func (this *Connection) Terminate() {
if this == nil || this.state == ConnStateClosed { if this == nil || this.state == ConnStateClosed {
return return
} }
@ -198,7 +189,7 @@ func (this *UDPSession) Terminate() {
this.writer.Close() this.writer.Close()
} }
func (this *UDPSession) NotifyTermination() { func (this *Connection) NotifyTermination() {
for i := 0; i < 16; i++ { for i := 0; i < 16; i++ {
this.RLock() this.RLock()
if this.state == ConnStateClosed { if this.state == ConnStateClosed {
@ -217,102 +208,102 @@ func (this *UDPSession) NotifyTermination() {
} }
// Close closes the connection. // Close closes the connection.
func (s *UDPSession) Close() error { func (this *Connection) Close() error {
if s == nil || s.state == ConnStateClosed || s.state == ConnStateReadyToClose { if this == nil || this.state == ConnStateClosed || this.state == ConnStateReadyToClose {
return errClosedConnection return errClosedConnection
} }
log.Debug("KCP|Connection: Closing connection to ", s.remote) log.Debug("KCP|Connection: Closing connection to ", this.remote)
s.Lock() this.Lock()
defer s.Unlock() defer this.Unlock()
if s.state == ConnStateActive { if this.state == ConnStateActive {
s.state = ConnStateReadyToClose this.state = ConnStateReadyToClose
if s.kcp.WaitSnd() == 0 { if this.kcp.WaitSnd() == 0 {
go s.NotifyTermination() go this.NotifyTermination()
} }
} }
if s.state == ConnStatePeerClosed { if this.state == ConnStatePeerClosed {
go s.Terminate() go this.Terminate()
} }
return nil return nil
} }
// LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it. // LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.
func (s *UDPSession) LocalAddr() net.Addr { func (this *Connection) LocalAddr() net.Addr {
if s == nil { if this == nil {
return nil return nil
} }
return s.local return this.local
} }
// RemoteAddr returns the remote network address. The Addr returned is shared by all invocations of RemoteAddr, so do not modify it. // RemoteAddr returns the remote network address. The Addr returned is shared by all invocations of RemoteAddr, so do not modify it.
func (s *UDPSession) RemoteAddr() net.Addr { func (this *Connection) RemoteAddr() net.Addr {
if s == nil { if this == nil {
return nil return nil
} }
return s.remote return this.remote
} }
// SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline. // SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.
func (s *UDPSession) SetDeadline(t time.Time) error { func (this *Connection) SetDeadline(t time.Time) error {
if s == nil || s.state != ConnStateActive { if this == nil || this.state != ConnStateActive {
return errClosedConnection return errClosedConnection
} }
s.Lock() this.Lock()
defer s.Unlock() defer this.Unlock()
s.rd = t this.rd = t
s.wd = t this.wd = t
return nil return nil
} }
// SetReadDeadline implements the Conn SetReadDeadline method. // SetReadDeadline implements the Conn SetReadDeadline method.
func (s *UDPSession) SetReadDeadline(t time.Time) error { func (this *Connection) SetReadDeadline(t time.Time) error {
if s == nil || s.state != ConnStateActive { if this == nil || this.state != ConnStateActive {
return errClosedConnection return errClosedConnection
} }
s.Lock() this.Lock()
defer s.Unlock() defer this.Unlock()
s.rd = t this.rd = t
return nil return nil
} }
// SetWriteDeadline implements the Conn SetWriteDeadline method. // SetWriteDeadline implements the Conn SetWriteDeadline method.
func (s *UDPSession) SetWriteDeadline(t time.Time) error { func (this *Connection) SetWriteDeadline(t time.Time) error {
if s == nil || s.state != ConnStateActive { if this == nil || this.state != ConnStateActive {
return errClosedConnection return errClosedConnection
} }
s.Lock() this.Lock()
defer s.Unlock() defer this.Unlock()
s.wd = t this.wd = t
return nil return nil
} }
func (s *UDPSession) output(payload *alloc.Buffer) { func (this *Connection) output(payload *alloc.Buffer) {
defer payload.Release() defer payload.Release()
if s == nil { if this == nil {
return return
} }
s.RLock() this.RLock()
defer s.RUnlock() defer this.RUnlock()
if s.state == ConnStatePeerClosed || s.state == ConnStateClosed { if this.state == ConnStatePeerClosed || this.state == ConnStateClosed {
return return
} }
s.block.Seal(payload) this.block.Seal(payload)
s.writer.Write(payload.Value) this.writer.Write(payload.Value)
} }
// kcp update, input loop // kcp update, input loop
func (s *UDPSession) updateTask() { func (this *Connection) updateTask() {
for s.state != ConnStateClosed { for this.state != ConnStateClosed {
current := s.Elapsed() current := this.Elapsed()
s.kcpAccess.Lock() this.kcpAccess.Lock()
s.kcp.Update(current) this.kcp.Update(current)
interval := s.kcp.Check(s.Elapsed()) interval := this.kcp.Check(this.Elapsed())
s.kcpAccess.Unlock() this.kcpAccess.Unlock()
sleep := interval - current sleep := interval - current
if sleep < 10 { if sleep < 10 {
sleep = 10 sleep = 10
@ -321,14 +312,14 @@ func (s *UDPSession) updateTask() {
} }
} }
func (s *UDPSession) notifyReadEvent() { func (this *Connection) notifyReadEvent() {
select { select {
case s.chReadEvent <- struct{}{}: case this.chReadEvent <- struct{}{}:
default: default:
} }
} }
func (this *UDPSession) MarkPeerClose() { func (this *Connection) MarkPeerClose() {
this.Lock() this.Lock()
defer this.Unlock() defer this.Unlock()
if this.state == ConnStateReadyToClose { if this.state == ConnStateReadyToClose {
@ -341,25 +332,25 @@ func (this *UDPSession) MarkPeerClose() {
} }
} }
func (s *UDPSession) kcpInput(data []byte) { func (this *Connection) kcpInput(data []byte) {
cmd := Command(data[0]) cmd := Command(data[0])
opt := Option(data[1]) opt := Option(data[1])
if cmd == CommandTerminate { if cmd == CommandTerminate {
go s.Terminate() go this.Terminate()
return return
} }
if opt == OptionClose { if opt == OptionClose {
go s.MarkPeerClose() go this.MarkPeerClose()
} }
s.kcpAccess.Lock() this.kcpAccess.Lock()
s.kcp.current = s.Elapsed() this.kcp.current = this.Elapsed()
s.kcp.Input(data[2:]) this.kcp.Input(data[2:])
s.kcpAccess.Unlock() this.kcpAccess.Unlock()
s.notifyReadEvent() this.notifyReadEvent()
} }
func (this *UDPSession) FetchInputFrom(conn net.Conn) { func (this *Connection) FetchInputFrom(conn net.Conn) {
go func() { go func() {
for { for {
payload := alloc.NewBuffer() payload := alloc.NewBuffer()
@ -378,8 +369,8 @@ func (this *UDPSession) FetchInputFrom(conn net.Conn) {
}() }()
} }
func (this *UDPSession) Reusable() bool { func (this *Connection) Reusable() bool {
return false return false
} }
func (this *UDPSession) SetReusable(b bool) {} func (this *Connection) SetReusable(b bool) {}

View File

@ -23,7 +23,7 @@ func DialKCP(src v2net.Address, dest v2net.Destination) (internet.Connection, er
} }
cpip := NewSimpleAuthenticator() cpip := NewSimpleAuthenticator()
session := newUDPSession(rand.Uint32(), conn, conn.LocalAddr().(*net.UDPAddr), conn.RemoteAddr().(*net.UDPAddr), cpip) session := NewConnection(rand.Uint32(), conn, conn.LocalAddr().(*net.UDPAddr), conn.RemoteAddr().(*net.UDPAddr), cpip)
session.FetchInputFrom(conn) session.FetchInputFrom(conn)
return session, nil return session, nil

View File

@ -18,8 +18,8 @@ type Listener struct {
sync.Mutex sync.Mutex
running bool running bool
block Authenticator block Authenticator
sessions map[string]*UDPSession sessions map[string]*Connection
awaitingConns chan *UDPSession awaitingConns chan *Connection
hub *udp.UDPHub hub *udp.UDPHub
localAddr *net.UDPAddr localAddr *net.UDPAddr
} }
@ -27,8 +27,8 @@ type Listener struct {
func NewListener(address v2net.Address, port v2net.Port) (*Listener, error) { func NewListener(address v2net.Address, port v2net.Port) (*Listener, error) {
l := &Listener{ l := &Listener{
block: NewSimpleAuthenticator(), block: NewSimpleAuthenticator(),
sessions: make(map[string]*UDPSession), sessions: make(map[string]*Connection),
awaitingConns: make(chan *UDPSession, 64), awaitingConns: make(chan *Connection, 64),
localAddr: &net.UDPAddr{ localAddr: &net.UDPAddr{
IP: address.IP(), IP: address.IP(),
Port: int(port), Port: int(port),
@ -72,7 +72,7 @@ func (this *Listener) OnReceive(payload *alloc.Buffer, src v2net.Destination) {
IP: src.Address().IP(), IP: src.Address().IP(),
Port: int(src.Port()), Port: int(src.Port()),
} }
conn = newUDPSession(conv, writer, this.localAddr, srcAddr, this.block) conn = NewConnection(conv, writer, this.localAddr, srcAddr, this.block)
select { select {
case this.awaitingConns <- conn: case this.awaitingConns <- conn:
case <-time.After(time.Second * 5): case <-time.After(time.Second * 5):