You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/ws/connection.go

102 lines
1.8 KiB

package ws
import (
"io"
"net"
"time"
"v2ray.com/core/common/errors"
)
var (
ErrInvalidConn = errors.New("Invalid Connection.")
)
type ConnectionManager interface {
Recycle(string, *wsconn)
}
type Connection struct {
dest string
conn *wsconn
listener ConnectionManager
reusable bool
config *Config
}
func NewConnection(dest string, conn *wsconn, manager ConnectionManager, config *Config) *Connection {
return &Connection{
dest: dest,
conn: conn,
listener: manager,
reusable: config.ConnectionReuse.IsEnabled(),
config: config,
}
}
8 years ago
func (v *Connection) Read(b []byte) (int, error) {
if v == nil || v.conn == nil {
return 0, io.EOF
}
8 years ago
return v.conn.Read(b)
}
8 years ago
func (v *Connection) Write(b []byte) (int, error) {
if v == nil || v.conn == nil {
return 0, io.ErrClosedPipe
}
8 years ago
return v.conn.Write(b)
}
8 years ago
func (v *Connection) Close() error {
if v == nil || v.conn == nil {
return io.ErrClosedPipe
}
8 years ago
if v.Reusable() {
v.listener.Recycle(v.dest, v.conn)
return nil
}
8 years ago
err := v.conn.Close()
v.conn = nil
return err
}
8 years ago
func (v *Connection) LocalAddr() net.Addr {
return v.conn.LocalAddr()
}
8 years ago
func (v *Connection) RemoteAddr() net.Addr {
return v.conn.RemoteAddr()
}
8 years ago
func (v *Connection) SetDeadline(t time.Time) error {
return v.conn.SetDeadline(t)
}
8 years ago
func (v *Connection) SetReadDeadline(t time.Time) error {
return v.conn.SetReadDeadline(t)
}
8 years ago
func (v *Connection) SetWriteDeadline(t time.Time) error {
return v.conn.SetWriteDeadline(t)
}
8 years ago
func (v *Connection) SetReusable(reusable bool) {
if !v.config.ConnectionReuse.IsEnabled() {
return
}
8 years ago
v.reusable = reusable
}
8 years ago
func (v *Connection) Reusable() bool {
return v.reusable
}
8 years ago
func (v *Connection) SysFd() (int, error) {
return getSysFd(v.conn)
}
func getSysFd(conn net.Conn) (int, error) {
return 0, ErrInvalidConn
}