public void RunAsyn() { CtkLog.RegisterEveryLogWrite((ss, ea) => { var now = DateTime.Now; var sb = new StringBuilder(); sb.AppendFormat("[{0}] ", now.ToString("yyyyMMdd HH:mm:ss")); sb.AppendFormat("{0} ", ea.Message); sb.AppendFormat("{0}", ea.Exception.StackTrace); CtkLog.InfoNs(this, sb.ToString()); }); DateTime?prevTime = DateTime.Now; this.listener = new CtkTcpListener("127.0.0.1", 5003); listener.NonStopRunStart(); var rnd = new Random((int)DateTime.Now.Ticks); listener.EhFirstConnect += (ss, ee) => { var myea = ee as CtkNonStopTcpStateEventArgs; var sb = new StringBuilder(); sb.Append("evtFirstConnect:\n"); sb.Append(this.CmdState()); CtkLog.InfoNs(this, sb.ToString()); }; listener.EhDataReceive += (ss, ee) => { var myea = ee as CtkNonStopTcpStateEventArgs; var ctkBuffer = myea.TrxMessageBuffer; var msg = Encoding.UTF8.GetString(ctkBuffer.Buffer, ctkBuffer.Offset, ctkBuffer.Length); if (!msg.Contains("\n")) { return; } var sb = new StringBuilder(); sb.Append("cmd -respData -svid 1 -data "); sb.Append(rnd.NextDouble()); sb.AppendLine(); myea.WriteMsg(sb.ToString()); }; }
public void RunAsyn() { CtkLog.RegisterEveryLogWrite((ss, ea) => { var now = DateTime.Now; var sb = new StringBuilder(); sb.AppendFormat("[{0}] ", now.ToString("yyyyMMdd HH:mm:ss")); sb.AppendFormat("{0} ", ea.Message); if (ea.Exception != null) { sb.AppendFormat("{0}", ea.Exception.StackTrace); } CtkLog.InfoNs(this, sb.ToString()); }); var len = 512; var sampleRate = 512.0; var sin1 = new DenseVector(Generate.Sinusoidal(len, sampleRate, 10.0, 1.0)); var sin2 = new DenseVector(Generate.Sinusoidal(len, sampleRate, 60.0, 0.5)); var wave = sin1 + sin2; var waveIndex = 0; DateTime?prevTime = DateTime.Now; this.listener = new CtkTcpListener("127.0.0.1", 5003); listener.NonStopRunStart(); listener.EhFirstConnect += (ss, ee) => { var myea = ee as CtkNonStopTcpStateEventArgs; var sb = new StringBuilder(); sb.Append("evtFirstConnect:\n"); sb.Append(this.CmdState()); CtkLog.InfoNs(this, sb.ToString()); }; listener.EhDataReceive += (ss, ee) => { var myea = ee as CtkNonStopTcpStateEventArgs; var ctkBuffer = myea.TrxMessageBuffer; var msg = Encoding.UTF8.GetString(ctkBuffer.Buffer, ctkBuffer.Offset, ctkBuffer.Length); if (!msg.Contains("\n")) { return; } var sb = new StringBuilder(); sb.Append("cmd -respData -svid 0 -data"); var now = DateTime.Now; var ts = now - prevTime.Value; prevTime = now; var limit = ts.Ticks * 1.0 / TimeSpan.TicksPerSecond * sampleRate; if (limit <= 0) { limit = 1; } if (ts.TotalMilliseconds > 500) { limit = 1; } for (var idx = 0; idx < limit; idx++) { sb.AppendFormat(" {0}", wave[waveIndex++]); if (waveIndex >= wave.Count) { waveIndex = 0; } } sb.AppendLine(); myea.WriteMsg(sb.ToString()); }; }