mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-13 22:45:13 +00:00
rename transport/listener to hub
This commit is contained in:
1 parent
30f131b9ee
commit
925c06aeed
6 files changed
+21
-21
No files matched your search
@@ -0,0 +1,110 @@
|
||||
package hub
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
)
|
||||
|
||||
type TCPConn struct {
|
||||
conn *net.TCPConn
|
||||
listener *TCPListener
|
||||
dirty bool
|
||||
}
|
||||
|
||||
func (this *TCPConn) Read(b []byte) (int, error) {
|
||||
return this.conn.Read(b)
|
||||
}
|
||||
|
||||
func (this *TCPConn) Write(b []byte) (int, error) {
|
||||
return this.conn.Write(b)
|
||||
}
|
||||
|
||||
func (this *TCPConn) Close() error {
|
||||
return this.conn.Close()
|
||||
}
|
||||
|
||||
func (this *TCPConn) Release() {
|
||||
if this.dirty {
|
||||
this.Close()
|
||||
return
|
||||
}
|
||||
this.listener.recycle(this.conn)
|
||||
}
|
||||
|
||||
func (this *TCPConn) LocalAddr() net.Addr {
|
||||
return this.conn.LocalAddr()
|
||||
}
|
||||
|
||||
func (this *TCPConn) RemoteAddr() net.Addr {
|
||||
return this.conn.RemoteAddr()
|
||||
}
|
||||
|
||||
func (this *TCPConn) SetDeadline(t time.Time) error {
|
||||
return this.conn.SetDeadline(t)
|
||||
}
|
||||
|
||||
func (this *TCPConn) SetReadDeadline(t time.Time) error {
|
||||
return this.conn.SetReadDeadline(t)
|
||||
}
|
||||
|
||||
func (this *TCPConn) SetWriteDeadline(t time.Time) error {
|
||||
return this.conn.SetWriteDeadline(t)
|
||||
}
|
||||
|
||||
func (this *TCPConn) CloseRead() error {
|
||||
return this.conn.CloseRead()
|
||||
}
|
||||
|
||||
func (this *TCPConn) CloseWrite() error {
|
||||
return this.conn.CloseWrite()
|
||||
}
|
||||
|
||||
type TCPListener struct {
|
||||
listener *net.TCPListener
|
||||
connCallback func(*TCPConn)
|
||||
accepting bool
|
||||
}
|
||||
|
||||
func ListenTCP(port v2net.Port, callback func(*TCPConn)) (*TCPListener, error) {
|
||||
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
||||
IP: []byte{0, 0, 0, 0},
|
||||
Port: int(port),
|
||||
Zone: "",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tcpListener := &TCPListener{
|
||||
listener: listener,
|
||||
connCallback: callback,
|
||||
}
|
||||
go tcpListener.start()
|
||||
return tcpListener, nil
|
||||
}
|
||||
|
||||
func (this *TCPListener) Close() {
|
||||
this.accepting = false
|
||||
this.listener.Close()
|
||||
}
|
||||
|
||||
func (this *TCPListener) start() {
|
||||
this.accepting = true
|
||||
for this.accepting {
|
||||
conn, err := this.listener.AcceptTCP()
|
||||
if err != nil {
|
||||
log.Warning("Listener: Failed to accept new TCP connection: ", err)
|
||||
continue
|
||||
}
|
||||
go this.connCallback(&TCPConn{
|
||||
conn: conn,
|
||||
listener: this,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (this *TCPListener) recycle(conn *net.TCPConn) {
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user