コード例 #1
0
            public ReceiveFromAsyncResult(Socket socket, ArraySegment <byte> buffer, EndPoint remoteEndPoint, int messageSize, int timeToLive, AsyncCallback userCallback, object userState) :
                base(userCallback, userState)
            {
                this.RemoteEndPoint = remoteEndPoint;
                this.MessageSize    = messageSize;
                this.socket         = socket;
                this.Buffer         = buffer;
                this.TimeToLive     = timeToLive;

                ArraySegment <byte> data = default(ArraySegment <byte>);

                try
                {
                    IAsyncResult socketAsyncResult = this.socket.BeginReceiveFrom(this.Buffer.Array,
                                                                                  this.Buffer.Offset,
                                                                                  this.Buffer.Count,
                                                                                  SocketFlags.None,
                                                                                  ref remoteEndPoint,
                                                                                  onReceiveMessageFromCallback,
                                                                                  this);

                    if (!socketAsyncResult.CompletedSynchronously)
                    {
                        return;
                    }

                    data = EndReceiveFrom(socketAsyncResult);
                }
                catch (SocketException socketException)
                {
                    throw FxTrace.Exception.AsError(UdpSocket.ConvertNetworkError(socketException, this));
                }

                Complete(data, true);
            }
コード例 #2
0
            static void OnReceiveMessageFrom(IAsyncResult result)
            {
                if (result.CompletedSynchronously)
                {
                    return;
                }

                ReceiveFromAsyncResult asyncResult = (ReceiveFromAsyncResult)result.AsyncState;

                Exception           completionException = null;
                ArraySegment <byte> data = default(ArraySegment <byte>);

                try
                {
                    data = asyncResult.EndReceiveFrom(result);
                }
                catch (SocketException socketException)
                {
                    completionException = UdpSocket.ConvertNetworkError(socketException, asyncResult);
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    completionException = exception;
                }

                if (completionException != null)
                {
                    asyncResult.Complete(false, completionException);
                }
                else
                {
                    asyncResult.Complete(data, false);
                }
            }