yavsc/src/cli/Commands/Streamer.cs

124 lines
4.8 KiB
C#
Raw Normal View History

2019-06-25 02:58:35 +01:00
using System;
using System.IO;
2019-06-24 14:30:47 +01:00
using System.Net.WebSockets;
2019-06-25 02:58:35 +01:00
using System.Threading;
using System.Threading.Tasks;
using cli.Model;
using Microsoft.Extensions.CommandLineUtils;
2019-06-25 01:51:23 +01:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
2019-10-08 23:41:19 +01:00
using Yavsc.Abstract;
2019-06-24 14:30:47 +01:00
2019-06-25 01:51:23 +01:00
namespace cli {
2019-06-25 02:58:35 +01:00
public class Streamer: ICommander {
2019-06-25 01:51:23 +01:00
private ClientWebSocket _client;
private ILogger _logger;
private ConnectionSettings _cxSettings;
private UserConnectionSettings _userCxSettings;
2019-06-25 02:58:35 +01:00
private CommandOption _fileOption;
private CommandArgument _flowIdArg;
private CancellationTokenSource _tokenSource;
2019-06-24 14:30:47 +01:00
2019-06-25 01:51:23 +01:00
public Streamer(ILoggerFactory loggerFactory,
IOptions<ConnectionSettings> cxSettings,
IOptions<UserConnectionSettings> userCxSettings
)
{
_logger = loggerFactory.CreateLogger<Streamer>();
_cxSettings = cxSettings.Value;
_userCxSettings = userCxSettings.Value;
_client = new ClientWebSocket();
_client.Options.SetRequestHeader("Authorization", $"Bearer {_userCxSettings.AccessToken}");
}
2019-06-25 02:58:35 +01:00
public CommandLineApplication Integrate(CommandLineApplication rootApp)
{
CommandLineApplication streamCmd = rootApp.Command("stream",
(target) =>
{
target.FullName = "Stream to server";
target.Description = "Stream arbitrary binary data to your server channel";
_fileOption = target.Option("-f | --file", "use given file as input stream", CommandOptionType.SingleValue);
_flowIdArg = target.Argument("flowId", "Remote Id for this channel", false);
target.HelpOption("-? | -h | --help");
});
streamCmd.OnExecute(async() => await DoExecute());
return streamCmd;
}
private async Task <int> DoExecute()
{
2019-06-25 17:11:16 +01:00
2019-06-25 02:58:35 +01:00
if (_fileOption.HasValue()){
var fi = new FileInfo(_fileOption.Value());
if (!fi.Exists) {
_logger.LogError("Input file doesn´t exist.");
return -2;
}
using (var stream = fi.OpenRead())
{
2019-06-25 17:11:16 +01:00
_logger.LogInformation("DoExecute from given file");
2019-06-25 02:58:35 +01:00
await DoStream(stream);
}
return 0;
}
else
{
using(var stream = Console.OpenStandardInput())
{
2019-06-25 17:11:16 +01:00
_logger.LogInformation("DoExecute from standard input");
2019-06-25 02:58:35 +01:00
await DoStream(stream);
}
return 0;
}
}
async Task DoStream (Stream stream)
{
2019-06-25 17:11:16 +01:00
2019-06-25 02:58:35 +01:00
_tokenSource = new CancellationTokenSource();
2019-06-25 17:11:16 +01:00
var url = _cxSettings.StreamingUrl+"/"+_flowIdArg.Value;
_logger.LogInformation("Connecting to "+url);
await _client.ConnectAsync(new Uri(url), _tokenSource.Token);
_logger.LogInformation("Connected");
2019-06-25 09:21:10 +01:00
const int bufLen = Constants.WebSocketsMaxBufLen;
byte [] buffer = new byte[bufLen+4*sizeof(int)];
2019-06-25 02:58:35 +01:00
const int offset=0;
int read = 0;
2019-06-25 17:11:16 +01:00
/*
var reciving = Task.Run(async ()=> {
byte [] readbuffer = new byte[bufLen];
var rb = new ArraySegment<byte>(readbuffer, 0, bufLen);
bool continueReading = false;
do {
var result = await _client.ReceiveAsync(rb, _tokenSource.Token);
_logger.LogInformation($"received {result.Count} bytes");
continueReading = !result.CloseStatus.HasValue;
} while (continueReading);
} ); */
2019-06-25 02:58:35 +01:00
do {
read = await stream.ReadAsync(buffer, offset + sizeof(int), bufLen);
if (read>0) {
// assert sizeof(int)==4
buffer[3]= (byte) (read % 256);
var left = read / 256;
buffer[2]= (byte) (left % 256);
left = left / 256;
buffer[1] = (byte) (left % 256);
left = left /256;
buffer[0]=(byte) (byte) (left % 256);
var segment = new ArraySegment<byte>(buffer, offset, read+4);
2019-06-25 17:11:16 +01:00
await _client.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Binary, false, _tokenSource.Token);
_logger.LogInformation($"sent {segment.Count} ");
}
} while (read>0);
2019-06-25 17:11:16 +01:00
// reciving.Wait();
await _client.CloseAsync(WebSocketCloseStatus.NormalClosure, "EOF", _tokenSource.Token);
2019-06-25 02:58:35 +01:00
}
2019-06-25 01:51:23 +01:00
}
2019-10-08 23:41:19 +01:00
}