Click here to Skip to main content
14,209,092 members
Click here to Skip to main content

Tagged as

Stats

209.6K views
25.6K downloads
311 bookmarked

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

,
Rate this:
4.96 (85 votes)
Please Sign up or sign in to vote.
4.96 (85 votes)
23 May 2012     Apache    
Write asynchronous, multithreaded servers in a few lines of code. Monitor realtime activity with a deploy-only dashboard.
ChatAPI
ChatAPI.vcproj
ChatClient
ChatClient.vcproj
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
ChatPackets.vcproj
ChatProtocol
ChatProtocol.vcproj
ChatServer
ChatServer.vcproj
ChatServer.vcproj.INTERNAL.Ahmed.Charfeddine.user
output
TCPSocket
TCPSocket.vcproj
TCPSocket.zip
XMLProtocol
XMLProtocol.vcproj
XMLProtocol.zip
ChatRobots
ChatRobots.vcproj
ProtoBufExampleClient
ProtoBufExampleClient.vcproj
ProtoBufExampleProtocol
ProtoBufExampleProtocol.vcproj
ProtoBufExampleServer
ProtoBufExampleServer.vcproj
include
PushFramework.dll
PushFramework.lib
private
PushFramework.vcproj
QoSExampleClient
QoSExampleClient.vcproj
QoSExampleProtocol
QoSExampleProtocol.vcproj
QoSExampleServer
QoSExampleServer.vcproj
#include "StdAfx.h"
#include "XMLProtocolImpl.h"

#include "XMLRequest.h"
#include "XMLResponse.h"

#define SignatureStart 16018
#define SignatureEnd 16108

#include "ScopedLock.h"


XMLProtocolImpl::XMLProtocolImpl(void)
{
	::InitializeCriticalSection(&csInPackets);
	::InitializeCriticalSection(&csOutPackets);
}

XMLProtocolImpl::~XMLProtocolImpl(void)
{
	::DeleteCriticalSection(&csInPackets);
	::DeleteCriticalSection(&csOutPackets);
}



int XMLProtocolImpl::encodeOutgoingPacket( PushFramework::OutgoingPacket& packet )
{
	OutgoingXMLPacket& xmlPacket = (OutgoingXMLPacket&) packet;

	return xmlPacket.Encode() ? PushFramework::Protocol::Success : PushFramework::Protocol::eEncodingFailure;
}


int XMLProtocolImpl::frameOutgoingPacket( PushFramework::OutgoingPacket& packet, PushFramework::DataBuffer& buffer, unsigned int& nWrittenBytes )
{
	OutgoingXMLPacket& xmlPacket = (OutgoingXMLPacket&) packet;

	nWrittenBytes = 6 + xmlPacket.getBufferLen() + 2;

	if(nWrittenBytes > buffer.getRemainingSize())
		return PushFramework::Protocol::eInsufficientBuffer;

	unsigned int sStart = SignatureStart;
	unsigned int packetLen = nWrittenBytes;
	unsigned int commandID = xmlPacket.getRequestId();
	unsigned int sEnd = SignatureEnd;

	buffer.Append((char*)&sStart, 2);
	buffer.Append((char*)&packetLen, 2);
	buffer.Append((char*)&commandID, 2);

	buffer.Append(xmlPacket.getBuffer(), xmlPacket.getBufferLen());

	buffer.Append((char*)&sEnd, 2);

	return PushFramework::Protocol::Success;
}

int XMLProtocolImpl::tryDeframeIncomingPacket( PushFramework::DataBuffer& buffer, PushFramework::IncomingPacket*& pPacket, int& serviceId, unsigned int& nExtractedBytes )
{
	if(buffer.GetDataSize() <= 8)
		return PushFramework::Protocol::eIncompletePacket;

	char* pBuffer = buffer.GetBuffer();

	unsigned int sStart = 0;
	unsigned int packetLen = 0;
	serviceId = 0;
	unsigned int sEnd = 0;

	for (int i=0;i<2;i++)
	{
		*((BYTE*)(&sStart)+i)=pBuffer[i];
		*((BYTE*)(&packetLen)+i)=pBuffer[i+2];
		*((BYTE*)(&serviceId)+i)=pBuffer[i+4];
	}

	if (sStart!=SignatureStart)
		return PushFramework::Protocol::eCorruptPacket;

	if (packetLen > buffer.GetDataSize())
		return PushFramework::Protocol::eIncompletePacket;

	for (int i=0;i<2;i++)
		*((BYTE*)(&sEnd)+i)=pBuffer[packetLen-2+i];

	if(sEnd!=SignatureEnd)
		return PushFramework::Protocol::eCorruptPacket;

	char* pData = pBuffer + 6;
	unsigned int dataSize = packetLen - 8;

	nExtractedBytes = packetLen;

	IncomingXMLPacket* pIncomingXMLPacket = (IncomingXMLPacket*)createIncomingPacket(serviceId);
	if(pIncomingXMLPacket==NULL)
		return PushFramework::Protocol::eUndefinedFailure;

	if(!pIncomingXMLPacket->Decode(pData, dataSize))
	{
		disposeIncomingPacket(pIncomingXMLPacket);
		return PushFramework::Protocol::eDecodingFailure;
	}

	pPacket = (PushFramework::IncomingPacket*) pIncomingXMLPacket;
	return PushFramework::Protocol::Success;
}



void XMLProtocolImpl::disposeIncomingPacket( PushFramework::IncomingPacket* pPacket )
{
	delete pPacket;	
}


PushFramework::IncomingPacket* XMLProtocolImpl::createIncomingPacket( unsigned int serviceId )
{
	ScopedLock cs(csInPackets);

	inFactoryMapT::iterator it = inFactoryMap.find(serviceId);
	if(it==inFactoryMap.end())
		return NULL;


	IncomingPacketTypeInfo* pInfo = it->second;

	return (PushFramework::IncomingPacket*) pInfo->pTemplate->CreateInstance();	
}

void XMLProtocolImpl::disposeOutgoingPacket( PushFramework::OutgoingPacket* pPacket )
{
	delete pPacket;
}
PushFramework::OutgoingPacket* XMLProtocolImpl::createOutgoingPacket( unsigned int requestId )
{
	ScopedLock cs(csOutPackets);

	outFactoryMapT::iterator it = outFactoryMap.find(requestId);
	if(it==outFactoryMap.end())
		return NULL;


	OutgoingPacketTypeInfo* pInfo = it->second;

	return (PushFramework::OutgoingPacket*) pInfo->pTemplate->CreateInstance();
}

void XMLProtocolImpl::registerIncomingPacketTemplate( unsigned int serviceId, PushFramework::IncomingPacket* pTemplate, unsigned int nMaxPool )
{
	IncomingPacketTypeInfo* pInfo = new IncomingPacketTypeInfo;
	pInfo->pTemplate = (IncomingXMLPacket*) pTemplate;
	inFactoryMap[serviceId] = pInfo;
}

void XMLProtocolImpl::registerOutgoingPacketTemplate( unsigned int requestId, PushFramework::OutgoingPacket* pTemplate, unsigned int nMaxPool )
{
	OutgoingPacketTypeInfo* pInfo = new OutgoingPacketTypeInfo;
	pInfo->pTemplate = (OutgoingXMLPacket*)pTemplate;
	outFactoryMap[requestId] = pInfo;
}

int XMLProtocolImpl::decodeIncomingPacket( PushFramework::IncomingPacket* pPacket, int& serviceId )
{
	return PushFramework::Protocol::Success;
}


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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190617.3 | Last Updated 23 May 2012
Article Copyright 2011 by Ahmed Charfeddine
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid