public static byte[] AssemblePacket(INanoPacket packet, NanoChannelContext context)
        {
            if (packet.Channel == NanoChannel.Unknown)
            {
                throw new NanoPackingException("AssemblePacket: INanoPacket.Channel is UNKNOWN");
            }

            BEWriter     packetWriter  = new BEWriter();
            BinaryWriter payloadWriter = new BinaryWriter(new MemoryStream());

            packet.Serialize(payloadWriter);
            byte[] padding = Padding.CreatePaddingData(
                PaddingType.ANSI_X923,
                payloadWriter.ToBytes(),
                alignment: 4);
            payloadWriter.Write(padding);

            if (padding.Length > 0)
            {
                packet.Header.Padding = true;
            }

            packet.Header.ChannelId = context.GetChannelId(packet.Channel);

            packet.Header.Serialize(packetWriter);
            packetWriter.Write(payloadWriter.ToBytes());

            return(packetWriter.ToBytes());
        }
Beispiel #2
0
        public static byte[] AssemblePacket(INanoPacket packet, NanoChannelContext context)
        {
            if (packet.Channel == NanoChannel.Unknown)
            {
                throw new NanoPackingException("AssemblePacket: INanoPacket.Channel is UNKNOWN");
            }

            // Serialize payload and append padding if needed
            byte[] payloadData = null;
            using (EndianWriter payloadWriter = new EndianWriter())
            {
                packet.Serialize(payloadWriter);
                byte[] padding = Padding.CreatePaddingData(
                    PaddingType.ANSI_X923,
                    payloadWriter.ToBytes(),
                    alignment: 4);

                // Append padding
                if (padding.Length > 0)
                {
                    payloadWriter.Write(padding);
                    packet.Header.Padding = true;
                }

                payloadData = payloadWriter.ToBytes();
            }

            EndianWriter packetWriter = new EndianWriter();

            packet.Header.ChannelId = context.GetChannelId(packet.Channel);

            // Serialize header
            packet.Header.Serialize(packetWriter);
            // Append payload to header
            packetWriter.Write(payloadData);

            return(packetWriter.ToBytes());
        }
Beispiel #3
0
        public static INanoPacket ParsePacket(byte[] data, NanoChannelContext context)
        {
            EndianReader packetReader = new EndianReader(data);
            RtpHeader    header       = new RtpHeader();

            header.Deserialize(packetReader);
            NanoPayloadType payloadType = header.PayloadType;

            // It might be NanoChannel.Unknown at this point, if ChannelCreate
            // packet was not processed yet
            // It gets processed at the end of the function
            NanoChannel channel = context.GetChannel(header.ChannelId);

            INanoPacket packet        = null;
            long        payloadOffset = packetReader.Position;

            switch (payloadType)
            {
            // FIXME: Create from Attribute is broken
            case NanoPayloadType.UDPHandshake:
                packet = new UdpHandshake();
                break;

            case NanoPayloadType.ControlHandshake:
                packet = new ControlHandshake();
                break;

            case NanoPayloadType.ChannelControl:
                // Read type to pinpoint exact payload
                ChannelControlType cct =
                    (ChannelControlType)packetReader.ReadUInt32LE();
                packet = CreateFromChannelControlType(cct);
                break;

            case NanoPayloadType.Streamer:
                packet = CreateFromStreamerHeader(packetReader, channel);
                break;

            default:
                throw new NanoPackingException(
                          $"Unknown packet type received: {payloadType}");
            }

            if (packet == null)
            {
                throw new NanoPackingException("Failed to find matching body for packet");
            }

            packetReader.Seek(payloadOffset, SeekOrigin.Begin);
            packet.Deserialize(packetReader);
            packet.Header = header;

            if (packet as ChannelCreate != null)
            {
                string channelName = ((ChannelCreate)packet).Name;
                channel = NanoChannelClass.GetIdByClassName(channelName);
            }

            if (channel == NanoChannel.Unknown)
            {
                throw new NanoPackingException("ParsePacket: INanoPacket.Channel is UNKNOWN");
            }

            packet.Channel = channel;
            return(packet);
        }