|
| 1 | +package websocket |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "math" |
| 7 | + "net" |
| 8 | + "time" |
| 9 | + |
| 10 | + "golang.org/x/xerrors" |
| 11 | +) |
| 12 | + |
| 13 | +// NetConn converts a *websocket.Conn into a net.Conn. |
| 14 | +// Every Write to the net.Conn will correspond to a binary message |
| 15 | +// write on *webscoket.Conn. |
| 16 | +// Close will close the *websocket.Conn with StatusNormalClosure. |
| 17 | +// When a deadline is hit, the connection will be closed. This is |
| 18 | +// different from most net.Conn implementations where only the |
| 19 | +// reading/writing goroutines are interrupted but the connection is kept alive. |
| 20 | +// The Addr methods will return a mock net.Addr. |
| 21 | +func NetConn(c *Conn) net.Conn { |
| 22 | + nc := &netConn{ |
| 23 | + c: c, |
| 24 | + } |
| 25 | + |
| 26 | + var cancel context.CancelFunc |
| 27 | + nc.writeContext, cancel = context.WithCancel(context.Background()) |
| 28 | + nc.writeTimer = time.AfterFunc(math.MaxInt64, cancel) |
| 29 | + nc.writeTimer.Stop() |
| 30 | + |
| 31 | + nc.readContext, cancel = context.WithCancel(context.Background()) |
| 32 | + nc.readTimer = time.AfterFunc(math.MaxInt64, cancel) |
| 33 | + nc.readTimer.Stop() |
| 34 | + |
| 35 | + return nc |
| 36 | +} |
| 37 | + |
| 38 | +type netConn struct { |
| 39 | + c *Conn |
| 40 | + |
| 41 | + writeTimer *time.Timer |
| 42 | + writeContext context.Context |
| 43 | + |
| 44 | + readTimer *time.Timer |
| 45 | + readContext context.Context |
| 46 | + |
| 47 | + reader io.Reader |
| 48 | +} |
| 49 | + |
| 50 | +var _ net.Conn = &netConn{} |
| 51 | + |
| 52 | +func (c *netConn) Close() error { |
| 53 | + return c.c.Close(StatusNormalClosure, "") |
| 54 | +} |
| 55 | + |
| 56 | +func (c *netConn) Write(p []byte) (int, error) { |
| 57 | + err := c.c.Write(c.writeContext, MessageBinary, p) |
| 58 | + if err != nil { |
| 59 | + return 0, err |
| 60 | + } |
| 61 | + return len(p), nil |
| 62 | +} |
| 63 | + |
| 64 | +func (c *netConn) Read(p []byte) (int, error) { |
| 65 | + if c.reader == nil { |
| 66 | + typ, r, err := c.c.Reader(c.readContext) |
| 67 | + if err != nil { |
| 68 | + return 0, err |
| 69 | + } |
| 70 | + if typ != MessageBinary { |
| 71 | + c.c.Close(StatusUnsupportedData, "can only accept binary messages") |
| 72 | + return 0, xerrors.Errorf("unexpected frame type read for net conn adapter (expected %v): %v", MessageBinary, typ) |
| 73 | + } |
| 74 | + c.reader = r |
| 75 | + } |
| 76 | + |
| 77 | + n, err := c.reader.Read(p) |
| 78 | + if err == io.EOF { |
| 79 | + c.reader = nil |
| 80 | + } |
| 81 | + return n, err |
| 82 | +} |
| 83 | + |
| 84 | +type unknownAddr struct { |
| 85 | +} |
| 86 | + |
| 87 | +func (a unknownAddr) Network() string { |
| 88 | + return "unknown" |
| 89 | +} |
| 90 | + |
| 91 | +func (a unknownAddr) String() string { |
| 92 | + return "unknown" |
| 93 | +} |
| 94 | + |
| 95 | +func (c *netConn) RemoteAddr() net.Addr { |
| 96 | + return unknownAddr{} |
| 97 | +} |
| 98 | + |
| 99 | +func (c *netConn) LocalAddr() net.Addr { |
| 100 | + return unknownAddr{} |
| 101 | +} |
| 102 | + |
| 103 | +func (c *netConn) SetDeadline(t time.Time) error { |
| 104 | + c.SetWriteDeadline(t) |
| 105 | + c.SetReadDeadline(t) |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func (c *netConn) SetWriteDeadline(t time.Time) error { |
| 110 | + c.writeTimer.Reset(t.Sub(time.Now())) |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func (c *netConn) SetReadDeadline(t time.Time) error { |
| 115 | + c.readTimer.Reset(t.Sub(time.Now())) |
| 116 | + return nil |
| 117 | +} |
0 commit comments