Skip to content
Snippets Groups Projects
dap_chain_node_cli.c 40.7 KiB
Newer Older
                        l_listen_unix_socket_path );
ANTA's avatar
ANTA committed
            else
                log_it( L_ERROR, "Server can't start(err=%d). May be problem with file=%s?", errno, l_listen_unix_socket_path );
ANTA's avatar
ANTA committed
            closesocket( sockfd );
ANTA's avatar
ANTA committed
        }
        chmod(l_listen_unix_socket_path,l_listen_unix_socket_permissions);
ANTA's avatar
ANTA committed

      #else

ANTA's avatar
ANTA committed
//    Sleep( 3000 );
ANTA's avatar
ANTA committed

        if( pthread_create(&threadId, NULL, thread_pipe_func, (void*) (intptr_t) sockfd) != 0 ) {
            closesocket( sockfd );
ANTA's avatar
ANTA committed
        }

        return 0;
      #endif
    else if (l_listen_port ){
ANTA's avatar
ANTA committed

Constantin Papizh's avatar
Constantin Papizh committed
        const char *l_listen_addr_str = dap_config_get_item_str(g_config, "conserver", "listen_address");
ANTA's avatar
ANTA committed

        log_it( L_INFO, "Console interace on addr %s port %u ", l_listen_addr_str, l_listen_port );
Roman Khlopkov's avatar
Roman Khlopkov committed

        server_addr.sin_family = AF_INET;
Constantin Papizh's avatar
Constantin Papizh committed
#ifdef _WIN32
        struct in_addr _in_addr = { { .S_addr = htonl(INADDR_LOOPBACK) } };
Constantin Papizh's avatar
Constantin Papizh committed
        server_addr.sin_addr = _in_addr;
        server_addr.sin_port = l_listen_port;
Constantin Papizh's avatar
Constantin Papizh committed
#else
        inet_pton( AF_INET, l_listen_addr_str, &server_addr.sin_addr );
        server_addr.sin_port = htons( (uint16_t)l_listen_port );
#endif
ANTA's avatar
ANTA committed
        // create socket
        if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET ) {
Constantin Papizh's avatar
Constantin Papizh committed
#ifdef __WIN32
            _set_errno(WSAGetLastError());
#endif
            log_it( L_ERROR, "Console Server: can't create socket, err %d", errno );
ANTA's avatar
ANTA committed
        }

        // connecting the address with a socket
        if ( bind(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == SOCKET_ERROR ) {
Constantin Papizh's avatar
Constantin Papizh committed
#ifdef __WIN32
            _set_errno(WSAGetLastError());
#endif
Constantin Papizh's avatar
Constantin Papizh committed
            log_it( L_ERROR, "Console Server: can't bind socket, err %d", errno );
ANTA's avatar
ANTA committed
            closesocket( sockfd );
ANTA's avatar
ANTA committed
        }
    }else {
        log_it (L_INFO, "Not defined console interface");
        return 0;
ANTA's avatar
ANTA committed

    // turn on reception of connections
ANTA's avatar
ANTA committed
    if( listen(sockfd, MAX_CONSOLE_CLIENTS) == SOCKET_ERROR )
ANTA's avatar
ANTA committed

    if( pthread_create(&l_thread_id, NULL, thread_main_func, (void*) (intptr_t) sockfd) != 0 ) {
ANTA's avatar
ANTA committed
        closesocket( sockfd );
ANTA's avatar
ANTA committed

    // in order to thread not remain in state "dead" after completion
    pthread_detach( l_thread_id );
ANTA's avatar
ANTA committed

    return 0;
}

/**
 * Deinitialization of the server side
 *
 */
void dap_chain_node_cli_delete(void)
{
    if(server_sockfd >= 0)
        closesocket(server_sockfd);
Constantin Papizh's avatar
Constantin Papizh committed
#ifdef __WIN32
    WSACleanup();
#endif
    // deinit client for handshake
Dmitriy A. Gerasimov's avatar
Dmitriy A. Gerasimov committed
    dap_chain_node_client_deinit();