|
| 1 | +package packetize |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "errors" |
| 7 | + bufpool "github.com/valyala/bytebufferpool" |
| 8 | + "io" |
| 9 | + "net" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +type Conn struct { |
| 14 | + conn net.Conn |
| 15 | + readDeadline <-chan time.Time |
| 16 | + packetChan chan []byte |
| 17 | + |
| 18 | + closeCtx context.Context |
| 19 | + close context.CancelFunc |
| 20 | +} |
| 21 | + |
| 22 | +func (conn *Conn) Read(b []byte) (n int, err error) { |
| 23 | + select { |
| 24 | + case packet := <-conn.packetChan: |
| 25 | + if len(b) < len(packet) { |
| 26 | + err = errors.New("message sent on a socket was larger than the buffer used to receive the message into") |
| 27 | + } |
| 28 | + return copy(b, packet), err |
| 29 | + case <-conn.closeCtx.Done(): |
| 30 | + return 0, errors.New("error reading from conn: connection closed") |
| 31 | + case <-conn.readDeadline: |
| 32 | + return 0, errors.New("error reading from conn: read timeout") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (conn *Conn) Write(b []byte) (n int, err error) { |
| 37 | + if conn.closeCtx.Err() != nil { |
| 38 | + return 0, errors.New("error writing to conn: connection closed") |
| 39 | + } |
| 40 | + |
| 41 | + pk := bufpool.Get() |
| 42 | + defer bufpool.Put(pk) |
| 43 | + |
| 44 | + err = binary.Write(pk, binary.BigEndian, uint16(len(b))) |
| 45 | + if err != nil { |
| 46 | + return |
| 47 | + } |
| 48 | + _, err = pk.Write(b) |
| 49 | + if err != nil { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + _, err = conn.conn.Write(pk.B) |
| 54 | + return |
| 55 | +} |
| 56 | + |
| 57 | +func (conn *Conn) Close() error { |
| 58 | + if conn.closeCtx.Err() != nil { |
| 59 | + return errors.New("conn is already closed") |
| 60 | + } |
| 61 | + |
| 62 | + conn.close() |
| 63 | + return conn.conn.Close() |
| 64 | +} |
| 65 | + |
| 66 | +func (conn *Conn) LocalAddr() net.Addr { |
| 67 | + return conn.conn.LocalAddr() |
| 68 | +} |
| 69 | + |
| 70 | +func (conn *Conn) RemoteAddr() net.Addr { |
| 71 | + return conn.conn.RemoteAddr() |
| 72 | +} |
| 73 | + |
| 74 | +func (conn *Conn) SetDeadline(t time.Time) error { |
| 75 | + return conn.SetReadDeadline(t) |
| 76 | +} |
| 77 | + |
| 78 | +func (conn *Conn) SetReadDeadline(t time.Time) error { |
| 79 | + if t.IsZero() { |
| 80 | + conn.readDeadline = make(chan time.Time) |
| 81 | + return nil |
| 82 | + } |
| 83 | + if t.Before(time.Now()) { |
| 84 | + return errors.New("read deadline cannot be before now") |
| 85 | + } |
| 86 | + conn.readDeadline = time.After(t.Sub(time.Now())) |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func (conn *Conn) SetWriteDeadline(time.Time) error { |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func (conn *Conn) process() { |
| 95 | + lenBuf := make([]byte, 2) |
| 96 | + |
| 97 | + for conn.closeCtx.Err() == nil { |
| 98 | + length, err := io.ReadFull(conn.conn, lenBuf) |
| 99 | + if err != nil || length != 2 { |
| 100 | + _ = conn.Close() |
| 101 | + break |
| 102 | + } |
| 103 | + |
| 104 | + pkLen := int(binary.BigEndian.Uint16(lenBuf)) |
| 105 | + |
| 106 | + pk := make([]byte, pkLen) |
| 107 | + |
| 108 | + length, err = io.ReadFull(conn.conn, pk) |
| 109 | + if err != nil || length != pkLen { |
| 110 | + _ = conn.Close() |
| 111 | + break |
| 112 | + } |
| 113 | + |
| 114 | + conn.packetChan <- pk |
| 115 | + } |
| 116 | +} |
0 commit comments