Click here to Skip to main content
13,096,660 members (85,163 online)
Click here to Skip to main content

Stats

175.3K views
24.7K downloads
305 bookmarked
Posted 23 Mar 2011

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.
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
ChatRobots.ini
ProtoBufExampleClient
ProtoBufExampleProtocol
requests.pb.cc
responses.pb.cc
ProtoBufExampleServer
include
PushFramework.dll
PushFramework.lib
private
QoSExampleClient
QoSExampleProtocol
QoSExampleServer
// ChatServer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include "DirectChatRequestService.h"
#include "JoinRoomRequestService.h"
#include "LeaveRoomRequestService.h"
#include "RoomChatRequestService.h"
#include "LogoutRequestService.h"

#include "ChatParticipantFactory.h"

class ChatServer : public PushFramework::Server
{
	virtual void disposeOutgoingPacket(PushFramework::OutgoingPacket* pPacket)
	{
		delete pPacket;
	}
};



typedef struct Options_t
{
	int port;
	int monitorPort;
	std::string monitorPwd;
	int sampleRate;
	bool disableRooms;
}Options_t;
void readConfig(std::string& strIniFile, Options_t& options);
bool getConfigFile( std::string& path );
Options_t globalOptions;




int _tmain(int argc, _TCHAR* argv[])
{
	std::string strIniFile;
	if (!getConfigFile(strIniFile)){
		cout << "Unable to find conf file" << endl;
		return -1;
	}
	readConfig(strIniFile, globalOptions);


	ChatServer server;

	server.setServerInfos("Chat Server by Ahmed Charfeddine");



	server.registerService(DirectChatRequestID, new DirectChatRequestService, "directChat");
	server.registerService(JoinRoomRequestID, new JoinRoomRequestService, "joinRoom");
	server.registerService(LeaveRoomRequestID, new LeaveRoomRequestService, "leaveRoom");
	server.registerService(RoomChatRequestID, new RoomChatRequestService, "roomChat");
	server.registerService(LogoutRequestID, new LogoutRequestService, "logout");

	server.setClientFactory(new ChatParticipantFactory);
	server.setMaxConnections(10000);
	server.setLoginExpiryDuration(60);
	server.setProtocol(new ChatServerProtocol);

	PushFramework::ListenerOptions options;
	options.listeningBackLog = 10000;
	options.uSendBufferSize = 8196;
	options.uReadBufferSize = 8192;
	options.uIntermediateSendBufferSize = 8192*2;
	options.uIntermediateReceiveBufferSize = 8192*3;


	server.createListener(globalOptions.port, &options);
	server.enableRemoteMonitor(globalOptions.monitorPort, globalOptions.monitorPwd.c_str());
	server.enableProfiling(globalOptions.sampleRate);

	//Broadcast channels :

	if (!globalOptions.disableRooms)
	{
		server.getBroadcastManager()->createChannel("participants", 1000, false, 10, 20);
		server.getBroadcastManager()->createChannel("signals", 20, false,  7, 1);

		server.getBroadcastManager()->createChannel("room1", 20, true, 5, 3);
		server.getBroadcastManager()->createChannel("room2", 20, true, 5, 3);
		server.getBroadcastManager()->createChannel("room3", 20, true, 5, 3);
	}
	
	


	


	try
	{
		server.start(true);
	}
	catch (std::exception& e)
	{
		cout << "Failed to start server. Exception : " << e.what() << std::endl;
		return 0;
	}

	RoomsResponse* pRoomsPacket = new RoomsResponse;
	pRoomsPacket->addRoom("room1");
	pRoomsPacket->addRoom("room2");
	pRoomsPacket->addRoom("room3");
	server.getBroadcastManager()->pushPacket(pRoomsPacket, "signals", "", 0);
	

	int ch;
	
	do 
	{
		ch = _getch();
		ch = toupper(ch);
	} while (ch !='Q');

	server.stop();

	return 0;
}




void readConfig(std::string& strIniFile, Options_t& options)
{
	options.port = GetPrivateProfileIntA("server",  "port", 2011, strIniFile.c_str());
	options.monitorPort = GetPrivateProfileIntA("server",  "monitoringPort", 2012, strIniFile.c_str());

	char pwd[256];
	GetPrivateProfileStringA("server",  "monitoringPwd", "ahmed", pwd, 256, strIniFile.c_str());
	options.monitorPwd = pwd;

	options.sampleRate = GetPrivateProfileIntA("server",  "sampleRate", 10, strIniFile.c_str());
	options.disableRooms = (GetPrivateProfileIntA("chat",  "disableRooms", 0, strIniFile.c_str()) == 1);
}
bool getConfigFile( std::string& path )
{
	char szPath[MAX_PATH];
	if( !GetModuleFileNameA( NULL, szPath, MAX_PATH ) )
		return 1;
	std::string strPath(szPath);
	std::string::size_type pos1 = strPath.rfind('\\');
	std::string::size_type pos2 = strPath.rfind('/');
	std::string::size_type pos = (pos1==-1) ? pos2 : (pos2==-1) ? pos1 : (max(pos1, pos2));
	szPath[pos] = '\0';
	std::string strModuleDirectory(szPath);

	path = strModuleDirectory + "\\ChatServer.ini";

	return true;
}

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
Services:
http://www.pushframework.com/?page_id=890

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 23 May 2012
Article Copyright 2011 by Ahmed Charfeddine
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid