Beispiel #1
0
        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);
        }
Beispiel #2
0
        private static async Task ReconnectAsync(PhotonWireApplicationBase applicationBase, IPEndPoint ipEndPoint, string applicationName, string groupName, Action <PhotonWireOutboundS2SPeer> onReconnected, PhotonWireOutboundS2SPeer outboundPeer, long reconnectInterval, object customInitObject, bool useMux)
        {
            var resw = Stopwatch.StartNew();

            if (await outboundPeer.ConnectTcpAsync(ipEndPoint, applicationName, customInitObject, useMux).ConfigureAwait(false))
            {
                resw.Stop();
                applicationBase.Logger.ReconnectToOutboundServer(applicationBase.ApplicationName, ipEndPoint.ToString(), applicationName, resw.Elapsed.TotalMilliseconds);

                PeerManager.OutboundServerConnections.Add(outboundPeer);
                if (groupName != null)
                {
                    PeerManager.OutboundServerConnections.AddGroup(groupName, outboundPeer);
                }

                if (onReconnected != null)
                {
                    onReconnected(outboundPeer);
                }
            }
            else
            {
                resw.Stop();
                applicationBase.Logger.ReconnectToOutboundServerFailed(applicationBase.ApplicationName, ipEndPoint.ToString(), applicationName, resw.Elapsed.TotalMilliseconds);
            }
        }