コード例 #1
0
ファイル: Server.cs プロジェクト: dianaslv/SwimmingContestApp
        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);
        }
コード例 #2
0
ファイル: Client.cs プロジェクト: dianaslv/SwimmingContestApp
        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;
            }
        }
コード例 #3
0
ファイル: Server.cs プロジェクト: dianaslv/SwimmingContestApp
        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");
            }
        }