Пример #1
0
        void ReceiveCallback(IAsyncResult result)
        {
            Monitor.Enter(callbackLock);
            Byte[] bytes = null;

            try
            {
                bytes = receivingUdpClient.EndReceive(result, ref RemoteIpEndPoint);
            }
            catch (ObjectDisposedException)
            {
                // Ignore if disposed. This happens when closing the listener
            }

            // Process bytes
            if (bytes != null && bytes.Length > 0)
            {
                if (BytePacketCallback != null)
                {
                    BytePacketCallback(bytes);
                }
                else if (OscPacketCallback != null)
                {
                    OscPacket packet = null;
                    try
                    {
                        packet = OscPacket.GetPacket(bytes);
                    }
                    catch (Exception)
                    {
                        // If there is an error reading the packet, null is sent to the callback
                    }

                    OscPacketCallback(packet);
                }
                else
                {
                    lock (queue)
                    {
                        queue.Enqueue(bytes);
                    }
                }
            }

            if (closing)
            {
                ClosingEvent.Set();
            }
            else
            {
                // Setup next async event
                AsyncCallback callBack = new AsyncCallback(ReceiveCallback);
                receivingUdpClient.BeginReceive(callBack, null);
            }
            Monitor.Exit(callbackLock);
        }
Пример #2
0
        public OscPacket Receive()
        {
            if (closing)
            {
                throw new Exception("UDPListener has been closed.");
            }

            if (OscPacketCallback != null)
            {
                lock (queue)
                {
                    if (queue.Count > 0)
                    {
                        byte[] bytes  = queue.Dequeue();
                        var    packet = OscPacket.GetPacket(bytes);
                        return(packet);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            else
            {
                if (queue.Count > 0)
                {
                    byte[] bytes  = queue.Dequeue();
                    var    packet = OscPacket.GetPacket(bytes);
                    return(packet);
                }
                else
                {
                    return(null);
                }
            }
        }
Пример #3
0
 public void Send(OscPacket packet)
 {
     byte[] data = packet.GetBytes();
     Send(data);
 }