From f98540bd7fd20a56178b6c6a1c1ba04af201c09d Mon Sep 17 00:00:00 2001 From: steavy29 Date: Tue, 6 Sep 2016 18:20:06 +0300 Subject: [PATCH] Changed session store to return null when couldn't load instead of exception. --- TLSharp.Core/Session.cs | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/TLSharp.Core/Session.cs b/TLSharp.Core/Session.cs index 68da520..be1bf28 100644 --- a/TLSharp.Core/Session.cs +++ b/TLSharp.Core/Session.cs @@ -24,7 +24,11 @@ namespace TLSharp.Core public Session Load(string sessionUserId) { - using (var stream = new FileStream($"{sessionUserId}.dat", FileMode.Open)) + var sessionFileName = $"{sessionUserId}.dat"; + if (!File.Exists(sessionFileName)) + return null; + + using (var stream = new FileStream(sessionFileName, FileMode.Open)) { var buffer = new byte[2048]; stream.Read(buffer, 0, 2048); @@ -43,7 +47,7 @@ namespace TLSharp.Core public Session Load(string sessionUserId) { - throw new NotImplementedException(); + return null; } } @@ -151,24 +155,13 @@ namespace TLSharp.Core public static Session TryLoadOrCreateNew(ISessionStore store, string sessionUserId) { - Session session; - - try + return store.Load(sessionUserId) ?? new Session(store) { - session = store.Load(sessionUserId); - } - catch - { - session = new Session(store) - { - Id = GenerateRandomUlong(), - SessionUserId = sessionUserId, - ServerAddress = defaultConnectionAddress, - Port = defaultConnectionPort - }; - } - - return session; + Id = GenerateRandomUlong(), + SessionUserId = sessionUserId, + ServerAddress = defaultConnectionAddress, + Port = defaultConnectionPort + }; } private static ulong GenerateRandomUlong()