/// <summary>
        /// Try to receive a osc message, this method is non-blocking and will return imediatly with a message or null
        /// </summary>
        /// <param name="message">an osc message if one is ready else null if there are none</param>
        /// <returns>true if a message was ready</returns>
        public bool TryReceive(out OscPacket message)
        {
            message = null;

            if (State == OscSocketState.Connected)
            {
                if (m_Count > 0)
                {
                    lock (m_Lock)
                    {
                        message = m_ReceiveQueue[m_ReadIndex];

                        m_ReadIndex = NextReadIndex;

                        m_Count--;

                        return(true);
                    }
                }
                // if we are not receiving then start
                else if (m_IsReceiving == false)
                {
                    lock (m_Lock)
                    {
                        if (m_IsReceiving == false && State == OscSocketState.Connected)
                        {
                            BeginReceiving();
                        }
                    }
                }
            }

            return(false);
        }
        private void DtdtDataReceived(Rug.Osc.OscPacket data)
        {
            var positionData = dtdtHandler.StripBlobPositionData();

            for (int i = 0; i < positionData.trackerData.Count; i++)
            {
                var id = positionData.trackerData[i].id;
                for (int j = 0; j < oldTrackingData.trackerData.Count; j++)
                {
                    if (id == oldTrackingData.trackerData[j].id)
                    {
                        positionData.trackerData[i].position = Point.expoAverage(oldTrackingData.trackerData[j].position,
                                                                                 positionData.trackerData[i].position, 0.8f);
                        break;
                    }
                }
            }

            screenSaverTimer.Interval = screensaverInterval;

            SendPositonData(positionData);
            DwellableCollection.DetectDwell(positionData);

            oldTrackingData = positionData;
            //UpdateActiveUsersDict(positionData);
        }
        /// <summary>
        /// Add a osc message to the send queue
        /// </summary>
        /// <param name="message">message to send</param>
        public void Send(OscPacket message)
        {
            if (State == OscSocketState.Connected)
            {
                lock (m_Lock)
                {
                    m_QueueEmpty.Reset();

                    if (m_Count >= m_SendQueue.Length)
                    {
                        return;
                    }

                    m_SendQueue[m_WriteIndex] = message;

                    m_WriteIndex = NextWriteIndex;
                    m_Count++;

                    if (m_Count == 1)
                    {
                        int size = message.Write(m_Bytes);

                        Socket.BeginSend(m_Bytes, 0, size, SocketFlags, Send_Callback, message);
                    }
                }
            }
        }
Esempio n. 4
0
        private void NewKinectData(Rug.Osc.OscPacket data, ref byte[] mask, int listenPort)
        {
            var pkg = (OscMessage)data;

            int numPkgs = (int)pkg[1];

            for (int i = 0; i < numPkgs; i++)
            {
                int x = (int)pkg[dtdtX + (i * 8)];
                int y = (int)pkg[dtdtY + (i * 8)];

                switch (applicationState)
                {
                case ApplicationState.MaskCreation:
                    AddToMask(ref mask, x, y);
                    FillNeighbours(ref mask, 8, x, y);
                    break;

                case ApplicationState.Filtering:
                    FilterAndSend(data, mask, x, y, listenPort);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            //Console.WriteLine(data);
        }
        public static bool TryParse(string str, IFormatProvider provider, out OscPacket packet)
        {
            try
            {
                packet = Parse(str, provider);

                return(true);
            }
            catch
            {
                packet = default(OscPacket);

                return(false);
            }
        }
 /// <summary>
 /// Invoke a osc packet
 /// </summary>
 /// <param name="packet">the packet</param>
 /// <returns>true if any thing was invoked</returns>
 public bool Invoke(OscPacket packet)
 {
     if (packet is OscMessage)
     {
         return(Invoke(packet as OscMessage));
     }
     else if (packet is OscBundle)
     {
         return(Invoke(packet as OscBundle));
     }
     else
     {
         throw new Exception(String.Format(Strings.Listener_UnknownOscPacketType, packet.ToString()));
     }
 }
        /// <summary>
        /// Writes a single packet to the stream at the current position.
        /// </summary>
        /// <param name="packet">A osc packet</param>
        public void Write(OscPacket packet)
        {
            if (Format == OscPacketFormat.Binary)
            {
                byte[] bytes = packet.ToByteArray();

                // write the length
                Helper.Write(m_BinaryWriter, bytes.Length);

                // write the packet
                m_BinaryWriter.Write(bytes);
            }
            else
            {
                // write as a string
                m_StringWriter.WriteLine(packet.ToString());
            }
        }
        void Receive_Callback(IAsyncResult ar)
        {
            try
            {
                // create an empty origin
                EndPoint origin = UseIPv6 ? Helper.EmptyEndPointIPv6 : Helper.EmptyEndPoint;

                int count = Socket.EndReceiveFrom(ar, ref origin);

                OscPacket message = OscPacket.Read(m_Bytes, count, (IPEndPoint)origin);

                lock (m_Lock)
                {
                    if (m_Count < m_ReceiveQueue.Length)
                    {
                        m_ReceiveQueue[m_WriteIndex] = message;

                        m_WriteIndex = NextWriteIndex;

                        m_Count++;

                        // if this was the first message then signal
                        if (m_Count == 1)
                        {
                            m_MessageReceived.Set();
                        }
                    }
                }
            }
            catch
            {
            }

            if (State == OscSocketState.Connected)
            {
                // create an empty origin
                EndPoint origin = UseIPv6 ? Helper.EmptyEndPointIPv6 : Helper.EmptyEndPoint;

                Socket.BeginReceiveFrom(m_Bytes, 0, m_Bytes.Length, SocketFlags, ref origin, Receive_Callback, null);
            }
        }
        /// <summary>
        /// Read a single packet from the stream at the current position
        /// </summary>
        /// <returns>An osc packet</returns>
        public OscPacket Read()
        {
            if (Format == OscPacketFormat.Binary)
            {
                int length = Helper.ReadInt32(m_BinaryReader);

                byte[] bytes = new byte[length];

                m_BinaryReader.Read(bytes, 0, length);

                return(OscPacket.Read(bytes, length));
            }
            else
            {
                string    line = m_StringReader.ReadLine();
                OscPacket packet;

                if (OscPacket.TryParse(line, out packet) == false)
                {
                    StringBuilder sb = new StringBuilder();

                    sb.AppendLine(line);

                    while (EndOfStream == false)
                    {
                        sb.Append(m_StringReader.ReadLine());

                        if (OscPacket.TryParse(sb.ToString(), out packet) == true)
                        {
                            return(packet);
                        }

                        sb.AppendLine();
                    }

                    return(OscMessage.ParseError);
                }

                return(packet);
            }
        }
Esempio n. 10
0
        private void ListenLoop()
        {
            try
            {
                while (m_Receiver.State != OscSocketState.Closed)
                {
                    // if we are in a state to recieve
                    if (m_Receiver.State == OscSocketState.Connected)
                    {
                        // get the next message
                        // this will block until one arrives or the socket is closed
                        OscPacket packet = m_Receiver.Receive();

                        switch (m_Listener.ShouldInvoke(packet))
                        {
                        case OscPacketInvokeAction.Invoke:
                            m_Listener.Invoke(packet);
                            break;

                        case OscPacketInvokeAction.DontInvoke:
                            break;

                        case OscPacketInvokeAction.HasError:
                            break;

                        case OscPacketInvokeAction.Pospone:
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        void Send_Callback(IAsyncResult ar)
        {
            lock (m_Lock)
            {
                try
                {
                    SocketError error;

                    Socket.EndSend(ar, out error);

                    if (m_SendQueue[m_ReadIndex].IsSameInstance(ar.AsyncState as OscPacket) == false)
                    {
                        Debug.WriteLine("Objects do not match at index " + m_ReadIndex);
                    }

                    m_Count--;
                    m_ReadIndex = NextReadIndex;

                    if (m_Count > 0 && State == OscSocketState.Connected)
                    {
                        OscPacket packet = m_SendQueue[m_ReadIndex];

                        int size = packet.Write(m_Bytes);

                        Socket.BeginSend(m_Bytes, 0, size, SocketFlags, Send_Callback, packet);
                    }
                    else
                    {
                        m_QueueEmpty.Set();
                    }
                }
                catch
                {
                    m_QueueEmpty.Set();
                }
            }
        }
        /// <summary>
        /// Receive a osc message, this method is blocking and will only return once a message is recived
        /// </summary>
        /// <returns>an osc message</returns>
        public OscPacket Receive()
        {
            try
            {
                if (State == OscSocketState.Connected)
                {
                    // if we are not receiving then start
                    if (m_IsReceiving == false)
                    {
                        lock (m_Lock)
                        {
                            if (m_IsReceiving == false && State == OscSocketState.Connected)
                            {
                                BeginReceiving();
                            }
                        }
                    }

                    if (m_Count > 0)
                    {
                        lock (m_Lock)
                        {
                            OscPacket message = m_ReceiveQueue[m_ReadIndex];

                            m_ReadIndex = NextReadIndex;

                            m_Count--;

                            // if we have eaten all the messages then reset the signal
                            if (m_Count == 0)
                            {
                                m_MessageReceived.Reset();
                            }

                            return(message);
                        }
                    }

                    // wait for a new message
                    m_MessageReceived.WaitOne();
                    m_MessageReceived.Reset();

                    if (m_Count > 0)
                    {
                        lock (m_Lock)
                        {
                            OscPacket message = m_ReceiveQueue[m_ReadIndex];

                            m_ReadIndex = NextReadIndex;

                            m_Count--;

                            return(message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(Strings.Receiver_ErrorWhileWaitingForMessage, ex);
            }

            if (State == OscSocketState.Connected)
            {
                throw new Exception(Strings.Receiver_ErrorWhileWaitingForMessage);
            }

            throw new Exception(Strings.Receiver_SocketIsClosed);
        }
        /// <summary>
        /// Determin if the packet should be invoked
        /// </summary>
        /// <param name="packet">A packet</param>
        /// <returns>The appropriate action that should be taken with the packet</returns>
        public OscPacketInvokeAction ShouldInvoke(OscPacket packet)
        {
            if (packet.Error != OscPacketError.None)
            {
                return(OscPacketInvokeAction.HasError);
            }

            if (packet is OscMessage)
            {
                return(OscPacketInvokeAction.Invoke);
            }

            if (packet is OscBundle)
            {
                OscBundle bundle = packet as OscBundle;

                if (BundleInvokeMode == OscBundleInvokeMode.NeverInvoke)
                {
                    return(OscPacketInvokeAction.DontInvoke);
                }
                else if (BundleInvokeMode != OscBundleInvokeMode.InvokeAllBundlesImmediately)
                {
                    double delay;

                    IOscTimeProvider provider = TimeProvider;

                    if (TimeProvider == null)
                    {
                        provider = DefaultTimeProvider.Instance;
                    }

                    delay = provider.DifferenceInSeconds(bundle.Timestamp);

                    if ((BundleInvokeMode & OscBundleInvokeMode.InvokeEarlyBundlesImmediately) !=
                        OscBundleInvokeMode.InvokeEarlyBundlesImmediately)
                    {
                        if (delay > 0 && provider.IsWithinTimeFrame(bundle.Timestamp) == false)
                        {
                            if ((BundleInvokeMode & OscBundleInvokeMode.PosponeEarlyBundles) !=
                                OscBundleInvokeMode.PosponeEarlyBundles)
                            {
                                return(OscPacketInvokeAction.Pospone);
                            }
                            else
                            {
                                return(OscPacketInvokeAction.DontInvoke);
                            }
                        }
                    }

                    if ((BundleInvokeMode & OscBundleInvokeMode.InvokeLateBundlesImmediately) !=
                        OscBundleInvokeMode.InvokeLateBundlesImmediately)
                    {
                        if (delay < 0 && provider.IsWithinTimeFrame(bundle.Timestamp) == false)
                        {
                            return(OscPacketInvokeAction.DontInvoke);
                        }
                    }

                    if ((BundleInvokeMode & OscBundleInvokeMode.InvokeOnTimeBundles) !=
                        OscBundleInvokeMode.InvokeOnTimeBundles)
                    {
                        if (provider.IsWithinTimeFrame(bundle.Timestamp) == true)
                        {
                            return(OscPacketInvokeAction.DontInvoke);
                        }
                    }
                }

                return(OscPacketInvokeAction.Invoke);
            }
            else
            {
                return(OscPacketInvokeAction.DontInvoke);
            }
        }