v2ray-core/transport/internet/websocket/connection.go

106 lines
2.1 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"
"github.com/gorilla/websocket"
"v2ray.com/core/common/buf"
2017-02-08 17:30:16 +00:00
"v2ray.com/core/common/errors"
2016-08-13 13:44:36 +00:00
)
2017-04-20 09:00:15 +00:00
var (
_ buf.MultiBufferReader = (*connection)(nil)
_ buf.MultiBufferWriter = (*connection)(nil)
)
2017-02-16 15:53:31 +00:00
// connection is a wrapper for net.Conn over WebSocket connection.
type connection struct {
2017-04-20 09:00:15 +00:00
wsc *websocket.Conn
reader io.Reader
mergingReader buf.Reader
mergingWriter buf.Writer
2016-08-13 13:44:36 +00:00
}
2017-02-16 15:53:31 +00:00
// Read implements net.Conn.Read()
func (c *connection) Read(b []byte) (int, error) {
2017-02-09 10:42:17 +00:00
for {
reader, err := c.getReader()
2016-08-15 10:56:33 +00:00
if err != nil {
return 0, err
2016-08-13 13:44:36 +00:00
}
2017-02-09 10:42:17 +00:00
nBytes, err := reader.Read(b)
if errors.Cause(err) == io.EOF {
2017-02-09 11:31:26 +00:00
c.reader = nil
2017-02-09 10:42:17 +00:00
continue
2016-08-15 10:56:33 +00:00
}
2017-02-09 10:42:17 +00:00
return nBytes, err
2016-08-13 13:44:36 +00:00
}
}
2017-04-20 09:00:15 +00:00
func (c *connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
if c.mergingReader == nil {
c.mergingReader = buf.NewMergingReader(c)
}
return c.mergingReader.Read()
}
2017-02-16 15:53:31 +00:00
func (c *connection) getReader() (io.Reader, error) {
2017-02-09 10:42:17 +00:00
if c.reader != nil {
return c.reader, nil
2016-08-13 13:44:36 +00:00
}
2016-08-15 10:59:14 +00:00
2017-02-09 10:42:17 +00:00
_, reader, err := c.wsc.NextReader()
2016-08-15 10:59:14 +00:00
if err != nil {
2017-02-09 10:42:17 +00:00
return nil, err
2016-08-15 10:59:14 +00:00
}
2017-02-09 10:42:17 +00:00
c.reader = reader
return reader, nil
}
2017-04-21 13:36:05 +00:00
// Write implements io.Writer.
2017-02-16 15:53:31 +00:00
func (c *connection) Write(b []byte) (int, error) {
2017-02-09 10:42:17 +00:00
if err := c.wsc.WriteMessage(websocket.BinaryMessage, b); err != nil {
2016-08-15 10:59:14 +00:00
return 0, err
}
2017-02-09 10:42:17 +00:00
return len(b), nil
2016-08-15 10:59:14 +00:00
}
2017-04-23 11:41:52 +00:00
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
2017-04-20 09:00:15 +00:00
if c.mergingWriter == nil {
c.mergingWriter = buf.NewMergingWriter(c)
}
2017-04-23 11:41:52 +00:00
return c.mergingWriter.Write(mb)
}
2017-02-16 15:53:31 +00:00
func (c *connection) Close() error {
2017-02-09 10:42:17 +00:00
c.wsc.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5))
return c.wsc.Close()
2016-08-13 13:44:36 +00:00
}
2017-02-09 10:42:17 +00:00
2017-02-16 15:53:31 +00:00
func (c *connection) LocalAddr() net.Addr {
2017-02-09 10:42:17 +00:00
return c.wsc.LocalAddr()
2016-08-13 13:44:36 +00:00
}
2017-02-09 10:42:17 +00:00
2017-02-16 15:53:31 +00:00
func (c *connection) RemoteAddr() net.Addr {
2017-02-09 10:42:17 +00:00
return c.wsc.RemoteAddr()
2016-08-13 13:44:36 +00:00
}
2017-02-09 10:42:17 +00:00
2017-02-16 15:53:31 +00:00
func (c *connection) SetDeadline(t time.Time) error {
2017-02-09 10:42:17 +00:00
if err := c.SetReadDeadline(t); err != nil {
2017-01-04 12:29:41 +00:00
return err
}
2017-02-09 10:42:17 +00:00
return c.SetWriteDeadline(t)
2016-08-13 13:44:36 +00:00
}
2016-08-15 09:53:11 +00:00
2017-02-16 15:53:31 +00:00
func (c *connection) SetReadDeadline(t time.Time) error {
2017-02-09 10:42:17 +00:00
return c.wsc.SetReadDeadline(t)
2016-08-13 13:44:36 +00:00
}
2017-02-16 15:53:31 +00:00
func (c *connection) SetWriteDeadline(t time.Time) error {
2017-02-09 10:42:17 +00:00
return c.wsc.SetWriteDeadline(t)
2016-08-13 13:44:36 +00:00
}