static async Task Main(string[] args) { Encoding Utf8Encoding = new UTF8Encoding(); var pipelineFilter = new CommandLinePipelineFilter { Decoder = new DefaultStringPackageDecoder() }; IEasyClient <StringPackageInfo> client = new EasyClient <StringPackageInfo>(pipelineFilter); StringPackageInfo package = null; client.PackageHandler += async(s, p) => { package = p; if (string.IsNullOrWhiteSpace(p.Key)) { Console.WriteLine("unexpected result ---------"); } else { Console.WriteLine(p.Key); } await Task.CompletedTask; }; var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 8899)); Console.WriteLine(connected); client.StartReceive(); byte[] bytes1 = Utf8Encoding.GetBytes(new char[] { (char)0x00, (char)0x00, (char)0x01, (char)0x61 }); byte[] bytes2 = Utf8Encoding.GetBytes(new char[] { (char)0x00, (char)0x00, (char)0x01, (char)0x61, (char)0x62 }); byte[] bytes3 = Utf8Encoding.GetBytes(new char[] { (char)0x00, (char)0x00, (char)0x01, (char)0x61 }); byte[] bytes4 = Utf8Encoding.GetBytes(new char[] { (char)0x00, (char)0x00, (char)0x01, (char)0x61 }); await client.SendAsync(bytes1); //expected result: a, true result: a await Task.Delay(1000); await client.SendAsync(bytes2); //expected result: a, true result: a await Task.Delay(1000); await client.SendAsync(bytes3); //expected result: a, true result: null await Task.Delay(1000); await client.SendAsync(bytes4); //expected result: a, tree result: null await Task.Delay(1000); await client.CloseAsync(); }
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)); } } }
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 void CreateClient() { var options = new ChannelOptions { Logger = NullLogger.Instance, ReadAsDemand = true }; var client = new EasyClient <TextPackageInfo>(new LinePipelineFilter(), options).AsClient(); var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 8888)); var connectPackage = new TcpPackage() { OpCode = OpCode.Connect, LocalName = new Guid().ToString(), RemoteName = "Server", MessageType = MessageType.Text }; await client.SendAsync( new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(connectPackage.ToString()))); while (true) { var msgPackage = new TcpPackage() { OpCode = OpCode.Connect, LocalName = new Guid().ToString(), RemoteName = "All", MessageType = MessageType.Text }; await client.SendAsync( new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(msgPackage.ToString()))); Task.Delay(500).Wait(); } }
static async Task Test3() { MyBeginEndMarkPipelineFilter filter = new MyBeginEndMarkPipelineFilter(); var client = new EasyClient <StringPackageInfo>(filter).AsClient(); if (!await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 6001))) { Console.WriteLine("Failed to connect the target server."); return; } client.PackageHandler += Client_PackageHandler; 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|||||||||| "; string d1 = Encoding.UTF8.GetString(filter.BeginMark.ToArray()); string d2 = Encoding.UTF8.GetString(filter.EndMark.ToArray()); msg = d1 + msg + d2; byte[] bytes = Encoding.UTF8.GetBytes(msg); try { await client.SendAsync(new ReadOnlyMemory <byte>(bytes)); client.StartReceive(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } while (i++ < 2); }
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); } } }