Пример #1
0
        private static void WaitForAnotherInstance()
        {
            //var appSingleton = new Mutex(false, "talkysingleinstancemtx");
            TalkyLog.Debug("Server|MutexWaiting|");

            try
            {
                wh.WaitOne();
                //appSingleton.WaitOne();
            }
            catch (AbandonedMutexException e)
            {
                TalkyLog.Debug(e.ToString());
                //appSingleton.WaitOne();

                //another process exited
            }
            catch (System.Exception e)
            {
                TalkyLog.Debug(e.ToString());
                //ManageInstances();
                Environment.Exit(-1);
            }
            TalkyLog.Debug("Server|MutexAcquired|");
        }
Пример #2
0
        public void Start()
        {
            _isActive = true;

            while (_isActive)
            {
                try
                {
                    if (!_listenerThread.IsAlive)
                    {
                        _listenerThread.Start();
                    }

                    IReadOnlyCollection <ClientChannel> clientChannels = _channelRepository.Get <ClientChannel>();

                    if (clientChannels.Count > 0)
                    {
                        foreach (ClientChannel clientChannel in clientChannels)
                        {
                            if (_clientRepository.Find(clientChannel).Count == 0)
                            {
                                _channelRepository.Remove(clientChannel);
                            }
                        }
                    }

                    var clients = _clientRepository.All();
                    if (clients.Count > 0)
                    {
                        foreach (var client in clients)
                        {
                            int now = (int)(DateTime.UtcNow.Subtract(_started)).TotalSeconds;
                            if (client.LastActivity < now - 300)
                            {
                                client.Disconnect("Idle for " + (now - client.LastActivity) + " seconds.");
                            }
                        }
                    }

                    Console.Clear();
                    Console.WriteLine("Talky | Created by SysVoid");
                    Console.WriteLine("==========================");
                    Console.WriteLine("Clients: " + UserCount);
                    Console.WriteLine("Channels: " + ChannelCount);
                    Console.WriteLine("Commands: " + CommandCount);
                    Console.WriteLine("==========================");
                    _consoleWaitEvent.WaitOne(1000);
                }
                catch (System.Exception ex)
                {
                    TalkyLog.Debug(ex.ToString());
                }
            }
        }
Пример #3
0
        private void ListenForClients()
        {
            //simulrate listening tread crash
            int          numconnected = 0;
            TcpClient    tcpClient    = null;
            ServerClient serverClient = null;

            while (_isActive)
            {
                try
                {
                    if (_listener == null)
                    {
                        _listener = new TcpListener(IPAddress.Any, _port);
                        _listener.Start();
                        TalkyLog.Debug("Server|Listener|Started|");
                    }

                    TalkyLog.Debug("Server|Listener|Accepting|");
                    tcpClient    = _listener.AcceptTcpClient();
                    serverClient = new ServerClient(tcpClient);

                    if (_channelRepository.GetLobby() == null)
                    {
                        serverClient.Disconnect("§2Server Error: No Lobby!");
                        continue;
                    }

                    numconnected++;
                    if (UserCount > 1)
                    {
                        throw new ApplicationException("Simulate crash");
                    }

                    var clientThread = new Thread(new ServerConnection(serverClient).HandleMessages);
                    clientThread.Start();
                }
                catch (System.Exception ex)
                {
                    TalkyLog.Debug("Server|Listener|Crashed");
                    serverClient?.Disconnect();
                    tcpClient?.Close();
                    TalkyLog.Debug(ex.ToString());
                }
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            _handler += Handler;
            SetConsoleCtrlHandler(_handler, true);
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

            TalkyLog.Debug("Server|Starting|");
            Console.Write("Starting server... ");
            int port = 0;

            if (args.Length > 0)
            {
                string thePort = args[0];
                int.TryParse(thePort, out port);
            }

            if (port <= 0)
            {
                port = 4096;
            }

            Console.WriteLine($" on port {port}");

            _chatServer1 = new ChatServer(port);

            WaitForAnotherInstance();
            ManageInstances();
            _chatServer1.Init();

            try
            {
                (new Thread(_chatServer1.Start)).Start();
            }
            catch (System.Exception ex)
            {
                Console.Write($"Server start error : {ex}");
            }
        }
Пример #5
0
        private void Listen()
        {
            _isRunning = true;
            while (_isRunning)
            {
                //move message processing into different function
                string line = _connection.Read();

                if (!_isRunning)
                {
                    break;
                }

                if (!_connection.IsConnected())
                {
                    var sw = System.Diagnostics.Stopwatch.StartNew();
                    //TalkyLog.Debug($"Client|Disconnected|");
                    _connection.Connect();
                    TalkyLog.Debug($"Client|Connected|{sw.ElapsedMilliseconds}");
                    //Task.Delay(500);
                    continue;
                }

                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                if (line.StartsWith("M:"))
                {
                    ChatMessage theMessage = new ChatMessage(line.Substring(2));

                    _messageLog.Invoke((MethodInvoker) delegate
                    {
                        foreach (string piece in theMessage.Pieces.Keys)
                        {
                            _messageLog.SelectionColor = theMessage.Pieces[piece];
                            _messageLog.AppendText(piece);
                        }
                        _messageLog.AppendText(Environment.NewLine);
                    });

                    if (_connection.IsConnected())
                    {
                        _connection.Send("S:ChannelClientList");
                    }
                }
                else if (line.StartsWith("S:ChannelList:"))
                {
                    string[] channels = line.Substring(14).Split(';');
                    new ChannelList(channels, _connection).ShowDialog();
                }
                else if (line.StartsWith("S:ChannelClientList:"))
                {
                    string[] clients = line.Substring(20).Split(';');
                    _clientListComboBox.Invoke((MethodInvoker) delegate
                    {
                        _clientListComboBox.Items.Clear();
                        _clientListComboBox.Items.AddRange(clients);
                    });
                }
                else if (line.StartsWith("S:Client:"))
                {
                    string[] data     = line.Substring(9).Split(';');
                    string   username = data[0];
                    string   muted    = data[1];
                    string   channel  = data[2];

                    _titleLabel.Invoke((MethodInvoker) delegate
                    {
                        _titleLabel.Text = $@"[{username}]  {channel} on {_connection.Host}:{_connection.Port}";
                    });

                    Invoke((MethodInvoker) delegate
                    {
                        Text = $@"[{username}] {channel} on {_connection.Host}:{_connection.Port}";
                    });
                }
                else if (line.StartsWith("S:Account:"))
                {
                    string[] data      = line.Substring(9).Split(';');
                    string   accountId = data[0];
                    string   username  = data[1];
                    string   role      = data[2];

                    _accountLabel.Invoke((MethodInvoker) delegate
                    {
                        _accountLabel.Visible = true;
                    });

                    _accountUsernameLabel.Invoke((MethodInvoker) delegate
                    {
                        _accountUsernameLabel.Visible = true;
                        _accountUsernameLabel.Text    = username;
                    });

                    _accountRoleLabel.Invoke((MethodInvoker) delegate
                    {
                        _accountRoleLabel.Visible = true;
                        _accountRoleLabel.Text    = role;
                    });

                    _connection.Send("S:ChannelClientList");
                }
            }
        }