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.
Prize winner in Competition "Best C++/MFC article of March 2011"
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
#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
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
Web04 | 2.8.140821.2 | Last Updated 23 May 2012
Article Copyright 2011 by Ahmed Charfeddine
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid