Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        /// <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);
            }
        }