⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multi_lo_select.c

📁 eCos/RedBoot for勤研ARM AnywhereII(4510) 含全部源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
        pexit("bind /source_1/ error");
    }
    listen(s_s1, SOMAXCONN);

    if ( dual ) {
        s_s2 = socket(AF_INET, SOCK_STREAM, 0);    
        if (s_s2 < 0) {    
            pexit("stream socket 2");
        }  
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_len = sizeof(local);
        local.sin_port = ntohs(SOURCE_PORT2 + which);
        local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        if(bind(s_s2, (struct sockaddr *) &local, sizeof(local)) < 0) {    
            pexit("bind /source_2/ error");
        }
        listen(s_s2, SOMAXCONN);
    }

    while (true) {
        FD_ZERO(&in_fds);
        FD_SET(s_s1, &in_fds);
        if ( dual )
            FD_SET(s_s2, &in_fds);
        num = select ( max(s_s1,s_s2)+1, &in_fds,0,0,0);
        
        if (FD_ISSET(s_s1,&in_fds)) {
            len = sizeof(e_s1_addr);
            if ((e_s1 = accept(s_s1,(struct sockaddr *)&e_s1_addr,&len))<0) {
                pexit("accept /source_1/");
            }
            diag_printf("TCP SERVER connection from %s: %d\n",
                        inet_ntoa(e_s1_addr.sin_addr),ntohs(e_s1_addr.sin_port));
        }

        if ( dual ) {
            if (FD_ISSET(s_s2,&in_fds)) {
                len = sizeof(e_s2_addr);
                if ((e_s2 = accept(s_s2,(struct sockaddr *)&e_s2_addr,&len))<0) {
                    pexit("accept /source_2/");
                }
                diag_printf("TCP SERVER connection from %s: %d\n",
                            inet_ntoa(e_s2_addr.sin_addr), ntohs(e_s2_addr.sin_port));
            }
        }
        if ((e_s1 != 0) || ( e_s2 != 0)) {
            break;
        }
    }   /* while (true) */ 

    CYG_TEST_CHECK( 0 != e_s1, "No connection made on s1!" );
    
    if ((len = read(e_s1, data_buf1[which], MAX_BUF)) < 0  ) {
        perror("I/O error s1");
        CYG_TEST_FAIL( "Read s1 failed" );
    }
    diag_printf("Listener %d: %s\n", which, data_buf1[which]);

    close( s_s1 );
    if ( dual )
        close( s_s2 );
    if ( 0 != e_s1 )
        close ( e_s1 );
    if ( 0 != e_s2 )
        close ( e_s2 );

    cyg_semaphore_post( &listen_sema[which] ); // Verify that I was here
    cyg_semaphore_post( &recv_sema );          // Count receptions

    cyg_thread_exit(); // explicitly
}

// ------------------------------------------------------------------------
static void sender( cyg_addrword_t which ) // which means which set (odd/even) here...
{
    int s_source;
    struct sockaddr_in local;
    int len;

    diag_printf("client %d [%s] :started\n", which, (which & 1) ? "odd" : "even" );

    for ( /* which as is */; which < NLISTENERS; which += 2 ) {

        s_source = socket(AF_INET, SOCK_STREAM, 0);
        if (s_source < 0) {
            pexit("stream socket");
        }
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_len = sizeof(local);
        local.sin_port = htons( SOURCE_PORT1 + which );
        local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    
        if (connect(s_source, (struct sockaddr *)&local, sizeof(local)) < 0) {
            pexit("Can't connect to target");
        }
    
        if ((len = write(s_source,data_buf_write1,sizeof(data_buf_write1) )) < 0) {
            CYG_TEST_FAIL_FINISH("Error writing buffer");
        } 
        cyg_semaphore_wait( &listen_sema[which] ); // wait for the appropriate semaphore "reply"
        cyg_semaphore_post( &send_sema ); // count up successful sends

        close ( s_source );
    }
    cyg_thread_exit(); // explicitly
}


static void
master(cyg_addrword_t param)
{
    int i;
    cyg_handle_t self = cyg_thread_self();

    cyg_semaphore_init( &send_sema, 0 );
    cyg_semaphore_init( &recv_sema, 0 );

    for ( i = 0 ; i < NLISTENERS; i++ )
        cyg_semaphore_init( &listen_sema[i], 0 );

    init_all_network_interfaces();
    CYG_TEST_INFO("Start multiple loopback select test");
#if NLOOP > 0
    // We are currently running at high prio, so we can just go and make
    // loads of threads:

    // Some at higher prio
    for ( i = 0; i < NLISTENERS/2; i++ )
        cyg_thread_create(PRIO_LISTENER_HI,       // Priority
                          listener,               // entry
                          i,                      // entry parameter
                          "listener",             // Name
                          &stack_listener[i][0],  // Stack
                          STACK_SIZE,             // Size
                          &listener_thread_handle[i], // Handle
                          &listener_thread_data[i] // Thread data structure
            );
    // the rest at lower prio
    for (      ; i < NLISTENERS  ; i++ )
        cyg_thread_create(PRIO_LISTENER_LO,       // Priority
                          listener,               // entry
                          i,                      // entry parameter
                          "listener",             // Name
                          &stack_listener[i][0],  // Stack
                          STACK_SIZE,             // Size
                          &listener_thread_handle[i], // Handle
                          &listener_thread_data[i] // Thread data structure
            );

    // make the dummy event-grabber threads
    for ( i = 0; i < NDUMMIES; i++ )
        cyg_thread_create(PRIO_DUMMY,             // Priority
                          dummy,                  // entry
                          i,                      // entry parameter
                          "dummy",                // Name
                          &stack_dummy[i][0],     // Stack
                          STACK_SIZE,             // Size
                          &dummy_thread_handle[i], // Handle
                          &dummy_thread_data[i]   // Thread data structure
            );

    // Start those threads
    for ( i = 0; i < NLISTENERS; i++ )
        cyg_thread_resume(listener_thread_handle[i]);
    for ( i = 0; i < NDUMMIES; i++ )
        cyg_thread_resume(   dummy_thread_handle[i]);

    // and let them start up and start listening...
    cyg_thread_set_priority( self, PRIO_MASTERLOW );
    CYG_TEST_INFO("All listeners should be go now");
    cyg_thread_set_priority( self, PRIO_MASTERHIGH );

    for ( i = 0; i < NSENDERS; i++ ) {
        cyg_thread_create( (0 == i)
                           ?PRIO_SENDER_MID
                           : PRIO_SENDER_LOW,     // Priority
                           sender,                // entry
                           i,                     // entry parameter
                           "sender",              // Name
                           &stack_sender[i][0],   // Stack
                           STACK_SIZE,            // Size
                           &sender_thread_handle[i], // Handle
                           &sender_thread_data[i] // Thread data structure
            );
        cyg_thread_resume(sender_thread_handle[i]);
    }

    // Now we are still higher priority; so go low and let everyone else
    // have their head.  When we next run after this, it should all be
    // over.
    cyg_thread_set_priority( self, PRIO_MASTERLOW );

    cyg_semaphore_peek( &recv_sema, &i );
    CYG_TEST_CHECK( NLISTENERS == i, "Not enough recvs occurred!" );
    
    cyg_semaphore_peek( &send_sema, &i );
    CYG_TEST_CHECK( NLISTENERS == i, "Not enough sends occurred!" );

    CYG_TEST_PASS_FINISH("Master returned OK");
#endif
    CYG_TEST_NA( "No loopback devs" );
}

void
cyg_user_start(void)
{
    CYG_TEST_INIT();

    cyg_thread_create(PRIO_MASTERHIGH,            // Priority
                      master,                     // entry
                      0,                          // entry parameter
                      "master",                   // Name
                      &stack_master[0],           // Stack
                      MASTER_STACK_SIZE,          // Size
                      &master_thread_handle,      // Handle
                      &master_thread_data         // Thread data structure
            );
    cyg_thread_resume(master_thread_handle);      // Start it
}

// EOF multi_lo_select.c

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -