Beispiel #1
0
 private void CleanUp()
 {
     try
     {
         _disable = true;
         _server.OnDisconnect(this);
         _writer.Close();
     }
     catch (Exception e)
     {
         Logger.Warn("An error occurred while cleaning up client! " + e);
     }
 }
Beispiel #2
0
        internal Client(int id, TcpClient socket, GodotServer server)
        {
            ID       = id;
            _server  = server;
            _disable = false;
            _socket  = socket;
            _writer  = new StreamWriter(socket.GetStream());
            var thread = new Thread(() =>
            {
                try
                {
                    while (!_disable)
                    {
                        string data = "";
                        do
                        {
                            byte[] buffer = new byte[_socket.Available];
                            int length    = _socket.GetStream().Read(buffer, 0, _socket.Available);
                            data         += Encoding.ASCII.GetString(buffer, 0, length);
                        } while (_socket.GetStream().DataAvailable);

                        if (!data.StartsWith("{"))
                        {
                            data = data.Substring(data.IndexOf("{", StringComparison.Ordinal));
                        }
                        OnMessage(data);
                    }
                }
                catch
                {
                    Logger.Warn("Client connection lost because of an error but it will be ignored because it should be wanted!");
                    CleanUp();
                }
            })
            {
                IsBackground = true
            };

            thread.Start();
        }