Ejemplo n.º 1
0
bool EClientSocket::eConnect( const char *host, UINT port, int clientId, bool extraAuth)
{
	// already connected?
	if( isConnected()) {
		getWrapper()->error( NO_VALID_ID, ALREADY_CONNECTED.code(), ALREADY_CONNECTED.msg());
		return false;
	}

	// init sockets
	AfxSocketInit();

	// close open connection if there was one
	eDisconnect();

	// create socket
	m_pSocket.reset(new MySocket(this));
	if( !m_pSocket->Create()) {
		eDisconnect();
		getWrapper()->winError( "Failed to create socket", GetLastError() );
		getWrapper()->error( NO_VALID_ID, FAIL_CREATE_SOCK.code(), FAIL_CREATE_SOCK.msg());
		return false;
	}

	// use local machine if no host passed in
	if( !(host && *host)) {
		host = "127.0.0.1";
	}

	// connect to server
	if( !m_pSocket->Connect(host, port)) {
		int lastError = GetLastError();
		if( lastError != WSAEWOULDBLOCK && !handleSocketError(GetLastError())) {
			return false;
		}
	}

	setClientId( clientId);
	setExtraAuth( extraAuth);

	{
		// Wait till we are fully connected (or for an error)
		CWinThread* pThread = AfxGetThread();
		while( m_pSocket.get() && !isConnected()) {
			if (!pThread->PumpMessage())
				return false;
		}
	}
	return true;
}
Ejemplo n.º 2
0
void CSbproAux::DoEvents()
{
	CWinThread* pThread = AfxGetThread();
	pThread->OnIdle(0);
	MSG msg;
    for (int i = 0;
    	 i < 100 && ::PeekMessage(&msg,0,0,0,PM_NOREMOVE);
    	 ++i)
    {
    	if (msg.message == WM_QUIT)
    		break;

		pThread->PumpMessage();
	}
}