1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 11:35:23 +00:00
v2fly/transport/internet/websocket/connection.go

100 lines
1.8 KiB
Go
Raw Normal View History

2016-12-22 23:30:46 +00:00
package websocket
2016-08-13 13:44:36 +00:00
import (
"io"
"net"
"time"
2016-12-22 23:30:46 +00:00
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
2016-08-13 13:44:36 +00:00
)
var (
ErrInvalidConn = errors.New("Invalid Connection.")
)
type ConnectionManager interface {
Recycle(string, *wsconn)
}
type Connection struct {
dest string
conn *wsconn
listener ConnectionManager
reusable bool
2016-10-02 21:43:58 +00:00
config *Config
2016-08-13 13:44:36 +00:00
}
2016-10-02 21:43:58 +00:00
func NewConnection(dest string, conn *wsconn, manager ConnectionManager, config *Config) *Connection {
2016-08-13 13:44:36 +00:00
return &Connection{
dest: dest,
conn: conn,
listener: manager,
2017-01-01 20:19:12 +00:00
reusable: config.IsConnectionReuse(),
2016-10-02 21:43:58 +00:00
config: config,
2016-08-13 13:44:36 +00:00
}
}
2016-11-27 20:39:09 +00:00
func (v *Connection) Read(b []byte) (int, error) {
if v == nil || v.conn == nil {
2016-08-13 13:44:36 +00:00
return 0, io.EOF
}
2016-11-27 20:39:09 +00:00
return v.conn.Read(b)
2016-08-13 13:44:36 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Connection) Write(b []byte) (int, error) {
if v == nil || v.conn == nil {
2016-08-13 13:44:36 +00:00
return 0, io.ErrClosedPipe
}
2016-11-27 20:39:09 +00:00
return v.conn.Write(b)
2016-08-13 13:44:36 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Connection) Close() error {
if v == nil || v.conn == nil {
2016-08-13 13:44:36 +00:00
return io.ErrClosedPipe
}
2016-11-27 20:39:09 +00:00
if v.Reusable() {
v.listener.Recycle(v.dest, v.conn)
2016-08-13 13:44:36 +00:00
return nil
}
2016-11-27 20:39:09 +00:00
err := v.conn.Close()
v.conn = nil
2016-08-13 13:44:36 +00:00
return err
}
2016-11-27 20:39:09 +00:00
func (v *Connection) LocalAddr() net.Addr {
return v.conn.LocalAddr()
2016-08-13 13:44:36 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Connection) RemoteAddr() net.Addr {
return v.conn.RemoteAddr()
2016-08-13 13:44:36 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Connection) SetDeadline(t time.Time) error {
return v.conn.SetDeadline(t)
2016-08-13 13:44:36 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Connection) SetReadDeadline(t time.Time) error {
return v.conn.SetReadDeadline(t)
2016-08-13 13:44:36 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Connection) SetWriteDeadline(t time.Time) error {
return v.conn.SetWriteDeadline(t)
2016-08-13 13:44:36 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Connection) SetReusable(reusable bool) {
v.reusable = reusable
2016-08-13 13:44:36 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Connection) Reusable() bool {
2017-01-01 20:19:12 +00:00
return v.config.IsConnectionReuse() && v.reusable
2016-08-13 13:44:36 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Connection) SysFd() (int, error) {
return getSysFd(v.conn)
2016-08-13 13:44:36 +00:00
}
func getSysFd(conn net.Conn) (int, error) {
return 0, ErrInvalidConn
}