// File: lotuskip[P]yuxtapa/client/network.cpp
// Function: Network::receive_n_handle()
bool Network::receive_n_handle()
{
	unsigned char mid;
	char handled = 0;
	short msglen;
	while(handled++ < 5 && // handle at most 5 messages; then return control
		(msglen = do_receive()) != -1)
	{
		mid = recv_buffer.read_ch();
		switch(mid)
		{
		case MID_QUIT:
			return true;
		case MID_VIEW_UPD:
		{
			curturn = recv_buffer.read_sh(); // new turn number
			// NOTE: if an earlier turn update should arrive later than a
			// newer one, we might "fall back". This is rare enough to
			// bypass, hopefully.
			axn_counter = 0;

			// Return a "pong":
			send_buffer.clear();
			send_buffer.add((unsigned char)MID_VIEW_UPD);
			send_buffer.add(cur_id);
			send_buffer.add(curturn);
			do_send();

			if(msglen > 5) // msg is not very small => view has changed
			{
				if(recv_buffer.read_compressed(viewbuffer))
					break; // could not read package, ignore it...
				if(clientstate != CS_LIMBO && clientstate != CS_HELP
					&& clientstate != CS_TEAM_INFO)
					redraw_view();
			}
			Base::viewtick();
			break;
		}
		case MID_ADD_MSG:
		{
			unsigned char cp = recv_buffer.read_ch();
			string msg_str;
			for(mid = recv_buffer.read_ch(); mid > 0; --mid)
			{
				recv_buffer.read_str(msg_str);
				add_msg(msg_str, cp);
			}
			break;
		}
		case MID_SAY_CHAT:
		{
			recv_buffer.read_sh(); // skip id
			string line;
			recv_buffer.read_str(line);
			string speaker_nick;
			recv_buffer.read_str(speaker_nick);
			add_to_chat(line, speaker_nick, recv_buffer.read_ch());
			break;
		}
		case MID_STATE_UPD:
			ClassCPV::state_upd(recv_buffer);
			break;
		case MID_STATE_CHANGE:
			mid = recv_buffer.read_ch(); // class
			ClassCPV::state_change(mid, recv_buffer.read_ch()); // and team
			break;
		case MID_GAME_UPD:
		{
			mid = recv_buffer.read_ch(); // # of green
			unsigned char ch = recv_buffer.read_ch(); // # of purples
			string st_str1, st_str2;
			recv_buffer.read_str(st_str1);
			recv_buffer.read_str(st_str2);
			Base::print_teams_upd(mid, ch, st_str1 += st_str2);	
			break;
		}
		case MID_FLAG_UPD:
		{
			string tmp;
			for(char ch = 0; ch < MAX_D+1; ++ch)
				tmp += recv_buffer.read_ch();
			Base::print_flags(tmp);
			break;
		}
		case MID_TIME_UPD:
		{
			unsigned short sh = recv_buffer.read_sh();
			clocksync(sh, recv_buffer.read_ch());
			break;
		}
		case MID_TEAM_INFO:
			// This is a respond to player's request, so view the info now
			if(recv_buffer.read_compressed(teambuffer))
				break;// corrupt package? Ignore and don't switch cs
			draw_team_info();
			clientstate = CS_TEAM_INFO;
			Base::type_cursor(0);
			break;
		default: break;
		}
	}
	return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movb $0x0, -0x3(%rbp)
movb -0x3(%rbp), %al
movb %al, %cl
addb $0x1, %cl
movb %cl, -0x3(%rbp)
movsbl %al, %ecx
xorl %eax, %eax
cmpl $0x5, %ecx
movb %al, -0xe5(%rbp)
jge 0x13d10
callq 0x13790
movw %ax, -0x6(%rbp)
cwtl
cmpl $-0x1, %eax
setne %al
movb %al, -0xe5(%rbp)
movb -0xe5(%rbp), %al
testb $0x1, %al
jne 0x13d1f
jmp 0x141f0
leaq 0x155a2(%rip), %rdi     # 0x292c8
callq 0x19ea0
movb %al, -0x2(%rbp)
movzbl -0x2(%rbp), %eax
addl $-0x2, %eax
movl %eax, %ecx
movq %rcx, -0xf0(%rbp)
subl $0xd, %eax
ja 0x141e9
movq -0xf0(%rbp), %rax
leaq 0x8913(%rip), %rcx      # 0x1c668
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, -0x1(%rbp)
jmp 0x141f4
leaq 0x1555a(%rip), %rdi     # 0x292c8
callq 0x19e60
movw %ax, 0x16640(%rip)      # 0x2a3ba
movb $0x0, 0x1663b(%rip)     # 0x2a3bc
leaq 0x15d50(%rip), %rdi     # 0x29ad8
callq 0x19c70
leaq 0x15d44(%rip), %rdi     # 0x29ad8
movl $0x3, %esi
callq 0x19c90
leaq 0x15d33(%rip), %rdi     # 0x29ad8
movzwl 0x1660c(%rip), %esi     # 0x2a3b8
callq 0x19cd0
leaq 0x15d20(%rip), %rdi     # 0x29ad8
movzwl 0x165fb(%rip), %esi     # 0x2a3ba
callq 0x19cd0
callq 0x13720
movswl -0x6(%rbp), %eax
cmpl $0x5, %eax
jle 0x13e1b
leaq 0x154ef(%rip), %rdi     # 0x292c8
leaq 0x16a40(%rip), %rsi     # 0x2a820
callq 0x19f90
testb $0x1, %al
jne 0x13deb
jmp 0x13df0
jmp 0x141eb
leaq 0x151e9(%rip), %rax     # 0x28fe0
cmpl $0x3, (%rax)
je 0x13e19
leaq 0x151dd(%rip), %rax     # 0x28fe0
cmpl $0x4, (%rax)
je 0x13e19
leaq 0x151d1(%rip), %rax     # 0x28fe0
cmpl $0x5, (%rax)
je 0x13e19
callq 0x19160
jmp 0x13e1b
callq 0x9a00
jmp 0x141eb
leaq 0x1549c(%rip), %rdi     # 0x292c8
movq %rdi, -0x100(%rbp)
callq 0x19ea0
movb %al, -0x7(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x56f0
movq -0x100(%rbp), %rdi
callq 0x19ea0
movb %al, -0xf1(%rbp)
jmp 0x13e58
movb -0xf1(%rbp), %al
movb %al, -0x2(%rbp)
movzbl -0x2(%rbp), %eax
cmpl $0x0, %eax
jle 0x13eb1
leaq 0x15457(%rip), %rdi     # 0x292c8
leaq -0x28(%rbp), %rsi
callq 0x19ed0
jmp 0x13e7c
movzbl -0x7(%rbp), %esi
leaq -0x28(%rbp), %rdi
callq 0xe180
jmp 0x13e8b
jmp 0x13e8d
movb -0x2(%rbp), %al
addb $-0x1, %al
movb %al, -0x2(%rbp)
jmp 0x13e61
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x5968
jmp 0x14202
leaq -0x28(%rbp), %rdi
callq 0x5968
jmp 0x141eb
leaq 0x15402(%rip), %rdi     # 0x292c8
movq %rdi, -0x110(%rbp)
callq 0x19e60
leaq -0x58(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x56f0
movq -0x110(%rbp), %rdi
movq -0x108(%rbp), %rsi
callq 0x19ed0
jmp 0x13ef7
leaq -0x78(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x56f0
movq -0x118(%rbp), %rsi
leaq 0x153b3(%rip), %rdi     # 0x292c8
callq 0x19ed0
jmp 0x13f1c
leaq 0x153a5(%rip), %rdi     # 0x292c8
callq 0x19ea0
movb %al, -0x119(%rbp)
jmp 0x13f30
movb -0x119(%rbp), %al
movzbl %al, %edx
leaq -0x58(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xec80
jmp 0x13f48
leaq -0x78(%rbp), %rdi
callq 0x5968
leaq -0x58(%rbp), %rdi
callq 0x5968
jmp 0x141eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x13f82
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x5968
leaq -0x58(%rbp), %rdi
callq 0x5968
jmp 0x14202
leaq 0x15331(%rip), %rdi     # 0x292c8
callq 0xb5f0
jmp 0x141eb
leaq 0x15320(%rip), %rdi     # 0x292c8
callq 0x19ea0
movb %al, -0x2(%rbp)
movb -0x2(%rbp), %al
movb %al, -0x11a(%rbp)
leaq 0x15308(%rip), %rdi     # 0x292c8
callq 0x19ea0
movb -0x11a(%rbp), %cl
movzbl %cl, %edi
movzbl %al, %esi
callq 0xb470
jmp 0x141eb
leaq 0x152e6(%rip), %rdi     # 0x292c8
movq %rdi, -0x130(%rbp)
callq 0x19ea0
movq -0x130(%rbp), %rdi
movb %al, -0x2(%rbp)
callq 0x19ea0
movb %al, -0x79(%rbp)
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x56f0
leaq -0xc0(%rbp), %rdi
callq 0x56f0
movq -0x130(%rbp), %rdi
movq -0x128(%rbp), %rsi
callq 0x19ed0
jmp 0x14034
leaq 0x1528d(%rip), %rdi     # 0x292c8
leaq -0xc0(%rbp), %rsi
callq 0x19ed0
jmp 0x14049
movb -0x2(%rbp), %al
movb %al, -0x13a(%rbp)
movb -0x79(%rbp), %al
movb %al, -0x139(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x5670
movq %rax, -0x138(%rbp)
jmp 0x14077
movq -0x138(%rbp), %rdx
movb -0x139(%rbp), %al
movb -0x13a(%rbp), %cl
movzbl %cl, %edi
movzbl %al, %esi
callq 0x9b40
jmp 0x14097
leaq -0xc0(%rbp), %rdi
callq 0x5968
leaq -0xa0(%rbp), %rdi
callq 0x5968
jmp 0x141eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x5968
leaq -0xa0(%rbp), %rdi
callq 0x5968
jmp 0x14202
leaq -0xe0(%rbp), %rdi
callq 0x56f0
movb $0x0, -0xe1(%rbp)
movsbl -0xe1(%rbp), %eax
cmpl $0x9, %eax
jge 0x14156
leaq 0x151c5(%rip), %rdi     # 0x292c8
callq 0x19ea0
movb %al, -0x13b(%rbp)
jmp 0x14110
movb -0x13b(%rbp), %al
movsbl %al, %esi
leaq -0xe0(%rbp), %rdi
callq 0x52f0
jmp 0x14127
jmp 0x14129
movb -0xe1(%rbp), %al
addb $0x1, %al
movb %al, -0xe1(%rbp)
jmp 0x140f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x5968
jmp 0x14202
leaq -0xe0(%rbp), %rdi
callq 0x9e00
jmp 0x14164
leaq -0xe0(%rbp), %rdi
callq 0x5968
jmp 0x141eb
leaq 0x1514f(%rip), %rdi     # 0x292c8
callq 0x19e60
movw %ax, -0xe4(%rbp)
movw -0xe4(%rbp), %ax
movw %ax, -0x13e(%rbp)
leaq 0x1512e(%rip), %rdi     # 0x292c8
callq 0x19ea0
movw -0x13e(%rbp), %cx
movzwl %cx, %edi
movzbl %al, %esi
callq 0xf5a0
jmp 0x141eb
leaq 0x1510e(%rip), %rdi     # 0x292c8
leaq 0x16e5f(%rip), %rsi     # 0x2b020
callq 0x19f90
testb $0x1, %al
jne 0x141cc
jmp 0x141ce
jmp 0x141eb
callq 0x19790
leaq 0x14e06(%rip), %rax     # 0x28fe0
movl $0x5, (%rax)
xorl %edi, %edi
callq 0x99d0
jmp 0x141eb
jmp 0x141eb
jmp 0x13cdf
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x5830
nopl (%rax,%rax)