1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/transport/internet/websocket/connection.go

115 lines
2.3 KiB
Go
Raw Normal View History

2019-02-01 14:08:21 -05:00
// +build !confonly
2016-12-22 18:30:46 -05:00
package websocket
2016-08-13 09:44:36 -04:00
import (
"io"
"net"
"time"
"github.com/gorilla/websocket"
"v2ray.com/core/common/buf"
2017-02-08 12:30:16 -05:00
"v2ray.com/core/common/errors"
2018-05-31 05:55:11 -04:00
"v2ray.com/core/common/serial"
2016-08-13 09:44:36 -04:00
)
2017-04-20 05:00:15 -04:00
var (
2017-11-09 16:33:15 -05:00
_ buf.Writer = (*connection)(nil)
2017-04-20 05:00:15 -04:00
)
2017-02-16 10:53:31 -05:00
// connection is a wrapper for net.Conn over WebSocket connection.
type connection struct {
2018-12-27 10:36:48 -05:00
conn *websocket.Conn
reader io.Reader
remoteAddr net.Addr
2016-08-13 09:44:36 -04:00
}
func newConnection(conn *websocket.Conn, remoteAddr net.Addr) *connection {
2017-10-20 16:45:14 -04:00
return &connection{
conn: conn,
remoteAddr: remoteAddr,
2017-10-20 16:45:14 -04:00
}
}
2017-02-16 10:53:31 -05:00
// Read implements net.Conn.Read()
func (c *connection) Read(b []byte) (int, error) {
2017-02-09 05:42:17 -05:00
for {
reader, err := c.getReader()
2016-08-15 06:56:33 -04:00
if err != nil {
return 0, err
2016-08-13 09:44:36 -04:00
}
2017-02-09 05:42:17 -05:00
nBytes, err := reader.Read(b)
if errors.Cause(err) == io.EOF {
2017-02-09 06:31:26 -05:00
c.reader = nil
2017-02-09 05:42:17 -05:00
continue
2016-08-15 06:56:33 -04:00
}
2017-02-09 05:42:17 -05:00
return nBytes, err
2016-08-13 09:44:36 -04:00
}
}
2017-02-16 10:53:31 -05:00
func (c *connection) getReader() (io.Reader, error) {
2017-02-09 05:42:17 -05:00
if c.reader != nil {
return c.reader, nil
2016-08-13 09:44:36 -04:00
}
2016-08-15 06:59:14 -04:00
2017-10-21 03:45:27 -04:00
_, reader, err := c.conn.NextReader()
2016-08-15 06:59:14 -04:00
if err != nil {
2017-02-09 05:42:17 -05:00
return nil, err
2016-08-15 06:59:14 -04:00
}
2017-02-09 05:42:17 -05:00
c.reader = reader
return reader, nil
}
2017-04-21 09:36:05 -04:00
// Write implements io.Writer.
2017-02-16 10:53:31 -05:00
func (c *connection) Write(b []byte) (int, error) {
2017-10-21 03:45:27 -04:00
if err := c.conn.WriteMessage(websocket.BinaryMessage, b); err != nil {
2016-08-15 06:59:14 -04:00
return 0, err
}
2017-02-09 05:42:17 -05:00
return len(b), nil
2016-08-15 06:59:14 -04:00
}
2017-04-23 07:41:52 -04:00
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
2018-12-27 10:36:48 -05:00
mb = buf.Compact(mb)
2018-12-27 11:00:34 -05:00
mb, err := buf.WriteMultiBuffer(c, mb)
buf.ReleaseMulti(mb)
return err
}
2017-02-16 10:53:31 -05:00
func (c *connection) Close() error {
2018-05-31 05:55:11 -04:00
var errors []interface{}
if err := c.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5)); err != nil {
errors = append(errors, err)
}
if err := c.conn.Close(); err != nil {
errors = append(errors, err)
}
if len(errors) > 0 {
return newError("failed to close connection").Base(newError(serial.Concat(errors...)))
}
return nil
2016-08-13 09:44:36 -04:00
}
2017-02-09 05:42:17 -05:00
2017-02-16 10:53:31 -05:00
func (c *connection) LocalAddr() net.Addr {
2017-10-21 03:45:27 -04:00
return c.conn.LocalAddr()
2016-08-13 09:44:36 -04:00
}
2017-02-09 05:42:17 -05:00
2017-02-16 10:53:31 -05:00
func (c *connection) RemoteAddr() net.Addr {
return c.remoteAddr
2016-08-13 09:44:36 -04:00
}
2017-02-09 05:42:17 -05:00
2017-02-16 10:53:31 -05:00
func (c *connection) SetDeadline(t time.Time) error {
2017-02-09 05:42:17 -05:00
if err := c.SetReadDeadline(t); err != nil {
2017-01-04 07:29:41 -05:00
return err
}
2017-02-09 05:42:17 -05:00
return c.SetWriteDeadline(t)
2016-08-13 09:44:36 -04:00
}
2016-08-15 05:53:11 -04:00
2017-02-16 10:53:31 -05:00
func (c *connection) SetReadDeadline(t time.Time) error {
2017-10-21 03:45:27 -04:00
return c.conn.SetReadDeadline(t)
2016-08-13 09:44:36 -04:00
}
2017-02-16 10:53:31 -05:00
func (c *connection) SetWriteDeadline(t time.Time) error {
2017-10-21 03:45:27 -04:00
return c.conn.SetWriteDeadline(t)
2016-08-13 09:44:36 -04:00
}