Fun.FunapiTcpTransport.ReceiveBytesCb C# (CSharp) Method

ReceiveBytesCb() private method

private ReceiveBytesCb ( IAsyncResult ar ) : void
ar IAsyncResult
return void
        private void ReceiveBytesCb(IAsyncResult ar)
        {
            DebugUtils.DebugLog("ReceiveBytesCb called.");

            try
            {
                if (sock_ == null)
                {
                    last_error_code_ = ErrorCode.kReceiveFailed;
                    last_error_message_ = "sock is null.";
                    DebugUtils.Log(last_error_message_);
                    return;
                }

                lock (receive_lock_)
                {
                    int nRead = sock_.EndReceive(ar);
                    if (nRead > 0)
                    {
                        received_size_ += nRead;
                        DebugUtils.DebugLog("Received {0} bytes. Buffer has {1} bytes.",
                                            nRead, received_size_ - next_decoding_offset_);
                    }

                    // Decoding a messages
                    TryToDecodeMessage();

                    if (nRead > 0)
                    {
                        // Checks buffer space
                        CheckReceiveBuffer();

                        // Starts another async receive
                        ArraySegment<byte> residual = new ArraySegment<byte>(receive_buffer_, received_size_, receive_buffer_.Length - received_size_);
                        List<ArraySegment<byte>> buffer = new List<ArraySegment<byte>>();
                        buffer.Add(residual);
                        sock_.BeginReceive(buffer, 0, new AsyncCallback(this.ReceiveBytesCb), this);
                        DebugUtils.DebugLog("Ready to receive more. We can receive upto {0} more bytes",
                                            receive_buffer_.Length - received_size_);

                        last_error_code_ = ErrorCode.kNone;
                        last_error_message_ = "";
                    }
                    else
                    {
                        DebugUtils.Log("Socket closed");
                        if (received_size_ - next_decoding_offset_ > 0)
                        {
                            DebugUtils.Log("Buffer has {0} bytes. But they failed to decode. Discarding.",
                                           receive_buffer_.Length - received_size_);
                        }

                        last_error_code_ = ErrorCode.kDisconnected;
                        last_error_message_ = "Can not receive messages. Maybe the socket is closed.";
                        DebugUtils.Log(last_error_message_);
                        AddToEventQueue(OnDisconnected);
                    }
                }
            }
            catch (ObjectDisposedException e)
            {
                DebugUtils.Log("BeginReceive operation has been Cancelled.");
                DebugUtils.DebugLog(e.ToString());
            }
            catch (NullReferenceException e)
            {
                // When Stop is called Socket.EndReceive may return a NullReferenceException
                DebugUtils.Log("BeginReceive operation has been Cancelled.");
                DebugUtils.DebugLog(e.ToString());
            }
            catch (Exception e)
            {
                last_error_code_ = ErrorCode.kReceiveFailed;
                last_error_message_ = "Failure in ReceiveBytesCb: " + e.ToString();
                DebugUtils.Log(last_error_message_);
                AddToEventQueue(OnFailure);
            }
        }