// File: llefranc[P]42_webserv/srcs/server/HttpServer.cpp
// Function: HttpServer::etablishConnection(std::map<int, std::vector<ServerInfo, std::allocator<ServerInfo>>, std::less<int>, std::allocator<std::pair<int const, std::vector<ServerInfo, std::allocator<ServerInfo>>>>>&)
void HttpServer::etablishConnection(std::map<int, std::vector<ServerInfo> >& mSrv)
{
	struct timeval tv;

	while (true)
	{
		tv.tv_sec = MAX_SEC_TIMEOUT;
		tv.tv_usec = MAX_USEC_TIMEOUT;

		// Setting readFd with all passive accept sockets and all clients previously connected
		FD_ZERO(&_readFds);
		FD_ZERO(&_writeFds);
		
		addSocketsToReadFdSet<ServerSocket>(_serverSocks);
		addSocketsToReadFdSet<ClientSocket>(_clientSocks);
		addSocketsToWriteFdSet<ClientSocket>(_clientSocks);

		// Waiting for incoming connections on server sockets / client sockets
		if ((_nbReadyFds = select(FD_SETSIZE, &_readFds, &_writeFds, NULL, &tv)) < 0)
			throw std::runtime_error("Fatal error: select function failed\n");
		else if (!_nbReadyFds)
			throw std::runtime_error("Webserv timeout\n");

		// Send responses to clients
		sendToClients();
		// If a passive socket was activated, creates a new client connection
		connectNewClients(mSrv);
		// Receive from client
		requestHandler();
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x3e8, -0x20(%rbp)     # imm = 0x3E8
movq $0x0, -0x18(%rbp)
movq -0x58(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpq $0x10, %rax
jae 0x1316e
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1314b
jmp 0x13170
jmp 0x13172
movq -0x58(%rbp), %rax
addq $0xb0, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpq $0x10, %rax
jae 0x131aa
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x13187
jmp 0x131ac
movq -0x58(%rbp), %rsi
movq %rsi, %rdi
callq 0x143f0
movq -0x58(%rbp), %rdi
movq %rdi, %rsi
addq $0x18, %rsi
callq 0x144b0
movq -0x58(%rbp), %rdi
movq %rdi, %rsi
addq $0x18, %rsi
callq 0x14570
movq -0x58(%rbp), %rdx
movq %rdx, %rsi
addq $0x30, %rsi
addq $0xb0, %rdx
movl $0x400, %edi            # imm = 0x400
xorl %eax, %eax
movl %eax, %ecx
leaq -0x20(%rbp), %r8
callq 0x5510
movq -0x58(%rbp), %rcx
movl %eax, 0x130(%rcx)
cmpl $0x0, %eax
jge 0x1325e
movl $0x10, %edi
callq 0x5290
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x60(%rbp)
leaq 0x101ac(%rip), %rsi     # 0x233d2
callq 0x5210
jmp 0x1322d
movq -0x60(%rbp), %rdi
movq 0x1ddb8(%rip), %rsi     # 0x30ff0
movq 0x1dd89(%rip), %rdx     # 0x30fc8
callq 0x58a0
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x53f0
jmp 0x132e1
movq -0x58(%rbp), %rax
cmpl $0x0, 0x130(%rax)
jne 0x132bb
movl $0x10, %edi
callq 0x5290
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x68(%rbp)
leaq 0x10171(%rip), %rsi     # 0x233f7
callq 0x5210
jmp 0x1328d
movq -0x68(%rbp), %rdi
movq 0x1dd58(%rip), %rsi     # 0x30ff0
movq 0x1dd29(%rip), %rdx     # 0x30fc8
callq 0x58a0
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x53f0
jmp 0x132e1
jmp 0x132bd
movq -0x58(%rbp), %rdi
callq 0x132f0
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x13900
movq -0x58(%rbp), %rdi
callq 0x13c40
jmp 0x13128
movq -0x48(%rbp), %rdi
callq 0x58e0
nopw (%rax,%rax)