Beispiel #1
0
        /// <summary>
        /// Serialize the packet.
        /// </summary>
        /// <returns>The newly serialized packet.</returns>
        public override byte[] ToByteArray()
        {
            List <byte> data = new List <byte>();

            data.AddRange(OscPacket.ValueToByteArray(mAddress));
            OscPacket.PadNull(data);

            data.AddRange(OscPacket.ValueToByteArray(mTypeTag));
            OscPacket.PadNull(data);

            foreach (object value in mData)
            {
                byte[] bytes = OscPacket.ValueToByteArray(value);
                if (bytes != null)
                {
                    data.AddRange(bytes);
                    if (value is string || value is byte[])
                    {
                        OscPacket.PadNull(data);
                    }
                }
            }

            return(data.ToArray());
        }
Beispiel #2
0
 /// <summary>
 /// Raises the <see cref="PacketReceived"/> event.
 /// </summary>
 /// <param name="packet">The packet to include in the event arguments.</param>
 private void OnPacketReceived(OscPacket packet)
 {
     if (PacketReceived != null)
     {
         PacketReceived(this, new OscPacketReceivedEventArgs(packet));
     }
 }
Beispiel #3
0
        /// <summary>
        /// Deserialize the packet.
        /// </summary>
        /// <param name="sourceEndPoint">The packet origin.</param>
        /// <param name="data">The serialized packet.</param>
        /// <param name="start">The starting index into the serialized data stream.</param>
        /// <param name="end">The ending index into the serialized data stream.</param>
        /// <returns>The newly deserialized packet.</returns>
        public static new OscBundle FromByteArray(IPEndPoint sourceEndPoint, byte[] data, ref int start, int end)
        {
            string address = OscPacket.ValueFromByteArray <string>(data, ref start);

            Assert.IsTrue(address == BundlePrefix);

            OscTimeTag timeStamp = OscPacket.ValueFromByteArray <OscTimeTag>(data, ref start);
            OscBundle  bundle    = new OscBundle(sourceEndPoint, timeStamp);

            while (start < end)
            {
                int length    = OscPacket.ValueFromByteArray <int>(data, ref start);
                int packetEnd = start + length;
                bundle.Append(OscPacket.FromByteArray(sourceEndPoint, data, ref start, packetEnd));
            }

            return(bundle);
        }
Beispiel #4
0
        /// <summary>
        /// Process the data received event.
        /// </summary>
        /// <param name="connection">The <see cref="TcpConnection" /> object associated with this data.</param>
        /// <param name="sourceEndPoint">The source endpoint.</param>
        /// <param name="data">The received data.</param>
        private void DataReceived(TcpConnection connection, IPEndPoint sourceEndPoint, byte[] data)
        {
            if (mHandleMessages)
            {
                try
                {
                    OscPacket packet = OscPacket.FromByteArray(sourceEndPoint, data);
                    if (TransportType == TransportType.Tcp)
                    {
                        packet.Client = new OscClient(connection);
                    }

                    OnPacketReceived(packet);

                    if (packet.IsBundle)
                    {
                        OnBundleReceived(packet as OscBundle);
                    }
                    else
                    {
                        if (FilterRegisteredMethods)
                        {
                            if (mRegisteredMethods.Contains(packet.Address))
                            {
                                OnMessageReceived(packet as OscMessage);
                            }
                        }
                        else
                        {
                            OnMessageReceived(packet as OscMessage);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ConsumeParsingExceptions == false)
                    {
                        OnReceiveErrored(ex);
                    }
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Serialize the packet.
        /// </summary>
        /// <returns>The newly serialized packet.</returns>
        public override byte[] ToByteArray()
        {
            List <byte> data = new List <byte>();

            data.AddRange(OscPacket.ValueToByteArray(mAddress));
            OscPacket.PadNull(data);

            data.AddRange(OscPacket.ValueToByteArray(TimeStamp));

            foreach (object value in mData)
            {
                if ((value is OscPacket))
                {
                    byte[] packetBytes = ((OscPacket)value).ToByteArray();
                    Assert.IsTrue(packetBytes.Length % 4 == 0);

                    data.AddRange(OscPacket.ValueToByteArray(packetBytes.Length));
                    data.AddRange(packetBytes);
                }
            }

            return(data.ToArray());
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="OscMessageReceivedEventArgs"/> class.
        /// </summary>
        /// <param name="packet">The <see cref="OscPacket"/> received.</param>
        public OscPacketReceivedEventArgs(OscPacket packet)
        {
            Assert.ParamIsNotNull(packet);

            Packet = packet;
        }
Beispiel #7
0
 /// <summary>
 /// Send an OscPacket over the connection.
 /// </summary>
 /// <param name="packet">The <see cref="OscPacket"/> to send.</param>
 public void Send(OscPacket packet)
 {
     byte[] packetData = packet.ToByteArray();
     Connection.Writer.Write(OscPacket.ValueToByteArray(packetData));
 }
Beispiel #8
0
 /// <summary>
 /// Transmit an Osc packet via TCP through the connected OscClient.
 /// </summary>
 /// <param name="packet">The packet to transmit.</param>
 /// <param name="client">The OscClient to communicate through.</param>
 /// <remarks>The OscClient must be connected for successful transmission.</remarks>
 public static void Send(OscPacket packet, OscClient client)
 {
     client.Send(packet);
 }
Beispiel #9
0
 /// <summary>
 /// Transmit an Osc packet via UDP.
 /// </summary>
 /// <param name="packet">The packet to transmit.</param>
 /// <param name="destination">The packet's destination.</param>
 public static void Send(OscPacket packet, IPEndPoint destination)
 {
     packet.Send(destination);
 }
Beispiel #10
0
        /// <summary>
        /// Deserialize the packet.
        /// </summary>
        /// <param name="sourceEndPoint">The packet origin.</param>
        /// <param name="data">The serialized packet.</param>
        /// <param name="start">The starting index into the serialized data stream.</param>
        /// <returns>The newly deserialized packet.</returns>
        public static OscMessage FromByteArray(IPEndPoint sourceEndPoint, byte[] data, ref int start)
        {
            string     address = OscPacket.ValueFromByteArray <string>(data, ref start);
            OscMessage message = new OscMessage(sourceEndPoint, address);

            char[] tags = OscPacket.ValueFromByteArray <string>(data, ref start).ToCharArray();
            foreach (char tag in tags)
            {
                object value;
                switch (tag)
                {
                case DefaultTag:
                    continue;

                case IntegerTag:
                    value = OscPacket.ValueFromByteArray <int>(data, ref start);
                    break;

                case LongTag:
                    value = OscPacket.ValueFromByteArray <long>(data, ref start);
                    break;

                case FloatTag:
                    value = OscPacket.ValueFromByteArray <float>(data, ref start);
                    break;

                case DoubleTag:
                    value = OscPacket.ValueFromByteArray <double>(data, ref start);
                    break;

                case StringTag:
                case SymbolTag:
                    value = OscPacket.ValueFromByteArray <string>(data, ref start);
                    break;

                case BlobTag:
                    value = OscPacket.ValueFromByteArray <byte[]>(data, ref start);
                    break;

                case TimeTag:
                    value = OscPacket.ValueFromByteArray <OscTimeTag>(data, ref start);
                    break;

                case CharacterTag:
                    value = OscPacket.ValueFromByteArray <char>(data, ref start);
                    break;

                case ColorTag:
                    value = OscPacket.ValueFromByteArray <Color>(data, ref start);
                    break;

                case TrueTag:
                    value = true;
                    break;

                case FalseTag:
                    value = false;
                    break;

                case NilTag:
                    value = null;
                    break;

                case InfinitumTag:
                    value = float.PositiveInfinity;
                    break;

                default:
                    Debug.WriteLine("Unknown tag: " + tag);
                    continue;
                }

                message.Append(value);
            }

            return(message);
        }