netif.cpp

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

CPP
597
字号
/* -*- Mode: C++ -*- *Header: *File: netif.C (for C++ & cc) *Author: Noda Itsuki *Date: 1996/02/14 *EndHeader: *//* *Copyright: Copyright (C) 1996-2000 Electrotechnical Laboratory. Itsuki Noda, Yasuo Kuniyoshi and Hitoshi Matsubara. Copyright (C) 2000, 2001 RoboCup Soccer Server Maintainance Group. Patrick Riley, Tom Howard, Daniel Polani, Itsuki Noda, Mikhail Prokopenko, Jan Wendler This file is a part of SoccerServer. This code is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *EndCopyright: */// [2000/01/14:I.Noda]//  escape prototype of recvfrom//  for escape conversion of signed-unsigned ints//#define recvfrom _escape_recvfrom//#ifdef HAVE_CONFIG_H#include "config.h"#endif#include "field.h"#include "param.h"#include "types.h"#include "utility.h"#include "object.h"#include "coach.h"#include "player.h"#include "random.h"#include "serializer.h"#include "clangparser.h"#include "clangmsg.h"//#include "coach_lang_comp.h"#include "clangmsgbuilder.h"#include "xpmholder.h"#include "remoteclient.h"#include "monitor.h"#include <rcssbase/net/udpsocket.hpp>#include <cstdio>#include <cstdlib>#include <cstring>#include <cerrno>// #ifdef __APPLE__// extern "C" {//     int isnan (double);// }// #endif/* *=================================================================== *Part: Recv *=================================================================== */template < class T >voidStadium::recv( std::vector< T >& clients ){    std::random_shuffle( clients.begin(), clients.end(),                         irand ); //rcss::random::UniformRNG::instance() );    for ( typename std::vector< T >::iterator i = clients.begin();          i != clients.end(); )    {        if ( (*i)->recv() == -1 )        {            ++i;        }    }}voidStadium::udp_recv_message(){    recv( M_remote_players );    recv( M_monitors );    while ( 1 )    {        char message[MaxMesg];        std::memset( &message, 0, sizeof( char ) * MaxMesg );        rcss::net::Addr cli_addr;        int len = M_player_socket.recv( message, MaxMesg, cli_addr );        if ( len > 0 )        {            //              std::cerr << "Got: ";            //              std::cerr.write( message, iMsgLength );            //              std::cerr << std::endl;            bool found = false;            for ( PlayerCont::iterator i = M_remote_players.begin();                  i != M_remote_players.end();                  ++i )            {                if ( (*i)->getDest() == cli_addr )                {                    (*i)->undedicatedRecv( message, len );                    found = true;                    break;                }            }            if ( ! found )            {                for ( MonitorCont::iterator i = M_monitors.begin();                      i != M_monitors.end();                      ++i )                {                    if ( (*i)->getDest() == cli_addr )                    {                        (*i)->undedicatedRecv( message, len );                        found = true;                        break;                    }                }            }            if ( ! found )            {                // a new monitor or a new player                /* chop newline */                if ( message[len - 1] == '\n' )                {                    --len;                }                message[len] = '\0';                if ( ! parseMonitorInit( message, cli_addr ) )                {                    parsePlayerInit( message, cli_addr );                }            }        }        else if ( errno != EWOULDBLOCK )        {            std::cerr << __FILE__ << ": " << __LINE__                      << ": Error recv'ing from socket: "                      << std::strerror( errno ) << std::endl;        }        if ( len < 0 )        {            break;        }    }}voidStadium::parsePlayerInit( const char * message,                          const rcss::net::Addr & cli_addr ){    if ( ! std::strncmp( message, "(reconnect ", std::strlen( "(reconnect " ) ) )    {        //              std::cerr << "Got reconnect" << std::endl;        // a player reconnects to the server        if ( playmode() == PM_PlayOn )        {            sendToPlayer( "(error cannot_reconnect_while_playon)", cli_addr );        }        else        {            Player * p = reconnectPlayer( message, cli_addr );            if ( p )            {                M_logger.writePlayerLog( *p, message, RECV );            }        }    }    else if ( ! std::strncmp( message, "(init ", std::strlen( "(init " ) ) )    {        // a new player connects to the server        Player * p = initPlayer( message, cli_addr );        if ( p )        {            M_logger.writePlayerLog( *p, message, RECV );        }    }    else    {        sendToPlayer( "(error unknown_command)", cli_addr );    }}boolStadium::parseMonitorInit( const char * message,                           const rcss::net::Addr & addr ){    double ver = 1.0;    if ( ! std::strcmp( message, "(dispinit)" ) )    {        if ( ServerParam::instance().maxMonitors() > 0             && ( static_cast< int >( M_monitors.size() )                  >= ServerParam::instance().maxMonitors()  ) )        {            sendToPlayer( "(error no_more_monitor)", addr );            return true;        }        Monitor * mon = new Monitor( *this, 1 );        if( ! mon->connect( addr ) )        {            delete mon;            return true;        }        if ( ! mon->setSenders() )        {            delete mon;            return true;        }        std::cout << "a new (v1) monitor connected\n";        mon->setEnforceDedicatedPort( false );        M_monitors.push_back( mon );        return true;    }    else if ( std::sscanf( message, "(dispinit version %lf)", &ver ) == 1 )    {        if ( ServerParam::instance().maxMonitors() > 0             && ( static_cast< int >( M_monitors.size() )                  >= ServerParam::instance().maxMonitors()  ) )        {            sendToPlayer( "(error no_more_monitor)", addr );            return true;        }//         if ( ver < 1.0 || 5.0 <= ver )//         {//             std::cout << "Unsupported monitor protocol version. " << ver//                       << std::endl;//             return true;//         }        // a new monitor connected        Monitor * mon = new Monitor( *this, ver );        if( ! mon->connect( addr ) )        {            delete mon;            return true;        }        if ( ! mon->setSenders() )        {            delete mon;            return true;        }        std::cout << "a new (v" << ver << ") monitor connected\n";        mon->setEnforceDedicatedPort( ver >= 2.0 );        M_monitors.push_back( mon );        // send server parameter information to monitor        if ( ver >= 3.0 )        {            mon->sendInit();        }        else if ( ver >= 2.0 )        {            mon->sendInit();        }        else if ( ver >= 1.0 )

⌨️ 快捷键说明

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