private void AddPairs(G2Protocol protocol, G2Frame sync, byte name, PairList pairs) { if (pairs != null && pairs.Count > 0) { protocol.WritePacket(sync, name, pairs.ToBytes()); } }
public static void Test() { // create var packet = new GenericPacket("name"); packet.Data = new Dictionary <string, string>() { { "test1", "val1" }, { "test2", "val2" } }; // send var protocol = new G2Protocol(); var encoded = packet.Encode(protocol); // recv var recvPacket = new G2Header(encoded); int start = 0; int size = encoded.Length; var streamStatus = G2Protocol.ReadNextPacket(recvPacket, ref start, ref size); // decode var check = GenericPacket.Decode(recvPacket); Debug.Assert(check.Data["test1"] == "val1"); Debug.Assert(check.Data["test2"] == "val2"); }
private void AddSet(G2Protocol protocol, G2Frame sync, byte name, HashSet <int> set) { if (set != null && set.Count > 0) { protocol.WritePacket(sync, name, set.ToBytes()); } }
public override byte[] Encode(G2Protocol protocol) { lock (protocol.WriteSection) { protocol.WritePacket(null, PacketType.Padding, Filler); return(protocol.WriteFinish()); } }
public override byte[] Encode(G2Protocol protocol) { lock (protocol.WriteSection) { protocol.WritePacket(null, PacketType.Padding, Filler); return protocol.WriteFinish(); } }
public PacketStream(Stream stream, G2Protocol protocol, FileAccess access) { ParentStream = stream; Protocol = protocol; Access = access; if (access == FileAccess.Read) { ReadBuffer = new byte[4096]; // break/resume relies on 4kb buffer } }
private void AddPairMap(G2Protocol protocol, G2Frame sync, byte name, Dictionary <int, PairList> map) { if (map != null && map.Count > 0) { foreach (var pairList in map) { var keyValuePair = protocol.WritePacket(sync, name, null); protocol.WritePacket(keyValuePair, ChildPacket_ThreadID, BitConverter.GetBytes(pairList.Key)); protocol.WritePacket(keyValuePair, ChildPacket_PairList, pairList.Value.ToBytes()); } } }
public override byte[] Encode(G2Protocol protocol) { lock (protocol.WriteSection) { var dat = protocol.WritePacket(null, PacketType.Dat, null); protocol.WritePacket(dat, Packet_Pos, BitConverter.GetBytes(Pos)); protocol.WritePacket(dat, Packet_Data, Data); return(protocol.WriteFinish()); } }
public static IEnumerable <G2Header> EnumerateChildren(G2Header root) { G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { // set payload pos vars G2Protocol.ReadPayload(child); yield return(child); } }
public override byte[] Encode(G2Protocol protocol) { lock (protocol.WriteSection) { var sync = protocol.WritePacket(null, PacketType.Sync, null); AddSet(protocol, sync, Packet_FunctionHit, FunctionHits); AddSet(protocol, sync, Packet_ExceptionHit, ExceptionHits); AddSet(protocol, sync, Packet_ConstructedHit, ConstructedHits); AddSet(protocol, sync, Packet_DisposedHit, DisposeHits); AddPairs(protocol, sync, Packet_NewCalls, NewCalls); AddSet(protocol, sync, Packet_CallHits, CallHits); if (CallStats != null) { protocol.WritePacket(sync, Packet_CallStats, CallStats.ToBytes()); } AddPairs(protocol, sync, Packet_Inits, Inits); if (NewThreads != null) { foreach (var item in NewThreads) { var keyValuePair = protocol.WritePacket(sync, Packet_NewThreads, null); protocol.WritePacket(keyValuePair, ChildPacket_ThreadID, BitConverter.GetBytes(item.Key)); protocol.WritePacket(keyValuePair, ChildPacket_ThreadName, UTF8Encoding.UTF8.GetBytes(item.Value.Item1)); protocol.WritePacket(keyValuePair, ChildPacket_ThreadAlive, BitConverter.GetBytes(item.Value.Item2)); } } if (ThreadChanges != null) { foreach (var item in ThreadChanges) { var keyValuePair = protocol.WritePacket(sync, Packet_ThreadChanges, null); protocol.WritePacket(keyValuePair, ChildPacket_ThreadID, BitConverter.GetBytes(item.Key)); protocol.WritePacket(keyValuePair, ChildPacket_ThreadAlive, BitConverter.GetBytes(item.Value)); } } AddPairs(protocol, sync, Packet_NodeThreads, NodeThreads); AddPairs(protocol, sync, Packet_CallThreads, CallThreads); AddPairMap(protocol, sync, Packet_Threadlines, Threadlines); AddPairMap(protocol, sync, Packet_ThreadStacks, ThreadStacks); return(protocol.WriteFinish()); } }
public override byte[] Encode(G2Protocol protocol) { lock (protocol.WriteSection) { var packet = protocol.WritePacket(null, PacketType.Generic, UTF8Encoding.UTF8.GetBytes(Name)); if (Data != null) { foreach (var item in Data) { var keyValuePair = protocol.WritePacket(packet, Packet_Item, null); protocol.WritePacket(keyValuePair, Packet_Key, UTF8Encoding.UTF8.GetBytes(item.Key)); protocol.WritePacket(keyValuePair, Packet_Value, UTF8Encoding.UTF8.GetBytes(item.Value)); } } return(protocol.WriteFinish()); } }
public override byte[] Encode(G2Protocol protocol) { lock (protocol.WriteSection) { var instance = protocol.WritePacket(null, PacketType.Instance, null); protocol.WritePacket(instance, Packet_Type, BitConverter.GetBytes((int)Type)); protocol.WritePacket(instance, Packet_ThreadID, BitConverter.GetBytes(ThreadID)); protocol.WritePacket(instance, Packet_FieldID, BitConverter.GetBytes(FieldID)); if (Details != null) { protocol.WritePacket(instance, Packet_Details, UTF8Encoding.UTF8.GetBytes(Details)); } if (Columns != null) { foreach (var column in Columns) { protocol.WritePacket(instance, Packet_Column, UTF8Encoding.UTF8.GetBytes(column)); } } if (Fields != null) { foreach (var field in Fields) { var fieldPacket = protocol.WritePacket(instance, Packet_Field, null); protocol.WritePacket(fieldPacket, Packet_SubNodesFlag, BitConverter.GetBytes(field.PossibleSubNodes)); protocol.WritePacket(fieldPacket, Packet_FieldID, BitConverter.GetBytes(field.ID)); foreach (var cell in field.Cells) { protocol.WritePacket(fieldPacket, Packet_Cell, UTF8Encoding.UTF8.GetBytes(cell)); } } } return(protocol.WriteFinish()); } }
public static DatPacket Decode(G2Header root) { var dat = new DatPacket(); foreach (var child in G2Protocol.EnumerateChildren(root)) { switch (child.Name) { case Packet_Pos: dat.Pos = BitConverter.ToInt64(child.Data, child.PayloadPos); break; case Packet_Data: dat.Data = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; } } return(dat); }
void ReceivePackets() { int Start = 0; G2ReadResult streamStatus = G2ReadResult.PACKET_GOOD; while (streamStatus == G2ReadResult.PACKET_GOOD) { G2ReceivedPacket packet = new G2ReceivedPacket(); packet.Root = new G2Header(FinalRecvBuffer); streamStatus = G2Protocol.ReadNextPacket(packet.Root, ref Start, ref FinalRecvBuffSize); if (streamStatus != G2ReadResult.PACKET_GOOD) { break; } packet.Tcp = this; // extract data from final recv buffer so it can be referenced without being overwritten by this thread byte[] extracted = Utilities.ExtractBytes(packet.Root.Data, packet.Root.PacketPos, packet.Root.PacketSize); packet.Root = new G2Header(extracted); G2Protocol.ReadPacket(packet.Root); PacketLogEntry logEntry = new PacketLogEntry(DateTime.Now, DirectionType.In, ToString(), packet.Root.Data); LogPacket(logEntry); Remote.IncomingPacket(this, packet); } // re-align buffer if (Start > 0 && FinalRecvBuffSize > 0) { Buffer.BlockCopy(FinalRecvBuffer, Start, FinalRecvBuffer, 0, FinalRecvBuffSize); //Network.UpdateConsole(PacketBytesReady.ToString() + " bytes moved to front of receive buffer"); } }
private bool ReadNext(G2Header root) { if (ReadSize > 0) { int prevStart = Start; ReadStatus = G2Protocol.ReadNextPacket(root, ref Start, ref ReadSize); ParentPos += (Start - prevStart); if (ReadStatus == G2ReadResult.PACKET_GOOD) { return(true); } } // hit the exact end of the buffer read in, signal to read the next buffer in else { ReadStatus = G2ReadResult.PACKET_INCOMPLETE; } return(false); }
private static void ReadPairListMap(ref Dictionary <int, PairList> map, SyncPacket sync, G2Header child) { if (map == null) { map = new Dictionary <int, PairList>(); } int id = 0; PairList list = new PairList(); foreach (var sub in G2Protocol.EnumerateChildren(child)) { if (sub.Name == ChildPacket_ThreadID) { id = BitConverter.ToInt32(sub.Data, sub.PayloadPos); } else if (sub.Name == ChildPacket_PairList) { list = PairList.FromBytes(sub.Data, sub.PayloadPos, sub.PayloadSize); } } map[id] = list; }
public static GenericPacket Decode(G2Header root) { var generic = new GenericPacket(); if (G2Protocol.ReadPayload(root)) { generic.Name = UTF8Encoding.UTF8.GetString(root.Data, root.PayloadPos, root.PayloadSize); } foreach (var child in G2Protocol.EnumerateChildren(root)) { if (generic.Data == null) { generic.Data = new Dictionary <string, string>(); } string key = null; string value = null; foreach (var sub in G2Protocol.EnumerateChildren(child)) { if (sub.Name == Packet_Key) { key = UTF8Encoding.UTF8.GetString(sub.Data, sub.PayloadPos, sub.PayloadSize); } else if (sub.Name == Packet_Value) { value = UTF8Encoding.UTF8.GetString(sub.Data, sub.PayloadPos, sub.PayloadSize); } } generic.Data[key] = value; } return(generic); }
private void AddSet(G2Protocol protocol, G2Frame sync, byte name, HashSet<int> set) { if (set != null && set.Count > 0) protocol.WritePacket(sync, name, set.ToBytes()); }
private void AddPairs(G2Protocol protocol, G2Frame sync, byte name, PairList pairs) { if (pairs != null && pairs.Count > 0) protocol.WritePacket(sync, name, pairs.ToBytes()); }
private void AddPairMap(G2Protocol protocol, G2Frame sync, byte name, Dictionary<int, PairList> map) { if (map != null && map.Count > 0) foreach (var pairList in map) { var keyValuePair = protocol.WritePacket(sync, name, null); protocol.WritePacket(keyValuePair, ChildPacket_ThreadID, BitConverter.GetBytes(pairList.Key)); protocol.WritePacket(keyValuePair, ChildPacket_PairList, pairList.Value.ToBytes()); } }
public PacketStream(Stream stream, G2Protocol protocol, FileAccess access) { ParentStream = stream; Protocol = protocol; Access = access; if(access == FileAccess.Read) ReadBuffer = new byte[4096]; // break/resume relies on 4kb buffer }
public override byte[] Encode(G2Protocol protocol) { lock (protocol.WriteSection) { var sync = protocol.WritePacket(null, PacketType.Sync, null); AddSet(protocol, sync, Packet_FunctionHit, FunctionHits); AddSet(protocol, sync, Packet_ExceptionHit, ExceptionHits); AddSet(protocol, sync, Packet_ConstructedHit, ConstructedHits); AddSet(protocol, sync, Packet_DisposedHit, DisposeHits); AddPairs(protocol, sync, Packet_NewCalls, NewCalls); AddSet(protocol, sync, Packet_CallHits, CallHits); if (CallStats != null) protocol.WritePacket(sync, Packet_CallStats, CallStats.ToBytes()); AddPairs(protocol, sync, Packet_Inits, Inits); if (NewThreads != null) foreach (var item in NewThreads) { var keyValuePair = protocol.WritePacket(sync, Packet_NewThreads, null); protocol.WritePacket(keyValuePair, ChildPacket_ThreadID, BitConverter.GetBytes(item.Key)); protocol.WritePacket(keyValuePair, ChildPacket_ThreadName, UTF8Encoding.UTF8.GetBytes(item.Value.Item1)); protocol.WritePacket(keyValuePair, ChildPacket_ThreadAlive, BitConverter.GetBytes(item.Value.Item2)); } if (ThreadChanges != null) foreach (var item in ThreadChanges) { var keyValuePair = protocol.WritePacket(sync, Packet_ThreadChanges, null); protocol.WritePacket(keyValuePair, ChildPacket_ThreadID, BitConverter.GetBytes(item.Key)); protocol.WritePacket(keyValuePair, ChildPacket_ThreadAlive, BitConverter.GetBytes(item.Value)); } AddPairs(protocol, sync, Packet_NodeThreads, NodeThreads); AddPairs(protocol, sync, Packet_CallThreads, CallThreads); AddPairMap(protocol, sync, Packet_Threadlines, Threadlines); AddPairMap(protocol, sync, Packet_ThreadStacks, ThreadStacks); return protocol.WriteFinish(); } }
public override byte[] Encode(G2Protocol protocol) { lock (protocol.WriteSection) { var instance = protocol.WritePacket(null, PacketType.Instance, null); protocol.WritePacket(instance, Packet_Type, BitConverter.GetBytes((int)Type)); protocol.WritePacket(instance, Packet_ThreadID, BitConverter.GetBytes(ThreadID)); protocol.WritePacket(instance, Packet_FieldID, BitConverter.GetBytes(FieldID)); if(Details != null) protocol.WritePacket(instance, Packet_Details, UTF8Encoding.UTF8.GetBytes(Details)); if (Columns != null) foreach(var column in Columns) protocol.WritePacket(instance, Packet_Column, UTF8Encoding.UTF8.GetBytes(column)); if (Fields != null) foreach (var field in Fields) { var fieldPacket = protocol.WritePacket(instance, Packet_Field, null); protocol.WritePacket(fieldPacket, Packet_SubNodesFlag, BitConverter.GetBytes(field.PossibleSubNodes)); protocol.WritePacket(fieldPacket, Packet_FieldID, BitConverter.GetBytes(field.ID)); foreach(var cell in field.Cells) protocol.WritePacket(fieldPacket, Packet_Cell, UTF8Encoding.UTF8.GetBytes(cell)); } return protocol.WriteFinish(); } }
public override byte[] Encode(G2Protocol protocol) { lock (protocol.WriteSection) { var packet = protocol.WritePacket(null, PacketType.Generic, UTF8Encoding.UTF8.GetBytes(Name)); if(Data != null) foreach (var item in Data) { var keyValuePair = protocol.WritePacket(packet, Packet_Item, null); protocol.WritePacket(keyValuePair, Packet_Key, UTF8Encoding.UTF8.GetBytes(item.Key)); protocol.WritePacket(keyValuePair, Packet_Value, UTF8Encoding.UTF8.GetBytes(item.Value)); } return protocol.WriteFinish(); } }
public static void Test() { // create var packet = new GenericPacket("name"); packet.Data = new Dictionary<string, string>() { {"test1", "val1"}, {"test2", "val2"} }; // send var protocol = new G2Protocol(); var encoded = packet.Encode(protocol); // recv var recvPacket = new G2Header(encoded); int start = 0; int size = encoded.Length; var streamStatus = G2Protocol.ReadNextPacket(recvPacket, ref start, ref size); // decode var check = GenericPacket.Decode(recvPacket); Debug.Assert(check.Data["test1"] == "val1"); Debug.Assert(check.Data["test2"] == "val2"); }
public override byte[] Encode(G2Protocol protocol) { lock (protocol.WriteSection) { var dat = protocol.WritePacket(null, PacketType.Dat, null); protocol.WritePacket(dat, Packet_Pos, BitConverter.GetBytes(Pos)); protocol.WritePacket(dat, Packet_Data, Data); return protocol.WriteFinish(); } }
public virtual byte[] Encode(G2Protocol protocol) { return(null); }
public static SyncPacket Decode(G2Header root) { var sync = new SyncPacket(); foreach (var child in G2Protocol.EnumerateChildren(root)) { switch (child.Name) { case Packet_FunctionHit: sync.FunctionHits = PacketExts.HashSetFromBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_ExceptionHit: sync.ExceptionHits = PacketExts.HashSetFromBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_ConstructedHit: sync.ConstructedHits = PacketExts.HashSetFromBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_DisposedHit: sync.DisposeHits = PacketExts.HashSetFromBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_NewCalls: sync.NewCalls = PairList.FromBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_CallHits: sync.CallHits = PacketExts.HashSetFromBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_CallStats: sync.CallStats = PacketExts.StatsFromBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Inits: sync.Inits = PairList.FromBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_NewThreads: if (sync.NewThreads == null) { sync.NewThreads = new Dictionary <int, Tuple <string, bool> >(); } int id = 0; string name = null; bool alive = false; foreach (var sub in G2Protocol.EnumerateChildren(child)) { if (sub.Name == ChildPacket_ThreadID) { id = BitConverter.ToInt32(sub.Data, sub.PayloadPos); } else if (sub.Name == ChildPacket_ThreadName) { name = UTF8Encoding.UTF8.GetString(sub.Data, sub.PayloadPos, sub.PayloadSize); } else if (sub.Name == ChildPacket_ThreadAlive) { alive = BitConverter.ToBoolean(sub.Data, sub.PayloadPos); } } sync.NewThreads[id] = new Tuple <string, bool>(name, alive); break; case Packet_ThreadChanges: if (sync.ThreadChanges == null) { sync.ThreadChanges = new Dictionary <int, bool>(); } int id2 = 0; bool alive2 = false; foreach (var sub in G2Protocol.EnumerateChildren(child)) { if (sub.Name == ChildPacket_ThreadID) { id2 = BitConverter.ToInt32(sub.Data, sub.PayloadPos); } else if (sub.Name == ChildPacket_ThreadAlive) { alive2 = BitConverter.ToBoolean(sub.Data, sub.PayloadPos); } } sync.ThreadChanges[id2] = alive2; break; case Packet_NodeThreads: sync.NodeThreads = PairList.FromBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_CallThreads: sync.CallThreads = PairList.FromBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Threadlines: ReadPairListMap(ref sync.Threadlines, sync, child); break; case Packet_ThreadStacks: ReadPairListMap(ref sync.ThreadStacks, sync, child); break; } } return(sync); }
public static InstancePacket Decode(G2Header root) { var instance = new InstancePacket(); instance.Fields = new List <IFieldModel>(); // needs to be initd foreach (var child in G2Protocol.EnumerateChildren(root)) { switch (child.Name) { case Packet_Type: instance.Type = (InstancePacketType)BitConverter.ToInt32(child.Data, child.PayloadPos); break; case Packet_ThreadID: instance.ThreadID = BitConverter.ToInt32(child.Data, child.PayloadPos); break; case Packet_FieldID: instance.FieldID = BitConverter.ToInt32(child.Data, child.PayloadPos); break; case Packet_Details: instance.Details = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Column: if (instance.Columns == null) { instance.Columns = new List <string>(); } instance.Columns.Add(UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize)); break; case Packet_Field: if (instance.Fields == null) { instance.Fields = new List <IFieldModel>(); } var field = new RemoteFieldModel(); foreach (var sub in G2Protocol.EnumerateChildren(child)) { switch (sub.Name) { case Packet_SubNodesFlag: field.PossibleSubNodes = BitConverter.ToBoolean(sub.Data, sub.PayloadPos); break; case Packet_FieldID: field.ID = BitConverter.ToInt32(sub.Data, sub.PayloadPos); break; case Packet_Cell: if (field.Cells == null) { field.Cells = new List <string>(); } field.Cells.Add(UTF8Encoding.UTF8.GetString(sub.Data, sub.PayloadPos, sub.PayloadSize)); break; } } instance.Fields.Add(field); break; } } return(instance); }
public virtual byte[] Encode(G2Protocol protocol) { return null; }