// File: nesro[P]NCursesGomoku/src/server.cpp
// Function: CServer::CServer(CBoard*, char, CGame*)
CServer::CServer(CBoard * board, char mark, CGame * game) :
CPlayer(board, mark), CNetwork(game) {

	this->m_ClientLength = 0;
	this->m_ClientSocketFileDescriptor = 0;

    /* connect to server only once */
    if (m_Game->m_IsServerUp == true) {
        return;
    } else {
        m_Game->m_IsServerUp = true;
        m_Game->m_IsConnectionUp = true;
    }

    sockaddr_in sockName;

    if ((m_Game->m_ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) ==
            -1) {
        throw CNetworkException("Fatal network error: Server socket failed.");
    }

    int intWithNumberOne = 1;
    if (setsockopt(m_Game->m_ServerSocket, SOL_SOCKET, SO_REUSEADDR,
            (char *) & intWithNumberOne, sizeof (intWithNumberOne)) < 0) {
        close(m_Game->m_ServerSocket);
        throw CNetworkException("Fatal network error: Server REUSEADDR fail.");
    }

    sockName.sin_family = AF_INET;
    sockName.sin_port = htons(m_Game->m_Port);
    sockName.sin_addr.s_addr = INADDR_ANY;

    if (bind(m_Game->m_ServerSocket, (sockaddr *) & sockName, sizeof (sockName))
            == -1) {
        throw CNetworkException("Fatal network error: Server bind failed. \
Program maybe was killed and port was not closed. Please, rerun program with \
another port.");
    }

    if (listen(m_Game->m_ServerSocket, 10) == -1) {
        throw CNetworkException("Fatal network error: Server listen failed.");
    }


    sockaddr_in clientInfo;
    socklen_t addrlen;

    addrlen = sizeof (clientInfo);

    m_Game->clearScreen();

    mvprintw(0, 0, "==============================");
    mvprintw(1, 0, "   >> Waiting for client <<   ");
    mvprintw(2, 0, "==============================");
    refresh();

    m_Game->m_ConnectionSocket = accept(m_Game->m_ServerSocket,
            (sockaddr *) & clientInfo, &addrlen);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x60(%rbp)
movq -0x10(%rbp), %rsi
movsbl -0x11(%rbp), %edx
callq 0x6910
movq -0x60(%rbp), %rdi
addq $0x18, %rdi
movq -0x20(%rbp), %rsi
callq 0x6450
jmp 0x6a64
movq -0x60(%rbp), %rax
leaq 0x52a1(%rip), %rcx      # 0xbd10
addq $0x10, %rcx
movq %rcx, (%rax)
movl $0x0, 0x28(%rax)
movl $0x0, 0x2c(%rax)
movq 0x20(%rax), %rax
movb 0x4(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x6aab
jmp 0x6d88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x6d9e
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rcx
movb $0x1, 0x4(%rcx)
movq 0x20(%rax), %rax
movb $0x1, 0x10(%rax)
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x30a0
movq -0x60(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl %eax, 0x8(%rcx)
cmpl $-0x1, %eax
jne 0x6b4c
movl $0x28, %edi
callq 0x3160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x68(%rbp)
leaq 0x2308(%rip), %rsi      # 0x8e06
callq 0x6480
jmp 0x6b05
movq -0x68(%rbp), %rdi
leaq 0x51b0(%rip), %rsi      # 0xbcc0
leaq -0x5f7(%rip), %rdx      # 0x6520
callq 0x3400
jmp 0x6db0
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x31d0
jmp 0x6d91
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x6d91
movq -0x60(%rbp), %rax
movl $0x1, -0x40(%rbp)
movq 0x20(%rax), %rax
movl 0x8(%rax), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x40(%rbp), %rcx
movl $0x4, %r8d
callq 0x3430
cmpl $0x0, %eax
jge 0x6be6
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movl 0x8(%rax), %edi
callq 0x3460
jmp 0x6b8e
movl $0x28, %edi
callq 0x3160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
leaq 0x2288(%rip), %rsi      # 0x8e31
callq 0x6480
jmp 0x6bb0
movq -0x70(%rbp), %rdi
leaq 0x5105(%rip), %rsi      # 0xbcc0
leaq -0x6a2(%rip), %rdx      # 0x6520
callq 0x3400
jmp 0x6db0
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x31d0
jmp 0x6d91
movq -0x60(%rbp), %rax
movw $0x2, -0x3c(%rbp)
movq 0x20(%rax), %rax
movl 0x38(%rax), %eax
movzwl %ax, %edi
callq 0x3040
movw %ax, %cx
movq -0x60(%rbp), %rax
movw %cx, -0x3a(%rbp)
movl $0x0, -0x38(%rbp)
movq 0x20(%rax), %rax
movl 0x8(%rax), %edi
leaq -0x3c(%rbp), %rsi
movl $0x10, %edx
callq 0x3050
cmpl $-0x1, %eax
jne 0x6c83
movl $0x28, %edi
callq 0x3160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x78(%rbp)
leaq 0x2217(%rip), %rsi      # 0x8e5d
callq 0x6480
jmp 0x6c4d
movq -0x78(%rbp), %rdi
leaq 0x5068(%rip), %rsi      # 0xbcc0
leaq -0x73f(%rip), %rdx      # 0x6520
callq 0x3400
jmp 0x6db0
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x31d0
jmp 0x6d91
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movl 0x8(%rax), %edi
movl $0xa, %esi
callq 0x32f0
cmpl $-0x1, %eax
jne 0x6cf5
movl $0x28, %edi
callq 0x3160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80(%rbp)
leaq 0x2229(%rip), %rsi      # 0x8ee1
callq 0x6480
jmp 0x6cbf
movq -0x80(%rbp), %rdi
leaq 0x4ff6(%rip), %rsi      # 0xbcc0
leaq -0x7b1(%rip), %rdx      # 0x6520
callq 0x3400
jmp 0x6db0
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x31d0
jmp 0x6d91
movq -0x60(%rbp), %rax
movl $0x10, -0x54(%rbp)
movq 0x20(%rax), %rdi
callq 0x5690
jmp 0x6d0b
leaq 0x21fa(%rip), %rdx      # 0x8f0c
xorl %esi, %esi
movb %sil, %al
movl %esi, %edi
callq 0x3360
jmp 0x6d20
leaq 0x2204(%rip), %rdx      # 0x8f2b
xorl %esi, %esi
movb %sil, %al
movl $0x1, %edi
callq 0x3360
jmp 0x6d38
leaq 0x21cd(%rip), %rdx      # 0x8f0c
xorl %esi, %esi
movb %sil, %al
movl $0x2, %edi
callq 0x3360
jmp 0x6d50
callq 0x33f0
jmp 0x6d57
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movl 0x8(%rax), %edi
leaq -0x50(%rbp), %rsi
leaq -0x54(%rbp), %rdx
callq 0x30d0
movl %eax, -0x84(%rbp)
jmp 0x6d77
movl -0x84(%rbp), %ecx
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movl %ecx, 0xc(%rax)
addq $0x90, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
addq $0x18, %rdi
callq 0x6470
movq -0x60(%rbp), %rdi
callq 0x6950
movq -0x28(%rbp), %rdi
callq 0x3410