Esempio n. 1
0
        private async void BeginReceiveUdp()
        {
            try
            {
                while (!this.disposed)
                {
                    UdpReceiveResult Data = await this.udpClient.ReceiveAsync();

                    if (!this.disposed)
                    {
                        UdpDatagramEvent h = this.OnUdpDatagramReceived;
                        if (h != null)
                        {
                            try
                            {
                                h(this, new UdpDatagramEventArgs(Data.RemoteEndPoint, Data.Buffer));
                            }
                            catch (Exception ex)
                            {
                                Log.Critical(ex);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Critical(ex);
            }
        }
Esempio n. 2
0
        private void EndReceiveUdp(IAsyncResult ar)
        {
            try
            {
                if (!(this.udpClient is null))
                {
                    IPEndPoint RemoteEndpoint = null;

                    byte[] Data = this.udpClient.EndReceive(ar, ref RemoteEndpoint);
                    if (!(RemoteEndpoint is null) && !(Data is null))
                    {
                        UdpDatagramEvent h = this.OnUdpDatagramReceived;
                        if (!(h is null))
                        {
                            try
                            {
                                h(this, new UdpDatagramEventArgs(RemoteEndpoint, Data));
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                                Debug.WriteLine(ex.StackTrace.ToString());
                            }
                        }
                    }

                    this.udpClient.BeginReceive(this.EndReceiveUdp, null);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace.ToString());
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Sends an UDP datagram to a remote destination.
        /// </summary>
        /// <param name="RemoteEndpoint">Remote endpoint of destination.</param>
        /// <param name="Datagram">UDP Datagram to send.</param>
        public async void SendUdp(IPEndPoint RemoteEndpoint, byte[] Datagram)
        {
            lock (this.writeQueue)
            {
                if (this.isWriting)
                {
                    this.writeQueue.AddLast(new KeyValuePair <IPEndPoint, byte[]>(RemoteEndpoint, Datagram));
                    return;
                }
                else
                {
                    this.isWriting = true;
                }
            }

            try
            {
                while (!this.disposed && Datagram != null)
                {
                    await this.udpClient.SendAsync(Datagram, Datagram.Length, RemoteEndpoint);

                    UdpDatagramEvent h = this.OnUdpDatagramSent;
                    if (h != null)
                    {
                        try
                        {
                            h(this, new UdpDatagramEventArgs(RemoteEndpoint, Datagram));
                        }
                        catch (Exception ex)
                        {
                            Log.Critical(ex);
                        }
                    }

                    lock (this.writeQueue)
                    {
                        if (this.writeQueue.First != null)
                        {
                            KeyValuePair <IPEndPoint, byte[]> Rec = this.writeQueue.First.Value;
                            this.writeQueue.RemoveFirst();

                            RemoteEndpoint = Rec.Key;
                            Datagram       = Rec.Value;
                        }
                        else
                        {
                            this.isWriting = false;
                            Datagram       = null;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                lock (this.writeQueue)
                {
                    this.isWriting = false;
                    this.writeQueue.Clear();
                }

                Log.Critical(ex);
            }
        }