// File: /workspace/github_cpp_makefile_star2_O2/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 0x4004
leaq 0x18(%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x3eb0
leaq 0x4c6c(%rip), %rax      # 0x8d30
movq %rax, (%rbx)
andq $0x0, 0x28(%rbx)
movq 0x20(%rbx), %rax
cmpb $0x0, 0x4(%rax)
jne 0x41d6
movb $0x1, %cl
movb %cl, 0x4(%rax)
movb %cl, 0x10(%rax)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x2080
movq 0x20(%rbx), %rcx
movl %eax, 0x8(%rcx)
cmpl $-0x1, %eax
je 0x41e2
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x2330
testl %eax, %eax
js 0x4212
leaq 0x18(%rsp), %rsi
movw $0x2, (%rsi)
movq 0x20(%rbx), %rax
movzwl 0x38(%rax), %ecx
rolw $0x8, %cx
movw %cx, 0x2(%rsi)
andl $0x0, 0x4(%rsi)
movl 0x8(%rax), %edi
pushq $0x10
popq %rdx
callq 0x2040
cmpl $-0x1, %eax
je 0x423a
movq 0x20(%rbx), %rax
movl 0x8(%rax), %edi
pushq $0xa
popq %rsi
callq 0x2240
cmpl $-0x1, %eax
je 0x4256
movl $0x10, (%rsp)
movq 0x20(%rbx), %rdi
callq 0x36e0
leaq 0x1cd9(%rip), %rdx      # 0x5e5e
xorl %edi, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x22a0
leaq 0x1ce6(%rip), %rdx      # 0x5e7d
pushq $0x1
popq %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x22a0
leaq 0x1cb4(%rip), %rdx      # 0x5e5e
pushq $0x2
popq %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x22a0
callq 0x2300
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
pushq $0x28
popq %rdi
callq 0x2150
movq %rax, %r12
leaq 0x1b64(%rip), %rsi      # 0x5d58
movq %rax, %rdi
callq 0x3ebc
leaq 0x4acd(%rip), %rsi      # 0x8cd0
leaq -0x314(%rip), %rdx      # 0x3ef6
movq %r12, %rdi
callq 0x2310
movq 0x20(%rbx), %rax
movl 0x8(%rax), %edi
callq 0x2370
pushq $0x28
popq %rdi
callq 0x2150
movq %rax, %r12
leaq 0x1b53(%rip), %rsi      # 0x5d83
movq %rax, %rdi
callq 0x3ebc
jmp 0x4270
pushq $0x28
popq %rdi
callq 0x2150
movq %rax, %r12
leaq 0x1b63(%rip), %rsi      # 0x5daf
movq %rax, %rdi
callq 0x3ebc
jmp 0x4270
pushq $0x28
popq %rdi
callq 0x2150
movq %rax, %r12
leaq 0x1bcb(%rip), %rsi      # 0x5e33
movq %rax, %rdi
callq 0x3ebc
leaq 0x4a59(%rip), %rsi      # 0x8cd0
leaq -0x388(%rip), %rdx      # 0x3ef6
movq %r12, %rdi
callq 0x2310
jmp 0x428e
jmp 0x428e
jmp 0x428e
jmp 0x42a2
movq %rax, %r15
movq %r12, %rdi
callq 0x2190
jmp 0x42a5
jmp 0x42a2
movq %rax, %r15
jmp 0x42ad
movq %rax, %r15
movq %r14, %rdi
callq 0x3eb8
movq %rbx, %rdi
callq 0x401e
movq %r15, %rdi
callq 0x2320
nop