/// <summary> /// Prints the packet to output. /// </summary> /// <param name="time">packet time value</param> /// <param name="ipPacket"><see cref="IPPacket"/> class instance which holds IP address of source and destination.</param> /// <param name="packet">Packet to print</param> public static void Print(DateTime time, IPPacket ipPacket, TransportPacket packet) { //If packet is empty don't print it if (packet == null) { return; } var srcHostName = GetDomainName(ipPacket.SourceAddress); var destHostName = GetDomainName(ipPacket.DestinationAddress); //Write the packet time source host name : packet > destination host name : port Console.WriteLine( $"{time.Hour}:{time.Minute}:{time.Second}.{time.Millisecond} {srcHostName} : {packet.SourcePort} > {destHostName} : {packet.DestinationPort}"); Console.WriteLine(); uint byteCount = 0; //Write header PrintPacket(packet.HeaderData, ref byteCount); Console.WriteLine(); //Write payload PrintPacket(packet.PayloadData, ref byteCount); Console.WriteLine(); }
/// <summary> /// Constructs the Messagizer as a session of transport using uri and resources. /// </summary> /// <param name="transport">used to deliver packets to the peer.</param> /// <param name="uri">the uri being used to configure the transport stack.</param> /// <param name="resources">the associated set of resources for this service.</param> public Messagizer(TransportPacket transport, URL uri, Resources resources) { // find the format. this.transport = transport; String format = uri.GetTerm(FORMAT, FormatFactory.BINARY); // find the format factory. FormatFactory ff = FormatFactory.Get(format); if (ff == null) { throw new ArgumentException( String.Format("format factory is not defined as '{0}' in format factories", format)); } // find the value factory. ValueFactory vf = ( ValueFactory )resources.Get(TransportConsts.VALUE_FACTORY); if (vf == null) { throw new ArgumentException(String.Format( "value factory is not defined as '{0}' in resources", TransportConsts.VALUE_FACTORY)); } tdi = ff.NewTaggedDataInput(vf, uri.ToString()); tdo = ff.NewTaggedDataOutput(vf, uri.ToString()); transport.SetSession(this); }
protected override TransportMessage NewTransport(string uri, Resources resources) { UdpListener udpListener = resources.Get(UDP_LISTENER) as UdpListener; IPEndPoint ipEndPoint = resources.Get(SOCKET_ADDRESS) as IPEndPoint; URL url = new URL(uri); TransportPacket transportPacket = null; if (udpListener != null) { transportPacket = new UdpConnection(ipEndPoint, udpListener); } else { transportPacket = new UdpConnection(url); } TransportMessage transportMessage = new Messagizer(transportPacket, url, resources); transportMessage = AddFilters(transportMessage, url, resources); ValueFactory vf = (ValueFactory)resources.Get(TransportConsts.VALUE_FACTORY); vf.LockDynamicTypes(); return(transportMessage); }
public void Process(TransportPacket packet) { switch (packet.Type) { case PacketType.Initialize: SetPlayerIdentifier(packet); return; case PacketType.Update: RenderField(packet); return; } }
public PayloadItem(ProtocolType protocolType, IPPacket sourcePacket, TransportPacket payloadPacket, bool clean) : this(protocolType, sourcePacket, clean) { if (payloadPacket == null) { Log.Error($"PayloadItem - payloadPacket was null"); throw new ArgumentNullException(nameof(payloadPacket)); } SourceIPAddress = sourcePacket.SourceAddress.ToString(); SourcePort = payloadPacket.SourcePort; DestinationPort = payloadPacket.DestinationPort; HeaderSize = payloadPacket.HeaderData.Length; PayloadSize = payloadPacket.PayloadData.Length; PacketContent = BitConverter.ToString(payloadPacket.PayloadData); }
private void ProcessPacket(Packet packet) { var ipPacket = packet?.PayloadPacket as IPPacket; TransportPacket transportPacket = null; switch (ipPacket?.PayloadPacket) { case TcpPacket tcpPacket: transportPacket = tcpPacket; break; case UdpPacket udpPacket: transportPacket = udpPacket; break; } var loggingMessage = $"{ipPacket?.SourceAddress}({transportPacket?.SourcePort}) <-> {ipPacket?.DestinationAddress}({transportPacket?.DestinationPort})"; this._logger.LogInformation(loggingMessage); }
/// <summary> /// Record the fragment at the position. If recorded, then /// must retain the fragment. /// </summary> /// <param name="fragNo"></param> /// <param name="frag"></param> /// <returns>true if the fragment hadn't been previously seen</returns> public bool RecordFragment(uint fragNo, TransportPacket frag) { ResizeIfNecessary(fragNo); if (fragments[fragNo] != null) { return false; } fragments[fragNo] = frag; frag.Retain(); seen++; return true; }
public void Unmarshal(TransportPacket input, ITransportDeliveryCharacteristics tdc, EventHandler<MessageEventArgs> messageAvailable) { // <item>Message fits within the transport packet length, so sent unmodified // <pre>[byte:message-type] [byte:channelId] [uint32:packet-size] // [bytes:content]</pre> // </item> // <item> if the message is the first fragment, then the high-bit is // set on the message-type; the number of fragments is encoded using // the adaptive <see cref="ByteUtils.EncodeLength(int)"/> format. // <pre>[byte:message-type'] [byte:channelId] [uint32:packet-size] // [byte:seqno] [bytes:encoded-#-fragments] [bytes:frag]</pre> // </item> // <item> for all subsequent fragments; seqno' = seqno | 128; // the number of fragments is encoded using the adaptive // <see cref="ByteUtils.EncodeLength(int)"/> format. // <pre>[byte:message-type'] [byte:channelId] [uint32:packet-size] // [byte:seqno'] [bytes:encoded-fragment-#] [bytes:frag]</pre> // Fastpath: if the message-type doesn't have the high-bit set, // then this is a non-fragmented message if ((input.ByteAt(0) & 128) == 0) { // If the submarshaller uses LWMCFv1.1 then we would have just // sent the packet as-is; if not, then we'll have prefixed a // LWMCFv1.1 header which must be first removed if (!subMarshallerIsLwmcf11) { input.RemoveBytes(0, (int)LWMCFv11.HeaderSize); } subMarshaller.Unmarshal(input, tdc, messageAvailable); return; } MessageType type; byte channelId; uint contentLength; Stream s = input.AsReadStream(); LWMCFv11.DecodeHeader(out type, out channelId, out contentLength, s); byte seqNo = (byte)s.ReadByte(); TransportPacket subPacket; if ((seqNo & 128) == 0) { // This starts a new message uint numFrags = (uint)ByteUtils.DecodeLength(s); subPacket = UnmarshalFirstFragment(seqNo, numFrags, input.SplitOut((int)(s.Length - s.Position)), tdc); } else { // This is a message in progress seqNo = (byte)(seqNo & ~128); uint fragNo = (uint)ByteUtils.DecodeLength(s); subPacket = UnmarshalFragInProgress(seqNo, fragNo, input.SplitOut((int)(s.Length - s.Position)), tdc); } if (subPacket != null) { subMarshaller.Unmarshal(subPacket, tdc, messageAvailable); subPacket.Dispose(); } }
private TransportPacket UnmarshalFragInProgress(byte seqNo, uint fragNo, TransportPacket frag, ITransportDeliveryCharacteristics tdc) { Sequences sequences; if (!accumulatedReceived.TryGetValue(tdc, out sequences)) { accumulatedReceived[tdc] = sequences = Sequences.ForTransport(tdc, windowSize, SeqNoCapacity); } return sequences.ReceivedFrag(seqNo, fragNo, frag); }
public void SendPacket(ITransport transport, TransportPacket packet) { try { packet.Retain(); SentPackets.Add(packet); transport.SendPacket(packet); } catch (TransportError e) { } }
public void ReportDisposition(NetworkEmulatorTransport.PacketMode mode, NetworkEmulatorTransport.PacketEffect effect, TransportPacket packet) { StringBuilder report = new StringBuilder(60); switch(effect) { case NetworkEmulatorTransport.PacketEffect.Delayed: report.Append("D"); break; case NetworkEmulatorTransport.PacketEffect.Reordered: report.Append("R"); break; case NetworkEmulatorTransport.PacketEffect.Dropped: report.Append("_"); break; case NetworkEmulatorTransport.PacketEffect.None: report.Append(" "); break; } report.Append(Text.Length > 60 ? Text.Substring(0, 58) : Text); Text = report.ToString(); }
public virtual void SendPacket(TransportPacket packet) { Wrapped.SendPacket(packet); }
protected void NotifyPacketSent(TransportPacket packet, ITransport transport) { if (PacketSent != null) { PacketSent(packet, this); } }
public MyPacketHandler(TransportPacket tp) { _tp = tp; _tp.SetSession(this); }
public override void SendPacket(TransportPacket packet) { Debug.Assert(packet.Length < MaximumCapacity, "transport can never have sufficient capacity to send this message"); QueuePacket(packet); if (!TrySending()) { NotifyError(new ErrorSummary(Severity.Information, SummaryErrorCode.TransportBacklogged, "Capacity exceeded: packet queued", this, null)); return; } }
public override TransportPacket ReceivedFirstFrag(uint seqNo, uint numFrags, TransportPacket fragment) { FragmentedMessage inProgress; if (!pendingMessages.TryGetValue(seqNo, out inProgress)) { pendingMessages[seqNo] = inProgress = NewFragmentedMessage(numFrags); } else { inProgress.SetNumberFragments(numFrags); } inProgress.RecordFragment(0, fragment); // FragmentedMessage will Retain() if (!inProgress.Finished) { return null; } pendingMessages.Remove(seqNo); return inProgress.Assemble(); // properly disposes too }
private void SetPlayerIdentifier(TransportPacket packet) { field.SetPlayerIdentifier(packet.changes[0].Id); }
private void RenderField(TransportPacket packet) { field.Map = packet.changes; }
public override void UpdateTransportChecksum() { // Update TCP checksum TransportPacket.UpdateTCPChecksum(); }
public override void UpdateTransportChecksum() { TransportPacket.UpdateUDPChecksum(); }
public abstract TransportPacket ReceivedFirstFrag(uint seqNo, uint numFrags, TransportPacket fragment);
static void Main(string[] args) { int offset = 0; string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string path = String.Format("{0}\\Videos\\20180510_1957 FTS-191-.TS", home.ToString()); if (File.Exists(path)) { Console.WriteLine("Yes"); Console.WriteLine(OtherConstants.SyncByteValue); try { FileStream stream = new FileStream(path, FileMode.Open); int streamLengthInBytes = (int)stream.Length; Console.WriteLine("Stream length: {0}MB", Calculations.BytesToMegs(stream.Length)); StreamReader reader = new StreamReader(stream); while (!reader.EndOfStream) { int b = reader.Read(); if (b == OtherConstants.SyncByteValue) { decimal percentageProgessed = ((decimal)offset / (decimal)streamLengthInBytes) * 100; Console.WriteLine("Offset: {0} / {2} ({3}%) SyncByte {1}", offset, b, streamLengthInBytes, percentageProgessed.ToString("0.0")); int byte2 = reader.Read(); int byte3 = reader.Read(); int byte4 = reader.Read(); int byte5 = reader.Read(); //watch out for end of file here. Make safe Console.WriteLine($"dec: {byte2} bin:{Converters.IntByteToBinaryString(byte2)} / dec: {byte3} bin: {Converters.IntByteToBinaryString(byte3)}"); ushort _16bit = Converters.BytesToPIDType(byte2, byte3); Console.WriteLine("Bytes: {0}", Converters.IntUShortToBinaryString(_16bit)); Console.WriteLine("PID Could be: {0} and in Hex: {1:X}", Converters.IntUShortToBinaryString(Converters.ExtractPIDTypeFromTSPacketUShort(_16bit)), Converters.ExtractPIDTypeFromTSPacketUShort(_16bit)); byte[] bytesForPacket = new byte[] { (byte)b, (byte)byte2, (byte)byte2, (byte)byte2, (byte)byte2 }; TransportPacket packet = new TransportPacket(bytesForPacket); Console.WriteLine(packet.GetDebugOutput()); Console.ReadKey(); } offset++; } stream.Close(); } catch (FileLoadException fex) { Console.WriteLine("File load error: {0}", fex.Message); } catch (IOException ioex) { Console.WriteLine("I/O error: {0}", ioex.Message); } catch (Exception ex) { Console.WriteLine("Generic error: {0}", ex.Message); } finally { } } Console.ReadKey(); }
protected void QueuePacket(TransportPacket packet) { if (contentsSize + packet.Length > MaximumCapacity) { NotifyError(new ErrorSummary(Severity.Warning, SummaryErrorCode.TransportBacklogged, "Capacity exceeded: packet discarded", this, null)); return; } bucketContents.Enqueue(packet); contentsSize += (uint)packet.Length; }
public override void SendPacket(TransportPacket packet) { InvalidStateException.Assert(Active, "Cannot send on disposed transport", this); ContractViolation.Assert(packet.Length > 0, "Cannot send 0-byte messages!"); ContractViolation.Assert(packet.Length - PacketHeaderSize <= MaximumPacketSize, String.Format("Packet exceeds transport capacity: {0} > {1}", packet.Length - PacketHeaderSize, MaximumPacketSize)); WritePacketHeader(packet); lock (this) { outstanding.Enqueue(packet); } FlushOutstandingPackets(); }
protected void QueuePacket(TransportPacket packet) { queuedPackets.Enqueue(packet); }
/// <summary> /// Provide an opportunity to subclasses to write out the packet header /// </summary> /// <param name="packet"></param> protected virtual void WritePacketHeader(TransportPacket packet) { // do nothing }
protected void NotifyPacketReceived(TransportPacket packet, ITransport transport) { if (PacketReceived == null) { NotifyError(new ErrorSummary(Severity.Warning, SummaryErrorCode.Configuration, "transport has no listeners for receiving incoming messages!", this, null)); } else { PacketReceived(packet, this); } }
protected override void TransportRejected(ITransport transport, IDictionary<string, string> capabilities) { TransportPacket tp = new TransportPacket(LWMCFv11.EncodeHeader(MessageType.System, (byte)SystemMessageType.IncompatibleVersion, 0)); transport.SendPacket(tp); base.TransportRejected(transport, capabilities); }
protected virtual void _wrapped_PacketSentEvent(TransportPacket packet, ITransport transport) { NotifyPacketSent(packet, transport); }
public void SendPacket(TransportPacket packet) { Debug.Assert(packet.Length != 0, "Shouldn't send zero-length packets!"); bytesSent += (uint)packet.Length; if (PacketSent != null) { PacketSent(packet, this); } packet.Dispose(); }
private void getData(ISampleDataProvider dataProvider, AnalysisParameters analysisParameters, BackgroundWorker worker) { Logger.Instance.Write("Starting analysis"); analysisParameters.ScanningFrequency.CollectionType = CollectionType.MHEG5; FrequencyScanner frequencyScanner = new FrequencyScanner(dataProvider, worker); Collection <TVStation> stations = frequencyScanner.FindTVStations(); pidList = new Collection <PidSpec>(); dataProvider.ChangePidMapping(new int[] { -1 }); IntPtr memoryPointer = dataProvider.BufferAddress; int currentOffset = 0; byte[] buffer = new byte[188]; DateTime startTime = DateTime.Now; int packetCount = 0; int errorPackets = 0; int nullPackets = 0; while ((DateTime.Now - startTime).TotalSeconds < analysisParameters.DataCollectionTimeout && !worker.CancellationPending) { if (currentOffset >= dataProvider.BufferSpaceUsed) { Thread.Sleep(2000); if (currentOffset >= dataProvider.BufferSpaceUsed) { Logger.Instance.Write("Analysis resetting pid after " + packetCount + " packets (errors = " + errorPackets + " null = " + nullPackets + ")"); dataProvider.ChangePidMapping(new int[] { -1 }); currentOffset = 0; } } else { IntPtr currentPointer = new IntPtr(memoryPointer.ToInt64() + currentOffset + 136); Marshal.Copy(currentPointer, buffer, 0, 188); packetCount++; /*if (dumpCount < 10000) * { * Logger.Instance.Dump("atsc " + dumpCount, buffer, buffer.Length); * dumpCount++; * }*/ TransportPacket transportPacket = new TransportPacket(); try { transportPacket.Process(buffer); if (transportPacket.ErrorIndicator) { errorPackets++; } if (transportPacket.IsNullPacket) { nullPackets++; } if (!transportPacket.ErrorIndicator) { bool ignorePid = checkPid(transportPacket.PID, stations); if (!ignorePid) { PidSpec pidSpec = findPidSpec(pidList, transportPacket.PID); if (pidSpec == null) { pidSpec = new PidSpec(transportPacket.PID); addPid(pidList, new PidSpec(transportPacket.PID)); } pidSpec.ProcessPacket(buffer, transportPacket); } } else { Logger.Instance.Write("Transport packet error in packet " + packetCount); } } catch (ArgumentOutOfRangeException) { Logger.Instance.Write("Failed to parse packet " + packetCount); } currentOffset += buffer.Length; } } Logger.Instance.Write("Analysis completed: " + pidList.Count + " PID's loaded from " + packetCount + " packets"); }
public void Inject(uint seqNo, TransportPacket payload) { Debug.Assert(PacketHeaderSize == 4); payload.Prepend(DataConverter.Converter.GetBytes(seqNo)); NotifyPacketReceived(payload); }
protected void ClientReceivedPacket(TransportPacket packet, ITransport transport) { byte[] received = packet.ToArray(); bool ok = true; if (received[0] != (byte)(clientPacketCount + clientMissedOffset)) { Console.WriteLine("client: ERROR: expected packet#" + (clientPacketCount + clientMissedOffset) + " but received packet #" + received[0]); ok = false; } else { Debug("client: received expected packet#{0}", (byte)(clientPacketCount + clientMissedOffset)); } if (!CheckPacket(received, "client")) { ok = false; } Console.Write(ok ? '+' : '!'); clientPacketCount++; }
protected void ServerReceivedPacket(TransportPacket packet, ITransport transport) { byte[] received = packet.ToArray(); if (received[0] != (byte)(serverPacketCount + serverMissedOffset)) { Console.WriteLine("server: ERROR: expected packet#" + (serverPacketCount + serverMissedOffset) + " but received packet #" + received[0]); } else { Debug("server: received expected packet#{0}", (byte)(serverPacketCount + serverMissedOffset)); } CheckPacket(received, "server"); Debug("==> server: replying with byte array"); packet.Retain(); // must retain since SendPacket() will dispose server.SendPacket(packet); serverPacketCount++; }
private void FragmentMessage(Message message, ITransportDeliveryCharacteristics tdc, TransportPacket packet, MarshalledResult mr) { // <item> if the message is the first fragment, then the high-bit is // set on the message-type; the number of fragments is encoded using // the adaptive <see cref="ByteUtils.EncodeLength(int)"/> format. // <pre>[byte:message-type'] [byte:channelId] [uint32:packet-size] // [byte:seqno] [bytes:encoded-#-fragments] [bytes:frag]</pre> // </item> // <item> for all subsequent fragments; seqno' = seqno | 128; // the number of fragments is encoded using the adaptive // <see cref="ByteUtils.EncodeLength(int)"/> format. // <pre>[byte:message-type'] [byte:channelId] [uint32:packet-size] // [byte:seqno'] [bytes:encoded-fragment-#] [bytes:frag]</pre> // Although we use an adaptive scheme for encoding the number, // we assume a maximum of 4 bytes for encoding # frags // for a total of 4 extra bytes bytes; we determine the frag // size from the message size - MaxHeaderSize const uint maxFragHeaderSize = 1 /*seqno*/ + 4; const uint maxHeaderSize = LWMCFv11.HeaderSize + maxFragHeaderSize; uint maxPacketSize = Math.Max(maxHeaderSize, tdc.MaximumPacketSize - maxHeaderSize); // round up the number of possible fragments uint numFragments = (uint)(packet.Length + maxPacketSize - 1) / maxPacketSize; Debug.Assert(numFragments > 1); uint seqNo = AllocateOutgoingSeqNo(tdc); for (uint fragNo = 0; fragNo < numFragments; fragNo++) { TransportPacket newPacket = new TransportPacket(); Stream s = newPacket.AsWriteStream(); uint fragSize = (uint)Math.Min(maxPacketSize, packet.Length - (fragNo * maxPacketSize)); if (fragNo == 0) { s.WriteByte((byte)seqNo); ByteUtils.EncodeLength(numFragments, s); } else { s.WriteByte((byte)(seqNo | 128)); ByteUtils.EncodeLength(fragNo, s); } newPacket.Prepend(LWMCFv11.EncodeHeader((MessageType)((byte)message.MessageType | 128), message.ChannelId, (uint)(fragSize + s.Length))); newPacket.Append(packet, (int)(fragNo * maxPacketSize), (int)fragSize); mr.AddPacket(newPacket); } packet.Dispose(); }
public abstract void SendPacket(TransportPacket packet);
public TransportPacket Assemble() { Debug.Assert(Finished); TransportPacket packet = new TransportPacket(); foreach (TransportPacket frag in fragments) { packet.Append(frag, 0, frag.Length); frag.Dispose(); } fragments = null; return packet; }
protected virtual void NotifyPacketReceived(TransportPacket packet) { // DebugUtils.DumpMessage(this.ToString() + " notifying of received message", buffer, offset, count); if (PacketReceived == null) { NotifyError(new ErrorSummary(Severity.Warning, SummaryErrorCode.Configuration, "transport has no listeners for receiving incoming messages!", this, null)); } else { PacketReceived(packet, this); } // event listeners are responsible for calling Retain() if they // want to use it for longer. packet.Dispose(); }
public override TransportPacket ReceivedFrag(uint seqNo, uint fragNo, TransportPacket fragment) { if (!sw.Seen(seqNo)) { return null; } return base.ReceivedFrag(seqNo, fragNo, fragment); }
protected virtual void NotifyPacketSent(TransportPacket packet) { if (PacketSent != null) { PacketSent(packet, this); } packet.Dispose(); }
public override TransportPacket ReceivedFrag(uint seqNo, uint fragNo, TransportPacket fragment) { FragmentedMessage inProgress; if (!pendingMessages.TryGetValue(seqNo, out inProgress)) { pendingMessages[seqNo] = inProgress = NewFragmentedMessage(0); } inProgress.RecordFragment(fragNo, fragment); // FragmentedMessage should Retain() if (!inProgress.Finished) { return null; } pendingMessages.Remove(seqNo); return inProgress.Assemble(); // properly disposes too }
public override void SendPacket(TransportPacket packet) { Debug.Assert(packet.Length <= DrainageAmount, "leaky bucket will never have sufficient capacity to send a packet of size " + packet.Length); CheckDrain(); DrainPackets(); QueuePacket(packet); DrainPackets(); }
public abstract TransportPacket ReceivedFrag(uint seqNo, uint fragNo, TransportPacket fragment);
/// <summary> /// Constructs the Messagizer as a session of transport using uri and resources. /// </summary> /// <param name="transport">used to deliver packets to the peer.</param> /// <param name="uri">the uri being used to configure the transport stack.</param> /// <param name="resources">the associated set of resources for this service.</param> public Messagizer(TransportPacket transport, string uri, Resources resources) : this(transport, new URL(uri), resources) { }