Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Add support for custom server address #640

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions TLSharp.Core/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}

Expand Down
25 changes: 22 additions & 3 deletions TLSharp.Core/TelegramClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using TLSharp.Core.Network;
using TLSharp.Core.Utils;
using TLAuthorization = TeleSharp.TL.Auth.TLAuthorization;
using System.Net;

namespace TLSharp.Core
{
Expand All @@ -30,8 +31,13 @@ public class TelegramClient : IDisposable
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");
Expand All @@ -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);
}

Expand Down