ReadNextPacket() public static method

public static ReadNextPacket ( G2Header packet, int &readPos, int &readSize ) : G2ReadResult
packet G2Header
readPos int
readSize int
return G2ReadResult
Example #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");
        }
Example #2
0
        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");
            }
        }
Example #3
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);
        }