// File: /workspace/github_cpp_makefile_star2_O1/luigix25[P]SSLP/server_files/server.cpp
// Function: main
int main(int argc,char **argv){


	if(argc != 2){
		cout<<"[Error] missing port!"<<endl;
		exit(-1);
	}

	signal(SIGPIPE, SIG_IGN);					//ignoro sigpipe
	signal (SIGINT,close_handler);				//gestisco i ctrl-c

	struct sockaddr_in clientAddress;

	int port = atoi(argv[1]);
	int fdmax;
	unsigned int addrlen;
	int i,status,new_sock;
	int cmd;
	fd_set read_fds;
		
	if(port <= 0 || port >= USHRT_MAX){
		cout<<"[Error] Invalid Port"<<endl;
		exit(-1);
	}


	memset(&clientAddress,0,sizeof(clientAddress));

	server_socket = initialize_server(port);

	cout<<"[LOG] Waiting connections on port "<<port<<endl;

	
	FD_ZERO(&master);	
	FD_ZERO(&read_fds);
	FD_SET(server_socket,&master);
	fdmax = server_socket;

	client_socket = NetSocket();

	bool alreadyConnected = false;


	while(true){
		read_fds = master;
		if(select(fdmax+1,&read_fds,NULL,NULL,NULL) <=0){
			perror("[Error] Select");
			exit(-1);
		}

		for(i = 0; i <= fdmax; i++){
			if(FD_ISSET(i,&read_fds)){	
				if(i == server_socket){ 						//new connection
					memset(&clientAddress,0,sizeof(clientAddress));
					addrlen = sizeof(clientAddress);
					new_sock = accept(server_socket,(struct sockaddr*)&clientAddress,&addrlen);
					
					if(new_sock < 0){
						perror("[Error] accept\n");
						continue;
					}

					if(alreadyConnected){					
						close(new_sock);						//refuse new connections
						continue;
					}

					FD_SET(new_sock,&master);
					if(new_sock > fdmax) 
						fdmax = new_sock;
					
					client_socket.setSocket(new_sock);
					alreadyConnected = true;

					if(!initial_protocol(client_socket)){
						client_socket.closeConnection();
						KeyManager::destroyKeys();
						alreadyConnected = false;
						FD_CLR(new_sock,&master);
					}

					continue;


				} else {

					status = receive_command(cmd);
					if(!status){
						cout<<"[LOG] Client Disconnected"<<endl;
						client_socket.closeConnection();
						KeyManager::destroyKeys();
						alreadyConnected = false;
						FD_CLR(i,&master);							//remove socket from select

					} else {
						if(!select_command(cmd)){
							client_socket.closeConnection();
							KeyManager::destroyKeys();
							alreadyConnected = false;
							FD_CLR(i,&master);
						}
					}

				}
			}

		}	

	}
	
	//close(server_socket);											//no more clients allowed

	return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
cmpl $0x2, %edi
jne 0x62a9
movq %rsi, %rbx
movl $0x1, %r12d
movl $0x1, %esi
movl $0xd, %edi
callq 0x4590
leaq -0x14ea(%rip), %rsi     # 0x4d8c
movl $0x2, %edi
callq 0x4590
movq 0x8(%rbx), %rdi
callq 0x4750
movl %eax, %r14d
movl $0xffff0001, %eax       # imm = 0xFFFF0001
leal (%r14,%rax), %ecx
cmpl %eax, %ecx
ja 0x62c9
movq 0x9d30(%rip), %rdi      # 0xffd0
leaq 0x4fa6(%rip), %rsi      # 0xb24d
jmp 0x62b7
movq 0x9d20(%rip), %rdi      # 0xffd0
leaq 0x4f80(%rip), %rsi      # 0xb237
callq 0x4450
movq %rax, %rdi
callq 0x4230
jmp 0x65d6
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movl %r14d, %edi
callq 0x56d9
movl %eax, 0xa2a9(%rip)      # 0x10588
movq 0x9cea(%rip), %rbx      # 0xffd0
leaq 0x4f75(%rip), %rsi      # 0xb262
movl $0x22, %edx
movq %rbx, %rdi
callq 0x4530
movq %rbx, %rdi
movl %r14d, %esi
callq 0x47f0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4440
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4330
xorps %xmm0, %xmm0
movups %xmm0, 0xa1c7(%rip)     # 0x10500
movups %xmm0, 0xa1d0(%rip)     # 0x10510
movups %xmm0, 0xa1d9(%rip)     # 0x10520
movups %xmm0, 0xa1e2(%rip)     # 0x10530
movups %xmm0, 0xa1eb(%rip)     # 0x10540
movups %xmm0, 0xa1f4(%rip)     # 0x10550
movups %xmm0, 0xa1fd(%rip)     # 0x10560
movups %xmm0, 0xa206(%rip)     # 0x10570
leaq 0xa18f(%rip), %rbx      # 0x10500
leaq 0x20(%rsp), %r15
movaps %xmm0, (%r15)
movaps %xmm0, 0x10(%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x30(%r15)
movaps %xmm0, 0x40(%r15)
movaps %xmm0, 0x50(%r15)
movaps %xmm0, 0x60(%r15)
movaps %xmm0, 0x70(%r15)
movl 0xa1e4(%rip), %r13d     # 0x10588
leal 0x3f(%r13), %eax
testl %r13d, %r13d
cmovnsl %r13d, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %r13d, %ecx
subl %edx, %ecx
shlq %cl, %r12
sarl $0x6, %eax
cltq
orq %r12, (%rbx,%rax,8)
leaq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x682c
movb 0x4(%r12), %al
movb %al, 0xa1a7(%rip)       # 0x10584
movl (%r12), %eax
movl %eax, 0xa199(%rip)      # 0x10580
movl $0x10, %ecx
movq %r15, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
leal 0x1(%r13), %edi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4430
testl %eax, %eax
jle 0x65ca
xorl %r15d, %r15d
leaq 0x10(%rsp), %rbp
testl %r13d, %r13d
js 0x6598
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
incl %r14d
movl %r14d, %r12d
shrl $0x6, %r12d
movq 0x20(%rsp,%r12,8), %rax
movl $0x1, %ebx
movl %r14d, %ecx
shlq %cl, %rbx
btq %r14, %rax
jae 0x657f
movl 0xa137(%rip), %edi      # 0x10588
cmpl %edi, %r14d
jne 0x6491
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movl $0x10, 0x8(%rsp)
movq %rbp, %rsi
leaq 0x8(%rsp), %rdx
callq 0x41a0
testl %eax, %eax
js 0x658a
testb $0x1, %r15b
je 0x6517
movl %eax, %edi
callq 0x4920
jmp 0x657f
leaq 0x4(%rsp), %rdi
callq 0x5792
testb %al, %al
je 0x64b1
movl 0x4(%rsp), %edi
callq 0x56bb
testb %al, %al
je 0x64f3
jmp 0x657f
movl $0x19, %edx
movq 0x9b13(%rip), %r15      # 0xffd0
movq %r15, %rdi
leaq 0x4ddd(%rip), %rsi      # 0xb2a4
callq 0x4530
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4440
movsbl %al, %esi
movq %r15, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4330
leaq 0xa086(%rip), %rdi      # 0x10580
callq 0x6840
callq 0x9f40
notq %rbx
leaq 0x9ff2(%rip), %rax      # 0x10500
andq %rbx, (%rax,%r12,8)
xorl %r15d, %r15d
jmp 0x657f
movl $0x1, %ebx
movl %eax, %ecx
shlq %cl, %rbx
movl %eax, %r12d
shrl $0x6, %r12d
leaq 0x9fd1(%rip), %rbp      # 0x10500
orq %rbx, (%rbp,%r12,8)
cmpl %r13d, %eax
cmovgl %eax, %r13d
leaq 0xa03e(%rip), %r15      # 0x10580
movq %r15, %rdi
movl %eax, %esi
callq 0x6838
movq %r15, %rdi
callq 0x58b8
testb %al, %al
je 0x6562
leaq 0x10(%rsp), %rbp
movb $0x1, %r15b
jmp 0x657f
movq %r15, %rdi
callq 0x6840
callq 0x9f40
notq %rbx
andq %rbx, (%rbp,%r12,8)
xorl %r15d, %r15d
leaq 0x10(%rsp), %rbp
cmpl %r13d, %r14d
jl 0x6427
jmp 0x6598
leaq 0x4d03(%rip), %rdi      # 0xb294
callq 0x43e0
jmp 0x657f
movl $0x10, %ecx
leaq 0x20(%rsp), %rax
movq %rax, %rdi
leaq 0x9f54(%rip), %rsi      # 0x10500
rep  movsq (%rsi), %es:(%rdi)
leal 0x1(%r13), %edi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4430
testl %eax, %eax
jg 0x6418
leaq 0x4cb4(%rip), %rdi      # 0xb285
callq 0x43e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x45b0