FILE / ScuroNeko/mtg

server/ctxrwc.go

Исходный файл и его история в репозитории.
FILE 7072a7a6b68a2dfac344eb929141a2889a178f37
Files
mtg/server/ctxrwc.go
T
2018-05-31 09:54:20 +03:00

53 lines
1006 B
Go

package server
import (
"context"
"io"
"github.com/juju/errors"
)
type CtxReadWriteCloser struct {
ctx context.Context
conn io.ReadWriteCloser
cancel context.CancelFunc
}
func (c *CtxReadWriteCloser) 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 *CtxReadWriteCloser) 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 *CtxReadWriteCloser) Close() error {
return c.conn.Close()
}
func newCtxReadWriteCloser(conn io.ReadWriteCloser, ctx context.Context, cancel context.CancelFunc) io.ReadWriteCloser {
return &CtxReadWriteCloser{
conn: conn,
ctx: ctx,
cancel: cancel,
}
}