// File: /workspace/github_cpp_makefile_star2_O3/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 0x6290
movq %rsi, %rbx
movl $0x1, %r15d
movl $0x1, %esi
movl $0xd, %edi
callq 0x4590
leaq -0x14d1(%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 0x62b0
movq 0x9d49(%rip), %rdi      # 0xffd0
leaq 0x4fbf(%rip), %rsi      # 0xb24d
jmp 0x629e
movq 0x9d39(%rip), %rdi      # 0xffd0
leaq 0x4f99(%rip), %rsi      # 0xb237
callq 0x4450
movq %rax, %rdi
callq 0x4230
jmp 0x659d
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movl %r14d, %edi
callq 0x56b7
movl %eax, 0xa2c2(%rip)      # 0x10588
movq 0x9d03(%rip), %rbx      # 0xffd0
leaq 0x4f8e(%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, 0xa1e0(%rip)     # 0x10500
movups %xmm0, 0xa1e9(%rip)     # 0x10510
movups %xmm0, 0xa1f2(%rip)     # 0x10520
movups %xmm0, 0xa1fb(%rip)     # 0x10530
movups %xmm0, 0xa204(%rip)     # 0x10540
movups %xmm0, 0xa20d(%rip)     # 0x10550
movups %xmm0, 0xa216(%rip)     # 0x10560
movups %xmm0, 0xa21f(%rip)     # 0x10570
leaq 0xa1a8(%rip), %rbx      # 0x10500
movl 0xa229(%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, %r15
sarl $0x6, %eax
cltq
orq %r15, (%rbx,%rax,8)
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x6800
movb 0x4(%r15), %al
movb %al, 0xa1ed(%rip)       # 0x10584
movl (%r15), %eax
movl %eax, 0xa1e0(%rip)      # 0x10580
leaq 0x28(%rsp), %rax
movl $0x10, %ecx
movq %rax, %rdi
movq %rbx, %rsi
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
jle 0x6591
xorl %r15d, %r15d
leaq 0x10(%rsp), %rbp
testl %r13d, %r13d
js 0x655f
xorl %r12d, %r12d
movl %r12d, %r14d
shrl $0x6, %r14d
movq 0x28(%rsp,%r14,8), %rax
movl $0x1, %ebx
movl %r12d, %ecx
shlq %cl, %rbx
btq %r12, %rax
jae 0x653e
cmpl 0xa17e(%rip), %r12d     # 0x10588
jne 0x644a
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movl $0x10, 0xc(%rsp)
movl %r12d, %edi
movq %rbp, %rsi
leaq 0xc(%rsp), %rdx
callq 0x41a0
testl %eax, %eax
js 0x6551
testb $0x1, %r15b
je 0x64cd
movl %eax, %edi
callq 0x4920
jmp 0x6519
leaq 0x8(%rsp), %rdi
callq 0x5779
testb %al, %al
je 0x646a
movl 0x8(%rsp), %edi
callq 0x5699
testb %al, %al
je 0x64ac
jmp 0x653e
movl $0x19, %edx
movq 0x9b5a(%rip), %r15      # 0xffd0
movq %r15, %rdi
leaq 0x4e24(%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 0xa0cd(%rip), %rdi      # 0x10580
callq 0x6814
callq 0x9e7c
notq %rbx
leaq 0xa039(%rip), %rax      # 0x10500
andq %rbx, (%rax,%r14,8)
jmp 0x653b
movl $0x1, %ebx
movl %eax, %ecx
shlq %cl, %rbx
movl %eax, %r14d
shrl $0x6, %r14d
leaq 0xa01b(%rip), %rbp      # 0x10500
orq %rbx, (%rbp,%r14,8)
cmpl %r13d, %eax
cmovgl %eax, %r13d
movq %r13, %r15
leaq 0xa085(%rip), %r13      # 0x10580
movq %r13, %rdi
movl %eax, %esi
callq 0x680c
movq %r13, %rdi
callq 0x589f
testb %al, %al
je 0x651e
leaq 0x10(%rsp), %rbp
movq %r15, %r13
movb $0x1, %r15b
jmp 0x653e
movq %r13, %rdi
callq 0x6814
callq 0x9e7c
notq %rbx
andq %rbx, (%rbp,%r14,8)
leaq 0x10(%rsp), %rbp
movq %r15, %r13
xorl %r15d, %r15d
leal 0x1(%r12), %eax
cmpl %r13d, %r12d
movl %eax, %r12d
jl 0x63e2
jmp 0x655f
leaq 0x4d3c(%rip), %rdi      # 0xb294
callq 0x43e0
jmp 0x653e
movl $0x10, %ecx
leaq 0x28(%rsp), %rax
movq %rax, %rdi
leaq 0x9f8d(%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 0x63d6
leaq 0x4ced(%rip), %rdi      # 0xb285
callq 0x43e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x45b0
nop