/// <summary> /// 采集信号保存到Bvalue[]数组 /// </summary> void RcvUdpdataToBvalue_ThreatStart() { List <ArraySegment <byte> > buffer = new List <ArraySegment <byte> > { new ArraySegment <byte>(new byte[1282]), }; int ret; Stopwatch stopwatch = new Stopwatch(); while (true) { RcvResetEvent.WaitOne(); try { stopwatch.Start(); ret = MSocket.Receive(buffer); if (ret <= 0) { continue; } int offset = 0; var bytedata = buffer[0].Array; if (bytedata == null) { continue; } int chl = bytedata[0]; //通道 int tiv = bytedata[1]; //时分 int index = tiv * 8 + chl; // 1时分8通道 offset += 2; short[] sbdata = new short[(bytedata.Length - offset) / 2]; var purdata = new byte[bytedata.Length - offset]; Array.Copy(bytedata, offset, purdata, 0, purdata.Length); for (int i = 0; i < sbdata.Length; i++)//大端数据转小端 { var t = new byte[2]; t[0] = purdata[2 * i + 1]; t[1] = purdata[2 * i]; sbdata[i] = BitConverter.ToInt16(t, 0); } BvalueDatas[index] = new short[sbdata.Length]; double progressvalue = (index + 1) * 100.0 / 64; //进度条 Console.WriteLine("通道: " + index); Array.Copy(sbdata, BvalueDatas[index], sbdata.Length); DisPlayWindow.HMainWindow.bvaulue_pgbar.Dispatcher.Invoke(() => { DisPlayWindow.HMainWindow.bvaulue_pgbar.Value = progressvalue; }); //进度条 } catch (Exception e) { Console.WriteLine(e); throw; } stopwatch.Stop(); Console.WriteLine("RCVTime:" + stopwatch.ElapsedMilliseconds); RcvResetEvent.Reset(); } }
/// <summary> /// 接收网络流相位信号线程函数 /// </summary> void RcvUdpdataToPhase_ThreatStart() { List <ArraySegment <byte> > buffer = new List <ArraySegment <byte> > { new ArraySegment <byte>(new byte[1282]) }; int ret; Stopwatch stopwatch = new Stopwatch(); while (true) { RcvResetEvent.WaitOne(); try { stopwatch.Start(); ret = MSocket.Receive(buffer); if (ret <= 0) { continue; } int offset = 0; var bytedata = buffer[0].Array; if (bytedata == null) { continue; } int chl = bytedata[0]; //通道号 int tiv = bytedata[1]; //时分 int index = tiv * 8 + chl; //1时分表示8通道 offset += 2; short[] sbdata = new short[(bytedata.Length - offset) / 2]; //2byte转short数据 var purdata = new byte[bytedata.Length - offset]; //接收byte数组 Array.Copy(bytedata, offset, purdata, 0, purdata.Length); for (int i = 0; i < sbdata.Length; i++) //大端数据 { var t = new byte[2]; // 大端转小端 t[0] = purdata[2 * i + 1]; t[1] = purdata[2 * i]; sbdata[i] = BitConverter.ToInt16(t, 0); } if (index < 0 || index > 31) { Console.WriteLine("Index Error!"); } RcvPhaseData[index] = new short[sbdata.Length]; Console.WriteLine("通道: " + index); Array.Copy(sbdata, RcvPhaseData[index], sbdata.Length); } catch (Exception e) { Console.WriteLine(e); throw; } stopwatch.Stop(); Console.WriteLine("RCVTime:" + stopwatch.ElapsedMilliseconds); RcvResetEvent.Reset(); // 线程继续等待 } }
/// <summary> /// Receive <see cref="AsyncCallback"/> method. /// </summary> /// <exception cref="InvalidOperationException" /> protected override unsafe void ReceiveCallback(IAsyncResult ar) { try { MReceivedLength += MSocket.EndReceive(ar); fixed(byte *buf = MReceiveBuffer) { if (!MHeaderReceived) //get packet capacity { L2Buffer.Extend(ref MReceiveBuffer, 0, *(int *)buf - sizeof(int)); MReceivedLength = 0; MHeaderReceived = true; } if (MReceivedLength == MReceiveBuffer.Length) // all data received { Handle(new Packet(2, MReceiveBuffer)); MReceivedLength = 0; MReceiveBuffer = MDefaultBuffer; MHeaderReceived = false; MSocket.BeginReceive(MReceiveBuffer, 0, 4, 0, ReceiveCallback, null); } else { if (MReceivedLength < MReceiveBuffer.Length) // not all data received { MSocket.BeginReceive(MReceiveBuffer, MReceivedLength, MReceiveBuffer.Length - MReceivedLength, 0, MReceiveCallback, null); } else { throw new InvalidOperationException(); } } } } catch (SocketException se) { Log.Info(string.Format("{0} \r\nError code: {1}", se.ToString(), se.ErrorCode)); CloseConnection(); OnDisconnected?.Invoke(se.ErrorCode, this, ConnectionId); } catch (Exception e) { Log.Error(e); CloseConnection(); OnDisconnected?.Invoke(-1, this, ConnectionId); } }
void Dispose(bool disposing) { ReleaseUnmanagedResources(); if (disposing) { if (RcvResetEvent != null) { RcvResetEvent.Dispose(); } if (MSocket != null) { MSocket.Shutdown(SocketShutdown.Both); MSocket.Close(); MSocket.Dispose(); } } }
/// <summary> /// 开启计算B值流程 /// </summary> /// <param name="ip">绑定本地节点</param> /// <param name="udpCommandSocket">接收发送命令socket</param> public void StartCaclBvalue(IPEndPoint ip, UdpCommandSocket udpCommandSocket) { try { UCommandSocket = udpCommandSocket; MSocket.ReceiveBufferSize = ConstUdpArg.ORIG_FRAME_LENGTH; MSocket.Bind(ip); RcvThread = new Thread(RcvUdpdataToBvalue_ThreatStart) { IsBackground = true }; RcvResetEvent = new AutoResetEvent(false); BvalueDatas = new short[64][]; RcvThread.Start(); } catch (Exception e) { Console.WriteLine(e); throw; } }
/// <summary> /// Begins receive from connection socket. /// </summary> public override void BeginReceive() { MSocket.BeginReceive(MReceiveBuffer, 0, 4, 0, MReceiveCallback, null); }