Esempio n. 1
0
        private async Task ListenAsync()
        {
            while (true)
            {
                var buffer = new byte[4096];
                var stream = ClientSocket?.GetStream();
                if (stream == null || !stream.CanRead)
                {
                    continue;
                }
                await stream.ReadAsync(buffer, 0, buffer.Length, Cts.Token);

                var response = RequestParser.DeserializeRequestClient(buffer);
                if (response == null)
                {
                    continue;
                }
                if (response.RequestType.Equals(RequestType.DataChanged))
                {
                    m_logger.LogInformation($"Registered Broadcast {JsonConvert.SerializeObject(response, Formatting.Indented)}");
                    Observer.DataChanged = true;
                    continue;
                }
                m_responses.Add(response);
                m_logger.LogInformation($"Registered response {JsonConvert.SerializeObject(response, Formatting.Indented)}");
            }
        }
Esempio n. 2
0
        private async Task GetClientMessage(TcpClient client)
        {
            while (true)
            {
                var buffer = new byte[4096];
                var stream = client.GetStream();
                await stream.ReadAsync(buffer, 0, buffer.Length);

                Request request;
                try
                {
                    request = RequestParser.DeserializeRequest(buffer);
                    if (request == null)
                    {
                        continue;
                    }
                    client.GetStream().Flush();
                    Console.WriteLine($"Received Request {JsonConvert.SerializeObject(request)}");
                    new Thread(async() => await ProcessRequest(request, client)).Start();
                }
                catch (Exception)
                {
                    //ignore
                }
            }
        }
Esempio n. 3
0
        private static async Task SendClientMessage(TcpClient client, Request request)
        {
            var stream = client.GetStream();
            var data   = RequestParser.SerializeRequestAsByte(request);

            Console.WriteLine(data.Length);
            await stream.WriteAsync(data, 0, data.Length);
        }
Esempio n. 4
0
        public async Task SendMessageAsync(Request request)
        {
            while (!Cts.IsCancellationRequested)
            {
                var requestData = RequestParser.SerializeRequestAsByte(request);
                var stream      = ClientSocket.GetStream();
                await stream.WriteAsync(requestData, 0, requestData.Length);

                m_logger.LogInformation($"Sent Request : {JsonConvert.SerializeObject(request, Formatting.Indented)}");
                await stream.FlushAsync();

                await Task.Delay(100);

                return;
            }
        }
Esempio n. 5
0
        private async Task AcceptNewClientAsync(int?totalClients, ICollection <Task> tasks)
        {
            var newClient = await ListenAsync();

            if (!totalClients.HasValue || tasks.Count < totalClients.Value)
            {
                tasks.Add(GetClientMessage(newClient));
            }
            else
            {
                var request = RequestParser.SerializeRequestAsByte(new Request
                {
                    Body        = "Server Is Full, Please Try Later",
                    Headers     = null,
                    RequestType = RequestType.Logout
                });
                await newClient.GetStream().FlushAsync();

                await newClient.GetStream().WriteAsync(request, 0, request.Length);

                Console.WriteLine("A client tried to connect but server is full");
            }
        }