-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
rpc.go
72 lines (62 loc) · 1.74 KB
/
rpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package mtproto
import (
"context"
"errors"
"go.uber.org/zap"
"golang.org/x/xerrors"
"github.com/gotd/td/bin"
"github.com/gotd/td/internal/mt"
"github.com/gotd/td/internal/rpc"
)
// InvokeRaw sends input and decodes result into output.
//
// NOTE: Assuming that call contains content message (seqno increment).
func (c *Conn) InvokeRaw(ctx context.Context, input bin.Encoder, output bin.Decoder) error {
msgID, seqNo := c.nextMsgSeq(true)
req := rpc.Request{
MsgID: msgID,
SeqNo: seqNo,
Input: input,
Output: output,
}
log := c.log.With(
zap.Int64("msg_id", req.MsgID),
)
log.Debug("Invoke start")
defer log.Debug("Invoke end")
if err := c.rpc.Do(ctx, req); err != nil {
var badMsgErr *badMessageError
if errors.As(err, &badMsgErr) && badMsgErr.Code == codeIncorrectServerSalt {
// Should retry with new salt.
c.log.Debug("Setting server salt")
// Store salt from server.
c.storeSalt(badMsgErr.NewSalt)
// Reset saved salts to fetch new.
c.salts.Reset()
c.log.Info("Retrying request after basMsgErr", zap.Int64("msg_id", req.MsgID))
return c.rpc.Do(ctx, req)
}
return xerrors.Errorf("rpcDoRequest: %w", err)
}
return nil
}
func (c *Conn) dropRPC(req rpc.Request) error {
ctx, cancel := context.WithTimeout(context.Background(),
c.getTimeout(mt.RPCDropAnswerRequestTypeID),
)
defer cancel()
var resp mt.RPCDropAnswerBox
if err := c.InvokeRaw(ctx, &mt.RPCDropAnswerRequest{
ReqMsgID: req.MsgID,
}, &resp); err != nil {
return err
}
switch resp.RpcDropAnswer.(type) {
case *mt.RPCAnswerDropped, *mt.RPCAnswerDroppedRunning:
return nil
case *mt.RPCAnswerUnknown:
return xerrors.Errorf("answer unknown")
default:
return xerrors.Errorf("unexpected response type: %T", resp.RpcDropAnswer)
}
}