All pastes #266810 Raw Edit

dr oetker

public text v1 · immutable
·published 2006-12-04 11:43 UTC
rendered paste body
private void ReadResponse_Callback(IAsyncResult iar)
        {
            InputOutput io = (InputOutput)iar.AsyncState;
            int recv = io.sNetworkStream.EndRead(iar);
            Boolean again = false;
            if (io.iContentLength == 0)
            {
                string message = Encoding.ASCII.GetString(io.data, 0, recv);
                io.sHeader += message;
                if (io.sHeader.Contains("\r\n\r\n"))
                {
                    if (io.sHeader.Contains("Content-Length"))
                    {
                        io.iContentLength = Convert.ToInt32(Regex.Match(io.sHeader, "Content-Length: ([\\d\\-]+)").Groups[1].Value);
                        int headerend = io.sHeader.IndexOf("\r\n\r\n");
                        headerend += 4;
                        if (io.iContentLength == (io.sHeader.Length - headerend))
                        {
                            again = false;
                            io.cNetworkStream.Write(io.data, 0, recv);
                            io.data = new byte[io.data.Length];
                            io.cNetworkStream.Close();
                            io.cTcpClient.Close();
                            AddText(io.sHeader);
                            io.sHeader = "";
                        }
                        else
                        {
                            io.iContentLengthCurrent += recv - headerend;
                            again = true;
                        }
                    }
                    else
                    {
                        again = false;
                        AddText(io.sHeader);
                        io.cNetworkStream.Write(io.data, 0, recv);
                        io.data = new byte[io.data.Length];
                        io.cNetworkStream.Close();
                        io.cTcpClient.Close();
                        io.sHeader = "";
                    }
                }
                else
                {
                    io.iContentLengthCurrent += io.sHeader.Length;
                    again = true;
                }
            }
            else
            {
                io.iContentLengthCurrent += recv;
                if (io.iContentLengthCurrent == io.iContentLength)
                {
                    AddText(io.sHeader);
                    again = false;
                    io.cNetworkStream.Write(io.data, 0, recv);
                    io.data = new byte[io.data.Length];
                    io.cNetworkStream.Close();
                    io.cTcpClient.Close();
                    io.sHeader = "";
                }
                else again = true;
            }

            if (again)
            {
                io.cNetworkStream.Write(io.data, 0, recv);
                io.data = new byte[io.data.Length];
                io.sNetworkStream.BeginRead(io.data, 0, io.data.Length, new AsyncCallback(ReadResponse_Callback), io);
            }
            else
            {
                AddText("System message: Finished client.");
            }
        }