Beispiel #1
0
        private void CleanupClients()
        {
            var currentUnixTimestamp = BaseModel.GetCurrentUnixTimestamp();

            var hasChange = false;

            lock (Clients)
            {
                foreach (var client in Clients.ToList())
                {
                    if (currentUnixTimestamp - client.RefreshTime > CConstant.ClientCleanupPeriod / 1000)
                    {
                        Clients.Remove(client);

                        RunOnUiThread(delegate
                        {
                            ClientRemoved?.Invoke(client);
                        });

                        hasChange = true;
                    }
                }
            }

            if (hasChange)
            {
                RunOnUiThread(delegate
                {
                    ClientsChanged?.Invoke(Clients);
                });
            }
        }
Beispiel #2
0
        private void HandleRequestConnectMessage(ClientPacket protocolPacket, IClientProtocol protocol, IPAddress address)
        {
            var client = Clients.FirstOrDefault(x => x.Id == protocolPacket.Id) as CClient;

            if (client != null)
            {
                return;
            }

            var port = int.Parse(protocol.DataJson);

            if (!CConstant.UdpListenerPort.Contains(port))
            {
                return;
            }

            client = new CClient()
            {
                Id      = protocolPacket.Id,
                Name    = protocolPacket.Name,
                Port    = port,
                Address = address,
            };

            client.CalculateChecksum();
            client.Refresh();

            lock (Clients)
            {
                Clients.Add(client);
            }

            RunOnUiThread(delegate
            {
                ClientNew?.Invoke(client);

                ClientsChanged?.Invoke(Clients);

                var generator   = new Random();
                var r           = generator.Next(1, 1000000);
                var passcode    = r.ToString().PadLeft(6, '0');
                client.Passcode = passcode;
            });

            var responseProtocol = new ServerProtocol()
            {
                Message = ServerMessage.RequestPasscode
            };

            SendServerPacket(ServerPacketType.Protocol, JsonConvert.SerializeObject(responseProtocol), client);

            RunOnUiThread(delegate
            {
                client.State = ClientState.WaitingPasscode;
            });
        }
Beispiel #3
0
        private void ClientOnDisconnected(object sender, EventArgs e)
        {
            var client = (Client)sender;

            Logger.Debug("Notify administrations about the disconnect of CI-{0}", client.Id);
            lock (_administrationListLock)
                foreach (var administration in Administrations)
                {
                    administration.Value.NotifyClientDisconnected(client);
                }

            Clients.TryRemove(client.Id, out client);

            DynamicCommandManager.ClientDisconnected(client);

            ClientsChanged?.Invoke(this, EventArgs.Empty);
            DatabaseManager.SetLastSeen(client.Id);
        }
Beispiel #4
0
        private void RemoveClient(CClient client)
        {
            RunOnUiThread(delegate
            {
                client.State = ClientState.Refused;
            });

            lock (Clients)
            {
                Clients.Remove(client);
            }

            RunOnUiThread(delegate
            {
                ClientRemoved?.Invoke(client);

                ClientsChanged?.Invoke(Clients);
            });
        }
Beispiel #5
0
        private void ClientConnected(Client client, bool isNewClient)
        {
            Clients.TryAdd(client.Id, client);

            Logger.Debug("Notify administrations about the connection of CI-{0}", client.Id);
            lock (_administrationListLock)
                foreach (var administration in Administrations)
                {
                    if (!isNewClient)
                    {
                        administration.Value.NotifyClientConnected(client);
                    }
                    else
                    {
                        administration.Value.NotifyNewClient(client);
                    }
                }

            ClientsChanged?.Invoke(this, EventArgs.Empty);
            DynamicCommandManager.OnClientJoin(client);
        }