예제 #1
0
파일: G2Protocol.cs 프로젝트: nandub/DeOps
        public G2Frame WritePacket(G2Frame root, byte name, byte[] payload)
        {
            // If new packet
            if (root == null)
            {
                if (WriteOffset != 0)
                {
                    Frames.Clear();
                    WriteOffset = 0;

                    // caused by error building packet before
                    throw new Exception("Packet Frames not clear");                    //Debug.Assert(false); // Careful, can be caused by previous assert further down call stack
                }

                FinalSize = 0;
            }

            if (Frames.Count > MAX_FRAMES)
            {
                Debug.Assert(false);
                return(null);
            }

            // Create new frame
            G2Frame packet = new G2Frame();

            packet.Parent = root;
            packet.Name   = name;

            if (payload != null)
            {
                if (WriteOffset + payload.Length > MAX_WRITE_SIZE)
                {
                    Debug.Assert(false);
                    return(null);
                }

                packet.PayloadLength = payload.Length;
                packet.PayloadPos    = WriteOffset;
                payload.CopyTo(WriteData, WriteOffset);
                WriteOffset += payload.Length;
            }

            Frames.AddLast(packet);

            return(packet);
        }
예제 #2
0
파일: G2Protocol.cs 프로젝트: nandub/DeOps
        public byte[] WriteFinish()
        {
            // Reverse iterate through packet structure backwards, set lengths
            LinkedListNode <G2Frame> current = Frames.Last;

            while (current != null)
            {
                G2Frame packet = current.Value;

                if (packet.InternalLength > 0 && packet.PayloadLength > 0)
                {
                    packet.InternalLength += 1; // For endstream byte
                }
                packet.PayloadOffset   = packet.InternalLength;
                packet.InternalLength += packet.PayloadLength;

                packet.LenLen = 0;
                while (packet.InternalLength >= Math.Pow(256, packet.LenLen))
                {
                    packet.LenLen++;
                }

                Debug.Assert(packet.LenLen < 8);

                packet.HeaderLength = 1 + packet.LenLen + 1;

                if (packet.Parent != null)
                {
                    packet.Parent.InternalLength += packet.HeaderLength + packet.InternalLength;
                    packet.Parent.Compound        = 1;
                }

                current = current.Previous;
            }

            // Iterate through packet stucture forwards, build packet
            foreach (G2Frame packet in Frames)
            {
                int nextByte = 0;

                if (packet.Parent != null)
                {
                    Debug.Assert(packet.Parent.NextChild != 0);
                    nextByte = packet.Parent.NextChild;
                    packet.Parent.NextChild += packet.HeaderLength + packet.InternalLength;
                }
                else                 // beginning of packet
                {
                    FinalSize = packet.HeaderLength + packet.InternalLength;
                }

                byte control = 0;
                control |= (byte)(packet.LenLen << 5);
                control |= (byte)(1 << 3);
                control |= (byte)(packet.Compound << 2);

                Buffer.SetByte(FinalPacket, nextByte, control);
                nextByte += 1;

                // DNA should not pass packets greater than 4096, though pass through packets could be bigger
                if (packet.HeaderLength + packet.InternalLength > MAX_WRITE_SIZE)
                {
                    Debug.Assert(false);

                    Frames.Clear();
                    WriteOffset = 0;
                    FinalSize   = 0;

                    return(null);
                }

                byte [] lenData = BitConverter.GetBytes(packet.InternalLength);
                Buffer.BlockCopy(lenData, 0, FinalPacket, nextByte, packet.LenLen);
                nextByte += packet.LenLen;

                FinalPacket[nextByte] = packet.Name;
                nextByte += 1;

                if (packet.Compound == 1)
                {
                    packet.NextChild = nextByte;
                }

                if (packet.PayloadLength != 0)
                {
                    int finalPos = nextByte + packet.PayloadOffset;
                    Buffer.BlockCopy(WriteData, packet.PayloadPos, FinalPacket, finalPos, packet.PayloadLength);

                    if (packet.Compound == 1)                    // Set stream end
                    {
                        finalPos -= 1;
                        Buffer.SetByte(FinalPacket, finalPos, 0);
                    }
                }
            }

            Debug.Assert(FinalSize != 0);

            Frames.Clear();
            WriteOffset = 0;

            return(Utilities.ExtractBytes(FinalPacket, 0, FinalSize));
        }
예제 #3
0
        public G2Frame WritePacket(G2Frame root, byte name, byte[] payload)
        {
            // If new packet
            if(root == null)
            {
                if(WriteOffset != 0)
                {
                    Frames.Clear();
                    WriteOffset = 0;

                    // caused by error building packet before
                    throw new Exception("Packet Frames not clear");//Debug.Assert(false); // Careful, can be caused by previous assert further down call stack
                }

                FinalSize = 0;
            }

            if(Frames.Count > MAX_FRAMES)
            {
                Debug.Assert(false);
                return null;
            }

            // Create new frame
            G2Frame packet = new G2Frame();

            packet.Parent = root;
            packet.Name   = name;

            if(payload != null)
            {
                if(WriteOffset + payload.Length > MAX_WRITE_SIZE)
                {
                    Debug.Assert(false);
                    return null;
                }

                packet.PayloadLength = payload.Length;
                packet.PayloadPos    = WriteOffset;
                payload.CopyTo(WriteData, WriteOffset);
                WriteOffset += payload.Length;
            }

            Frames.AddLast(packet);

            return packet;
        }
예제 #4
0
        public new void WritePacket(G2Protocol protocol, G2Frame root, byte name)
        {
            byte[] payload = new byte[PAYLOAD_SIZE];

            BitConverter.GetBytes(UserID).CopyTo(payload, 0);
            BitConverter.GetBytes(ClientID).CopyTo(payload, 8);
            BitConverter.GetBytes(UdpPort).CopyTo(payload, 10);
            BitConverter.GetBytes(TcpPort).CopyTo(payload, 12);

            G2Frame address = protocol.WritePacket(root, name, payload);

            protocol.WritePacket(address, Packet_IP, IP.GetAddressBytes());

            if (TunnelServer != null)
                TunnelServer.WritePacket(protocol, address, Packet_Server);

            if (TunnelClient != null)
                protocol.WritePacket(address, Packet_Client, TunnelClient.ToBytes());
        }