Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C++

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

Rate me:
Please Sign up or sign in to vote.
4.96/5 (86 votes)
23 May 2012Apache15 min read 261.4K   26.9K   316  
Write asynchronous, multithreaded servers in a few lines of code. Monitor realtime activity with a deploy-only dashboard.
// ChatAPI.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "ChatAPI.h"

#include "ResponseHandlers.h"



void ChatAPI::OnLoginPuzzle( int puzzleQuestion )
{
	tryLogin(puzzleQuestion);
}

bool ChatAPI::tryConnect( std::string serverHost, unsigned int uPort )
{
	return TCPSocket::connect((char*) serverHost.c_str(), uPort);
}

bool ChatAPI::tryLogin( int loginPuzzle /*= -1*/ )
{
	if (getStatus() == Disconnected)
	{
		return false;
	}

	if (getStatus() != Connected)
	{
		return false;
	}

	if(loginPuzzle!=-1)
		this->loginPuzzle = loginPuzzle;

	LoginRequest request;
	//
	request.Pseudo(pseudo);
	request.LoginPuzzleReply(loginPuzzle + 1);

	return sendRequest(&request);
}

bool ChatAPI::SendDirectChat( std::string recipient, std::string msg )
{
	DirectChatRequest request;
	//
	request.Recipient(recipient);
	request.Msg(msg);

	return sendRequest(&request);
}

bool ChatAPI::JoinRoom( std::string room )
{
	JoinRoomRequest request;
	//
	request.Room(room);

	return sendRequest(&request);
}

bool ChatAPI::LeaveRoom( std::string room )
{
	LeaveRoomRequest request;
	//
	request.Room(room);

	return sendRequest(&request);
}

bool ChatAPI::SendRoomChat( std::string msg, std::string room )
{
	RoomChatRequest request;
	//
	request.Room(room);
	request.Msg(msg);

	return sendRequest(&request);
}

void ChatAPI::Logout()
{
	LogoutRequest request;
	sendRequest(&request);
	disconnect(true);
}

ChatAPI::ChatAPI( bool relayToUserThread /*= false */)
:TCPSocket(relayToUserThread)
{
	registerHandler(LoginPuzzleResponseID, new LoginPuzzleResponseHandler(this));
	registerHandler(LoginResponseID, new LoginResponseHandler(this));
	registerHandler(RoomsResponseID, new RoomsResponseHandler(this));
	registerHandler(ParticipantInResponseID, new ParticipantInResponseHandler(this));
	registerHandler(ParticipantOutResponseID, new ParticipantOutResponseHandler(this));
	registerHandler(DirectChatResponseID, new DirectChatResponseHandler(this));
	registerHandler(RoomChatResponseID, new RoomChatResponseHandler(this));
	registerHandler(JoinRoomResponseID, new JoinRoomResponseHandler(this));

	setProtocol(new ChatClientProtocol);
}

void ChatAPI::setLoginInfo( std::string pseudo )
{
	this->pseudo = pseudo;
}

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


Written By
Technical Lead
Tunisia Tunisia
Services:
http://www.pushframework.com/?page_id=890

Comments and Discussions