Esempio n. 1
0
        // UDP listening thread
        private void Listener()
        {
            while (!quitReceiver)
            {
                try
                {
                    // Receive UDP packet
                    byte[] buffer = udpClient.Receive(ref endPoint);

                    if (quitReceiver)
                    {
                        break;
                    }

                    // Create OscData object
                    OscData messageOrBundle = null;
                    try
                    {
                        messageOrBundle = OscData.FromByteArray(buffer);
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine("ERROR: Problem creating OscData from message (" + ex.Message + ").");
                    }

                    if (messageOrBundle != null)
                    {
                        if (messageOrBundle is OscMessage)
                        {
                            OscMessage message   = (OscMessage)messageOrBundle;
                            DateTime   timestamp = DateTime.UtcNow;
                            OnReceivedMessage(new OscMessageEventArgs(timestamp, message));
                        }
                        else if (messageOrBundle is OscBundle)
                        {
                            OscBundle bundle    = (OscBundle)messageOrBundle;
                            DateTime  timestamp = (bundle.Timestamp == OscData.TIMESTAMP_NOW) ? DateTime.UtcNow : OscData.DateTimeFromTimestamp(bundle.Timestamp);
                            OnReceivedBundle(new OscBundleEventArgs(timestamp, bundle));
                        }
                    }
                }
                catch (ThreadInterruptedException)
                {
                    Console.Error.WriteLine("WARNING: ThreadInterruptedException in Listener...");
                    if (quitReceiver)
                    {
                        break;
                    }
                }
                catch (SocketException)
                {
                    Console.Error.WriteLine("WARNING: SocketException in Listener...");
                    if (quitReceiver)
                    {
                        break;
                    }
                }
            }
            udpClient.Close();
        }
Esempio n. 2
0
        // Create an OSC bundle object for the specified binary buffer
        public OscBundle(byte[] buffer)
        {
            string         tag;
            ulong          timestamp = OscData.TIMESTAMP_NOW;
            List <OscData> parts     = new List <OscData>();

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer", "Buffer must not be null.");
            }
            using (MemoryStream stream = new MemoryStream(buffer))
            {
                tag = stream.ReadOscString();
                if (tag != OscBundle.BUNDLE_ADDRESS)
                {
                    throw new ArgumentException("Unrecognized bundle type.", "buffer");
                }
                timestamp = (ulong)stream.ReadOscLong();

                while (stream.Position < stream.Length)
                {
                    int length = stream.ReadOscInt();
                    if (length < 0)
                    {
                        throw new Exception("Bundle part has invalid length (<0).");
                    }
                    if (length > buffer.Length - stream.Position)
                    {
                        throw new Exception("Bundle part has invalid length.");
                    }
                    byte[] partBytes = new byte[length];
                    int    offset    = 0;
                    while (offset < length)
                    {
                        int read = stream.Read(partBytes, offset, length - offset);
                        if (read <= 0)
                        {
                            throw new Exception("EOF in bundle part.");
                        }
                        offset += read;
                    }
                    OscData part = OscData.FromByteArray(partBytes);
                    if (part == null)
                    {
                        break;
                    }
                    parts.Add(part);
                }
            }

            this.Buffer    = buffer;
            this.Timestamp = timestamp;
            this.Parts     = parts.ToArray();
        }