public async Task <IActionResult> Execute(CommandForm form) { var command = new Command { OperatorId = form.OperatorId, Ip = form.Ip, Content = form.Command }; string content; await using (var stream = new MemoryStream()) { command.WriteTo(stream); content = Encoding.UTF8.GetString(stream.ToArray()); } var pipelineFilter = new CommandLinePipelineFilter { Decoder = new PackageDecoder() }; var client = new EasyClient <PackageInfo>(pipelineFilter).AsClient(); var address = IPAddress.Parse(_serverIp); if (!await client.ConnectAsync(new IPEndPoint(address, _serverPort))) { return(StatusCode((int)HttpStatusCode.InternalServerError, "Failed to connect to the server.")); } await client.SendAsync(Encoding.UTF8.GetBytes($"{CommandKey.Connect} {ClientType.Short.ToString()}{Package.Terminator}")); await client.SendAsync(Encoding.UTF8.GetBytes($"{CommandKey.Execute} {content}{Package.Terminator}")); while (true) { var p = await client.ReceiveAsync(); if (p == null) { return(StatusCode((int)HttpStatusCode.InternalServerError, "Connection dropped.")); } if (!string.IsNullOrWhiteSpace(p.Content) && p.Content.Equals("Started", StringComparison.OrdinalIgnoreCase)) { await client.CloseAsync(); return(Ok(form.OperatorId)); } } }
static async Task Main(string[] args) { var filter = new MesMessagePipelineFilter(); var encoder = new MesMessagePackageEncoder(); var client = new EasyClient <MesMessage, MesMessage>(filter, encoder).AsClient(); if (!await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 3000))) { Console.WriteLine("Failed to connect the target server."); return; } //var pack = new RTDEPackage(RTDECommandEnum.Connect, "This is a test"); //var encoder = new RTDEPackageEncoder(); //await client.SendAsync(encoder, pack); var pack = new MesMessage { Time = DateTime.Now.ToLongDateString(), Command = "Mission", SourceId = "MES", TargetId = "CAC", Transaction = Guid.NewGuid().ToString(), CommandParameter = "This is only a test" }; await client.SendAsync <MesMessage>(encoder, pack); while (true) { try { var p = await client.ReceiveAsync(); if (p == null) // connection dropped { break; } Console.WriteLine(p.ToString()); } catch (Exception ex) { Console.WriteLine(ex); } } }
static async Task Test2() { MyBeginEndMarkPipelineFilter filter = new MyBeginEndMarkPipelineFilter(); var client = new EasyClient <StringPackageInfo, StringPackageInfo>(filter).AsClient(); if (!await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 6001))) { Console.WriteLine("Failed to connect the target server."); return; } int i = 0; do { string msg = @"MSH|^~\&|HIS|LIS|ABC|DEF|20200408143100|pv1|ADT^A08|A6B64388-1F8B-4763-BA4D-B4A76BE58DA9|P|2.4|||||CHN EVN|A08|20200408143100 PID|1|415320^^^&PATID|415320^^^&PATID~1221284^^^&BLH~800033^^^&BRKH^BLH~0^^^&YEXH||CESHI||19900408000000|M||||||||M^已婚||||||17^汉族||||||40^中国 NK1||CESHI|SEL^SEL PV1|1|I|4011^急诊外科^29^^^^1001^急诊外科病区||||6371^张三||||||1|||||||2001~现金||||||||||||||||||||1|||||||||| "; byte[] bytes = Encoding.UTF8.GetBytes(msg); List <byte> byteLists = new List <byte>(); byteLists.AddRange(filter.BeginMark.ToArray()); byteLists.AddRange(bytes); byteLists.AddRange(filter.EndMark.ToArray()); try { await client.SendAsync(new ReadOnlyMemory <byte>(byteLists.ToArray())); var pack = await client.ReceiveAsync(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } while (i++ < 2); }
public async Task <IActionResult> List() { var pipelineFilter = new CommandLinePipelineFilter { Decoder = new PackageDecoder() }; var client = new EasyClient <PackageInfo>(pipelineFilter).AsClient(); var address = IPAddress.Parse(_serverIp); if (!await client.ConnectAsync(new IPEndPoint(address, _serverPort))) { return(StatusCode((int)HttpStatusCode.InternalServerError, "Failed to connect to the server.")); } await client.SendAsync(Encoding.UTF8.GetBytes($"{CommandKey.Connect} {ClientType.Short.ToString()}{Package.Terminator}")); while (true) { var p = await client.ReceiveAsync(); if (p == null) { return(StatusCode((int)HttpStatusCode.InternalServerError, "Connection dropped.")); } switch (p.Key) { case CommandKey.Connected: await client.SendAsync(Encoding.UTF8.GetBytes($"{CommandKey.ListAgent}{Package.Terminator}")); break; default: var agents = JsonConvert.DeserializeObject <List <AgentInfo> >(p.Content); await client.CloseAsync(); return(Ok(agents)); } } }
static async Task Main(string[] args) { var client = new EasyClient <MyPackage>(new MyPackageFilter()).AsClient(); if (!await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 4041))) { Console.WriteLine("Failed to connect the target server."); return; } while (true) { var p = await client.ReceiveAsync(); if (p == null) // connection dropped { break; } Console.WriteLine(p.Body); } }