refactor websocket

pull/432/head
Darien Raymond 8 years ago
parent 020b436827
commit 935620104d
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -85,7 +85,7 @@ func wsDial(ctx context.Context, dest v2net.Destination) (net.Conn, error) {
return nil, errors.Base(err).Message("WebSocket|Dialer: Failed to dial to (", uri, "): ", reason)
}
return &wsconn{
return &connection{
wsc: conn,
}, nil
}

@ -115,7 +115,7 @@ func (ln *Listener) listenws(address v2net.Address, port v2net.Port) error {
return nil
}
func converttovws(w http.ResponseWriter, r *http.Request) (*wsconn, error) {
func converttovws(w http.ResponseWriter, r *http.Request) (*connection, error) {
var upgrader = websocket.Upgrader{
ReadBufferSize: 32 * 1024,
WriteBufferSize: 32 * 1024,
@ -126,7 +126,7 @@ func converttovws(w http.ResponseWriter, r *http.Request) (*wsconn, error) {
return nil, err
}
return &wsconn{wsc: conn}, nil
return &connection{wsc: conn}, nil
}
func (ln *Listener) Accept() (internet.Connection, error) {

@ -9,12 +9,14 @@ import (
"v2ray.com/core/common/errors"
)
type wsconn struct {
// connection is a wrapper for net.Conn over WebSocket connection.
type connection struct {
wsc *websocket.Conn
reader io.Reader
}
func (c *wsconn) Read(b []byte) (int, error) {
// Read implements net.Conn.Read()
func (c *connection) Read(b []byte) (int, error) {
for {
reader, err := c.getReader()
if err != nil {
@ -30,7 +32,7 @@ func (c *wsconn) Read(b []byte) (int, error) {
}
}
func (c *wsconn) getReader() (io.Reader, error) {
func (c *connection) getReader() (io.Reader, error) {
if c.reader != nil {
return c.reader, nil
}
@ -43,37 +45,37 @@ func (c *wsconn) getReader() (io.Reader, error) {
return reader, nil
}
func (c *wsconn) Write(b []byte) (int, error) {
func (c *connection) Write(b []byte) (int, error) {
if err := c.wsc.WriteMessage(websocket.BinaryMessage, b); err != nil {
return 0, err
}
return len(b), nil
}
func (c *wsconn) Close() error {
func (c *connection) Close() error {
c.wsc.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5))
return c.wsc.Close()
}
func (c *wsconn) LocalAddr() net.Addr {
func (c *connection) LocalAddr() net.Addr {
return c.wsc.LocalAddr()
}
func (c *wsconn) RemoteAddr() net.Addr {
func (c *connection) RemoteAddr() net.Addr {
return c.wsc.RemoteAddr()
}
func (c *wsconn) SetDeadline(t time.Time) error {
func (c *connection) SetDeadline(t time.Time) error {
if err := c.SetReadDeadline(t); err != nil {
return err
}
return c.SetWriteDeadline(t)
}
func (c *wsconn) SetReadDeadline(t time.Time) error {
func (c *connection) SetReadDeadline(t time.Time) error {
return c.wsc.SetReadDeadline(t)
}
func (c *wsconn) SetWriteDeadline(t time.Time) error {
func (c *connection) SetWriteDeadline(t time.Time) error {
return c.wsc.SetWriteDeadline(t)
}

Loading…
Cancel
Save