Example #1
0
IClient* CSmartClient::_Connect()
{
	if ( !IsConnecting() )
		return NULL;

#ifdef	CONSOLE_DEBUG
					cprintf( "Begin connect\n");
#endif	

	IClient *pClient = NULL;
	/*
	 * There is connectted the heaven by the rainbow
	 */

	pfnCreateClientInterface pClientFactroyFun = ( pfnCreateClientInterface )( m_theRainbowLib.GetProcAddress( _T( "CreateInterface" ) ) );
	IClientFactory *pClientFactory = NULL;
	
	if ( pClientFactroyFun && SUCCEEDED( pClientFactroyFun( IID_IClientFactory, reinterpret_cast< void ** >( &pClientFactory ) ) ) )
	{
		pClientFactory->SetEnvironment( 1024 * 1024 );
		
		pClientFactory->CreateClientInterface( IID_IESClient, reinterpret_cast< void ** >( &pClient ) );
		
		SAFE_RELEASE( pClientFactory );
	}
	
	if ( pClient )
	{
		pClient->Startup();
		
		pClient->RegisterMsgFilter( reinterpret_cast< void * >( this ), EventNotify );
		
		if ( FAILED( pClient->ConnectTo( m_sSvrIP.c_str(), m_nSvrPort ) ) )
		{
			pClient->Cleanup();
			
			SAFE_RELEASE( pClient );
				
#ifdef	CONSOLE_DEBUG
					cprintf( "Reconnect Failed\n");
#endif	
			::Sleep( 5000 );
			
			return NULL;
		}
		else
		{
			SetClientStatus(enumClientVerify);	
		}
	}
	
	return pClient;
}
/*
 * main
 */
int main(int argc, char* argv[])
{
	setcolor( enumCyanonBlack );

	cout << "Welcome to the example that it can be to test relay server." << endl << endl;

	setcolor( enumDefault );

	IClient *pClient = NULL;

	pfnCreateClientInterface pFactroyFun = 
		(pfnCreateClientInterface)g_theRainbowDll.GetProcAddress("CreateInterface");

	IClientFactory *pClientFactory = NULL;
 
	ASSERT(pFactroyFun);

	if (SUCCEEDED(pFactroyFun(IID_IClientFactory, reinterpret_cast< void ** >(&pClientFactory))))
	{
		pClientFactory->SetEnvironment(8192);
		
		pClientFactory->CreateClientInterface(IID_IESClient, reinterpret_cast< void ** >(&pClient ));
		
		pClientFactory->Release();
	}

	ASSERT(pClient);

	pClient->Startup();

	pClient->RegisterMsgFilter( reinterpret_cast< void * >( pClient ), ClientEventNotify );

	//cout << "Relay Server IP: ";
	string relayip = "192.168.20.15";
	//cin >> relayip;

	if ( FAILED( pClient->ConnectTo( relayip.c_str(), 7777 ) ) )
	{
		cout << "To connect the account server is failed!" << endl;

		exit( -1 );
	}

	IClient *pClonClient = NULL;
	pClient->QueryInterface( IID_IESClient, reinterpret_cast< void ** >( &pClonClient ) );

	unsigned int threadID = 0;
	
	HANDLE hThread = (HANDLE)::_beginthreadex(0,
		0, 
		ThreadFunction,
		( void * )pClonClient,
		0, 
		&threadID );

	ASSERT( hThread );
	
	SAFE_CLOSEHANDLE( hThread );		

	//m_theTaskEvent.Wait();

	/*
	 * System command
	 */
_TRY_AGAIN:
	if (argc == 1)
	{
		string sInfo;
		cout << "Command:" << endl;

		cin >> sInfo;
		if (0 == sInfo.compare( "relayto" ))
		{
			cout << "IP:";
			string sip;
			cin >> sip;
			cout << "id:";
			unsigned long id;
			cin >> id;

			for (int i = 0; i < 1000; i++)
				RelayTo(pClient, inet_addr(sip.c_str()), id);
		}