private void HandleChannelCreate(Packets.ChannelCreate payload, ushort channelNumber)
        {
            NanoChannelId id = NanoChannelClass.GetIdByClassName(payload.Name);

            _channels[channelNumber] = id;
            GetChannelById(id).OnChannelCreated(payload.Flags, channelNumber);
        }
Example #2
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);
        }