public static async Task<PhotonWireOutboundS2SPeer> ConnectToOutboundServerAsync(PhotonWireApplicationBase applicationBase, IPEndPoint ipEndPoint, string applicationName, string groupName = null, object customInitObject = null, bool useMux = false, long? reconnectIntervalMs = 1000, Action<PhotonWireOutboundS2SPeer> onReconnected = null)
{
var outboundPeer = new PhotonWireOutboundS2SPeer(applicationBase);
if (reconnectIntervalMs != null)
{
applicationBase.reconnectTimer = new Timer(async _ =>
{
if (applicationBase.isStopRequested)
{
// disable timer
applicationBase.reconnectTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
try
{
if (outboundPeer.ConnectionState == ConnectionState.Disconnected)
{
await ReconnectAsync(applicationBase, ipEndPoint, applicationName, groupName, onReconnected, outboundPeer, reconnectIntervalMs.Value, customInitObject, useMux).ConfigureAwait(false);
}
}
catch (Exception ex)
{
applicationBase.Logger.ConnectToOutboundReconnectTimerException(applicationBase.ApplicationName, ex.GetType().Name, ex.Message, ex.StackTrace);
}
}, null, reconnectIntervalMs.Value, reconnectIntervalMs.Value);
}
var sw = Stopwatch.StartNew();
if (await outboundPeer.ConnectTcpAsync(ipEndPoint, applicationName, customInitObject, useMux).ConfigureAwait(false))
{
sw.Stop();
applicationBase.Logger.ConnectToOutboundServer(applicationBase.ApplicationName, ipEndPoint.ToString(), applicationName, sw.Elapsed.TotalMilliseconds);
PeerManager.OutboundServerConnections.Add(outboundPeer);
if (groupName != null)
{
PeerManager.OutboundServerConnections.AddGroup(groupName, outboundPeer);
}
}
else
{
sw.Stop();
applicationBase.Logger.ConnectToOutboundServerFailed(applicationBase.ApplicationName, ipEndPoint.ToString(), applicationName, sw.Elapsed.TotalMilliseconds);
}
return outboundPeer;
}