1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 10:26:01 -05:00
v2fly/transport/internet/websocket/connection.go

107 lines
2.2 KiB
Go
Raw Normal View History

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"
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 {
conn *websocket.Conn
reader io.Reader
2017-11-09 16:33:15 -05:00
mergingWriter *buf.BufferedWriter
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 {
2017-04-20 05:00:15 -04:00
if c.mergingWriter == nil {
2017-11-09 16:33:15 -05:00
c.mergingWriter = buf.NewBufferedWriter(buf.NewBufferToBytesWriter(c))
}
if err := c.mergingWriter.WriteMultiBuffer(mb); err != nil {
return err
}
2017-11-09 16:33:15 -05:00
return c.mergingWriter.Flush()
}
2017-02-16 10:53:31 -05:00
func (c *connection) Close() error {
2017-10-21 03:45:27 -04:00
c.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5))
return c.conn.Close()
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
}