// File: /workspace/github_cpp_makefile_star2_O3/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 0x4398
leaq 0x18(%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x422c
leaq 0x48d4(%rip), %rax      # 0x8d30
movq %rax, (%rbx)
movq $0x0, 0x28(%rbx)
movq 0x20(%rbx), %rax
cmpb $0x0, 0x4(%rax)
jne 0x4588
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 0x4594
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 0x45c6
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 0x45f0
movq 0x20(%rbx), %rax
movl 0x8(%rax), %edi
movl $0xa, %esi
callq 0x2240
cmpl $-0x1, %eax
je 0x460e
movl $0x10, (%rsp)
movq 0x20(%rbx), %rdi
callq 0x3898
leaq 0x192b(%rip), %rdx      # 0x5e5e
xorl %edi, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x22b0
leaq 0x1938(%rip), %rdx      # 0x5e7d
movl $0x1, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x22b0
leaq 0x1904(%rip), %rdx      # 0x5e5e
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 0x17b0(%rip), %rsi      # 0x5d58
movq %rax, %rdi
callq 0x423c
leaq 0x4719(%rip), %rsi      # 0x8cd0
leaq -0x348(%rip), %rdx      # 0x4276
movq %r12, %rdi
callq 0x2330
movq 0x20(%rbx), %rax
movl 0x8(%rax), %edi
callq 0x2380
movl $0x28, %edi
callq 0x2140
movq %rax, %r12
leaq 0x179d(%rip), %rsi      # 0x5d83
movq %rax, %rdi
callq 0x423c
jmp 0x462a
movl $0x28, %edi
callq 0x2140
movq %rax, %r12
leaq 0x17ab(%rip), %rsi      # 0x5daf
movq %rax, %rdi
callq 0x423c
jmp 0x462a
movl $0x28, %edi
callq 0x2140
movq %rax, %r12
leaq 0x1811(%rip), %rsi      # 0x5e33
movq %rax, %rdi
callq 0x423c
leaq 0x469f(%rip), %rsi      # 0x8cd0
leaq -0x3c2(%rip), %rdx      # 0x4276
movq %r12, %rdi
callq 0x2330
jmp 0x4648
jmp 0x4648
jmp 0x4648
jmp 0x465c
movq %rax, %r15
movq %r12, %rdi
callq 0x2190
jmp 0x465f
jmp 0x465c
movq %rax, %r15
jmp 0x4667
movq %rax, %r15
movq %r14, %rdi
callq 0x4238
movq %rbx, %rdi
callq 0x43b2
movq %r15, %rdi
callq 0x2340
nop