Пример #1
0
////////////////////////////////////////////////////////////////////////////////
// leave
eSysStateReturn GaMatchmakingState::leave()
{
	BcScopedLock< BcMutex > Lock( Lock_ );
	BcPrintf( "Leave!\n" );
	if( pSession_ )
	{
		irc_disconnect( pSession_ );
		BcSleep( 0.1f );
		BcThread::join();
		BcSleep( 0.1f );
	}
	return sysSR_FINISHED;
}
Пример #2
0
//////////////////////////////////////////////////////////////////////////
// execute
//virtual
void SysKernel::execute()
{
	// Set main thread.
	extern void BcSetGameThread();
	BcSetGameThread();

	// Run until there are no more systems to run.
	do
	{
		// Mark main timer.
		MainTimer_.mark();
		
		// Tick systems.
		tick();
		
		// Sleep if we have a fixed rate specified, otherwise just yield.
		if( TickRate_ > 0.0f )
		{
			BcReal TimeSpent = MainTimer_.time();
			SleepAccumulator_ += BcMax( ( TickRate_ ) - TimeSpent, 0.0f );
		
			if( SleepAccumulator_ > 0.0f )
			{
				BcReal SleepTime = SleepAccumulator_;
				SleepAccumulator_ -= SleepTime;
				BcSleep( BcMin( SleepTime, TickRate_ ) );
			}
		}
		else
		{
			BcYield();
		}

		// Store frame time.
		FrameTime_ = BcMin( MainTimer_.time(), TickRate_ * 4.0f );

		BcAssert( FrameTime_ >= 0.0f );
	}
	while( SystemList_.size() > 0 );
}
Пример #3
0
////////////////////////////////////////////////////////////////////////////////
// update
eSysStateReturn GaMatchmakingState::main()
{
	BcScopedLock< BcMutex > Lock( Lock_ );
	BcReal Delta = SysKernel::pImpl()->getFrameTime();

	switch( HandshakeState_ )
	{
	case HSS_STUN:
		{
			// Only do once.
			if( MappedHandshakeAddr_ == 0 )
			{
				if( doSTUN() )
				{
					SysID_ = static_cast< BcU32 >( BcHash( (BcU8*)&MappedHandshakeAddr_, sizeof( MappedHandshakeAddr_ ) ) ); // Hash the mapped address to we don't broadcast it.
					if( MappedHandshakeAddr_ != 0 )
					{
						HandshakeState_ = HSS_IDLE;
					}
				}
				else
				{
					HandshakeState_ = HSS_STUN;
				}
			}
			else
			{
				HandshakeState_ = HSS_IDLE;
			}
		}
		break;
	case HSS_IDLE:
		{
			ConnectTimer_ -= Delta;

			if( ConnectTimer_ < 0.0f )
			{
				if( pSession_ == NULL )
				{
					pSession_ = irc_create_session( &Callbacks_ );
				}

				if( pSession_ != NULL && !irc_is_connected( pSession_ ) )
				{
					irc_set_ctx( pSession_, this );

					std::string Channel = "#testchannel";
					BcFile File;
					if( File.open( "config.json" ) )
					{
						char* pData = new char[ File.size() ];
						File.read( pData, File.size() );
						Json::Reader Reader;
		
						Json::Value Root;
						if( Reader.parse( pData, pData + File.size(), Root ) )
						{
							Channel = Root["channel"].asCString();
						}
						delete [] pData;
					}

					BcSPrintf( ScreenName_, "%s_%x", "PSY", BcRandom::Global.rand() );
					BcSPrintf( Channel_, Channel.c_str() );

					// Connect to the server.
					int RetVal = irc_connect( pSession_, "www.neilo.gd", 8000, NULL, ScreenName_, ScreenName_, ScreenName_ );

					if( RetVal == 0 )
					{
						// Start the thread to tick the client.
						BcThread::start( "EvtBridgeIRC" );

						ClientID_ = BcErrorCode;
						RemoteHandshakeAddr_ = 0;
						RemoteHandshakePort_ = 0;
						//LocalHandshakeAddr_ = 0;
						//LocalHandshakePort_ = 0;
						//MappedHandshakeAddr_ = 0;
						//MappedHandshakePort_ = 0;
						HandshakeState_ = HSS_WAIT_INVITE;
					}
					else
					{
						BcThread::join();

						irc_destroy_session( pSession_ );
						pSession_ = NULL;
					}
				}
			}
		}
		break;

	case HSS_WAIT_INVITE:
		{
			InviteTimer_ -= Delta;

			if( InviteTimer_ < 0.0f )
			{
				InviteTimer_ = BcAbs( BcRandom::Global.randReal() ) * 5.0f + 5.0f;

				// Send play with me message to channel.

				BcChar PlayBuffer[256];
				BcSPrintf( PlayBuffer, "REQ:%u", SysID_ );
				irc_cmd_msg( pSession_, Channel_, PlayBuffer );
			}
		}
		break;

	case HSS_WAIT_ADDR:
		{
			HandshakeTimer_ -= Delta;

			if( HandshakeTimer_ < 0.0f )
			{
				HandshakeState_ = HSS_WAIT_INVITE;
			}
		}
		break;

	case HSS_COMPLETE:
		{
			BcPrintf("GaMatchmakingState: Complete! ClientID of ours is %u\n", ClientID_);
			return sysSR_FINISHED;
		}
		break;
	}

	if( HandshakeState_ != HSS_STUN )
	{
		if( HandshakeState_ != HSS_IDLE && ( pSession_ == NULL || !irc_is_connected( pSession_ ) ) )
		{
			BcSleep( 0.1f );
			BcThread::join();
			BcSleep( 0.1f );
			if( pSession_ != NULL )
			{
				irc_destroy_session( pSession_ );
				pSession_ = NULL;
			}
			HandshakeState_ = HSS_IDLE;
			ConnectTimer_ = 10.0f;
		}
	}

	return sysSR_CONTINUE;
}