private async Task ProcessClientAsync(TcpClient client) { try { ConnectionPool.Add(client); var cts = new CancellationTokenSource(); cts.CancelAfter((int)ReadTimeOut.TotalMilliseconds); var token = cts.Token; //var buffer = new byte[256]; //client.GetStream().Read(buffer, 0, buffer.Length); //return; var connection = new ClientConnection(client); var caption = await connection.ReadStringAsync(token); if (caption != MessageCaption) { Console.WriteLine("Wrong request"); return; } var commandCode = (CommandCodeEn)await connection.ReadByteAsync(token); if (commandCode == CommandCodeEn.Ping) { var handler = new PingCommandHandler(); await handler.ProcessRequestAsync(client, token); } else if (commandCode == CommandCodeEn.GetDate) { var handler = new GetDateCommandHandler(); await handler.ProcessRequestAsync(client, token); } else if (commandCode == CommandCodeEn.PutFile) { var handler = new SaveFileCommandHandler(); await handler.ProcessRequestAsync(client, token); } else { throw new Exception($"Invalid command code: {commandCode}"); } var taken = ConnectionPool.TryTake(out client); } catch (Exception ex) { Console.WriteLine(ex); } }
public async Task Ping() { using var client = new TcpClient(); client.Connect(Ip, Port); var connection = new ClientConnection(client); var commandData = "ping"; var commandLength = commandData.Length + 1; await WriteHeader(connection); await WriteCommandCode(connection, CommandCodeEn.Ping); await WriteGuid(connection); await WriteDataLength(connection, commandLength); await WriteString(connection, commandData); var caption = await connection.ReadStringAsync(CancellationToken); var pong = await connection.ReadStringAsync(CancellationToken); Console.WriteLine($"<- {caption}{pong}"); }
public async Task UploadFile(string filePath) { using var client = new TcpClient(); client.Connect(Ip, Port); var connection = new ClientConnection(client); var fileName = filePath.Split('\\').Last(); var fileBytes = await File.ReadAllBytesAsync(filePath); await WriteHeader(connection); await WriteCommandCode(connection, CommandCodeEn.PutFile); await WriteGuid(connection); await WriteDataLength(connection, fileBytes.Length + fileName.Length + 1); await connection.WriteStringAsync(fileName, CancellationToken); await connection.WriteBytesAsync(fileBytes, CancellationToken); var caption = await connection.ReadStringAsync(CancellationToken); var savedFileName = await connection.ReadStringAsync(CancellationToken); Console.WriteLine($"<- {caption}{savedFileName}"); }
public async Task <DateTimeOffset> GetDate() { using var client = new TcpClient(); client.Connect(Ip, Port); var connection = new ClientConnection(client); await WriteHeader(connection); await WriteCommandCode(connection, CommandCodeEn.GetDate); await WriteGuid(connection); await WriteDataLength(connection, 0); var caption = await connection.ReadStringAsync(CancellationToken); var unixDate = await connection.ReadInt64Async(CancellationToken); var date = DateTimeOffset.FromUnixTimeSeconds(unixDate); Console.WriteLine($"<- {caption}{date}"); return(date); }