public static byte[] Pack(IPacket packet) { var stream = new MemoryStream(); var packetID = packetIDs[packet.GetType()]; stream.Add(packetID); packet.Pack(ref stream); return(stream.ToArray()); }
protected override void ProcessByte(byte current) { // if in segment if (PotentialStream.Count >= _startDelimiter.Length) { PotentialStream.Add(current); if (IsAccumulativeMatchComplete(_endDelimiter, current)) { // end segment // remove delimiters PotentialStream.RemoveRange(0, _startDelimiter.Length); PotentialStream.RemoveRange(PotentialStream.Count - _endDelimiter.Length, _endDelimiter.Length); var replacement = _byteArrayReplacer(PotentialStream.ToArray()); MemoryStream.AddRange(replacement); PotentialStream.Clear(); } return; } // if we match part of seg if (IsAccumulativeMatch(_startDelimiter, current)) { PotentialStream.Add(current); return; } // cancel potential if exists if (PotentialStream.Any()) { MemoryStream.AddRange(PotentialStream); PotentialStream.Clear(); } MemoryStream.Add(current); }
protected override void ProcessByte(byte current) { // if we match part of seg if (IsAccumulativeMatch(_match, current)) { PotentialStream.Add(current); if (IsAccumulativeMatchComplete(_match, current)) { MemoryStream.AddRange(_replace); PotentialStream.Clear(); } return; } // save potential, or normal if (PotentialStream.Any()) { // didnt match, add what we got MemoryStream.AddRange(PotentialStream); PotentialStream.Clear(); } MemoryStream.Add(current); }