Skip to content

Commit

Permalink
🏇 more tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
quenbyako committed Mar 23, 2021
1 parent c2f6508 commit 2e19c0c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 54 deletions.
6 changes: 1 addition & 5 deletions internal/transport/conn_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type tcpConn struct {
}

func NewTCP(host string, timeout time.Duration) (Conn, error) {
return NewTCPWithCtx(nil, host, timeout)
return NewTCPWithCtx(context.Background(), host, timeout)
}

func NewTCPWithCtx(ctx context.Context, host string, timeout time.Duration) (Conn, error) {
Expand All @@ -30,10 +30,6 @@ func NewTCPWithCtx(ctx context.Context, host string, timeout time.Duration) (Con
return nil, errors.Wrap(err, "dialing tcp")
}

if ctx == nil {
ctx = context.Background()
}

return &tcpConn{
cancelReader: ioutil.NewCancelableReader(ctx, conn),
conn: conn,
Expand Down
2 changes: 1 addition & 1 deletion internal/transport/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type Conn io.ReadWriteCloser

type Transport interface {
type Mode interface {
WriteMsg(msg []byte) error // this is not same as the io.Writer
ReadMsg() ([]byte, error)
}
2 changes: 1 addition & 1 deletion internal/transport/intermediate_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type intermediateMode struct {

var transportModeIntermediate = [...]byte{0xee, 0xee, 0xee, 0xee} // meta:immutable

func NewIntermediateMode(conn io.ReadWriter) (Transport, error) {
func NewIntermediateMode(conn io.ReadWriter) (Mode, error) {
if conn == nil {
return nil, errors.New("conn is nil")
}
Expand Down
33 changes: 33 additions & 0 deletions internal/transport/mode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package transport_test

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"
"github.com/xelaj/mtproto/internal/transport"
)

func TestMode(t *testing.T) {
buf := bytes.NewBuffer(nil)

tr, err := transport.NewIntermediateMode(buf)
require.NoError(t, err)

require.NoError(t, tr.WriteMsg([]byte("test message")))
require.Equal(t, buf.Bytes(), []byte{
0xee, 0xee, 0xee, 0xee, 0x0c, 0x00, 0x00, 0x00,
0x74, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73,
0x73, 0x61, 0x67, 0x65,
})

tr, err = transport.NewIntermediateMode(bytes.NewBuffer([]byte{
0x0c, 0x00, 0x00, 0x00, 0x74, 0x65, 0x73, 0x74,
0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
}))
require.NoError(t, err)

res, err := tr.ReadMsg()
require.NoError(t, err)
require.Equal(t, []byte("test message"), res)
}
24 changes: 0 additions & 24 deletions internal/transport/transport_test.go

This file was deleted.

18 changes: 3 additions & 15 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package mtproto
import (
"encoding/binary"
"fmt"
"time"

"github.com/pkg/errors"

Expand All @@ -17,11 +16,6 @@ import (
"github.com/xelaj/mtproto/internal/mtproto/objects"
)

const (
// если длина пакета больше или равн 127 слов, то кодируем 4 байтами, 1 это магическое число, оставшиеся 3 — дилна
magicValueSizeMoreThanSingleByte = 0x7f
)

// проверяет, надо ли ждать от сервера пинга
func isNullableResponse(t tl.Object) bool {
switch t.(type) {
Expand All @@ -32,12 +26,8 @@ func isNullableResponse(t tl.Object) bool {
}
}

const (
readTimeout = 2 * time.Second
)

func CatchResponseErrorCode(data []byte) error {
if len(data) == 4 {
if len(data) == tl.WordLen {
code := int(binary.LittleEndian.Uint32(data))
return &ErrResponseCode{Code: code}
}
Expand Down Expand Up @@ -70,11 +60,9 @@ func (m *MTProto) decodeRecievedData(data []byte) (messages.Common, error) {
return nil, errors.Wrap(err, "parsing message")
}

m.msgId = int64(msg.GetMsgID())
m.seqNo = int32(msg.GetSeqNo())
mod := m.msgId & 3
mod := msg.GetMsgID() & 3
if mod != 1 && mod != 3 {
return nil, fmt.Errorf("Wrong bits of message_id: %d", mod)
return nil, fmt.Errorf("wrong bits of message_id: %d", mod)
}

return msg, nil
Expand Down
18 changes: 10 additions & 8 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
package mtproto

import (
"context"
"io"

"github.com/xelaj/mtproto/internal/encoding/tl"
"github.com/xelaj/mtproto/internal/mtproto/objects"
)
Expand All @@ -23,14 +26,6 @@ func defaultDCList() map[int]string {
}
}

// https://core.telegram.org/mtproto/mtproto-transports
var (
transportModeAbridged = [...]byte{0xef} // meta:immutable
transportModeIntermediate = [...]byte{0xee, 0xee, 0xee, 0xee} // meta:immutable
transportModePaddedIntermediate = [...]byte{0xdd, 0xdd, 0xdd, 0xdd} // meta:immutable
transportModeFull = [...]byte{} // meta:immutable
)

func MessageRequireToAck(msg tl.Object) bool {
switch msg.(type) {
case /**objects.Ping,*/ *objects.MsgsAck:
Expand All @@ -39,3 +34,10 @@ func MessageRequireToAck(msg tl.Object) bool {
return true
}
}

func CloseOnCancel(ctx context.Context, c io.Closer) {
go func() {
<-ctx.Done()
c.Close()
}()
}

0 comments on commit 2e19c0c

Please sign in to comment.