This commit is contained in:
sh2ezo 2019-01-17 21:34:14 +00:00 committed by GitHub
commit 9801073b9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 7 deletions

View file

@ -155,14 +155,24 @@ namespace TLSharp.Core
_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
};
}

View file

@ -16,6 +16,7 @@ using TLSharp.Core.MTProto.Crypto;
using TLSharp.Core.Network;
using TLSharp.Core.Utils;
using TLAuthorization = TeleSharp.TL.Auth.TLAuthorization;
using System.Net;
namespace TLSharp.Core
{
@ -30,8 +31,13 @@ namespace TLSharp.Core
private List<TLDcOption> 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");
@ -45,7 +51,20 @@ namespace TLSharp.Core
_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);
}