package transport
import (
"context"
"net"
"sync"
"time"
"github.com/go-faster/errors"
"github.com/gotd/td/bin"
)
type Conn interface {
Send (ctx context .Context , b *bin .Buffer ) error
Recv (ctx context .Context , b *bin .Buffer ) error
Close () error
}
var _ Conn = (*connection )(nil )
type connection struct {
conn net .Conn
codec Codec
readMux sync .Mutex
writeMux sync .Mutex
}
func (c *connection ) Send (ctx context .Context , b *bin .Buffer ) error {
c .writeMux .Lock ()
defer c .writeMux .Unlock ()
if err := c .conn .SetWriteDeadline (time .Time {}); err != nil {
return errors .Wrap (err , "reset write deadline" )
}
if deadline , ok := ctx .Deadline (); ok {
if err := c .conn .SetWriteDeadline (deadline ); err != nil {
return errors .Wrap (err , "set write deadline" )
}
}
if err := c .codec .Write (c .conn , b ); err != nil {
return errors .Wrap (err , "write" )
}
return nil
}
func (c *connection ) Recv (ctx context .Context , b *bin .Buffer ) error {
c .readMux .Lock ()
defer c .readMux .Unlock ()
if err := c .conn .SetReadDeadline (time .Time {}); err != nil {
return errors .Wrap (err , "reset read deadline" )
}
if deadline , ok := ctx .Deadline (); ok {
if err := c .conn .SetReadDeadline (deadline ); err != nil {
return errors .Wrap (err , "set read deadline" )
}
}
if err := c .codec .Read (c .conn , b ); err != nil {
return errors .Wrap (err , "read" )
}
return nil
}
func (c *connection ) Close () error {
return c .conn .Close ()
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .