Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C++

Push Framework - A C++ toolkit for high performance server development

Rate me:
Please Sign up or sign in to vote.
4.96/5 (86 votes)
23 May 2012Apache15 min read 259.9K   26.9K   316  
Write asynchronous, multithreaded servers in a few lines of code. Monitor realtime activity with a deploy-only dashboard.
#include "StdAfx.h"
#include "ChatParticipantFactory.h"

#include "ChatParticipant.h"

ChatParticipantFactory::ChatParticipantFactory(void)
{
}

ChatParticipantFactory::~ChatParticipantFactory(void)
{
}

int ChatParticipantFactory::onFirstRequest( PushFramework::IncomingPacket& request, void* lpContext, PushFramework::Client*& lpClient, PushFramework::OutgoingPacket*& lpPacket )
{
	LoginRequest& packet = (LoginRequest&) request;
	
	std::string pseudo = packet.Pseudo();

	int* pQuestion = (int*) lpContext;
	if (packet.LoginPuzzleReply() != ( (*pQuestion) + 1 ) )
	{
		LoginResponse* pLoginResponse = new LoginResponse;
		pLoginResponse->BAccepted(false);
		pLoginResponse->Msg("login puzzle is false");

		lpPacket = pLoginResponse;

		return PushFramework::ClientFactory::RefuseRequest;
	}
	
	ChatParticipant* pClient = new ChatParticipant(pseudo);
	lpClient = pClient;

	return PushFramework::ClientFactory::CreateClient;
}

PushFramework::OutgoingPacket* ChatParticipantFactory::onNewConnection( void*& lpContext )
{
	cout << "new connection, sending challenge" << std::endl;
	//Send the login challenge :
	LoginPuzzleResponse* pPuzzle = new LoginPuzzleResponse;

	int *randomVal = new int;
	*randomVal = rand()%100;

	pPuzzle->PuzzleQuestion(*randomVal); 

	lpContext = (void*) randomVal;

	return pPuzzle;
}

void ChatParticipantFactory::disposeClient( PushFramework::Client* pClient )
{
	delete pClient;
}

void ChatParticipantFactory::onBeforeDisposeClient( PushFramework::Client* pClient )
{
	getServer()->getBroadcastManager()->removePacket(pClient->getKey(), 0, "participants");
	
	ParticipantOutResponse* pPacket = new ParticipantOutResponse;
	pPacket->Pseudo() = pClient->getKey();

	getServer()->getBroadcastManager()->pushPacket(pPacket, "signals", "", 0);
}

void ChatParticipantFactory::onClientConnected( ClientKey key )
{
	//When a client connects.
	ChatParticipant* pParticipant = (ChatParticipant*) getClient(key);
	if (pParticipant)
	{
		LoginResponse* pLoginResponse = new LoginResponse;
		pLoginResponse->BAccepted(true);
		pLoginResponse->Msg("welcome to server");
		pParticipant->pushPacket(pLoginResponse);


		ParticipantInResponse* pPacket = new ParticipantInResponse;
		pPacket->Pseudo(key);
		getServer()->getBroadcastManager()->pushPacket(pPacket, "participants", key, 0);


		returnClient(key);
	}
}

void ChatParticipantFactory::onClientDisconnected( ClientKey clientKey )
{
	ChatParticipant* pParticipant = (ChatParticipant*) getClient(clientKey);
	if(!pParticipant)
		return;

	pParticipant->disconnect(false);

	returnClient(clientKey);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Technical Lead
Tunisia Tunisia
Services:
http://www.pushframework.com/?page_id=890

Comments and Discussions