System.Net.Sockets.Socket.EndReceive C# (CSharp) Method

EndReceive() public method

public EndReceive ( IAsyncResult asyncResult, SocketError &errorCode ) : int
asyncResult IAsyncResult
errorCode SocketError
return int
        public int EndReceive(IAsyncResult asyncResult, out SocketError errorCode)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, asyncResult);

            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Validate input parameters.
            if (asyncResult == null)
            {
                throw new ArgumentNullException(nameof(asyncResult));
            }

            OverlappedAsyncResult castedAsyncResult = asyncResult as OverlappedAsyncResult;
            if (castedAsyncResult == null || castedAsyncResult.AsyncObject != this)
            {
                throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult));
            }
            if (castedAsyncResult.EndCalled)
            {
                throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, "EndReceive"));
            }

            int bytesTransferred = castedAsyncResult.InternalWaitForCompletionInt32Result();
            castedAsyncResult.EndCalled = true;

            if (s_perfCountersEnabled)
            {
                if (bytesTransferred > 0)
                {
                    SocketPerfCounter.Instance.Increment(SocketPerfCounterName.SocketBytesReceived, bytesTransferred);
                    if (Transport == TransportType.Udp)
                    {
                        SocketPerfCounter.Instance.Increment(SocketPerfCounterName.SocketDatagramsReceived);
                    }
                }
            }

#if TRACE_VERBOSE
            if (NetEventSource.IsEnabled)
            {
                try
                {
                    if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"SRC:{LocalEndPoint} DST:{RemoteEndPoint} bytesTransferred:{bytesTransferred}");
                }
                catch (ObjectDisposedException) { }
            }
#endif

            // Throw an appropriate SocketException if the native call failed asynchronously.
            errorCode = (SocketError)castedAsyncResult.ErrorCode;
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                UpdateStatusAfterSocketError(errorCode);
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Error(this, new SocketException((int)errorCode));
                    NetEventSource.Exit(this, 0);
                }
                return 0;
            }
            if (NetEventSource.IsEnabled) NetEventSource.Exit(this, bytesTransferred);
            return bytesTransferred;
        }

Same methods

Socket::EndReceive ( IAsyncResult asyncResult ) : int

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Gets triggered when the socket receives something
        /// </summary>
        /// <param name="ar">async result</param>
        private void OnReceiveCallback(IAsyncResult ar)
        {
            try
            {
                if (_socket == null)
                {
                    return;
                }

                var bytesRead = _socket.EndReceive(ar);

                if (bytesRead <= 0)
                {
                    Close();
                    return;
                }

                /**
                 * I thought the problem was this line because it triggers the command handler without checking if the packet
                 *   is "complete" it only checks if "its not null"
                 */
                //Console.WriteLine("BytesRead: {0} | Packet Length: {1}", bytesRead, BitConverter.ToInt16(new byte[] {_readBuffer[1], _readBuffer[0]}, 0));
                //if (BitConverter.ToInt16(new byte[] {_readBuffer[1], _readBuffer[0]}, 0) + 2 == bytesRead)
                //{
                //    OnReceiveData(_readBuffer);
                //}// what happens if it's incomplete just ignores it and waits for the next packet.. i'm not sure if that should work as expected
                OnReceiveData(_readBuffer);

                //Setup over
                _readBuffer = new byte[BufferSize];
                _socket.BeginReceive(_readBuffer, 0, _readBuffer.Length, 0, OnReceiveCallback, this);
            }
            catch { Close(); }
        }
All Usage Examples Of System.Net.Sockets.Socket::EndReceive