Ejemplo n.º 1
0
        public bool SendAsyncStart(byte[] buffer, int offset, int size)
        {
            if (m_stream == null || m_sock == null || !m_sock.Connected)
            {
                return(false);
            }

            if (offset + size > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset", offset, "offset + size is beyond end of buffer.");
            }

            bool ok = true;

            ContextTimeoutManager.ContextEnterActiveSend();
            try
            {
                m_stream.BeginWrite(buffer, offset, size, SendAsyncEnd, null);
            }
            catch (Exception e)
            {
                e.GetHashCode();
                ContextTimeoutManager.ContextLeaveActiveSend();
                ok = false;
            }

            if (!ok && m_stream != null)
            {
                Disconnect(SocketError.NoRecovery);
            }
            return(ok);
        }
Ejemplo n.º 2
0
        public async Task <bool> SendAsync(byte[] buffer, int offset, int size)
        {
            if (m_stream == null || m_sock == null || !m_sock.Connected)
            {
                return(false);
            }

            if (offset + size > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset", offset, "offset + size is beyond end of buffer.");
            }

            bool ok = true;

            ContextTimeoutManager.ContextEnterActiveSend();
            try
            {
                await m_stream.WriteAsync(buffer, offset, size).ConfigureAwait(false);
            }
            catch
            {
                ok = false;
            }

            ContextTimeoutManager.ContextLeaveActiveSend();

            if (!ok && m_stream != null)
            {
                Disconnect(SocketError.NoRecovery);
            }
            return(ok);
        }
Ejemplo n.º 3
0
        public async Task EndSendResponse(uint requestID, ConnectionType ctype)
        {
            isSendingResponse = false;
            m_currentResponse?.Clear();
            m_currentResponse = null;

            bool doclose = ctype == ConnectionType.Close;

            if (doclose)
            {
                m_isClosing = true;
                m_requests.Clear();
                TriggerKeepalive = true;
                return;
            }
            else
            {
                LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();
                if (Stream != null && Stream.CanWrite)
                {
                    ContextTimeoutManager.ContextEnterActiveSend();
                    try
                    {
                        await Stream.FlushAsync().ConfigureAwait(false);
                    }
                    catch
                    {
                    };
                    ContextTimeoutManager.ContextLeaveActiveSend();
                }

                if (Stream == null || !Stream.CanWrite)
                {
                    return;
                }

                TriggerKeepalive = true;
                lock (m_requestsLock)
                {
                    m_waitingResponse = false;
                    if (m_requests != null && m_requests.Count > 0)
                    {
                        HttpRequest nextRequest = m_requests.Dequeue();
                        if (nextRequest != null)
                        {
                            m_waitingResponse = true;
                            RequestReceived?.Invoke(this, new RequestEventArgs(nextRequest));
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public bool Send(byte[] buffer, int offset, int size)
        {
            if (m_stream == null || m_sock == null || !m_sock.Connected)
            {
                return(false);
            }

            if (offset + size > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset", offset, "offset + size is beyond end of buffer.");
            }

            LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();

            bool ok = true;

            ContextTimeoutManager.ContextEnterActiveSend();
            lock (sendLock) // can't have overlaps here
            {
                try
                {
                    m_stream.Write(buffer, offset, size);
                }
                catch
                {
                    ok = false;
                }
            }

            ContextTimeoutManager.ContextLeaveActiveSend();
            if (!ok && m_stream != null)
            {
                Disconnect(SocketError.NoRecovery);
            }
            return(ok);
        }