Exemplo n.º 1
0
        public void ParseAndAttachToBody(Tcp.TcpConnection connection, AsyncCallback callback)
        {
            // We are not worrying about onProgress, onTimeout, onError because the developer should
            // already have those listeners attached

            connection.ReceiveAsync(ParseAndAttachToBody_Callback, callback);
        }
        protected override void ParseAndAttachToBody_Callback(Tcp.TcpConnection sender, Tcp.TcpConnectionAsyncEventArgs e)
        {
            HttpNetworkStream ns;

            AsyncCallback callback = (AsyncCallback)e.UserToken;

            AppendAndParse(e.Buffer, 0, e.Length);
            if (AllHeadersReceived)
            {
                if (!Request.ContentLength.HasValue)
                {
                    throw new HttpNetworkStreamException("A Content-Length header was not found.");
                }

                ulong temp = (ulong)Request.ContentLength.Value;

                if (_remainingBufferAppendPosition > 0)
                {
                    // We need to take the left over buffer from _responseBuilder and prepend that
                    // to an HttpNetworkStream wrapping the _tcpConnection and then give the user
                    // that HttpNetworkStream... cake

                    byte[] newBuffer = new byte[_remainingBufferAppendPosition];

                    BytesReceived += e.BytesTransferred - newBuffer.Length;
                    MessageSize    = BytesReceived + Request.ContentLength.Value;
                    Buffer.BlockCopy(_remainingBuffer, 0, newBuffer, 0, newBuffer.Length);

                    ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Upload,
                                               temp, newBuffer, sender.Socket, System.IO.FileAccess.Write, false);
                }
                else
                {
                    BytesReceived += e.BytesTransferred;
                    MessageSize    = BytesReceived + Response.ContentLength.Value;

                    ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Upload,
                                               temp, sender.Socket, System.IO.FileAccess.Write, false);
                }

                if (Request.Headers.ContainsKey(new Message.ChunkedTransferEncodingHeader()))
                {
                    Request.Body.IsChunked = true;
                }

                Response.Body.ReceiveStream = ns;

                callback(this, Response);
            }
            else
            {
                BytesReceived += e.Length;
                sender.ReceiveAsync(ParseAndAttachToBody_Callback, callback);
            }
        }
Exemplo n.º 3
0
        private void ConnectAsync_OnHostResolved(HttpConnection sender, IPHostEntry host)
        {
            Tcp.Params.Connection param;
                        
            param = new Tcp.Params.Connection() 
            { 
                EndPoint = new IPEndPoint(_remoteHostEntry.AddressList[0], Uri.Port), 
                ReceiveBuffer = _receiveBufferSettings,
                SendBuffer = _sendBufferSettings
            };

            _tcpConnection = new Tcp.TcpConnection(param);

            _tcpConnection.OnConnect += new Tcp.TcpConnection.ConnectionDelegate(ConnectAsync_OnHostResolved_OnConnect);
            _tcpConnection.OnDisconnect += new Tcp.TcpConnection.ConnectionDelegate(ConnectAsync_OnHostResolved_OnDisconnect);
            _tcpConnection.OnError += new Tcp.TcpConnection.ErrorDelegate(ConnectAsync_OnHostResolved_OnError);
            _tcpConnection.OnTimeout += new Tcp.TcpConnection.ConnectionDelegate(ConnectAsync_OnHostResolved_OnTimeout);

            Logger.Network.Debug("HttpConnection ID: " + this.GetHashCode().ToString() + "\r\nTcpConnection ID: " + _tcpConnection.GetHashCode().ToString() + "\r\nHttpConnection establishing connection to remote host using TcpConnection.");

            _tcpConnection.ConnectAsync();
        }
Exemplo n.º 4
0
 protected abstract void ParseAndAttachToBody_Callback(Tcp.TcpConnection sender, Tcp.TcpConnectionAsyncEventArgs e);