示例#1
0
        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");
        }
示例#2
0
        public static void ResetPacket(G2Header packet)
        {
            packet.NextBytePos   = packet.InternalPos;
            packet.NextBytesLeft = packet.InternalSize;

            packet.PayloadPos  = 0;
            packet.PayloadSize = 0;
        }
示例#3
0
        public static G2ReadResult ReadNextChild(G2Header root, G2Header child)
        {
            if (!root.HasChildren)
            {
                return(G2ReadResult.STREAM_END);
            }

            return(ReadNextPacket(child, ref root.NextBytePos, ref root.NextBytesLeft));
        }
示例#4
0
        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);
            }
        }
示例#5
0
        public static bool ReadPacket(G2Header root)
        {
            int start  = 0;
            int length = root.Data.Length;

            if (G2ReadResult.PACKET_GOOD == ReadNextPacket(root, ref start, ref length))
            {
                return(true);
            }

            return(false);
        }
示例#6
0
        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);
        }
示例#7
0
        public bool ReadPacket(ref G2Header root)
        {
            root = new G2Header(ReadBuffer);

            // continue from left off, read another goo packete
            if (ReadNext(root))
            {
                return(true);
            }

            if (ReadStatus != G2ReadResult.PACKET_INCOMPLETE)
            {
                return(false);
            }

            // re-align
            if (ReadSize > 0)
            {
                Buffer.BlockCopy(ReadBuffer, Start, ReadBuffer, 0, ReadSize);
            }

            // incomplete, or just started, read some more from file
            Start = 0;

            int read = ParentStream.Read(ReadBuffer, ReadSize, ReadBuffer.Length - ReadSize);

            Pos      += read;
            ReadSize += read;

            if (ReadNext(root))
            {
                return(true);
            }

            return(false);
        }
示例#8
0
        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);
        }
示例#9
0
        public static bool ReadPayload(G2Header packet)
        {
            ResetPacket(packet);

            G2Header child = new G2Header(packet.Data);

            G2ReadResult streamStatus = G2ReadResult.PACKET_GOOD;

            while (streamStatus == G2ReadResult.PACKET_GOOD)
            {
                streamStatus = ReadNextChild(packet, child);
            }

            bool found = false;

            if (streamStatus == G2ReadResult.STREAM_END)
            {
                if (packet.NextBytesLeft > 0)
                {
                    packet.PayloadPos  = packet.NextBytePos;
                    packet.PayloadSize = packet.NextBytesLeft;

                    found = true;
                }
            }
            else if (packet.NextBytesLeft > 0)
            {
                // Payload Read Error
                //m_pG2Comm->m_pCore->DebugLog("G2 Network", "Payload Read Error: " + HexDump(packet.Packet, packet.PacketSize));
            }

            packet.NextBytePos   = packet.InternalPos;
            packet.NextBytesLeft = packet.InternalSize;

            return(found);
        }
示例#10
0
        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);
        }
示例#11
0
        public static G2ReadResult ReadNextPacket( G2Header packet, ref int readPos, ref int readSize )
        {
            if( readSize == 0 )
                return G2ReadResult.PACKET_INCOMPLETE;

            int beginPos   = readPos;
            int beginSize  = readSize;

            packet.PacketPos = readPos;

            // Read Control Byte
            byte control = Buffer.GetByte(packet.Data, readPos);

            readPos  += 1;
            readSize -= 1;

            if ( control == 0 )
                return G2ReadResult.STREAM_END;

            byte lenLen  = (byte) ( (control & 0xE0) >> 5); // 11100000
            byte nameLen = (byte) ( (control & 0x18) >> 3); // 00011000
            byte flags   = (byte)   (control & 0x07);       // 00000111

            bool bigEndian  = (flags & 0x02) != 0;
            bool isCompound = (flags & 0x04) != 0;

            if( bigEndian )
                return G2ReadResult.PACKET_ERROR;

            packet.HasChildren = isCompound;

            // Read Packet Length
            packet.InternalSize = 0;
            if( lenLen != 0)
            {
                if(readSize < lenLen)
                {
                    readPos  = beginPos;
                    readSize = beginSize;
                    return G2ReadResult.PACKET_INCOMPLETE;
                }

                byte[] lenData = new byte[8]; // create here because lenLen is less than 8 in size
                Buffer.BlockCopy(packet.Data, readPos, lenData, 0, lenLen);

                packet.InternalSize = BitConverter.ToInt32(lenData, 0); // only 4 bytes supported so far

                Debug.Assert(MAX_FINAL_SIZE < G2_PACKET_BUFF);
                if (packet.InternalSize < 0 || MAX_FINAL_SIZE < packet.InternalSize)
                {
                    Debug.Assert(false);
                    return G2ReadResult.PACKET_ERROR;
                }

                readPos  += lenLen;
                readSize -= lenLen;
            }

            // Read Packet Name
            if(readSize < nameLen)
            {
                readPos  = beginPos;
                readSize = beginSize;
                return G2ReadResult.PACKET_INCOMPLETE;
            }

            if(nameLen != 1)
                return G2ReadResult.PACKET_ERROR;

            /*if(packet.Name.Length + 1 + nameLen > MAX_NAME_SIZE - 1)
            {
                Debug.Assert(false);
                packet.Name = "ERROR";
            }
            else
            {
                packet.Name += "/" + StringEnc.GetString(packet.Data, readPos, nameLen);
            }*/

            packet.Name = packet.Data[readPos];

            readPos  += nameLen;
            readSize -= nameLen;

            // Check if full packet length available in stream
            if(readSize < packet.InternalSize)
            {
                readPos  = beginPos;
                readSize = beginSize;
                return G2ReadResult.PACKET_INCOMPLETE;
            }

            packet.InternalPos = (packet.InternalSize > 0) ? readPos : 0;

            packet.NextBytePos   = packet.InternalPos;
            packet.NextBytesLeft = packet.InternalSize;

            readPos  += packet.InternalSize;
            readSize -= packet.InternalSize;

            packet.PacketSize = 1 + lenLen + nameLen + packet.InternalSize;

            return G2ReadResult.PACKET_GOOD;
        }
示例#12
0
        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);
        }
示例#13
0
        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;
        }
示例#14
0
        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);
        }
示例#15
0
        public static G2ReadResult ReadNextPacket(G2Header packet, ref int readPos, ref int readSize)
        {
            if (readSize == 0)
            {
                return(G2ReadResult.PACKET_INCOMPLETE);
            }

            int beginPos  = readPos;
            int beginSize = readSize;

            packet.PacketPos = readPos;

            // Read Control Byte
            byte control = Buffer.GetByte(packet.Data, readPos);

            readPos  += 1;
            readSize -= 1;

            if (control == 0)
            {
                return(G2ReadResult.STREAM_END);
            }

            byte lenLen  = (byte)((control & 0xE0) >> 5);               // 11100000
            byte nameLen = (byte)((control & 0x18) >> 3);               // 00011000
            byte flags   = (byte)(control & 0x07);                      // 00000111

            bool bigEndian  = (flags & 0x02) != 0;
            bool isCompound = (flags & 0x04) != 0;

            if (bigEndian)
            {
                return(G2ReadResult.PACKET_ERROR);
            }

            packet.HasChildren = isCompound;

            // Read Packet Length
            packet.InternalSize = 0;
            if (lenLen != 0)
            {
                if (readSize < lenLen)
                {
                    readPos  = beginPos;
                    readSize = beginSize;
                    return(G2ReadResult.PACKET_INCOMPLETE);
                }

                byte[] lenData = new byte[8];                 // create here because lenLen is less than 8 in size
                Buffer.BlockCopy(packet.Data, readPos, lenData, 0, lenLen);

                packet.InternalSize = BitConverter.ToInt32(lenData, 0);                 // only 4 bytes supported so far

                Debug.Assert(MAX_FINAL_SIZE < G2_PACKET_BUFF);
                if (packet.InternalSize < 0 || MAX_FINAL_SIZE < packet.InternalSize)
                {
                    Debug.Assert(false);
                    return(G2ReadResult.PACKET_ERROR);
                }

                readPos  += lenLen;
                readSize -= lenLen;
            }

            // Read Packet Name
            if (readSize < nameLen)
            {
                readPos  = beginPos;
                readSize = beginSize;
                return(G2ReadResult.PACKET_INCOMPLETE);
            }

            if (nameLen != 1)
            {
                return(G2ReadResult.PACKET_ERROR);
            }

            /*if(packet.Name.Length + 1 + nameLen > MAX_NAME_SIZE - 1)
             * {
             *      Debug.Assert(false);
             *      packet.Name = "ERROR";
             * }
             * else
             * {
             *      packet.Name += "/" + StringEnc.GetString(packet.Data, readPos, nameLen);
             * }*/

            packet.Name = packet.Data[readPos];

            readPos  += nameLen;
            readSize -= nameLen;

            // Check if full packet length available in stream
            if (readSize < packet.InternalSize)
            {
                readPos  = beginPos;
                readSize = beginSize;
                return(G2ReadResult.PACKET_INCOMPLETE);
            }

            packet.InternalPos = (packet.InternalSize > 0) ? readPos : 0;

            packet.NextBytePos   = packet.InternalPos;
            packet.NextBytesLeft = packet.InternalSize;

            readPos  += packet.InternalSize;
            readSize -= packet.InternalSize;

            packet.PacketSize = 1 + lenLen + nameLen + packet.InternalSize;

            return(G2ReadResult.PACKET_GOOD);
        }
示例#16
0
        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");
        }
示例#17
0
        public static G2ReadResult ReadNextChild( G2Header root, G2Header child)
        {
            if( !root.HasChildren )
                return G2ReadResult.STREAM_END;

            return ReadNextPacket(child, ref root.NextBytePos, ref root.NextBytesLeft);
        }
示例#18
0
        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;
        }
示例#19
0
        public static bool ReadPayload(G2Header packet)
        {
            ResetPacket(packet);

            G2Header child = new G2Header(packet.Data);

            G2ReadResult streamStatus = G2ReadResult.PACKET_GOOD;
            while( streamStatus == G2ReadResult.PACKET_GOOD )
                streamStatus = ReadNextChild(packet, child);

            bool found = false;

            if(streamStatus == G2ReadResult.STREAM_END)
            {
                if( packet.NextBytesLeft > 0)
                {
                    packet.PayloadPos  = packet.NextBytePos;
                    packet.PayloadSize = packet.NextBytesLeft;

                    found = true;
                }
            }
            else if( packet.NextBytesLeft > 0)
            {
                // Payload Read Error
                //m_pG2Comm->m_pCore->DebugLog("G2 Network", "Payload Read Error: " + HexDump(packet.Packet, packet.PacketSize));
            }

            packet.NextBytePos = packet.InternalPos;
            packet.NextBytesLeft = packet.InternalSize;

            return found;
        }
示例#20
0
        public static void ResetPacket(G2Header packet)
        {
            packet.NextBytePos   = packet.InternalPos;
            packet.NextBytesLeft = packet.InternalSize;

            packet.PayloadPos  = 0;
            packet.PayloadSize = 0;
        }
示例#21
0
        public bool ReadPacket(ref G2Header root)
        {
            root = new G2Header(ReadBuffer);

            // continue from left off, read another goo packete
            if (ReadNext(root))
                return true;

            if (ReadStatus != G2ReadResult.PACKET_INCOMPLETE)
                return false;

            // re-align
            if (ReadSize > 0)
                Buffer.BlockCopy(ReadBuffer, Start, ReadBuffer, 0, ReadSize);

            // incomplete, or just started, read some more from file
            Start = 0;

            int read = ParentStream.Read(ReadBuffer, ReadSize, ReadBuffer.Length - ReadSize);
            Pos += read;
            ReadSize += read;

            if (ReadNext(root))
                return true;

            return false;
        }
示例#22
0
        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;
        }
示例#23
0
        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;
        }
示例#24
0
        public static bool ReadPacket(G2Header root)
        {
            int start  = 0;
            int length = root.Data.Length;

            if (G2ReadResult.PACKET_GOOD == ReadNextPacket(root, ref start, ref length))
                return true;

            return false;
        }
示例#25
0
        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;
        }
示例#26
0
        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;
        }
示例#27
0
        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;
        }
示例#28
0
        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;
             }
        }