From b12ecde8e659e6c9ccaf61029d04fcbdc32e9e63 Mon Sep 17 00:00:00 2001 From: sh2ezo Date: Thu, 23 Nov 2017 20:03:28 +0300 Subject: [PATCH] Added support for custom server address --- TLSharp.Core/Session.cs | 18 ++++++++++++++---- TLSharp.Core/TelegramClient.cs | 25 ++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/TLSharp.Core/Session.cs b/TLSharp.Core/Session.cs index 7a13da9c..37ccf533 100644 --- a/TLSharp.Core/Session.cs +++ b/TLSharp.Core/Session.cs @@ -155,14 +155,24 @@ public void Save() _store.Save(this); } - public static Session TryLoadOrCreateNew(ISessionStore store, string sessionUserId) + public static Session TryLoad(ISessionStore store, string sessionUserId) { - return store.Load(sessionUserId) ?? new Session(store) + return store.Load(sessionUserId); + } + + public static Session Create( + ISessionStore store, + string sessionUserId, + string serverAddress = defaultConnectionAddress, + int serverPort = defaultConnectionPort + ) + { + return new Session(store) { Id = GenerateRandomUlong(), SessionUserId = sessionUserId, - ServerAddress = defaultConnectionAddress, - Port = defaultConnectionPort + ServerAddress = serverAddress, + Port = serverPort }; } diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 06924869..e75b4239 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -16,6 +16,7 @@ using TLSharp.Core.Network; using TLSharp.Core.Utils; using TLAuthorization = TeleSharp.TL.Auth.TLAuthorization; +using System.Net; namespace TLSharp.Core { @@ -30,8 +31,13 @@ public class TelegramClient : IDisposable private List dcOptions; private TcpClientConnectionHandler _handler; - public TelegramClient(int apiId, string apiHash, - ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null) + public TelegramClient(int apiId, + string apiHash, + ISessionStore store = null, + string sessionUserId = "session", + TcpClientConnectionHandler handler = null, + IPEndPoint connectionAddress = null + ) { if (apiId == default(int)) throw new MissingApiConfigurationException("API_ID"); @@ -46,7 +52,20 @@ public TelegramClient(int apiId, string apiHash, _apiId = apiId; _handler = handler; - _session = Session.TryLoadOrCreateNew(store, sessionUserId); + _session = Session.TryLoad(store, sessionUserId); + + if(_session == null) + { + if(connectionAddress != null) + { + _session = Session.Create(store, sessionUserId, connectionAddress.Address.ToString(), connectionAddress.Port); + } + else + { + _session = Session.Create(store, sessionUserId); + } + } + _transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler); }