Fun.FunapiHttpTransport.ReadCb C# (CSharp) Méthode

ReadCb() private méthode

private ReadCb ( IAsyncResult ar ) : void
ar IAsyncResult
Résultat void
        private void ReadCb(IAsyncResult ar)
        {
            DebugUtils.DebugLog("ReadCb called.");

            try
            {
                WebState ws = (WebState)ar.AsyncState;
                int nRead = ws.stream.EndRead(ar);

                if (nRead > 0)
                {
                    DebugUtils.DebugLog("We need more bytes for response. Waiting.");
                    if (ws.read_offset + nRead > ws.read_data.Length)
                    {
                        byte[] temp = new byte[ws.read_data.Length + kUnitBufferSize];
                        Buffer.BlockCopy(ws.read_data, 0, temp, 0, ws.read_offset);
                        ws.read_data = temp;
                    }

                    Buffer.BlockCopy(ws.buffer, 0, ws.read_data, ws.read_offset, nRead);
                    ws.read_offset += nRead;

                    ws.stream.BeginRead(ws.buffer, 0, ws.buffer.Length, new AsyncCallback(ReadCb), ws);
                }
                else
                {
                    if (ws.response == null)
                    {
                        DebugUtils.LogWarning("Response instance is null.");
                        AddToEventQueue(OnFailure);
                        return;
                    }

                    lock (receive_lock_)
                    {
                        // Decodes message
                        byte[] header = ws.response.Headers.ToByteArray();
                        string str_header = System.Text.Encoding.ASCII.GetString(header, 0, header.Length);
                        DecodeMessage(str_header, new ArraySegment<byte>(ws.read_data, 0, ws.read_offset));

                        ws.stream.Close();
                        ws.stream = null;
                        list_.Remove(ws);

                        ClearRequest();

                        // Sends unsent messages
                        SendUnsentMessages();
                    }
                }
            }
            catch (Exception e)
            {
                last_error_code_ = ErrorCode.kReceiveFailed;
                last_error_message_ = "Failure in ReadCb: " + e.ToString();
                DebugUtils.Log(last_error_message_);
                AddToEventQueue(OnFailure);
            }
        }