Exemplo n.º 1
0
 public UdpSendErrorData(
     UdpOutgoingPacket packet,
     int numberOfBytesWrittenToTheSendBuffer,
     Exception exception) : base(exception)
 {
     this.Packet = packet;
     this.NumberOfBytesWrittenToTheSendBuffer = numberOfBytesWrittenToTheSendBuffer;
 }
Exemplo n.º 2
0
 public UdpSendErrorEventArgs(
     UdpOutgoingPacket packet,
     int numberOfBytesWrittenToTheSendBuffer,
     Exception exception)
 {
     this.Packet = packet;
     this.NumberOfBytesWrittenToTheSendBuffer = numberOfBytesWrittenToTheSendBuffer;
     this.Exception = exception;
 }
Exemplo n.º 3
0
        protected virtual async Task SendPacketAsync(UdpOutgoingPacket packet)
        {
            int numberOfBytesSent;

            try
            {
                if (packet.Buffer.Offset == 0)
                {
                    numberOfBytesSent = await this.UdpClient.SendWithCancellationTokenAsync(packet.Buffer.Memory, packet.Buffer.Count, packet.RemoteEndPoint, packet.CancellationToken).ConfigureAwait(false);
                }
                else
                {
                    var bytes = packet.Buffer.ToBytes();

                    numberOfBytesSent = await this.UdpClient.SendWithCancellationTokenAsync(bytes, bytes.Length, packet.RemoteEndPoint, packet.CancellationToken).ConfigureAwait(false);
                }

                if (numberOfBytesSent != packet.Buffer.Count)
                {
                    packet.SendTaskCompletionSource.TrySetResult(false);

                    this.OnUdpSendErrorOccured(new UdpSendErrorEventArgs(packet, numberOfBytesSent, null));
                }
                else
                {
                    packet.SendTaskCompletionSource.TrySetResult(true);
                }
            }

#if NET45
            catch (OperationCanceledException)
            {
                packet.SendTaskCompletionSource.TrySetCanceled();
            }
#else
            catch (OperationCanceledException ex)
            {
                packet.SendTaskCompletionSource.TrySetCanceled(ex.CancellationToken);
            }
#endif
            catch (Exception ex)
            {
                this.OnUdpSendErrorOccured(new UdpSendErrorEventArgs(packet, 0, ex));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sends data asynchronously
        /// </summary>
        /// <param name="buffer">Buffer containing data to send</param>
        /// <param name="offset">Data offset in <paramref name="buffer" /></param>
        /// <param name="count">Numbers of bytes to send</param>
        /// <param name="remoteEndPoint">Client/peer endpoint</param>
        /// <param name="cancellationToken">Cancellation token for cancelling this operation</param>
        /// <returns>True - data was sent. False - server is stopped or underlying send buffer is full or <paramref name="cancellationToken" /> was cancelled</returns>
        public virtual async Task <bool> SendAsync(byte[] buffer, int offset, int count, IPEndPoint remoteEndPoint, CancellationToken cancellationToken)
        {
            bool result;

            using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(this.CancellationToken, cancellationToken))
            {
                var packet = new UdpOutgoingPacket(remoteEndPoint, new Core.AsyncNetBuffer(buffer, offset, count), linkedCts.Token);

                try
                {
                    result = await this.SendQueueActionBlock.SendAsync(
                        packet,
                        linkedCts.Token).ConfigureAwait(false);

                    if (!result)
                    {
                        return(result);
                    }
#if NET45
                    using (linkedCts.Token.Register(() => packet.SendTaskCompletionSource.TrySetCanceled()))
#else
                    using (linkedCts.Token.Register(() => packet.SendTaskCompletionSource.TrySetCanceled(linkedCts.Token)))
#endif
                    {
                        result = await packet.SendTaskCompletionSource.Task.ConfigureAwait(false);
                    }
                }
                catch (OperationCanceledException)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        if (!this.CancellationToken.IsCancellationRequested)
                        {
                            throw;
                        }
                    }

                    result = false;
                }
            }

            return(result);
        }