diff --git a/TLSharp.Core/Session.cs b/TLSharp.Core/Session.cs index 987c5d2..f4bca81 100644 --- a/TLSharp.Core/Session.cs +++ b/TLSharp.Core/Session.cs @@ -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() diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 0692486..eae8b59 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -30,23 +30,22 @@ namespace TLSharp.Core private List 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); }