netif.cpp

来自「2009 ROBOCUP 仿真2DSERVER 源码」· C++ 代码 · 共 597 行 · 第 1/2 页

CPP
597
字号
        {            // nothing to do        }        return true;    }    return false;}voidStadium::udp_recv_from_coach(){    recv( M_remote_offline_coaches );    while ( 1 )    {        char message[MaxMesg];        std::memset( &message, 0, sizeof( char ) * MaxMesg );        rcss::net::Addr cli_addr;        int len = M_offline_coach_socket.recv( message, MaxMesg,  cli_addr );        if ( len > 0 )        {            bool found = false;            for ( OfflineCoachCont::iterator i = M_remote_offline_coaches.begin();                  i != M_remote_offline_coaches.end();                  ++i )            {                if ( (*i)->getDest() == cli_addr )                {                    (*i)->undedicatedRecv( message, len );                    found = true;                    break;                }            }            if ( ! found )            {                // a new offline coach                // chop newline                if ( message[len - 1] == '\n' )                {                    --len;                }                message[len] = '\0';                parseCoachInit( message, cli_addr );            }        }        else if ( errno != EWOULDBLOCK )        {            std::cerr << __FILE__ << ": " << __LINE__                      << ": Error recv'ing from socket: "                      << strerror( errno ) << std::endl;        }        if ( len < 0 )        {            break;        }    }}boolStadium::parseCoachInit( const char * message,                         const rcss::net::Addr & cli_addr ){    if ( ! M_remote_offline_coaches.empty() )    {        sendToCoach( "(error already_have_offline_coach)", cli_addr );        return false;    }    char command[128];    int n = std::sscanf( message, "(%127[-0-9a-zA-Z.+*/?<>_]", command );    if( n < 1 )    {        sendToCoach( "(error illegal_command_form)", cli_addr );        return false;    }    // (init[ (version <Ver>)])    if ( ! std::strcmp( command, "init" ) )    {        Coach * coach = initCoach( message, cli_addr );        if ( coach )        {            std::cout << "a new (v" << coach->version()                      << ") offline coach connected" << std::endl;            M_logger.writeCoachLog( message, RECV );        }    }    else    {        _Start( *this ); // need to remove this line if we        // dont want the server to start when the coach connects        M_coach->parse_command( message );        M_logger.writeCoachLog( message, RECV );    }    return true;}voidStadium::udp_recv_from_online_coach(){    recv( M_remote_online_coaches );    while ( 1 )    {        char message[MaxMesg];        std::memset( &message, 0, sizeof ( char ) * MaxMesg );        rcss::net::Addr cli_addr;        int len = M_online_coach_socket.recv( message, MaxMesg, cli_addr );        if ( len > 0 )        {            bool found = false;            for ( OnlineCoachCont::iterator i = M_remote_online_coaches.begin();                  i != M_remote_online_coaches.end();                  ++i )            {                if ( (*i)->getDest() == cli_addr )                {                    (*i)->undedicatedRecv( message, len );                    found = true;                    break;                }            }            if ( ! found )            {                // a new online coach                /* chop newline */                if ( message[len - 1] == '\n' )                {                    --len;                }                message[len] = '\0';                parseOnlineCoachInit( message, cli_addr );            }        }        else if ( errno != EWOULDBLOCK )        {            std::cerr << __FILE__ << ": " << __LINE__                      << ": Error recv'ing from socket: "                      << strerror( errno ) << std::endl;        }        if ( len < 0 )        {            break;        }    }}voidStadium::parseOnlineCoachInit( const char * message,                               const rcss::net::Addr & addr ){    if ( ! std::strncmp( message, "(init ", std::strlen( "(init " ) ) )    {        OnlineCoach * olc = initOnlineCoach( message, addr );        if ( olc )        {            M_logger.writeOnlineCoachLog( *olc, message, RECV );        }    }    else    {        sendToOnlineCoach( "(error illegal_command_form)",                           addr );    }}voidStadium::sendToPlayer( const char * msg,                       const rcss::net::Addr & cli_addr ){    if ( M_player_socket.send( msg, std::strlen( msg ) + 1, cli_addr ) == -1 )    {        std::cerr << __FILE__ ": " << __LINE__                  << ": Error sending to socket: "                  << strerror( errno ) << std::endl;    }}voidStadium::sendToCoach( const char * msg,                      const rcss::net::Addr & cli_addr ){    if ( M_offline_coach_socket.send( msg, std::strlen( msg ) + 1, cli_addr ) == -1 )    {        std::cerr << __FILE__ ": " << __LINE__                  << ": Error sending to socket: "                  << strerror( errno ) << std::endl;    }}voidStadium::sendToOnlineCoach( const char * msg,                            const rcss::net::Addr & cli_addr ){    if ( M_online_coach_socket.send( msg, std::strlen( msg ) + 1, cli_addr ) == -1 )    {        std::cerr << __FILE__ ": " << __LINE__                  << ": Error sending to socket: "                  << strerror( errno ) << std::endl;    }}voidStadium::broadcastSubstitution( const int side,                                const int unum,                                const int player_type ){    char allies[64];    char enemies[64];    snprintf( allies, 64, "(change_player_type %d %d)", unum, player_type );    snprintf( enemies, 64, "(change_player_type %d)", unum );    // tell players    for ( int i = 0 ; i < MAX_PLAYER * 2; ++i )    {        if ( M_players[i]->state() == DISABLE             || M_players[i]->version() < 7.0 )        {            continue;        }        if ( ( side == LEFT && M_players[i]->team() == M_team_l )             || ( side == RIGHT && M_players[i]->team() == M_team_r ) )        {            M_players[i]->send( allies );        }        else        {            M_players[i]->send( enemies );        }    }    // tell coaches    if ( M_olcoaches[1]         && M_olcoaches[1]->assigned()         && M_olcoaches[1]->version() >= 7.0 )    {        if ( side == RIGHT )        {            //team_r->olcoach()->send( allies );            M_olcoaches[1]->send( allies );        }        if ( side == LEFT )        {            //team_r->olcoach()->send( enemies );            M_olcoaches[1]->send( enemies );        }    }    if ( M_olcoaches[0]         && M_olcoaches[0]->assigned()         && M_olcoaches[0]->version() >= 7.0 )    {        if ( side == LEFT )        {            //team_l->olcoach()->send( allies );            M_olcoaches[0]->send( allies );        }        if ( side == RIGHT )        {            //team_l->olcoach()->send( enemies );            M_olcoaches[0]->send( enemies );        }    }    // TODO: send to offline coach    char buffer[64];    snprintf( buffer, 64,              "(change_player_type %s %d %d)",              ( side == LEFT ? "l" : "r" ),              unum, player_type );    for ( MonitorCont::iterator i = M_monitors.begin();          i != M_monitors.end(); ++i )    {        (*i)->sendMsg( MSG_BOARD, buffer );    }    M_logger.writeTextLog( buffer, SUBS );}

⌨️ 快捷键说明

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