예제 #1
0
        private void process_receive_buffer(int bytes)
        {
            for (int offset = 0; offset < bytes;)
            {
                ICoder coder      = session.GetCoder();
                UInt32 header_len = coder.GetHeaderLen();
                UInt32 body_len   = 0;
                UInt32 total_len  = 0;

                if (loop_buffer.LastFlag)
                {
                    Array.Copy(loop_buffer.Buffer, 0, loop_buffer.LastBuffer, loop_buffer.LastSize, bytes);
                    int old_lastsize = loop_buffer.LastSize;
                    loop_buffer.LastSize += bytes;

                    if (loop_buffer.LastSize < header_len)
                    {
                        Log.DebugAf("[Net] [Connection] ConnID={0} ProcessReceiveBuffer Lastbuffer Not Enough Header Length", conn_id);
                        return;
                    }

                    bool error_flag = coder.GetBodyLen(loop_buffer.LastBuffer, out body_len);
                    if (error_flag == false)
                    {
                        Log.ErrorAf("[Net] [Connection] ConnID={0} ProcessReceiveBuffer GetBodyLen Error", conn_id);
                        return;
                    }

                    total_len = header_len + body_len;
                    if (total_len > NetDef.MSG_BUFF_SIZE)
                    {
                        Log.ErrorAf("[Net] [Connection] ConnID={0} ProcessReceiveBuffer Lastbuffer Greater Than Package Length", conn_id);
                        return;
                    }

                    if (total_len > loop_buffer.LastBuffer.Length)
                    {
                        Log.DebugAf("[Net] [Connection] ConnID={0} ProcessReceiveBuffer Lastbuffer Not Enough One Package", conn_id);
                        return;
                    }

                    process_one_packet(loop_buffer.LastBuffer, (int)header_len, body_len);

                    int diff_len = (int)(total_len - old_lastsize);
                    offset += diff_len;
                    loop_buffer.AddReadIndex(diff_len);
                    loop_buffer.ResetLastBuffer();

                    continue;
                }

                if ((offset + header_len) > bytes)
                {
                    Log.Info("[Net] [Connection] ConnID={0} ProcessReceiveBuffer Buffer Last Package Not Enough Header");
                    loop_buffer.LastFlag = true;
                    loop_buffer.LastSize = bytes - offset;
                    Array.Copy(loop_buffer.Buffer, offset, loop_buffer.LastBuffer, 0, loop_buffer.LastSize);
                    return;
                }

                bool body_error_flag = coder.GetBodyLen(loop_buffer.Buffer, out body_len);
                if (body_error_flag == false)
                {
                    Log.ErrorAf("[Net] [Connection] ConnID={0} ProcessReceiveBuffer GetBodyLen Error", conn_id);
                    return;
                }

                total_len = header_len + body_len;

                if (total_len == (bytes - offset))
                {
                    process_one_packet(loop_buffer.Buffer, offset + (int)header_len, body_len);
                    return;
                }
                else if (total_len > (bytes - offset))
                {
                    Log.DebugAf("[Net] [Connection] ConnID={0} ProcessReceiveBuffer Buffer Last Package Not Enough Package", conn_id);
                    loop_buffer.LastFlag = true;
                    loop_buffer.LastSize = bytes - offset;
                    Array.Copy(loop_buffer.Buffer, offset, loop_buffer.LastBuffer, 0, loop_buffer.LastSize);
                    return;
                }
                else if (total_len < (bytes - offset))
                {
                    process_one_packet(loop_buffer.Buffer, offset + (int)header_len, body_len);
                    offset += (int)total_len;
                    loop_buffer.AddReadIndex((int)total_len);
                }
            }
        }