mirror of
https://github.com/wiz0u/WTelegramClient.git
synced 2025-12-06 06:52:01 +01:00
Correctly dispose session store on ctor exception (#128)
This commit is contained in:
parent
5d0fd6452f
commit
86796ebf0c
|
|
@ -166,7 +166,7 @@ See [Examples/Program_ListenUpdates.cs](https://github.com/wiz0u/WTelegramClient
|
||||||
|
|
||||||
An invalid API request can result in a `RpcException` being raised, reflecting the [error code and status text](https://revgram.github.io/errors.html) of the problem.
|
An invalid API request can result in a `RpcException` being raised, reflecting the [error code and status text](https://revgram.github.io/errors.html) of the problem.
|
||||||
|
|
||||||
The other configuration items that you can override include: **session_pathname, email, email_verification_code, session_key, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code, firebase, user_id, bot_token**
|
The other configuration items that you can provide include: **session_pathname, email, email_verification_code, session_key, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code, firebase, user_id, bot_token**
|
||||||
|
|
||||||
Optional API parameters have a default value of `null` when unset. Passing `null` for a required string/array is the same as *empty* (0-length).
|
Optional API parameters have a default value of `null` when unset. Passing `null` for a required string/array is the same as *empty* (0-length).
|
||||||
Required API parameters/fields can sometimes be set to 0 or `null` when unused (check API documentation or experiment).
|
Required API parameters/fields can sometimes be set to 0 or `null` when unused (check API documentation or experiment).
|
||||||
|
|
|
||||||
|
|
@ -96,8 +96,8 @@ namespace WTelegram
|
||||||
public Client(Func<string, string> configProvider = null, Stream sessionStore = null)
|
public Client(Func<string, string> configProvider = null, Stream sessionStore = null)
|
||||||
{
|
{
|
||||||
_config = configProvider ?? DefaultConfigOrAsk;
|
_config = configProvider ?? DefaultConfigOrAsk;
|
||||||
sessionStore ??= new SessionStore(Config("session_pathname"));
|
|
||||||
var session_key = _config("session_key") ?? (_apiHash = Config("api_hash"));
|
var session_key = _config("session_key") ?? (_apiHash = Config("api_hash"));
|
||||||
|
sessionStore ??= new SessionStore(Config("session_pathname"));
|
||||||
_session = Session.LoadOrCreate(sessionStore, Convert.FromHexString(session_key));
|
_session = Session.LoadOrCreate(sessionStore, Convert.FromHexString(session_key));
|
||||||
if (_session.ApiId == 0) _session.ApiId = int.Parse(Config("api_id"));
|
if (_session.ApiId == 0) _session.ApiId = int.Parse(Config("api_id"));
|
||||||
if (_session.MainDC != 0) _session.DCSessions.TryGetValue(_session.MainDC, out _dcSession);
|
if (_session.MainDC != 0) _session.DCSessions.TryGetValue(_session.MainDC, out _dcSession);
|
||||||
|
|
@ -699,15 +699,7 @@ namespace WTelegram
|
||||||
static async Task<TcpClient> DefaultTcpHandler(string host, int port)
|
static async Task<TcpClient> DefaultTcpHandler(string host, int port)
|
||||||
{
|
{
|
||||||
var tcpClient = new TcpClient();
|
var tcpClient = new TcpClient();
|
||||||
try
|
|
||||||
{
|
|
||||||
await tcpClient.ConnectAsync(host, port);
|
await tcpClient.ConnectAsync(host, port);
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
tcpClient.Dispose();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
return tcpClient;
|
return tcpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -113,12 +113,6 @@ namespace WTelegram
|
||||||
session = JsonSerializer.Deserialize<Session>(utf8Json.AsSpan(32), Helpers.JsonOptions);
|
session = JsonSerializer.Deserialize<Session>(utf8Json.AsSpan(32), Helpers.JsonOptions);
|
||||||
Helpers.Log(2, "Loaded previous session");
|
Helpers.Log(2, "Loaded previous session");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
store.Dispose();
|
|
||||||
throw new ApplicationException($"Exception while reading session file: {ex.Message}\nUse the correct api_hash/id/key, or delete the file to start a new session", ex);
|
|
||||||
}
|
|
||||||
session ??= new Session();
|
session ??= new Session();
|
||||||
session._store = store;
|
session._store = store;
|
||||||
Encryption.RNG.GetBytes(session._encrypted, 0, 16);
|
Encryption.RNG.GetBytes(session._encrypted, 0, 16);
|
||||||
|
|
@ -127,6 +121,12 @@ namespace WTelegram
|
||||||
session._jsonWriter = new Utf8JsonWriter(session._jsonStream, default);
|
session._jsonWriter = new Utf8JsonWriter(session._jsonStream, default);
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
store.Dispose();
|
||||||
|
throw new ApplicationException($"Exception while reading session file: {ex.Message}\nUse the correct api_hash/id/key, or delete the file to start a new session", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal void Save() // must be called with lock(session)
|
internal void Save() // must be called with lock(session)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue