当前位置: 首页>>代码示例>>C++>>正文


C++ NetClient::GetID方法代码示例

本文整理汇总了C++中NetClient::GetID方法的典型用法代码示例。如果您正苦于以下问题:C++ NetClient::GetID方法的具体用法?C++ NetClient::GetID怎么用?C++ NetClient::GetID使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NetClient的用法示例。


在下文中一共展示了NetClient::GetID方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: ServerUpdate

//--------------------------------------
void ServerUpdate( float dt )
{
	NetClient client;
	uint32 commands_in;
	uint32 commands_out;
	Player* player;
	Bullet* b;

	// Advance time
	gClock->AdvanceTime( dt );

	// Update players
	UpdatePlayers( gPlayers, dt );

	// Update network
	gSession->OnUpdate();

	// Check packets
	while ( gServer.IsDataReady() )
	{
		// Get next packet
		gServer.ReceiveData( gReader, client );

		// Get player for this client
		player = GetPlayerByID( gPlayers, client.GetID() );

		// This client didn't connect correctly!
		if ( !player )
		{
			ConsolePrintf( CONSOLE_WARNING, "Server : Unknown client %u\n", client.GetID() );
			continue;
		}

		// Read commands
		commands_in = gReader.Read< uint32 >();
		commands_out = 0;

		// Client id message
		if ( commands_in & NC_HELLO )
		{
			commands_out |= NC_PLAYER_ID;
			commands_out |= NC_ADD;
		}

		// Client name message
		if ( commands_in & NC_NAME )
		{
			gReader.ReadString( player->name, MAX_NAME_LEN );
			ConsolePrintf( "Got name msg: %s\n", player->name );
		}

		// Client position update
		if ( commands_in & NC_LOCATION )
		{
			player->pos = gReader.Read< Vec2f >();
			player->rotation = gReader.Read< float >();
			
			// Replicate location message to other clients
			gWriter.Write( (uint32) NC_LOCATION );
			gWriter.Write( 1 );
			gWriter.Write( player->index );
			gWriter.Write( player->pos );
			gWriter.Write( player->rotation );
			for ( int i = 0; i < MAX_PLAYERS; ++i )
			{
				if ( gPlayers[i].active && i != player->index )
				{
					gServer.SendData( PacketWriter( gWriter ), gPlayers[i].address );
				}
			}
			gWriter.Clear();
		}

		// Client requesting to fire
		if ( commands_in & NC_FIRE )
		{
//			ConsolePrintf( "Server : Got fire command from %u\n", client.GetID() );
			b = ServerFire( player );
			if ( b )
			{
				// Replicate fire message to other clients
				gWriter.Write( (uint32) NC_FIRE );
				gWriter.Write( player->index );
				gWriter.Write( b->index );
				gWriter.Write( b->vel );
				for ( int i = 0; i < MAX_PLAYERS; ++i )
				{
					if ( gPlayers[i].active )
					{
						gServer.SendData( PacketWriter( gWriter ), gPlayers[i].address );
					}
				}
				gWriter.Clear();
			}
		}

		// Send commands back to client
		if ( commands_out )
		{
//.........这里部分代码省略.........
开发者ID:hydraskillz,项目名称:NetworkShooter,代码行数:101,代码来源:Server.cpp


注:本文中的NetClient::GetID方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。