首页 > C/C++ > PushFramework 高性能服务器的C++框架的简单应用
2013
04-05

PushFramework 高性能服务器的C++框架的简单应用

PushFramework 是一个开源C + +的高性能服务器开发工具包。支持WINDOWS/LINUX系统。设计是理想的情况下,大量的连接的客户端进行实时。除了处理标准的请求-响应工作流程,图书馆也被设计成数据流(广播数据),大群体的关连客户保持整体效率具体到每个广播队列的QoS规则。使用部署仪表板你可以监视实时活动。它会显示详细的指标游客,带宽,处理(吞吐量)和广播服务质量。

 

 

例子的架构是 PushFramework + google Protocol Buffers + TCPSocket

PushFramework http://www.pushframework.com/

google Protocol Buffers http://code.google.com/p/protobuf/

用处:客户端到服务端的消息发送、客户端到客户端的消息发送、客户端广播消息

服务端主要代码

 

//main.cpp
#include "stdafx.h"
#include "server.h"
#include "protocol.h"
#include "clientfactory.h"
#include "services.h"

AppClientFacory clientFactory;
ProtobufProtocol protocol;

//消息发送
AppEchoService service1;
//消息重定向
AppRedirectService service2;
//消息广播
AppBroadcastService service3;

void configure();

int main(int argc, char* argv[])
{
	// Configure and link all objects together before running the server.
	configure();

	bool runInteractively = false;
	if (argc == 2)
	{
		char param = argv[1][0];
		if (param == 'i' || param == 'I')
		{
			//Install service into SCM :
			theServer.Install(L"App Server");
			return -1;
		}
		if (param == 'u' || param == 'U')
		{
			theServer.UnInstall();
			return -1;
		}
		if (param == 'm' || param == 'M')
		{
			runInteractively = true;
		}
	}
        //如果参数为m或M运行一个服务
	if (!runInteractively)
	{
		theServer.Run();
		return 0;
	}

	theServer.start(true);

	cout << "You are starting the server in interactive mode." << std::endl;
	cout << "Server should have started to listen for connections." << std::endl;
	cout << "Press q or Q to stop the server." << std::endl;

	while (true)
	{
		char ch;

		cin >> ch;

		if (ch == 'q')
		{
			break;
		}
	}

	theServer.stop();
	return 0;
}

void configure()
{
	theServer.setServerInfos("AppServer Description");
	theServer.setClientFactory(new AppClientFacory());
        //初始化参数
	PushFramework::options.uLoginExpireDuration = 40;
	PushFramework::options.uMaxClientIdleTime = 120;
	PushFramework::options.nMaxConnections = 55000;
	PushFramework::options.nStreamers = 1;

	ListenerOptions lOptions;
	lOptions.pProtocol = &protocol;
	theServer.createListener(10010, &lOptions);
        //注册消息协议
	protocol.registerMessage(&EchoRequest::default_instance());
	protocol.registerMessage(&EchoResponse::default_instance());
	protocol.registerMessage(&LoginRequest::default_instance());
	protocol.registerMessage(&LoginChallengeResponse::default_instance());
	protocol.registerMessage(&RoutedMessageRequest::default_instance());
	protocol.registerMessage(&RoutedMessageResponse::default_instance());
	protocol.registerMessage(&GroupMessageRequest::default_instance());
	protocol.registerMessage(&GroupMessageResponse::default_instance());

	theServer.registerService(ProtobufProtocol::hash(EchoRequest::default_instance().GetTypeName()), &service1, "EchoService");

	theServer.registerService(ProtobufProtocol::hash(RoutedMessageRequest::default_instance().GetTypeName()), &service2, "RedirectService");

	theServer.registerService(ProtobufProtocol::hash(GroupMessageRequest::default_instance().GetTypeName()), &service3, "BroadcastService");

	QueueOptions options;
	options.maxPackets = 20;
	options.requireRegistration = false;
	options.priority = 10;
	options.packetsQuota = 10;
	options.ignorePreviousPackets = false;
	options.fillRateThrottlingPeriod = 0;
	options.fillRateThrottlingMaxPackets = 0;
	broadcastManager.CreateQueue("AppQueue1", options);
       //监控选项
	PushFramework::options.isMonitorEnabled = true;
	PushFramework::options.password = "qiang";
	PushFramework::options.monitorPort = 222;
	PushFramework::options.isProfilingEnabled = true;
	PushFramework::options.samplingRate = 40;
}

客户端主要代码

// DemoClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <tchar.h>

#include "MyClient.h"
#include "protocol.h"

int _tmain(int argc, _TCHAR* argv[])
{       //初始TCPSocket
	TCPSocket::initializeWinsock();

	AppClient client;

	string serverIP;
	int port;
	string alias;

	cout << "input server ip:" << endl;
	cin >> serverIP;

	cout << "input server port:" << endl;
	cin >> port;

	cout << "input client alias:" << endl;
	cin >> alias;
        //客户端别名
	client.Alias(alias);
        //连接服务器
	if (!client.connect(serverIP.c_str(), port))
	{
		cout << "Unable to connect" <<endl;
		cout << "Tape to quit." <<endl;
		char ch;
		cin >> ch;
		return 0;
	}

	while (true)
	{
		cout << "tape q to quit, h for possible commands" << endl;

		char ch;
		cin >> ch;

		if (ch == 'q')
		{
			break;
		}

		if (ch == 'h')
		{
			cout << "e: sends a message to be echoed back.\nr: sends a message to be routed to a chosen recipient.\nb: broadcasts a message to evry client present on the server." << endl;
		}

		if (ch == 'e')
		{       //向服务端发送消息
			string message;
			cout << "input message:" << endl;
			cin >> message;

			EchoRequest request;
			request.set_msg(message);

			client.sendRequest(&request);
		}

		if (ch == 'r')
		{       //向指定客户端发送消息
			string message;
			cout << "input message:" << endl;
			cin >> message;

			string recipient;
			cout << "input recipient:" << endl;
			cin >> recipient;

			RoutedMessageRequest request;
			request.set_recipient(recipient);
			request.set_msg(message);

			client.sendRequest(&request);
		}

		if (ch == 'b')
		{       //向所有客户端广播消息
			string message;
			cout << "input message:" << endl;
			cin >> message;

			GroupMessageRequest request;
			request.set_msg(message);

			client.sendRequest(&request);
		}

	}
	return 0;
}

Unnamed QQ Screenshot20130405123100

源码下载 :http://pan.baidu.com/share/link?shareid=420528&uk=1594190059

最后编辑:
作者:dnybz
这个作者貌似有点懒,什么都没有留下。