// File: roberty65[P]bServer/frame/main/EventManager.cpp
// Function: beyondy::Async::EventManager::EventManager(beyondy::Async::Queue<beyondy::Async::Message>*, int)
EventManager::EventManager(Queue<Message> *_outQueue, int _esize)
	: epoll_fd(-1), event_next(0), event_total(0), epoll_size(_esize), epoll_events(NULL),
	  outQueue(_outQueue)
{
	if (epoll_size < 1) epoll_size = 1024;

	if ((epoll_fd = epoll_create(epoll_size)) < 0) {
		char buf[512];
		snprintf(buf, sizeof buf, "epoll_create(%ld) failed: %m", (long)epoll_size);
		throw std::runtime_error(buf);
	}

	epoll_events = (struct epoll_event *)malloc(epoll_size * sizeof(struct epoll_event));
	if (epoll_events == NULL) {
		::close(epoll_fd);

		char buf[512];
		snprintf(buf, sizeof buf, "new epoll events[%ld] failed: %m", (long)epoll_size);
		throw std::runtime_error(buf);
	}

	event_next = 0;
	event_total = 0;

	INIT_LIST_HEAD(&lruHead);
	INIT_LIST_HEAD(&connectorsHead);
		
	struct rlimit r0;
	int retval = getrlimit(RLIMIT_NOFILE, &r0);
	if (retval < 0) {
		throw std::runtime_error("rlimit(NOFILE) failed");
	}

	next_flow = 1;
	flowRoot.rb_node = NULL;

	perCheckOutQueue = 100;
	connectionMaxIdle = 5 * 1000;
	reconnectDelay = 2 * 1000;	// will delay 2, 4, 6, 8, 10
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x450(%rbp)
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x0, 0x4(%rax)
movl $0x0, 0x8(%rax)
movl -0x14(%rbp), %ecx
movl %ecx, 0xc(%rax)
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rcx
movq %rcx, 0x18(%rax)
cmpl $0x1, 0xc(%rax)
jge 0x72ff
movq -0x450(%rbp), %rax
movl $0x400, 0xc(%rax)       # imm = 0x400
movq -0x450(%rbp), %rax
movl 0xc(%rax), %edi
callq 0x3700
movq -0x450(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x0, %eax
jge 0x73ae
movq -0x450(%rbp), %rax
movslq 0xc(%rax), %rcx
leaq 0xb12c(%rip), %rdx      # 0x1245e
xorl %eax, %eax
leaq -0x220(%rbp), %rdi
movq %rdi, -0x460(%rbp)
movl $0x200, %esi            # imm = 0x200
callq 0x3620
movl $0x10, %edi
callq 0x31a0
movq -0x460(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x458(%rbp)
callq 0x3130
jmp 0x7371
movq -0x458(%rbp), %rdi
movq 0x12c79(%rip), %rsi     # 0x19ff8
movq 0x12c4a(%rip), %rdx     # 0x19fd0
callq 0x3670
movq -0x458(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
callq 0x3290
jmp 0x757b
movq -0x450(%rbp), %rax
movslq 0xc(%rax), %rax
imulq $0xc, %rax, %rdi
callq 0x3540
movq %rax, %rcx
movq -0x450(%rbp), %rax
movq %rcx, 0x10(%rax)
cmpq $0x0, 0x10(%rax)
jne 0x7477
movq -0x450(%rbp), %rax
movl (%rax), %edi
callq 0x3710
movq -0x450(%rbp), %rax
movslq 0xc(%rax), %rcx
leaq 0xb080(%rip), %rdx      # 0x1247b
xorl %eax, %eax
leaq -0x430(%rbp), %rdi
movq %rdi, -0x470(%rbp)
movl $0x200, %esi            # imm = 0x200
callq 0x3620
movl $0x10, %edi
callq 0x31a0
movq -0x470(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x468(%rbp)
callq 0x3130
jmp 0x743a
movq -0x468(%rbp), %rdi
movq 0x12bb0(%rip), %rsi     # 0x19ff8
movq 0x12b81(%rip), %rdx     # 0x19fd0
callq 0x3670
movq -0x468(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
callq 0x3290
jmp 0x757b
movq -0x450(%rbp), %rax
movl $0x0, 0x4(%rax)
movl $0x0, 0x8(%rax)
movq -0x450(%rbp), %rax
movq %rax, %rcx
addq $0x20, %rcx
movq %rcx, 0x20(%rax)
movq %rax, %rcx
addq $0x20, %rcx
movq %rcx, 0x28(%rax)
jmp 0x74ab
movq -0x450(%rbp), %rax
movq %rax, %rcx
addq $0x30, %rcx
movq %rcx, 0x30(%rax)
movq %rax, %rcx
addq $0x30, %rcx
movq %rcx, 0x38(%rax)
movl $0x7, %edi
leaq -0x440(%rbp), %rsi
callq 0x3580
movl %eax, -0x444(%rbp)
cmpl $0x0, -0x444(%rbp)
jge 0x7547
movl $0x10, %edi
callq 0x31a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x478(%rbp)
leaq 0xaf96(%rip), %rsi      # 0x1249c
callq 0x3130
jmp 0x750d
movq -0x478(%rbp), %rdi
movq 0x12add(%rip), %rsi     # 0x19ff8
movq 0x12aae(%rip), %rdx     # 0x19fd0
callq 0x3670
movq -0x478(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
callq 0x3290
jmp 0x757b
movq -0x450(%rbp), %rax
movl $0x1, 0x40(%rax)
movq $0x0, 0x48(%rax)
movl $0x64, 0x54(%rax)
movl $0x1388, 0x60(%rax)     # imm = 0x1388
movl $0x7d0, 0x6c(%rax)      # imm = 0x7D0
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
movq -0x228(%rbp), %rdi
callq 0x3690
nopw (%rax,%rax)