// File: /workspace/github_cpp_makefile_star2_O2/luigix25[P]SSLP/client_files/client.cpp
// Function: main
int main(int argc,char **argv){

	if(argc < 3){
		cout<<"[Error] IP and Port are mandatory!"<<endl;
		exit(-1);
	}

	signal(SIGPIPE, SIG_IGN);
	signal (SIGINT,close_handler);

	int status,portServer;
	struct sockaddr_in serverAddress;

	int i,fdmax;
	fd_set read_fds;

	portServer = atoi(argv[2]);

	if(portServer <= 0 || portServer >= USHRT_MAX){
		cout<<"[Error] Invalid Port"<<endl;
		exit(-1);
	}


	socket_tcp = socket(AF_INET,SOCK_STREAM,0);
	if(socket_tcp < 0){
		perror("[Error] socket\n");
		return -1;
	}

	memset(&serverAddress,0,sizeof(serverAddress));
	
	serverAddress.sin_family = AF_INET;
	serverAddress.sin_port = htons(portServer);
	if(inet_pton(AF_INET,argv[1],&serverAddress.sin_addr) <=0){
		cout<<"[Error] invalid IP"<<endl;
		exit(-1);
	}
	

	status = connect(socket_tcp, (struct sockaddr*)&serverAddress,sizeof(serverAddress));
	if(status < 0){
		perror("[Error] connect\n");
		return -1;
	}
	
	cout<<endl<<"Successfully connected to the server "<<argv[1]<<" : "<<portServer<<endl;
	server_socket = NetSocket(socket_tcp);

	if(!initial_protocol(server_socket)){
		cout<<"Something Strange Occurred"<<endl;
		exit(-1);
		//handle error
	}

	cmd_help();

	FD_ZERO(&master);	
	FD_ZERO(&read_fds);


	FD_SET(socket_tcp,&master);
	FD_SET(0,&master);		// 0 è stdin

	fdmax = socket_tcp;

	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 == 0){						//stdin
					if(!read_input()){
						KeyManager::destroyKeys();
						server_socket.closeConnection();
						exit(-1);
					}
					//continue;
				} else if(i == socket_tcp) {			//server tcp
					//non dovrei mai arrivare qui	
					cout<<"Protocolar Error!"<<endl;
					KeyManager::destroyKeys();
					server_socket.closeConnection();
					exit(-1);
				} 

			}

		}
		
	}

	
	return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
cmpl $0x2, %edi
jle 0x61c3
movq %rsi, %r14
pushq $0xd
popq %rdi
pushq $0x1
popq %rbx
movq %rbx, %rsi
callq 0x4520
leaq -0x143c(%rip), %rsi     # 0x4cdc
pushq $0x2
popq %rdi
callq 0x4520
movq 0x10(%r14), %rdi
callq 0x46f0
movl %eax, %r15d
movl $0xffff0001, %eax       # imm = 0xFFFF0001
leal (%r15,%rax), %ecx
cmpl %eax, %ecx
jbe 0x61d6
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x40e0
movl %eax, 0x93f0(%rip)      # 0xf540
testl %eax, %eax
jns 0x615d
leaq 0x423a(%rip), %rdi      # 0xa395
jmp 0x61a9
leaq 0xc(%rsp), %rdx
andl $0x0, 0x8(%rdx)
andq $0x0, (%rdx)
movw $0x2, -0x4(%rdx)
movl %r15d, %eax
rolw $0x8, %ax
movw %ax, -0x2(%rdx)
movq 0x8(%r14), %rsi
pushq $0x2
popq %rdi
callq 0x4820
testl %eax, %eax
jle 0x61e9
movl 0x93af(%rip), %edi      # 0xf540
leaq 0x8(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x4390
testl %eax, %eax
jns 0x61fc
leaq 0x420f(%rip), %rdi      # 0xa3b8
callq 0x43a0
pushq $-0x1
popq %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8dfe(%rip), %rdi      # 0xefc8
leaq 0x418c(%rip), %rsi      # 0xa35d
jmp 0x62aa
movq 0x8deb(%rip), %rdi      # 0xefc8
leaq 0x419c(%rip), %rsi      # 0xa380
jmp 0x62aa
movq 0x8dd8(%rip), %rdi      # 0xefc8
leaq 0x41ae(%rip), %rsi      # 0xa3a5
jmp 0x62aa
movq 0x8dc5(%rip), %rdi      # 0xefc8
callq 0x41d0
leaq 0x41ba(%rip), %rsi      # 0xa3c9
movq %rax, %rdi
callq 0x4400
movq 0x8(%r14), %rsi
movq %rax, %rdi
callq 0x4400
leaq 0x41c5(%rip), %rsi      # 0xa3ef
movq %rax, %rdi
callq 0x4400
movq %rax, %rdi
movl %r15d, %esi
callq 0x4770
movq %rax, %rdi
callq 0x41d0
movl 0x92f5(%rip), %esi      # 0xf540
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x6544
movb 0x4(%r14), %al
movb %al, 0x9302(%rip)       # 0xf564
movl (%r14), %eax
movl %eax, 0x92f5(%rip)      # 0xf560
leaq 0x92ee(%rip), %rdi      # 0xf560
callq 0x5815
testb %al, %al
je 0x629c
callq 0x4dc3
xorl %eax, %eax
leaq 0x9237(%rip), %r14      # 0xf4c0
cmpq $0x80, %rax
je 0x62bf
andq $0x0, (%rax,%r14)
addq $0x8, %rax
jmp 0x6289
movq 0x8d25(%rip), %rdi      # 0xefc8
leaq 0x4149(%rip), %rsi      # 0xa3f3
callq 0x4400
movq %rax, %rdi
callq 0x41d0
pushq $-0x1
popq %rdi
callq 0x4540
xorl %eax, %eax
cmpq $0x10, %rax
je 0x62d2
andq $0x0, 0x18(%rsp,%rax,8)
incq %rax
jmp 0x62c1
movl 0x9267(%rip), %r15d     # 0xf540
pushq $0x40
popq %rcx
movl %r15d, %eax
cltd
idivl %ecx
movslq %eax, %rdx
shll $0x6, %eax
movl %r15d, %ecx
subl %eax, %ecx
shlq %cl, %rbx
orq %rbx, (%r14,%rdx,8)
orb $0x1, 0x91c5(%rip)      # 0xf4c0
leal 0x1(%r15), %ebx
pushq $0x10
popq %r12
leaq 0x18(%rsp), %r13
movq %r12, %rcx
movq %r13, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %ebx, %edi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x43f0
testl %eax, %eax
jle 0x638c
xorl %ebp, %ebp
cmpl %r15d, %ebp
jg 0x6308
movl %ebp, %eax
shrl $0x6, %eax
movq 0x18(%rsp,%rax,8), %rax
btq %rbp, %rax
jae 0x6357
testl %ebp, %ebp
je 0x634e
cmpl 0x91f6(%rip), %ebp      # 0xf540
jne 0x6357
jmp 0x635b
callq 0x57a5
testb %al, %al
je 0x6376
incl %ebp
jmp 0x632b
movq 0x8c66(%rip), %rdi      # 0xefc8
leaq 0x40b4(%rip), %rsi      # 0xa41d
callq 0x4400
movq %rax, %rdi
callq 0x41d0
callq 0x923c
leaq 0x91de(%rip), %rdi      # 0xf560
callq 0x655c
jmp 0x62b7
leaq 0x407b(%rip), %rdi      # 0xa40e
callq 0x43a0
jmp 0x62b7
nop