mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 02:14:02 +03:00
FILE / ScuroNeko/mtg
mtglib/conns.go
Исходный файл и его история в репозитории.
46 lines
715 B
Go
46 lines
715 B
Go
package mtglib
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
type connTelegramTraffic struct {
|
|
net.Conn
|
|
|
|
connID string
|
|
stream EventStream
|
|
ctx context.Context
|
|
}
|
|
|
|
func (c connTelegramTraffic) Read(b []byte) (int, error) {
|
|
n, err := c.Conn.Read(b)
|
|
|
|
if n > 0 {
|
|
c.stream.Send(c.ctx, EventTraffic{
|
|
CreatedAt: time.Now(),
|
|
ConnID: c.connID,
|
|
Traffic: uint(n),
|
|
IsRead: true,
|
|
})
|
|
}
|
|
|
|
return n, err // nolint: wrapcheck
|
|
}
|
|
|
|
func (c connTelegramTraffic) Write(b []byte) (int, error) {
|
|
n, err := c.Conn.Write(b)
|
|
|
|
if n > 0 {
|
|
c.stream.Send(c.ctx, EventTraffic{
|
|
CreatedAt: time.Now(),
|
|
ConnID: c.connID,
|
|
Traffic: uint(n),
|
|
IsRead: false,
|
|
})
|
|
}
|
|
|
|
return n, err // nolint: wrapcheck
|
|
}
|