package codec
import (
"io"
"github.com/go-faster/errors"
"github.com/gotd/td/bin"
)
var IntermediateClientStart = [4 ]byte {0xee , 0xee , 0xee , 0xee }
type Intermediate struct {}
var (
_ TaggedCodec = Intermediate {}
)
func (i Intermediate ) WriteHeader (w io .Writer ) (err error ) {
if _ , err := w .Write (IntermediateClientStart [:]); err != nil {
return errors .Wrap (err , "write intermediate header" )
}
return nil
}
func (i Intermediate ) ReadHeader (r io .Reader ) (err error ) {
var b [4 ]byte
if _ , err := io .ReadFull (r , b [:]); err != nil {
return errors .Wrap (err , "read intermediate header" )
}
if b != IntermediateClientStart {
return ErrProtocolHeaderMismatch
}
return nil
}
func (i Intermediate ) ObfuscatedTag () [4 ]byte {
return IntermediateClientStart
}
func (i Intermediate ) Write (w io .Writer , b *bin .Buffer ) error {
if err := checkOutgoingMessage (b ); err != nil {
return err
}
if err := checkAlign (b , 4 ); err != nil {
return err
}
if err := writeIntermediate (w , b ); err != nil {
return errors .Wrap (err , "write intermediate" )
}
return nil
}
func (i Intermediate ) Read (r io .Reader , b *bin .Buffer ) error {
if err := readIntermediate (r , b , false ); err != nil {
return errors .Wrap (err , "read intermediate" )
}
return checkProtocolError (b )
}
func writeIntermediate (w io .Writer , b *bin .Buffer ) error {
length := b .Len ()
b .Expand (4 )
b .Buf = b .Buf [:length ]
inner := bin .Buffer {Buf : b .Buf [length :length ]}
inner .PutInt (b .Len ())
if _ , err := w .Write (inner .Buf ); err != nil {
return err
}
if _ , err := w .Write (b .Buf ); err != nil {
return err
}
return nil
}
func readIntermediate (r io .Reader , b *bin .Buffer , padding bool ) error {
n , err := readLen (r , b )
if err != nil {
return err
}
b .ResetN (n )
if _ , err := io .ReadFull (r , b .Buf ); err != nil {
return errors .Wrap (err , "read payload" )
}
if padding {
paddingLength := n % 4
b .Buf = b .Buf [:n -paddingLength ]
}
return nil
}
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 .