/// <summary>
 ///   Creates a I/O event argument object which contains an OSC message.
 /// </summary>
 public OscIoDeviceEventArgs(OscMessage message,
                             OscIoDeviceChannel deviceChannel,
                             Exception exception = null)
 {
     Message = message;
     DeviceChannel = deviceChannel;
     Exception = exception;
 }
 /// <summary>
 ///   Creates a I/O event argument object which contains an OSC bundle.
 /// </summary>
 public OscIoDeviceEventArgs(OscBundle bundle,
                             OscIoDeviceChannel deviceChannel,
                             Exception exception = null)
 {
     Bundle = bundle;
     DeviceChannel = deviceChannel;
     Exception = exception;
 }
        /// <summary>
        ///   Sends an OSC message to the given UDP address.
        /// </summary>
        public void Send(OscMessage message,
                         OscIoDeviceChannel deviceChannel)
        {
            if (ReferenceEquals(message, null))
                throw new ArgumentNullException("message");

            byte[] packet = message.ToOscPacketArray();
            IPEndPoint ipEndPoint = deviceChannel != null ? deviceChannel.IPEndPoint : _defaultRemoteEndPoint;
            OscIoDeviceEventArgs eventArgs = new OscIoDeviceEventArgs(message,
                                                                      new OscIoDeviceChannel(
                                                                          OscIoDeviceChannelType.Udp,
                                                                          ipEndPoint));
            // send message
            _udp.BeginSend(packet, packet.Length, ipEndPoint, OnSend, eventArgs);
        }
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                // get the datagram
                IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[] datagram = _udp.EndReceive(ar, ref remoteEndPoint);
                //                Console.WriteLine("OnReceive");
                //                Console.WriteLine(Encoding.ASCII.GetString(datagram));

                // create an device address for the remote end point
                OscIoDeviceChannel deviceAddress = new OscIoDeviceChannel(OscIoDeviceChannelType.Udp, remoteEndPoint);

                IOscElement element = OscParser.Parse(datagram);
                OscIoDeviceEventArgs eventArgs = null;
                switch (element.ElementType)
                {
                    case OscElementType.Message:
                        eventArgs = new OscIoDeviceEventArgs(element as OscMessage, deviceAddress);
                        break;
                    case OscElementType.Bundle:
                        eventArgs = new OscIoDeviceEventArgs(element as OscBundle, deviceAddress);
                        break;
                }

                // raise receive event
                if (DataReceived != null)
                {
                    DataReceived(this, eventArgs);
                }
            }
            finally
            {
                BeginReceive();
            }
        }