Beispiel #1
0
        private void StopServer()
        {
            try
            {
                if (_tcpListener != null)
                {
                    Application.Current.Dispatcher.Invoke(delegate
                    {
                        foreach (var client in ClientsOnServer)
                        {
                            client.TcpClient.PutDataToStream(new NetworkCommand(NetworkCommandTypes.ServerStop));
                        }
                        _tcpListener.Server.Close();
                        _tcpListener.Stop();
                        _isServerRun = false;

                        _thread.Abort();

                        ServerEvents.Add(new ServerEvent(ServerEventEnum.Stop, DateTime.Now));
                    });
                }
            }
            catch (Exception e)
            {
                ServerEvents.Add(new ServerEvent(ServerEventEnum.Error, DateTime.Now, e.ToString()));
            }
        }
Beispiel #2
0
        private void ShowSettings()
        {
            try
            {
                var settingsDialogVm = new SettingsViewModel(_serverName, _port, _networkInterface);

                var settingsDialog = new ServerSettingsView {
                    DataContext = settingsDialogVm
                };

                settingsDialog.Owner = Application.Current.MainWindow;

                if (settingsDialog.ShowDialog() == true)
                {
                    ServerName        = settingsDialogVm.Name;
                    _port             = settingsDialogVm.Port;
                    _networkInterface = settingsDialogVm.SelectedNetworkInterface;

                    var ipAddress = _networkInterface.GetIPProperties().UnicastAddresses.First(x => x.Address.AddressFamily == AddressFamily.InterNetwork); //получает IPv4
                    _ipEndPoint = new IPEndPoint(ipAddress.Address, _port);

                    StopServer();
                    _tcpListener = new TcpListener(_ipEndPoint);
                    // _tcpListener = new TcpListener(_ipAddress, _port);
                    StartServer();
                }
            }
            catch (Exception e)
            {
                ServerEvents.Add(new ServerEvent(ServerEventEnum.Error, DateTime.Now, e.ToString()));
            }
        }
Beispiel #3
0
 private void OpenHistory()
 {
     try
     {
         Process.Start(_logFileName);
     }
     catch (Exception e)
     {
         ServerEvents.Add(new ServerEvent(ServerEventEnum.Error, DateTime.Now, e.ToString()));
     }
 }
Beispiel #4
0
        private void ClientThread(Object stateInfo)
        {
            try
            {
                var client = (TcpClient)stateInfo;

                //получение имени клиента
                var newClientName = (string)client.GetDataFromStream();

                var clientOnServer = new ClientOnServer(newClientName, client)
                {
                    ConnectionTimer = { Interval = _timeSpan.TotalMilliseconds }  //интервал через который будет производиться проверка активности
                };

                clientOnServer.ConnectionTimer.Elapsed += delegate { DisconnectInactiveClient(clientOnServer); };

                Application.Current.Dispatcher.Invoke(delegate
                {
                    ClientsOnServer.Add(new ClientOnServer(newClientName, client));
                });

                //передаем новому клиенту список клиентов сети
                var networkClients = new ObservableCollection <NetworkClient>();

                foreach (var c in ClientsOnServer.Where(c => c.TcpClient != client))
                {
                    networkClients.Add(new NetworkClient(c.Name, c.TcpClient.Client.RemoteEndPoint));
                }

                client.PutDataToStream(new NetworkClientsCollection(networkClients));

                //передаем остальным клиентам сети данные нового клиента
                foreach (var c in ClientsOnServer)
                {
                    if (c.TcpClient.Client.RemoteEndPoint == client.Client.RemoteEndPoint)
                    {
                        continue;
                    }

                    var newClient = new NetworkClient(newClientName, client.Client.RemoteEndPoint);

                    c.TcpClient.PutDataToStream(newClient);
                }

                ReceiveMessage(client);
            }
            catch (Exception e)
            {
                ServerEvents.Add(new ServerEvent(ServerEventEnum.Error, DateTime.Now, e.ToString()));
            }
        }
Beispiel #5
0
        private void RemoveClient(ClientOnServer clientOnServer)
        {
            try
            {
                var networkClient = new NetworkClient(clientOnServer.Name,
                                                      clientOnServer.TcpClient.Client.RemoteEndPoint, false);

                Application.Current.Dispatcher.Invoke(delegate
                {
                    ClientsOnServer.Remove(clientOnServer);
                });

                foreach (var c in ClientsOnServer)
                {
                    c.TcpClient.PutDataToStream(networkClient);
                }
            }
            catch (Exception e)
            {
                ServerEvents.Add(new ServerEvent(ServerEventEnum.Error, DateTime.Now, e.ToString()));
            }
        }
Beispiel #6
0
        private void StartServer()
        {
            try
            {
                _tcpListener.Start();

                _isServerRun = true;

                _maxThreadsCount = Environment.ProcessorCount * 4;

                ThreadPool.SetMaxThreads(_maxThreadsCount, _maxThreadsCount);
                ThreadPool.SetMinThreads(2, 2);

                _thread = new Thread(AcceptConnections);
                _thread.Start();

                ServerEvents.Add(new ServerEvent(ServerEventEnum.Start, DateTime.Now));
            }
            catch (Exception e)
            {
                ServerEvents.Add(new ServerEvent(ServerEventEnum.Error, DateTime.Now, e.ToString()));
            }
        }
Beispiel #7
0
 public void ServerEventHandler(object sender, ServerEvent e)
 {
     ServerEvents.Add(e);
 }