public void Output(DmxData dmxData) { var packet = new Acn.Packets.sAcn.StreamingAcnDmxPacket(); packet.Framing.SourceName = AcnSourceName; packet.Framing.Universe = (short)dmxData.Universe; packet.Framing.Priority = this.priority; packet.Framing.SequenceNumber = (byte)dmxData.Sequence; packet.Dmx.StartCode = 0; packet.Dmx.Data = dmxData.Data; var destinationEP = Acn.Sockets.StreamingAcnSocket.GetUniverseEndPoint(dmxData.Universe); packet.Root.SenderId = AcnId; using (var data = new MemoryStream()) { var writer = new Acn.IO.AcnBinaryWriter(data); Acn.AcnPacket.WritePacket(packet, writer); using (var networkData = new MemoryStream()) { BuildNetworkPacket(networkData, destinationEP, (int)data.Position); SetIPCheckSum(networkData); data.Position = 0; data.WriteTo(networkData); SetUDPCheckSum(networkData); ulong secs = dmxData.TimestampMS / 1000; ulong usecs = (dmxData.TimestampMS * 1000) - (secs * 1000000); var pcapData = new PcapngUtils.Pcap.PcapPacket(secs, usecs, networkData.ToArray(), 0); this.writer.WritePacket(pcapData); } } }
/// <summary> /// Read all packet from a stream. After read any packet event OnReadPacketEvent is called. /// Function is NOT asynchronous! (blocking thread). If you want abort it, use CancellationToken /// </summary> /// <param name="cancellationToken"></param> public void ReadPackets(System.Threading.CancellationToken cancellationToken) { uint secs, usecs, caplen, len; long position = 0; byte[] data; while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length && !cancellationToken.IsCancellationRequested) { try { lock (syncRoot) { position = binaryReader.BaseStream.Position; secs = binaryReader.ReadUInt32().ReverseByteOrder(Header.ReverseByteOrder); usecs = binaryReader.ReadUInt32().ReverseByteOrder(Header.ReverseByteOrder); if (Header.NanoSecondResolution) { usecs = usecs / 1000; } caplen = binaryReader.ReadUInt32().ReverseByteOrder(Header.ReverseByteOrder); len = binaryReader.ReadUInt32().ReverseByteOrder(Header.ReverseByteOrder); data = binaryReader.ReadBytes((int)caplen); if (data.Length < caplen) { throw new EndOfStreamException("Unable to read beyond the end of the stream"); } } PcapPacket packet = new PcapPacket((UInt64)secs, (UInt64)usecs, data, position); OnReadPacket(packet); } catch (Exception exc) { OnException(exc); } } }
/// <summary> /// Read all packet from a stream. After read any packet event OnReadPacketEvent is called. /// Function is NOT asynchronous! (blocking thread). If you want abort it, use CancellationToken /// </summary> /// <param name="cancellationToken"></param> public void ReadPackets(System.Threading.CancellationToken cancellationToken) { uint secs, usecs,caplen,len; long position = 0; byte[] data; while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length && !cancellationToken.IsCancellationRequested) { try { lock (syncRoot) { position = binaryReader.BaseStream.Position; secs = binaryReader.ReadUInt32().ReverseByteOrder(Header.ReverseByteOrder); usecs = binaryReader.ReadUInt32().ReverseByteOrder(Header.ReverseByteOrder); if (Header.NanoSecondResolution) usecs = usecs / 1000; caplen = binaryReader.ReadUInt32().ReverseByteOrder(Header.ReverseByteOrder); len = binaryReader.ReadUInt32().ReverseByteOrder(Header.ReverseByteOrder); data = binaryReader.ReadBytes((int)caplen); if (data.Length < caplen) throw new EndOfStreamException("Unable to read beyond the end of the stream"); } PcapPacket packet = new PcapPacket((UInt64)secs, (UInt64)usecs, data,position); OnReadPacket(packet); } catch(Exception exc) { OnException(exc); } } }