Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 23 May 2012
Write asynchronous, multithreaded servers in a few lines of code. Monitor realtime activity with a deploy-only dashboard.
ChatApplication.zip
ChatAPI
ChatClient
res
.svn
entries
prop-base
ChatClient.ico.svn-base
props
text-base
ChatClient.ico.svn-base
ChatClient.rc2.svn-base
tmp
prop-base
props
text-base
ChatClient.ico
ChatPackets
ChatProtocol
ChatServer
ChatServer.vcproj.INTERNAL.Ahmed.Charfeddine.user
output
ChatServer.ini
TCPSocket
TCPSocket.zip
XMLProtocol
XMLProtocol.zip
ChatRobots.zip
ChatRobots
ChatRobots.ini
ProtoBufExample.zip
ProtoBufExampleClient
ProtoBufExampleProtocol
requests.pb.cc
responses.pb.cc
ProtoBufExampleServer
PushFramework_Essentials.zip
include
PushFramework_Sources.zip
private
QoS.zip
QoSExampleClient
QoSExampleProtocol
QoSExampleServer
// QoSExampleServer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "QoSClientFactory.h"
#include "QoSService.h"
#include <process.h>



#define nMaxPacket 200
#define nMaxReceiveBuffer 8192 
#define nMaxSendBuffer 8192*4 
#define nMaxBytesPerSend 8192
#define nMaxBytesPerceive 8192
#define nBroadcastThreshold -1 
#define nSmallPrioritPacket 5000
#define nMiddlePriorityPacket 6000
#define nHigPriorityPacket -1

#define nEmergencyBufferSize 8192



class MyServer : public PushFramework::Server
{
private:
	virtual PushFramework::OutgoingPacket* getTestPacket()
	{
		CQoSPacket* pPacket = new CQoSPacket;

		pPacket->setPacketId(1);
		pPacket->setChannelSourceId(1);

		return pPacket;
	}
	virtual void disposeOutgoingPacket(PushFramework::OutgoingPacket* pPacket)
	{
		CQoSPacket* pQoSPacket = (CQoSPacket*) pPacket;
		delete pQoSPacket;
	}
	
};

MyServer server;

HANDLE m_hEventAbort;

typedef struct threadPushCtxt
{
	unsigned int channelId;
	unsigned int pushRate;//Packets per seconds.
}threadPushCtxt;

void startThread(unsigned int channelId, unsigned int pushRate);
void PushPacket(unsigned int channelId, unsigned int uPacketId);
static unsigned __stdcall threadProc(LPVOID WorkContext);


class Scenario
{
public:
	virtual void CreateBroadcastingGroups() = 0;
	virtual void CreatePublishers() = 0;
};

class Scenario1 :public Scenario
{
public:
	virtual void CreateBroadcastingGroups()
	{
		server.getBroadcastManager()->createChannel("1", 100, false, 10, 10);
		server.getBroadcastManager()->createChannel("2", 100, false, 10, 10);
	}
	virtual void CreatePublishers()
	{
		startThread(1, 10000);
		startThread(2, 10000);
	}
};

class Scenario2 :public Scenario
{
public:
	virtual void CreateBroadcastingGroups()
	{
		server.getBroadcastManager()->createChannel("1", 100, false, 10, 10);
		server.getBroadcastManager()->createChannel("2", 100, false, 5, 10);
		server.getBroadcastManager()->createChannel("3", 100, false, 5, 20);
		server.getBroadcastManager()->createChannel("4", 100, false, 5, 5);
	}
	virtual void CreatePublishers()
	{
		startThread(1, 10000);
		startThread(2, 10000);
		startThread(3, 10000);
		startThread(4, 10000);
	}
};
class Scenario3 :public Scenario
{
public:
	virtual void CreateBroadcastingGroups()
	{
		server.getBroadcastManager()->createChannel("1", 100, false, 5, 10);
		server.getBroadcastManager()->createChannel("2", 100, false, 5, 20);
		server.getBroadcastManager()->createChannel("3", 100, false, 5, 5);
	}
	virtual void CreatePublishers()
	{
		startThread(1, 10000);
		startThread(2, 10000);
		startThread(3, 10000);
	}
};



int _tmain(int argc, _TCHAR* argv[])
{
	server.setServerInfos("QoS server");

	server.registerService(1, new CQoSService, "service");


	server.setClientFactory(new CQoSClientFactory);

	server.setLoginExpiryDuration(100);
	server.setProtocol(new QoSExampleProtocol);


	PushFramework::ListenerOptions options;
	options.uSendBufferSize = nMaxBytesPerSend;
	options.uReadBufferSize = nMaxBytesPerceive;
	options.uIntermediateSendBufferSize = nMaxSendBuffer;
	options.uIntermediateReceiveBufferSize = nMaxReceiveBuffer;


	server.createListener(2010, &options);


	//server.setWorkerCount(1);



	Scenario* pScenario = new Scenario3();


	pScenario->CreateBroadcastingGroups();



	try
	{
		server.start(true);
	}
	catch (std::exception& e)
	{
		cout << "Failed to start server. Exception : \n" << e.what() << std::endl;
		return 0;
	}
	//Abort event.
	m_hEventAbort = CreateEvent(NULL, TRUE, FALSE, NULL);
	ResetEvent(m_hEventAbort);




	pScenario->CreatePublishers();

	int ch;

	do 
	{
		ch = _getch();
		ch = toupper(ch);
	} while (ch !='Q');
	/*
	SetEvent(m_hEventAbort);
		CloseHandle(m_hEventAbort);
	
		Sleep(100);*/
	

	server.stop();


	delete pScenario;
	return 0;
}

void startThread(unsigned int channelId, unsigned int pushRate)
{
	threadPushCtxt* pCtxt = new threadPushCtxt;
	pCtxt->channelId = channelId;
	pCtxt->pushRate = pushRate;


	UINT  nThreadID;

	HANDLE hThread = (HANDLE)_beginthreadex(NULL,// Security
		0,						// Stack size - use default
		threadProc,     		// Thread fn entry point
		pCtxt,			// Param for thread
		0,						// Init flag
		&nThreadID);
}

void PushPacket(unsigned int channelId, unsigned int uPacketId)
{
	//Now Pump Packets into channels :
	CQoSPacket* pPacket = new CQoSPacket;

	char pChannelName[10];
	sprintf(pChannelName, "%d", channelId);


	pPacket->setPacketId(uPacketId);
	pPacket->setChannelSourceId(channelId);

	server.getBroadcastManager()->pushPacket(pPacket, pChannelName);

}

static unsigned __stdcall threadProc(LPVOID WorkContext)
{
	threadPushCtxt* pCtxt = reinterpret_cast<threadPushCtxt*>(WorkContext);


	unsigned int uPacketID = 1;



	//Periodic Timer
	HANDLE m_hEventTimer = CreateEvent(NULL, TRUE, FALSE, NULL);
	ResetEvent(m_hEventTimer);

	//
	HANDLE			hWaits[2];
	hWaits[0]		= m_hEventTimer;
	hWaits[1]		= m_hEventAbort;

	UINT			sleepTimeMs=100;//1 second.

	int nPacketsPerDeciSecond = pCtxt->pushRate / 10;

	//

	while (true)
	{
		PushPacket(pCtxt->channelId, uPacketID);
		uPacketID++;
		//Sleep(50);
	}
	return 0;

	while(true)
	{
		MMRESULT result = timeSetEvent(sleepTimeMs, 1,
			(LPTIMECALLBACK) m_hEventTimer, 0, TIME_ONESHOT|TIME_CALLBACK_EVENT_SET);
		_ASSERT(result != NULL);

		// Sleep here until the timer interval ends or abort occurs
		if(WaitForMultipleObjects(2, hWaits, FALSE, INFINITE)==0)
		{
			for(int i=0;i<nPacketsPerDeciSecond;i++){
				PushPacket(pCtxt->channelId, uPacketID);
				uPacketID++;
			}

			ResetEvent(m_hEventTimer);
		}
		else
		{
			//m_hEventAbort is set : break from while.
			break;
		}
		//
	}
	CloseHandle(m_hEventTimer);
	//quit blocking.
	return 0;
}

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

Share

About the Author

Ahmed Charfeddine
Technical Lead
Tunisia Tunisia
Engineer in telecommunications, C++ developer and Amateur Chess Programmer.
Websites:
http://www.pushframework.com
http://www.batchdocument.com
http://www.virtualglobe3d.com
http://www.chesscomposer.com
 
I am open to any collaboration or freelance project.
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 23 May 2012
Article Copyright 2011 by Ahmed Charfeddine
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid