Exemple #1
0
        protected override void BeginRequest(NetworkRequestArgs eventArgs)
        {
            var asyncContinuation = eventArgs.AsyncContinuation;
            var bytes             = eventArgs.RequestBuffer;
            var offset            = eventArgs.RequestBufferOffset;
            var length            = eventArgs.RequestBufferLength;

            var webRequest = HttpRequestFactory.CreateWebRequest(_addressUri);

            webRequest.Method = "POST";

            AsyncCallback onResponse =
                r =>
            {
                try
                {
                    using (var response = webRequest.EndGetResponse(r))
                    {
                        // Response successfully read
                    }

                    // completed fine
                    base.EndRequest(asyncContinuation, null);
                }
                catch (Exception ex)
                {
                    if (ex.MustBeRethrownImmediately())
                    {
                        throw;     // Throwing exceptions here will crash the entire application (.NET 2.0 behavior)
                    }

                    base.EndRequest(_ => asyncContinuation(ex), null);        // pendingException = null to keep sender alive
                }
            };

            AsyncCallback onRequestStream =
                r =>
            {
                try
                {
                    using (var stream = webRequest.EndGetRequestStream(r))
                    {
                        stream.Write(bytes, offset, length);
                    }

                    webRequest.BeginGetResponse(onResponse, null);
                }
                catch (Exception ex)
                {
                    if (ex.MustBeRethrownImmediately())
                    {
                        throw;      // Throwing exceptions here will crash the entire application (.NET 2.0 behavior)
                    }

                    base.EndRequest(_ => asyncContinuation(ex), null);        // pendingException = null to keep sender alive
                }
            };

            webRequest.BeginGetRequestStream(onRequestStream, null);
        }
Exemple #2
0
        protected override void BeginRequest(NetworkRequestArgs eventArgs)
        {
            var args = new MySocketAsyncEventArgs();

            args.SetBuffer(eventArgs.RequestBuffer, eventArgs.RequestBufferOffset, eventArgs.RequestBufferLength);
            args.UserToken  = eventArgs.AsyncContinuation;
            args.Completed += _socketOperationCompleted;

            bool asyncOperation = false;

            try
            {
                asyncOperation = _socket.SendAsync(args);
            }
            catch (SocketException ex)
            {
                InternalLogger.Error(ex, "NetworkTarget: Error sending tcp request");
                args.SocketError = ex.SocketErrorCode;
            }
            catch (Exception ex)
            {
                InternalLogger.Error(ex, "NetworkTarget: Error sending tcp request");
                if (ex.InnerException is SocketException socketException)
                {
                    args.SocketError = socketException.SocketErrorCode;
                }
                else
                {
                    args.SocketError = SocketError.OperationAborted;
                }
            }

            if (!asyncOperation)
            {
                SocketOperationCompleted(_socket, args);
            }
        }
Exemple #3
0
        /// <summary>
        /// Actually sends the given text over the specified protocol.
        /// </summary>
        /// <param name="bytes">The bytes to be sent.</param>
        /// <param name="offset">Offset in buffer.</param>
        /// <param name="length">Number of bytes to send.</param>
        /// <param name="asyncContinuation">The async continuation to be invoked after the buffer has been sent.</param>
        /// <remarks>To be overridden in inheriting classes.</remarks>
        protected override void DoSend(byte[] bytes, int offset, int length, AsyncContinuation asyncContinuation)
        {
            NetworkRequestArgs eventArgs = new NetworkRequestArgs(bytes, offset, length, asyncContinuation);

            lock (_pendingRequests)
            {
                if (MaxQueueSize > 0 && _pendingRequests.Count >= MaxQueueSize)
                {
                    var dequeued = _pendingRequests.Dequeue();
                    dequeued.AsyncContinuation?.Invoke(null);
                }

                if (!_asyncOperationInProgress && _pendingError == null)
                {
                    _asyncOperationInProgress = true;
                    BeginRequest(eventArgs);
                    return;
                }

                _pendingRequests.Enqueue(eventArgs);
            }

            ProcessNextQueuedItem();
        }
Exemple #4
0
 protected abstract void BeginRequest(NetworkRequestArgs eventArgs);