mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 23:14:02 +03:00
FILE / ScuroNeko/mtg
wrappers/ctx.go
Исходный файл и его история в репозитории.
77 lines
1.4 KiB
Go
77 lines
1.4 KiB
Go
package wrappers
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
|
|
"github.com/juju/errors"
|
|
)
|
|
|
|
type Ctx struct {
|
|
cancel context.CancelFunc
|
|
conn StreamReadWriteCloser
|
|
ctx context.Context
|
|
}
|
|
|
|
func (c *Ctx) Read(p []byte) (int, error) {
|
|
select {
|
|
case <-c.ctx.Done():
|
|
return 0, errors.Annotate(c.ctx.Err(), "Read is failed because of closed context")
|
|
default:
|
|
n, err := c.conn.Read(p)
|
|
if err != nil {
|
|
c.cancel()
|
|
}
|
|
return n, err
|
|
}
|
|
}
|
|
|
|
func (c *Ctx) Write(p []byte) (int, error) {
|
|
select {
|
|
case <-c.ctx.Done():
|
|
return 0, errors.Annotate(c.ctx.Err(), "Write is failed because of closed context")
|
|
default:
|
|
n, err := c.conn.Write(p)
|
|
if err != nil {
|
|
c.cancel()
|
|
}
|
|
return n, err
|
|
}
|
|
}
|
|
|
|
func (c *Ctx) LogDebug(msg string, data ...interface{}) {
|
|
c.conn.LogDebug(msg, data...)
|
|
}
|
|
|
|
func (c *Ctx) LogInfo(msg string, data ...interface{}) {
|
|
c.conn.LogInfo(msg, data...)
|
|
}
|
|
|
|
func (c *Ctx) LogWarn(msg string, data ...interface{}) {
|
|
c.conn.LogWarn(msg, data...)
|
|
}
|
|
|
|
func (c *Ctx) LogError(msg string, data ...interface{}) {
|
|
c.conn.LogError(msg, data...)
|
|
}
|
|
|
|
func (c *Ctx) LocalAddr() *net.TCPAddr {
|
|
return c.conn.LocalAddr()
|
|
}
|
|
|
|
func (c *Ctx) RemoteAddr() *net.TCPAddr {
|
|
return c.conn.RemoteAddr()
|
|
}
|
|
|
|
func (c *Ctx) Close() error {
|
|
return c.conn.Close()
|
|
}
|
|
|
|
func NewCtx(ctx context.Context, cancel context.CancelFunc, conn StreamReadWriteCloser) StreamReadWriteCloser {
|
|
return &Ctx{
|
|
ctx: ctx,
|
|
cancel: cancel,
|
|
conn: conn,
|
|
}
|
|
}
|