package mtproto

import (
	

	
	

	
	
	
)

func ( *Conn) ( *bin.Buffer) error {
	 := context.Background()
	var  mt.NewSessionCreated
	if  := .Decode();  != nil {
		return errors.Wrap(, "decode")
	}
	.gotSession.Signal()

	 := proto.MessageID(.FirstMsgID).Time()
	 := .clock.Now()
	.log.Debug(, "Session created",
		log.Int64("unique_id", .UniqueID),
		log.Int64("first_msg_id", .FirstMsgID),
		log.Time("first_msg_time", .Local()),
	)

	if (.Before() && .Sub() > maxPast) || .Sub() > maxFuture {
		.log.Warn(, "Local clock needs synchronization",
			log.Time("first_msg_time", ),
			log.Time("local", ),
			log.Duration("time_difference", .Sub()),
		)
	}

	.storeSalt(.ServerSalt)
	if  := .handler.OnSession(.session());  != nil {
		return errors.Wrap(, "handler.OnSession")
	}
	// Requests sent before first_msg_id belong to a server session that is
	// gone: send them again, like TDLib (Session::on_new_session_created).
	if .rpc != nil {
		.rpc.NotifyErrorBefore(.FirstMsgID, errSessionReset)
	}
	if ,  := .handler.(NewSessionHandler);  {
		if  := .OnNewSession(.FirstMsgID);  != nil {
			return errors.Wrap(, "handler.OnNewSession")
		}
	}
	return nil
}

// NewSessionHandler is implemented by a Handler that wants to know when the
// server created a new session (new_session_created), e.g. to recover
// updates it may have missed.
type NewSessionHandler interface {
	OnNewSession(firstMsgID int64) error
}