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

📄 netif.c

📁 在LINUX下运行的仿真机器人服务器源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* -*- 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 <rcssbase/net/udpsocket.hpp>#include <cstdio>#include <cstdlib>#include <cstring>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#if defined(HAVE_SYS_FILIO_H)#include <sys/filio.h>#elif defined(HAVE_SYS_IOCTL_H)#include <sys/ioctl.h>#endif#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <cerrno>#include <cmath>#include <fcntl.h>#ifdef __APPLE__extern "C" {    int isnan (double);}#endif#include <iostream>#ifdef HAVE_SSTREAM#include <sstream>#else#include <strstream>#endif#include "param.h"#include "types.h"#include "utility.h"#include "object.h"#include "field.h"#include "netif.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"void write_log ( Stadium& stad, const char *message, int flag);void write_log(Stadium& stad, Player& p, const char *message, int flag) ;void write_log(Stadium& stad, Coach& p, const char *message, int flag) ;void write_log(Stadium& stad, OnlineCoach& p, const char *message, int flag) ;Value distance(PVector *pos1, PVector *pos2) ;static char *PlayModeString[] = PLAYMODE_STRINGS;/* *=================================================================== *Part: Recv *=================================================================== */template < class T >void Stadium::recv ( std::vector< T >& clients ){    std::random_shuffle( clients.begin(), clients.end(), rcss::random::UniformRNG::instance() );        for( typename std::vector< T >::iterator i = clients.begin(); i != clients.end(); )    {        if( (*i)->recv() == -1 )            i++;    }}void Stadium::udp_recv_message(){  recv ( M_remote_players );  recv ( M_monitors );  for ( int iMsgLength = MaxMesg; iMsgLength >= 0; )    {      char message[MaxMesg];      memset ( &message, 0, sizeof ( char ) * MaxMesg ) ;                rcss::net::Addr cli_addr;      iMsgLength = MaxMesg;                iMsgLength = M_player_socket.recv( message, MaxMesg, cli_addr );           if ( iMsgLength > 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, iMsgLength );                  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, iMsgLength );                      found = true;                      break;                    }                }            }                    if ( !found )            {              // a new monitor or a new player//                unsigned long host = ntohl ( cli_addr.sin_addr.s_addr );//                int port = ntohs ( cli_addr.sin_port );                            /* chop newline */              if(message[iMsgLength - 1] == '\n')                 {                  iMsgLength--;                }	              message[iMsgLength] = NULLCHAR ;                            if ( !ParseMonitorCommand ( message, cli_addr ) )                {                    ParsePlayerCommand ( message, cli_addr );                }            }        }      else if( errno != EWOULDBLOCK )        {          std::cerr << __FILE__ << ": " << __LINE__               << ": Error recv'ing from socket: "               << strerror( errno ) << std::endl;        }    }}void Stadium::ParsePlayerCommand( const char *message,                                  const rcss::net::Addr& cli_addr ){		Player *p ;		static char tmp[MaxStringSize] ;		sscanf(message,"(%s",tmp);		if (!strcmp(tmp,"reconnect")) 		{//              std::cerr << "Got reconnect" << std::endl;				// a player reconnects to the server				if (mode == PM_PlayOn)                    udp_send( "(can't reconnect)", cli_addr );				else 				{                    p = reconnectClient( cli_addr, message) ;                    if(p == (Player *)NULL)                        udp_send("(error reconnect)", cli_addr );                    else                    {//                          std::cerr << "Sending info" << std::endl;//                              send_reconnect_info(*p) ;                        p->sendReconnect();                        p->sendServerParams();                        p->sendPlayerParams();                        p->sendPlayerTypes();                        p->sendChangedPlayers();//                          std::cerr << "Info Sent" << std::endl;                        write_log(*this, *p, message, RECV) ;                    }				}		}		else        {            // a new player connects to the server            p = newClient( cli_addr, message) ;            if(p == (Player *)NULL)                 udp_send("(error no_more_team_or_player_or_goalie)", cli_addr );            else            {                p->sendInit();                p->sendServerParams();                p->sendPlayerParams();                p->sendPlayerTypes();                p->sendChangedPlayers();                write_log(*this, *p, message, RECV) ;            }        }}bool Stadium::ParseMonitorCommand( const char *message, const rcss::net::Addr& addr){  float ver = 1.0;  if (!strcmp(message,"(dispinit)"))     {      std::cout << "a new (v1) monitor connected\n";      Monitor* mon = new Monitor ( this, 1 );      if( !mon->connect( addr ) )      {          delete mon;          return false;      }      mon->setEnforceDedicatedPort ( false );      M_monitors.push_back ( mon );      return true;    }  else if (sscanf(message,"(dispinit version %f)",&ver) == 1)    {      // a new monitor connected      std::cout << "a new (v" << ver << ") monitor connected\n";      Monitor* mon = new Monitor ( this, ver );            if( !mon->connect( addr ) )      {          delete mon;          return false;      }      mon->setEnforceDedicatedPort ( ver >= 3.0 );      M_monitors.push_back ( mon );      // send server parameter information to monitor      if ( ver >= 2.0 )        {          dispinfo_t2 di;                    di.mode = htons ( PARAM_MODE );          di.body.sparams = ServerParam::instance().convertToStruct ();          mon->RemoteClient::send ( (char*)&di, sizeof ( dispinfo_t2 ) );                    di.mode = htons ( PPARAM_MODE );          di.body.pparams = PlayerParam::instance().convertToStruct ();          mon->RemoteClient::send ( (char*)&di, sizeof ( dispinfo_t2 ) );                    di.mode = htons ( PT_MODE );          for ( int i = 0; i < PlayerParam::instance().playerTypes (); i++ )            {              di.body.ptinfo = player_types [ i ]->convertToStruct ( i );              mon->RemoteClient::send ( (char*)&di, sizeof ( dispinfo_t2 ) );            }        }      return true;    }  else    return false;}void Stadium::udp_recv_from_coach(){  recv ( M_remote_offline_coaches );   for ( int iMsgLength = MaxMesg; iMsgLength >= 0; )    {      char message[MaxMesg];      memset ( &message, 0, sizeof ( char ) * MaxMesg ) ;            rcss::net::Addr cli_addr;      iMsgLength = MaxMesg;            iMsgLength = M_offline_coach_socket.recv( message, MaxMesg,                                                cli_addr );                              if ( iMsgLength > 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, iMsgLength );                  found = true;                  break;                }            }          if ( !found )            {              // a new coach                            /* chop newline */              if(message[iMsgLength - 1] == '\n')                 {                  iMsgLength--;                }	              message[iMsgLength] = NULLCHAR ;                                          int n = coach->parse_init(*this,message, cli_addr ) ;              if ( n == -1 )                {                  coach->assignedp = True ;                  _Start(*this) ; // need to remove this line if we                  // dont want the server to start when the coach connects                  coach->parse_command( message );                  write_log(*this, *coach, message, RECV) ;                }              else if ( n == 1 )                {                  coach->assignedp = True ;                  write_log(*this, *coach, message, RECV) ;                }              }        }      else if( errno != EWOULDBLOCK )        {          std::cerr << __FILE__ << ": " << __LINE__               << ": Error recv'ing from socket: "               << strerror( errno ) << std::endl;        }    }}void Stadium::udp_recv_from_online_coach(){  recv ( M_remote_online_coaches );  for ( int iMsgLength = MaxMesg; iMsgLength >= 0; )    {      char message[MaxMesg];      memset ( &message, 0, sizeof ( char ) * MaxMesg ) ;            rcss::net::Addr cli_addr;      iMsgLength = MaxMesg;                  iMsgLength = M_online_coach_socket.recv( message, MaxMesg,                                                cli_addr );            if ( iMsgLength > 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, iMsgLength );                  found = true;                  break;                }            }          if ( !found )            {              // a new online coach                            /* chop newline */              if(message[iMsgLength - 1] == '\n')                 {                  iMsgLength--;                }	              message[iMsgLength] = NULLCHAR ;                            parseOnlineCoachInit ( message, cli_addr );            }        }      else if( errno != EWOULDBLOCK )        {          std::cerr << __FILE__ << ": " << __LINE__                << ": Error recv'ing from socket: "

⌨️ 快捷键说明

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