This commit is contained in:
ppanhoto78 2017-12-27 19:34:42 +00:00 committed by GitHub
commit d12da6d2f4
2 changed files with 13 additions and 15 deletions

View file

@ -69,13 +69,18 @@ namespace TLSharp.Core
public long LastMessageId { get; set; }
public int SessionExpires { get; set; }
public TLUser TLUser { get; set; }
public ISessionStore Store { get { return _store; }}
private Random random;
private ISessionStore _store;
public Session(ISessionStore store)
public Session(ISessionStore store, string sessionUserId)
{
random = new Random();
Id = GenerateRandomUlong ();
SessionUserId = sessionUserId;
ServerAddress = defaultConnectionAddress;
Port = defaultConnectionPort;
_store = store;
}
@ -133,7 +138,7 @@ namespace TLSharp.Core
var authData = Serializers.Bytes.read(reader);
return new Session(store)
return new Session(store, sessionUserId)
{
AuthKey = new AuthKey(authData),
Id = id,
@ -155,15 +160,9 @@ namespace TLSharp.Core
_store.Save(this);
}
public static Session TryLoadOrCreateNew(ISessionStore store, string sessionUserId)
public static Session GetSession(ISessionStore store, string sessionUserId, Session provided)
{
return store.Load(sessionUserId) ?? new Session(store)
{
Id = GenerateRandomUlong(),
SessionUserId = sessionUserId,
ServerAddress = defaultConnectionAddress,
Port = defaultConnectionPort
};
return store.Load (sessionUserId) ?? provided ?? new Session (store, sessionUserId);
}
private static ulong GenerateRandomUlong()

View file

@ -30,23 +30,22 @@ namespace TLSharp.Core
private List<TLDcOption> dcOptions;
private TcpClientConnectionHandler _handler;
public Session Session { get { return _session; } }
public TelegramClient(int apiId, string apiHash,
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
Session session = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
{
if (apiId == default(int))
throw new MissingApiConfigurationException("API_ID");
if (string.IsNullOrEmpty(apiHash))
throw new MissingApiConfigurationException("API_HASH");
if (store == null)
store = new FileSessionStore();
TLContext.Init();
_apiHash = apiHash;
_apiId = apiId;
_handler = handler;
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
_session = Session.GetSession(session?.Store ?? new FileSessionStore(), session?.SessionUserId ?? sessionUserId, session);
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
}