// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r15
movq %rdi, %rbx
callq 0x4598
leaq 0x18(%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x4430
leaq 0x46d4(%rip), %rax      # 0x8d30
movq %rax, (%rbx)
movq $0x0, 0x28(%rbx)
movq 0x20(%rbx), %rax
cmpb $0x0, 0x4(%rax)
jne 0x4788
movb $0x1, %cl
movb %cl, 0x4(%rax)
movb %cl, 0x10(%rax)
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x2080
movq 0x20(%rbx), %rcx
movl %eax, 0x8(%rcx)
cmpl $-0x1, %eax
je 0x4794
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x2350
testl %eax, %eax
js 0x47c6
leaq 0x18(%rsp), %rsi
movw $0x2, (%rsi)
movq 0x20(%rbx), %rax
movzwl 0x38(%rax), %ecx
rolw $0x8, %cx
movw %cx, 0x2(%rsi)
movl $0x0, 0x4(%rsi)
movl 0x8(%rax), %edi
movl $0x10, %edx
callq 0x2040
cmpl $-0x1, %eax
je 0x47f0
movq 0x20(%rbx), %rax
movl 0x8(%rax), %edi
movl $0xa, %esi
callq 0x2240
cmpl $-0x1, %eax
je 0x480e
movl $0x10, (%rsp)
movq 0x20(%rbx), %rdi
callq 0x3a9c
leaq 0x171b(%rip), %rdx      # 0x5e4e
xorl %edi, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x22b0
leaq 0x1728(%rip), %rdx      # 0x5e6d
movl $0x1, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x22b0
leaq 0x16f4(%rip), %rdx      # 0x5e4e
movl $0x2, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x22b0
callq 0x2320
movq 0x20(%rbx), %rax
movl 0x8(%rax), %edi
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
callq 0x20b0
movq 0x20(%rbx), %rcx
movl %eax, 0xc(%rcx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movl $0x28, %edi
callq 0x2140
movq %rax, %r12
leaq 0x15a0(%rip), %rsi      # 0x5d48
movq %rax, %rdi
callq 0x4440
leaq 0x4519(%rip), %rsi      # 0x8cd0
leaq -0x344(%rip), %rdx      # 0x447a
movq %r12, %rdi
callq 0x2330
movq 0x20(%rbx), %rax
movl 0x8(%rax), %edi
callq 0x2380
movl $0x28, %edi
callq 0x2140
movq %rax, %r12
leaq 0x158d(%rip), %rsi      # 0x5d73
movq %rax, %rdi
callq 0x4440
jmp 0x482a
movl $0x28, %edi
callq 0x2140
movq %rax, %r12
leaq 0x159b(%rip), %rsi      # 0x5d9f
movq %rax, %rdi
callq 0x4440
jmp 0x482a
movl $0x28, %edi
callq 0x2140
movq %rax, %r12
leaq 0x1601(%rip), %rsi      # 0x5e23
movq %rax, %rdi
callq 0x4440
leaq 0x449f(%rip), %rsi      # 0x8cd0
leaq -0x3be(%rip), %rdx      # 0x447a
movq %r12, %rdi
callq 0x2330
jmp 0x4848
jmp 0x4848
jmp 0x4848
jmp 0x485c
movq %rax, %r15
movq %r12, %rdi
callq 0x2190
jmp 0x485f
jmp 0x485c
movq %rax, %r15
jmp 0x4867
movq %rax, %r15
movq %r14, %rdi
callq 0x443c
movq %rbx, %rdi
callq 0x45b2
movq %r15, %rdi
callq 0x2340
nop