Esempio n. 1
0
        private async Task ProcessRegisterClientCmd(CommandHeader cmdHeader)
        {
            var clientIdBytes = await NetworkHelper.ReadBytes(_networkStream, cmdHeader.PayloadLength);

            var clientId = new Guid(clientIdBytes);

            var ret = new ServerResponseWithData <bool>();

            var cl = _config.Clients.FirstOrDefault(x => x.Id == clientId);

            if (cl != null)
            {
                ret.Data = true;
            }
            else
            {
                _config.Clients.Add(new RegisteredClient
                {
                    Id = clientId,
                });
                _config.Store();

                ret.Data = true;
            }

            await CommandHelper.WriteCommandResponse(_networkStream, Commands.RegisterClientCmd, ret);
        }
Esempio n. 2
0
        public void Stop()
        {
            try
            {
                Msg?.Invoke("Stopping main loop");

                _stop = true;

                Task[] connections;

                lock (_lock)
                {
                    connections = _connections.ToArray();
                }

                if (connections.Length > 0)
                {
                    Msg?.Invoke("Waiting for clients to complete...");

                    Task.WaitAll(connections, TimeSpan.FromSeconds(30));
                }

                Msg?.Invoke("Stopping listener");

                _tcpListener?.Stop();

                _listenerTask.Wait();
            }
            catch (Exception e)
            {
                Msg?.Invoke(e.ToString());
            }

            Msg?.Invoke("Saving config");

            _config.Store();
        }