FILE / ScuroNeko/mtg

mtglib/conns.go

Исходный файл и его история в репозитории.
FILE 7917434a375de0a4e6d058fdef85e104d20fe8c9
Files
mtg/mtglib/conns.go
T
2021-04-09 14:35:04 +03:00

69 lines
1.1 KiB
Go

package mtglib
import (
"bytes"
"context"
"io"
"net"
"sync"
)
type connTraffic struct {
net.Conn
streamID string
stream EventStream
ctx context.Context
}
func (c connTraffic) Read(b []byte) (int, error) {
n, err := c.Conn.Read(b)
if n > 0 {
c.stream.Send(c.ctx, NewEventTraffic(c.streamID, uint(n), true))
}
return n, err // nolint: wrapcheck
}
func (c connTraffic) Write(b []byte) (int, error) {
n, err := c.Conn.Write(b)
if n > 0 {
c.stream.Send(c.ctx, NewEventTraffic(c.streamID, uint(n), false))
}
return n, err // nolint: wrapcheck
}
type connRewind struct {
net.Conn
active io.Reader
buf bytes.Buffer
mutex sync.RWMutex
}
func (c *connRewind) Read(p []byte) (int, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.active.Read(p) // nolint: wrapcheck
}
func (c *connRewind) Rewind() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.active = io.MultiReader(&c.buf, c.Conn)
}
func newConnRewind(conn net.Conn) *connRewind {
rv := &connRewind{
Conn: conn,
}
rv.active = io.TeeReader(conn, &rv.buf)
return rv
}