Example #1
0
void MainLayer::menuCloseCallback(Object* pSender)
{
	SocketClient* socketClient = SocketClient::createSocketClient("127.0.0.1",12345);
	socketClient->retain();
	SocketMgr::set(socketClient);

	socketClient = SocketMgr::get();

	auto callback = std::bind(&MainLayer::onLoginResponse,this,std::placeholders::_1);
	socketClient->registCallbackT<LoginResponse>(1,callback);

	if (socketClient->connect())
	{
		LoginRequest* loginRequestMessage = new LoginRequest();
		loginRequestMessage->set_username("cmzx3444");
		loginRequestMessage->set_password("123456");
		socketClient->send(loginRequestMessage);

		loginRequestMessage = new LoginRequest();
		loginRequestMessage->set_username("cmzx3444");
		loginRequestMessage->set_password("123456");
		socketClient->send(loginRequestMessage);

		loginRequestMessage = new LoginRequest();
		loginRequestMessage->set_username("cmzx3444");
		loginRequestMessage->set_password("123456");
		socketClient->send(loginRequestMessage);

		loginRequestMessage = new LoginRequest();
		loginRequestMessage->set_username("cmzx3444");
		loginRequestMessage->set_password("123456");
		socketClient->send(loginRequestMessage);
	}
	else
	{
		socketClient->release();
	}

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
void tst_ServerWorkerTests::login(QString name, QString pass)
{
    LoginRequest loginRequest;
    loginRequest.set_name(name);
    loginRequest.set_password(pass);

    mc->addMessage( &loginRequest );
    QByteArray ba = mc->toArray();
    worker->readyRead(ba);
    mc->Clear();
}
void tst_ServerWorkerTests::loginChangesUserStatus()
{
    LoginRequest loginRequest;
    LoginRequestResponse loginResponse;

    loginRequest.set_name(QStringLiteral("testuser"));
    loginRequest.set_password(QStringLiteral("some_password"));

    mc->addMessage( &loginRequest );

    QByteArray ba = mc->toArray();
    worker->readyRead(ba);

    mc->Clear();
    QVERIFY(mc->fromArray(binaryMessage));
    QVERIFY(loginResponse.fromArray(mc->getCapsule(0).getData()));
    QVERIFY(loginResponse.replay() == protbuf::Replay::UserAlreadyLogged);
}
void tst_ServerWorkerTests::loginToServerBadUser()
{
    LoginRequest loginRequest;
    LoginRequestResponse loginResponse;

    loginRequest.set_name(QStringLiteral("bad_user"));
    loginRequest.set_password(QStringLiteral("bad_passwd"));

    mc->addMessage( &loginRequest );

    QByteArray ba = mc->toArray();
    worker->readyRead(ba);

    mc->Clear();
    QVERIFY(mc->fromArray(binaryMessage));
    QVERIFY(loginResponse.fromArray(mc->getCapsule(0).getData()));
    QVERIFY(loginResponse.replay() == protbuf::Replay::LoginDeny);
}