// File: roberty65[P]bServer/frame/main/Listener.cpp
// Function: beyondy::Async::Listener::onReadable(int)
int Listener::onReadable(int fd) {
	assert(this->fd == fd);

	int cfd = ::accept(fd, NULL, NULL);
	if (cfd < 0) {
		SYSLOG_WARN("listener(%s) accept failed: %m", address);
		return 0; // ignore
	}

	char peerName[128];
	if (beyondy::XbsGetPeername(cfd, peerName, sizeof(peerName)) < 0) {
		::close(cfd);
		SYSLOG_ERROR("listener(%s) new accepted fd=%d can not get peer name: %m", address, cfd);
		return 0; /* ignore (this is usually caused by peer closed immediately */
	}

	beyondy::XbsSetFlags(cfd, O_NONBLOCK, 0);
	if (connActive >= connMax) {
		::close(fd);
		SYSLOG_ERROR("listener(%s) reject connection from %s for overflow limit %d", address, peerName, connActive);
		return 0; // ignore
	}

	SYSLOG_DEBUG("listener(%s) accepted a connection: fd=%d peer=%s", address, cfd, peerName);
	Connection *connection = createConnection(cfd);
	if (connection == NULL) {
		::close(fd);
		SYSLOG_ERROR("listener(%s) failed to create connection from %s", address, peerName);
		return 0; // ignore
	}

	++connActive;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl 0x8(%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0xb947
jmp 0xb966
leaq 0x6f8e(%rip), %rdi      # 0x128dc
leaq 0x7951(%rip), %rsi      # 0x132a6
movl $0x1f, %edx
leaq 0x7952(%rip), %rcx      # 0x132b3
callq 0x3250
movl -0x14(%rbp), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x30f0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0xb9cf
jmp 0xb980
leaq 0xec61(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
movl $0x3, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xb9c1
movq -0xb0(%rbp), %rcx
leaq 0xec41(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
addq $0x10, %rcx
movl $0x3, %esi
leaq 0x792f(%rip), %rdx      # 0x132e9
movb $0x0, %al
callq 0xbf40
jmp 0xb9c3
movl $0x0, -0x4(%rbp)
jmp 0xbbb7
movl -0x18(%rbp), %edi
leaq -0xa0(%rbp), %rsi
movl $0x80, %edx
callq 0xc8b0
cmpl $0x0, %eax
jge 0xba43
movl -0x18(%rbp), %edi
callq 0x3710
leaq 0xebf1(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
movl $0x2, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xba35
movq -0xb0(%rbp), %rcx
leaq 0xebd1(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
addq $0x10, %rcx
movl -0x18(%rbp), %r8d
movl $0x2, %esi
leaq 0x78da(%rip), %rdx      # 0x13308
movb $0x0, %al
callq 0xbf40
jmp 0xba37
movl $0x0, -0x4(%rbp)
jmp 0xbbb7
movl -0x18(%rbp), %edi
movl $0x800, %esi            # imm = 0x800
xorl %edx, %edx
callq 0xca10
movq -0xb0(%rbp), %rcx
movl 0x90(%rcx), %eax
cmpl 0x94(%rcx), %eax
jl 0xbacf
movl -0x14(%rbp), %edi
callq 0x3710
leaq 0xeb72(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
movl $0x2, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xbac1
movq -0xb0(%rbp), %rax
leaq 0xeb52(%rip), %rcx      # 0x1a5e8
movq (%rcx), %rdi
movq %rax, %rcx
addq $0x10, %rcx
leaq -0xa0(%rbp), %r8
movl 0x90(%rax), %r9d
movl $0x2, %esi
leaq 0x7888(%rip), %rdx      # 0x13342
movb $0x0, %al
callq 0xbf40
jmp 0xbac3
movl $0x0, -0x4(%rbp)
jmp 0xbbb7
jmp 0xbad1
leaq 0xeb10(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xbb1d
movq -0xb0(%rbp), %rcx
leaq 0xeaf0(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
addq $0x10, %rcx
movl -0x18(%rbp), %r8d
leaq -0xa0(%rbp), %r9
movl $0x5, %esi
leaq 0x7869(%rip), %rdx      # 0x1337f
movb $0x0, %al
callq 0xbf40
jmp 0xbb1f
movq -0xb0(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0xbbd0
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jne 0xbb9a
movl -0x14(%rbp), %edi
callq 0x3710
leaq 0xea9a(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
movl $0x2, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xbb8f
movq -0xb0(%rbp), %rcx
leaq 0xea7a(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
addq $0x10, %rcx
leaq -0xa0(%rbp), %r8
movl $0x2, %esi
leaq 0x7829(%rip), %rdx      # 0x133b1
movb $0x0, %al
callq 0xbf40
jmp 0xbb91
movl $0x0, -0x4(%rbp)
jmp 0xbbb7
movq -0xb0(%rbp), %rax
movl 0x90(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x90(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)