package mtproto

import 

// SessionState is the server session of a connection slot: session_id and
// the number of content messages sent in it (for seqno). Connections that
// share a SessionState one at a time continue the same server session after
// a reconnect with the same auth key, so the server keeps its queued answers
// and updates and does not send new_session_created, like TDLib and Telegram
// Desktop do. A new session is started when the key changes.
//
// A SessionState must be used by one connection at a time: acquire it with
// TryAcquire before passing it in Options; the connection releases it when
// Run returns.
type SessionState struct {
	mux             sync.Mutex
	keyID           [8]byte
	id              int64
	contentMessages int32
	inUse           bool
	// lastBindAt is the time of the last successful auth.bindTempAuthKey
	// of the slot, Unix seconds (PFS key recovery policy).
	lastBindAt int64
}

// NewSessionState returns an empty SessionState.
func () *SessionState {
	return &SessionState{}
}

// TryAcquire marks the state as used by a connection. It reports false if
// another connection uses it.
func ( *SessionState) () bool {
	.mux.Lock()
	defer .mux.Unlock()
	if .inUse {
		return false
	}
	.inUse = true
	return true
}

// load returns the session of key keyID, if any.
func ( *SessionState) ( [8]byte) ( int64,  int32,  bool) {
	.mux.Lock()
	defer .mux.Unlock()
	if .id == 0 || .keyID !=  {
		return 0, 0, false
	}
	return .id, .contentMessages, true
}

// release saves the session of the connection and frees the state. A zero id
// forgets the session.
func ( *SessionState) ( [8]byte,  int64,  int32) {
	.mux.Lock()
	defer .mux.Unlock()
	.keyID = 
	.id = 
	.contentMessages = 
	.inUse = false
}

func ( *SessionState) ( int64) {
	.mux.Lock()
	.lastBindAt = 
	.mux.Unlock()
}

func ( *SessionState) () int64 {
	.mux.Lock()
	defer .mux.Unlock()
	return .lastBindAt
}