Exemple #1
0
 private void ShutDown()
 {
     StopWatchingFiles();
     AniDBDispose();
     StopHost();
     ServerShutdown?.Invoke(this, null);
 }
Exemple #2
0
        private void HandleSuperKill(NetworkMessage msg)
        {
            NetClient.Shutdown();

            if (ServerShutdown != null)
            {
                ServerShutdown.Invoke(this, EventArgs.Empty);
            }
        }
Exemple #3
0
        /// <summary>
        /// Runs server shutdown procedures (disconnects all clients)
        /// </summary>
        public virtual void Shutdown()
        {
            _serverRunning = false;
            _server.Stop();

            var _clientsCopy = new List <T>(_clients); //So I can run a foreach loop without stuff being removed in the middle

            foreach (var _client in _clientsCopy)
            {
                _client.Disconnect(new DisconnectArgs("Server shutdown"));
                //Removal of clients from _clients is handled by _clientDisconnected
            }

            ServerShutdown?.Invoke(this);
        }
Exemple #4
0
        public async Task <bool> TryShutdownAsync()
        {
            ExitRequested = true;

            if (Server != null)
            {
                if (Server.Server.Connected)
                {
                    Server.Stop();
                }

                while (Server.Server.Connected)
                {
                    await Task.Delay(1).ConfigureAwait(false);
                }

                Server = null;
            }

            ServerShutdown?.Invoke(this, new OnServerShutdownEventArgs(DateTime.Now, ExitRequested));
            return(true);
        }
Exemple #5
0
        /// <summary>
        /// Принимает сообщения от сервера.
        /// </summary>
        /// <param name="token">Токен отмены.</param>
        /// <returns></returns>
        public async ValueTask ReceiveMessagesAsync(CancellationToken token)
        {
            var messageBuilder = new StringBuilder();

            while (!token.IsCancellationRequested)
            {
                var stream = _client.GetStream();
                var buffer = new byte[256];
                messageBuilder.Clear();
                try
                {
                    do
                    {
                        var bytes = await stream.ReadAsync(buffer, 0, buffer.Length, token);

                        messageBuilder.Append(Encoding.Default.GetString(buffer, 0, bytes));
                    } while (stream.DataAvailable && !token.IsCancellationRequested);
                }
                catch (SocketException ex)
                {
                    NetworkStreamError?.Invoke(this, new SingleMessageEventArgs(ex.Message));
                    break;
                }

                if (token.IsCancellationRequested)
                {
                    break;
                }

                var message = messageBuilder.ToString();
                MessageReceived?.Invoke(this, new SingleMessageEventArgs(message));
                if (ServerShutdownMessage.Equals(message))
                {
                    ServerShutdown?.Invoke(this, EventArgs.Empty);
                }
            }
        }
Exemple #6
0
    public override void OnStopServer()
    {
        //Base handling
        base.OnStopServer();

        //Discconect and destroy all clients on the server and clients
        for (int i = 0; i < PlayersConnected.Count; i++)
        {
            NetworkPlayer p = PlayersConnected[i];
            if (p != null)
            {
                NetworkServer.Destroy(p.gameObject);
            }
        }
        PlayersConnected.Clear();
        //Reset network the scene name
        networkSceneName = string.Empty;

        //Fire evemt
        if (ServerShutdown != null)
        {
            ServerShutdown.Invoke();
        }
    }
Exemple #7
0
 internal static void InvokeServerShutdown()
 {
     ServerShutdown?.Invoke();
 }
Exemple #8
0
 protected void _invokeServerShutdown()
 {
     ServerShutdown.Invoke(this);
 }
Exemple #9
0
        /// <summary>
        ///     Receives messages from server and handles them
        /// </summary>
        /// <param name="state"></param>
        private void ReceiveUpdatesLoop(SocketState state)
        {
            if (state.ErrorOccured)
            {
                // inform the view
                Error?.Invoke(state.ErrorMessage, "Error In Event Loop");
                Disconnect();
                return;
            }

            string incomingMessages = state.GetData();

            string[] data = Regex.Split(incomingMessages, @"(?<=[\n])");

            foreach (string message in data.Where(message => message.Length != 0))
            {
                //Last Step of handshake if message is the id
                if (int.TryParse(message, out int i))
                {
                    IDReceive?.Invoke(i);
                    continue;
                }

                // The regex splitter will include the last string even if it doesn't end with a '\n',
                // So we need to ignore it if this happens.
                if (message.Last() != '\n' || message[0] != '{')
                {
                    continue;
                }

                Console.WriteLine("Received:" + message);

                //Parse message as json object
                var x = JObject.Parse(message);
                switch (x["messageType"]?.ToString())
                {
                case "cellUpdated":
                {
                    if (EditCell is null)
                    {
                        continue;
                    }
                    var updated = JsonConvert.DeserializeObject <CellUpdated>(message);
                    EditCell.Invoke(updated);
                    break;
                }

                case "cellSelected":
                {
                    if (SelectCell is null)
                    {
                        continue;
                    }
                    var selected = JsonConvert.DeserializeObject <CellSelected>(message);
                    SelectCell.Invoke(selected);
                    break;
                }

                case "serverError":
                {
                    if (ServerShutdown is null)
                    {
                        continue;
                    }
                    var error = JsonConvert.DeserializeObject <ServerShutdownError>(message);
                    ServerShutdown.Invoke(error);
                    break;
                }

                case "requestError":
                {
                    if (RequestError is null)
                    {
                        continue;
                    }
                    var error = JsonConvert.DeserializeObject <RequestError>(message);
                    RequestError.Invoke(error);
                    break;
                }

                case "disconnected":
                {
                    var d = JsonConvert.DeserializeObject <Disconnected>(message);
                    ClientDisconnected?.Invoke(d);
                    break;
                }
                }

                state.RemoveData(0, message.Length);
            }

            Networking.GetData(state);
        }
Exemple #10
0
 public void ShutdownServer()
 {
     ServerShutdown?.Invoke(this, null);
 }