Application Startup for PhotonWire.
상속: Photon.SocketServer.ApplicationBase
예제 #1
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);
            }
        }
예제 #2
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);
        }
예제 #3
0
 protected Task <PhotonWireOutboundS2SPeer> ConnectToOutboundServerAsync(IPEndPoint ipEndPoint, string applicationName, string groupName = null, object customInitObject = null, bool useMux = false, long?reconnectIntervalMs = 1000, Action <PhotonWireOutboundS2SPeer> onReconnected = null)
 {
     return(PhotonWireApplicationBase.ConnectToOutboundServerAsync(this, ipEndPoint, applicationName, groupName, customInitObject, useMux, reconnectIntervalMs, onReconnected));
 }
        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;
        }
        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);
            }
        }