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); }
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; } }
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"); } }