mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 20:54:02 +03:00
FILE / ScuroNeko/mtg
mtglib/conns.go
Исходный файл и его история в репозитории.
69 lines
1.1 KiB
Go
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
|
|
}
|