public static ArtNetPacket Create(ArtNetRecieveData data) { switch ((ArtNetOpCodes)data.OpCode) { case ArtNetOpCodes.Poll: return(new ArtPollPacket(data)); case ArtNetOpCodes.PollReply: return(new ArtPollReplyPacket(data)); case ArtNetOpCodes.Dmx: return(new ArtNetDmxPacket(data)); case ArtNetOpCodes.TodRequest: return(new ArtTodRequestPacket(data)); case ArtNetOpCodes.TodData: return(new ArtTodDataPacket(data)); case ArtNetOpCodes.TodControl: return(new ArtTodControlPacket(data)); case ArtNetOpCodes.Rdm: return(new ArtRdmPacket(data)); case ArtNetOpCodes.RdmSub: return(new ArtRdmSubPacket(data)); } return(null); }
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); } } } }
private void OnRecieve(IAsyncResult state) { EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); if (PortOpen) { try { ArtNetRecieveData recieveState = (ArtNetRecieveData)(state.AsyncState); if (recieveState != null) { recieveState.DataLength = EndReceiveFrom(state, ref remoteEndPoint); //Protect against UDP loopback where we recieve our own packets. if (LocalEndPoint != remoteEndPoint && recieveState.Valid) { LastPacket = DateTime.Now; ProcessPacket((IPEndPoint)remoteEndPoint, ArtNetPacket.Create(recieveState)); } } } catch (Exception ex) { OnUnhandledException(ex); } finally { //Attempt to recieve another packet. StartRecieve(); } } }
public void StartRecieve() { try { EndPoint localPort = new IPEndPoint(IPAddress.Any, Port); ArtNetRecieveData recieveState = new ArtNetRecieveData(); BeginReceiveFrom(recieveState.buffer, 0, recieveState.bufferSize, SocketFlags.None, ref localPort, new AsyncCallback(OnRecieve), recieveState); } catch (Exception ex) { OnUnhandledException(new ApplicationException("An error ocurred while trying to start recieving ArtNet.", ex)); } }
public static ArtNetPacket Create(ArtNetRecieveData data) { switch ((ArtNetOpCodes)data.OpCode) { case ArtNetOpCodes.Poll: return(new ArtPollPacket(data)); case ArtNetOpCodes.PollReply: return(new ArtPollReplyPacket(data)); case ArtNetOpCodes.Dmx: return(new ArtNetDmxPacket(data)); } return(null); }
/// <summary> /// 开始接收 /// </summary> public void StartRecieve() { try { EndPoint localPort = new IPEndPoint(IPAddress.Any, Port); ArtNetRecieveData recieveState = new ArtNetRecieveData(); // 开始从指定 IP 异步接收数据 // 第一个参数:存储接收数据字节数组,第二个参数为存储位置偏移量,第三个参数为接受字节数 // 第五个参数为:发送端的 IP 地址 // 第六个参数为:当接受到数据是的回调函数 BeginReceiveFrom(recieveState.buffer, 0, recieveState.bufferSize, SocketFlags.None, ref localPort, new AsyncCallback(OnRecieve), recieveState); } catch (Exception ex) { OnUnhandledException(new ApplicationException("An error ocurred while trying to start recieving ArtNet.", ex)); } }
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(); } } }
public ArtNetPacket(ArtNetRecieveData data) { ArtNetBinaryReader packetReader = new ArtNetBinaryReader(new MemoryStream(data.buffer)); ReadData(packetReader); }
public ArtTodControlPacket(ArtNetRecieveData data) : base(data) { }
public static ArtNetPacket Create(ArtNetRecieveData data) { switch ((ArtNetOpCodes)data.OpCode) { case ArtNetOpCodes.Poll: return new ArtPollPacket(data); case ArtNetOpCodes.PollReply: return new ArtPollReplyPacket(data); case ArtNetOpCodes.Dmx: return new ArtNetDmxPacket(data); case ArtNetOpCodes.TodRequest: return new ArtTodRequestPacket(data); //case ArtNetOpCodes.TodData: // return new ArtTodDataPacket(data); case ArtNetOpCodes.TodControl: return new ArtTodControlPacket(data); case ArtNetOpCodes.Rdm: return new ArtRdmPacket(data); //case ArtNetOpCodes.RdmSub: // return new ArtRdmSubPacket(data); } return null; }
public ArtTodRequestPacket(ArtNetRecieveData data) : base(data) { }
public ArtPollPacket(ArtNetRecieveData data) : base(data) { }
public ArtRdmPacket(ArtNetRecieveData data) : base(data) { }
public ArtTodControlPacket(ArtNetRecieveData data) : base(data) { }
public ArtNetPacket(ArtNetRecieveData data) { ArtNetBinaryReader packetReader = new ArtNetBinaryReader(new MemoryStream(data.buffer)); ReadData(packetReader); }
public ArtTodRequestPacket(ArtNetRecieveData data) : base(data) { }
public ArtPollPacket(ArtNetRecieveData data) : base(data) { }
public ArtTodDataPacket(ArtNetRecieveData data) : base(data) { }
public ArtNetDmxPacket(ArtNetRecieveData data) : base(data) { }
public ArtRdmSubPacket(ArtNetRecieveData data) : base(data) { }