Example #1
0
        private void Reconnect(object state)
        {
            lock (_sync)
            {
                try
                {
                    var now = DateTimeOffset.Now;

                    if (!IsConnected)
                    {
                        if ((now - _lastConnected).TotalSeconds > 5)
                        {
                            Start(true);
                        }
                    }
                    else if (_lastPing > _lastPong && (now - _lastPong).TotalSeconds > 20)
                    {
                        MainSocket?.Close();
                    }
                    else if ((now - _lastPing).TotalSeconds > 5)
                    {
                        _lastPing = DateTimeOffset.Now;
                        MainSocket.C2S_KeepAlive();
                    }
                }
                catch { }
                finally
                {
                    _reconnectTimer.Change(100, Timeout.Infinite);
                }
            }
        }
Example #2
0
 public void Dispose()
 {
     if (MainSocket != null)
     {
         MainSocket.Close();
     }
 }
Example #3
0
 public void Stop()
 {
     lock (_sync)
     {
         _reconnectTimer.Change(Timeout.Infinite, Timeout.Infinite);
         MainSocket?.Close();
     }
 }
        protected override void OnClose(TSession session, SocketCloseReason reason)
        {
            MainSocket.Close();

            TSession sessOut;

            // If the session is null, the connection timed out while trying to connect.
            if (session != null)
            {
                ConnectedSessions.TryRemove(Session.Id, out sessOut);
            }

            base.OnClose(session, reason);
        }
        /// <summary>
        /// Terminates this server and notify all connected clients.
        /// </summary>
        public void Stop()
        {
            if (_isStopped)
            {
                return;
            }
            TSession[] sessions = new TSession[ConnectedSessions.Values.Count];
            ConnectedSessions.Values.CopyTo(sessions, 0);

            foreach (var session in sessions)
            {
                session.Close(SocketCloseReason.ServerClosing);
            }

            MainSocket.Close();
            _isStopped = true;
        }
 public static void Close()
 {
     if (IsOpen)
     {
         MainSocket.Close();
         foreach (SausageConnection u in ConnectedUsers)
         {
             u.Disconnect();
         }
         UiCtx.Send(x => Vm.Messages = new ObservableCollection <IMessage>());
         UiCtx.Send(x => Vm.Messages.Add(new ServerMessage("Closed all sockets")));
         IsOpen = false;
     }
     else
     {
         MessageBox.Show("Server is already closed", "Sausage Server");
     }
 }
Example #7
0
 private void HandleNewSocket(Socket socket)
 {
     if (MainSocket == null || !MainSocket.IsConnected || (DateTimeOffset.Now - _lastPing).TotalSeconds > 15)
     {
         Console.WriteLine($"Replacing the old socket with one from {socket.RemoteEndPoint}");
         if (MainSocket != null)
         {
             MainSocket.Close();
         }
         _lastPing  = DateTimeOffset.Now;
         MainSocket = new ProtocolSocket(socket);
         MainSocket.C2S_OnStartNewConnectionReply = HandleConnectionReply;
         MainSocket.C2S_OnKeepAlive = HandleKeepAlive;
         MainSocket.Initialize();
     }
     else
     {
         socket.Close();
     }
 }
 public void Dispose()
 {
     MainSocket.Close();
 }