package mtproto

import (
	
	

	
	

	
	
	
)

// Invoke sends input and decodes result into output.
//
// NOTE: Assuming that call contains content message (seqno increment).
func ( *Conn) ( context.Context,  bin.Encoder,  bin.Decoder) error {
	 := 0
	for {
		 := .invokeOnce(, , )
		if errors.Is(, errSessionReset) {
			// The session was replaced while the request was pending: send it
			// again with a new msg_id and seqno of the new session.
			.log.Info(, "Resending request in new session")
			continue
		}
		if ,  := badMsgTimeCode();  &&  == codeMessageIDTooHigh {
			// The session is closed (failSession) and the server did not
			// process the request: send it again on the next connection,
			// in a new session.
			return errors.Wrap(rpc.ErrEngineClosed, "msg_id too high, resend in a new session")
		}
		if ,  := badMsgTimeCode();  &&  < maxTimeResends {
			// The server refused the msg_id because of its time; the server
			// time offset is corrected by now (see handleBadMsg and
			// observeServerTime), so send the request again with a new
			// msg_id, like TDLib does for 16, 17 and 20.
			++
			.log.Info(, "Resending request with corrected msg_id",
				log.Int("code", ),
				log.Duration("server_time_offset", .serverTimeOffset()),
			)
			continue
		}
		return 
	}
}

// maxTimeResends limits resends of one request after bad_msg_notification
// about its msg_id time.
const maxTimeResends = 3

// codeMessageTooOld is bad_msg_notification "message too old".
const codeMessageTooOld = 20

// badMsgTimeCode reports whether err is a bad_msg_notification about the
// msg_id time (16, 17 or 20), after which the request can be sent again.
func ( error) (int, bool) {
	var  *badMessageError
	if !errors.As(, &) {
		return 0, false
	}
	switch .Code {
	case codeMessageIDTooLow, codeMessageIDTooHigh, codeMessageTooOld:
		return .Code, true
	}
	return 0, false
}

// serverTimeOffset returns the server time offset, zero if unknown.
func ( *Conn) () time.Duration {
	if .serverTime == nil {
		return 0
	}
	return .serverTime.Offset()
}

func ( *Conn) ( context.Context,  bin.Encoder,  bin.Decoder) error {
	,  := .nextMsgSeq(true)
	 := rpc.Request{
		MsgID:  ,
		SeqNo:  ,
		Input:  ,
		Output: ,
	}

	 := .log.With(
		log.Int64("msg_id", .MsgID),
	)
	.Debug(, "Invoke start")
	defer .Debug(, "Invoke end")

	if .wire == nil {
		return .invoke(, )
	}
	 := .traceRequestStart()
	 := .invoke(, )
	.traceRequestDone(, , )
	return 
}

func ( *Conn) ( context.Context,  rpc.Request) error {
	if  := .rpc.Do(, );  != nil {
		var  *badMessageError
		if errors.As(, &) && .Code == codeIncorrectServerSalt {
			// Should retry with new salt.
			.log.Debug(, "Setting server salt")
			// Store salt from server.
			.storeSalt(.NewSalt)
			// Reset saved salts to fetch new.
			.salts.Reset()
			.log.Info(, "Retrying request after basMsgErr", log.Int64("msg_id", .MsgID))
			.traceResend(, "bad_server_salt")
			return .rpc.Do(, )
		}
		return errors.Wrap(, "rpcDoRequest")
	}

	return nil
}

func ( *Conn) ( rpc.Request) error {
	,  := context.WithTimeout(context.Background(),
		.getTimeout(mt.RPCDropAnswerRequestTypeID),
	)
	defer ()

	var  mt.RPCDropAnswerBox
	if  := .Invoke(, &mt.RPCDropAnswerRequest{
		ReqMsgID: .MsgID,
	}, &);  != nil {
		return 
	}

	switch .RpcDropAnswer.(type) {
	case *mt.RPCAnswerDropped, *mt.RPCAnswerDroppedRunning:
		return nil
	case *mt.RPCAnswerUnknown:
		return errors.New("answer unknown")
	default:
		return errors.Errorf("unexpected response type: %T", .RpcDropAnswer)
	}
}