private void OnReceive(IAsyncResult state) { EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); if (PortOpen) { try { ArtNetReceiveData receiveState = (ArtNetReceiveData)(state.AsyncState); if (receiveState != null) { receiveState.DataLength = EndReceiveFrom(state, ref remoteEndPoint); //Protect against UDP loopback where we receive our own packets. if (LocalEndPoint != remoteEndPoint && receiveState.Valid) { LastPacket = DateTime.Now; ProcessPacket((IPEndPoint)remoteEndPoint, ArtNetPacket.Create(receiveState)); } } } catch (Exception ex) { OnUnhandledException(ex); } finally { //Attempt to receive another packet. StartReceive(); } } }
protected override void OnReadPacket(byte[] buffer, int length, IPEndPoint source) { var recieveState = new ArtNetRecieveData(); recieveState.buffer = buffer; recieveState.DataLength = length; var artNetPacket = ArtNetPacket.Create(recieveState); if (0 < receiveLimit && receivedDataQueue.Count < receiveLimit) { lock (receivedDataQueue) receivedDataQueue.Enqueue(artNetPacket); if (artNetPacket.OpCode == ArtNet.Enums.ArtNetOpCodes.Dmx) { var dmxPacket = (ArtNetDmxPacket)artNetPacket; var universe = dmxPacket.Universe; var dmxData = dmxPacket.DmxData; var mergedDmxData = DmxMerge.GetMergedDmxData(universe, dmxData, dmxMergeMode, source); if (!universeDmxDataMap.ContainsKey(universe)) { universeDmxDataMap.Add(universe, Enumerable.Repeat((byte)0, 512).ToArray()); } System.Buffer.BlockCopy(mergedDmxData, 0, universeDmxDataMap[universe], 0, mergedDmxData.Length); if (!updateUniverseQueue.Contains(universe)) { lock (updateUniverseQueue) updateUniverseQueue.Enqueue(universe); } } } }
protected override void OnReadPacket(byte[] buffer, int length, IPEndPoint source) { if (recieveState == null) { recieveState = new ArtNetRecieveData(); } recieveState.buffer = buffer; recieveState.DataLength = length; var artNetPacket = ArtNetPacket.Create(recieveState); if (0 < receiveLimit && receivedDataQueue.Count < receiveLimit) { lock (receivedDataQueue) receivedDataQueue.Enqueue(artNetPacket); if (artNetPacket.OpCode == ArtNet.Enums.ArtNetOpCodes.Dmx) { var dmxPacket = (ArtNetDmxPacket)artNetPacket; var universe = dmxPacket.Universe; var dmxData = dmxPacket.DmxData; var mergedDmxData = DmxMerge.GetMergedDmxData(universe, dmxData, dmxMergeMode); if (dmx == null) { dmx = new byte[512][]; } if (dmx[universe] == null) { dmx[universe] = new byte[512]; } System.Buffer.BlockCopy(mergedDmxData, 0, dmx[universe], 0, mergedDmxData.Length); if (!updateUniverseQueue.Contains(universe)) { lock (updateUniverseQueue) updateUniverseQueue.Enqueue(universe); } } } }
/// <summary> /// 接收数据回调函数 /// </summary> /// <param name="state">状态信息以及自定义数据</param> private void OnRecieve(IAsyncResult state) { EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); if (PortOpen) { try { // 获取自定义数据 ArtNetRecieveData recieveState = (ArtNetRecieveData)(state.AsyncState); if (recieveState != null) { // 结束挂起的、从指定 IP 地址 进行异步读取 recieveState.DataLength = EndReceiveFrom(state, ref remoteEndPoint); // 防止UDP环回,不接收自己发出的数据包. if (remoteEndPoint != LocalEndPoint && recieveState.Valid) { LastPacket = DateTime.Now; // 根据接收数据的 OpCode 来创建对应的 ArtNet 数据包对象并开始解析 ProcessPacket((IPEndPoint)remoteEndPoint, ArtNetPacket.Create(recieveState)); } } } catch (Exception ex) { OnUnhandledException(ex); } finally { // 继续接收下一个数据包 StartRecieve(); } } }