package codecimport ()// AbridgedClientStart is starting bytes sent by client in Abridged mode.//// Note that server does not respond with it.varAbridgedClientStart = [1]byte{0xef}// Abridged is intermediate MTProto transport.//// See https://core.telegram.org/mtproto/mtproto-transports#abridgedtypeAbridgedstruct{}var ( _ TaggedCodec = Abridged{})// WriteHeader sends protocol tag.func ( Abridged) ( io.Writer) error {if , := .Write(AbridgedClientStart[:]); != nil {returnerrors.Wrap(, "write abridged header") }returnnil}// ReadHeader reads protocol tag.func ( Abridged) ( io.Reader) error {var [1]byteif , := io.ReadFull(, [:]); != nil {returnerrors.Wrap(, "read abridged header") }if != AbridgedClientStart {returnErrProtocolHeaderMismatch }returnnil}// ObfuscatedTag returns protocol tag for obfuscation.func ( Abridged) () ( [4]byte) { := AbridgedClientStart[0]return [4]byte{, , , }}// Write encode to writer message from given buffer.func ( Abridged) ( io.Writer, *bin.Buffer) error {if := checkOutgoingMessage(); != nil {return }if := checkAlign(, 4); != nil {return }if := writeAbridged(, ); != nil {returnerrors.Wrap(, "write abridged") }returnnil}// Read fills buffer with received message.func ( Abridged) ( io.Reader, *bin.Buffer) error {if := readAbridged(, ); != nil {returnerrors.Wrap(, "read abridged") }returncheckProtocolError()}func ( io.Writer, *bin.Buffer) error { := .Len()// Re-using b.Buf if possible to reduce allocations. .Expand(4) .Buf = .Buf[:]// Re-using b.Buf if possible to reduce allocations. := bin.Buffer{Buf: .Buf[:]} := .Len() >> 2// `0x7f == 127`, literally use one bit to distinguish length byte size.if < 127 {// Payloads are wrapped in the following envelope: // // Length: payload length, divided by four, and encoded as a single byte, // only if the resulting packet length is a value between 0x01..0x7e. .Put([]byte{byte()}) } else {// If the packet length divided by four is bigger than or equal to 127 (>= 0x7f), // the following envelope must be used, instead: //var [5]byte// Header: A single byte of value 0x7f [0] = 0x7f// Length: payload length, divided by four, and encoded as 3 length bytes (little endian)binary.LittleEndian.PutUint32([1:], uint32()) .Put([:4]) }if , := .Write(.Buf); != nil {return }if , := .Write(.Raw()); != nil {return }returnnil}func ( io.Reader, *bin.Buffer) error { .ResetN(bin.Word) , := io.ReadFull(, .Buf[:1])if != nil {return }if .Buf[0] >= 127 { , := io.ReadFull(, .Buf[0:3])if != nil {return } } , := .Int()if != nil {return } .ResetN( << 2)if , := io.ReadFull(, .Buf); != nil {returnerrors.Wrap(, "read payload") }returnnil}
The pages are generated with Goldsv0.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.