⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverpackethandler.cpp

📁 swain-0.5.2.zip的源代码,比较好用,希望大家喜欢.
💻 CPP
字号:
/*
This file is part of SWAIN (http://sourceforge.net/projects/swain).
Copyright (C) 2006  Daniel Lindstr鰉 and Daniel Nilsson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA  02110-1301, USA.
*/
#include "StdAfx.h"
#include "ServerPacketHandler.h"
#include "../common/packetid.h"
// Include packets this handler needs to send...
#include "../common/PacketWelcome.h"
#include "../common/PacketAnswer.h"
#include "../common/PacketName.h"
#include "../common/PacketStatus.h"
#include "../common/PacketError.h"
#include "../common/PacketConnectTo.h"

ServerPacketHandler::ServerPacketHandler(UserDB *userdb){
	this->incoming = new Queue();
	this->run = false;
	this->handle_thread = NULL;
	this->userdb = userdb;
}

ServerPacketHandler::~ServerPacketHandler(void){
	//d鰀a tr錮en
	this->run = false;
	while(!this->incoming->isEmpty()){
		delete(this->incoming->dequeue());
	}
	delete(this->incoming);
}

void ServerPacketHandler::setConnectionHandler(ConnectionHandler *ch){
	this->ch = ch;
}

void ServerPacketHandler::handle(Packet *p){
	
	this->incoming->enqueue(p);
}

void ServerPacketHandler::newConnection(int cid) {
	printf("New connection (cid = %d)\n", cid);
}

void ServerPacketHandler::closedConnection(int cid) {
	int uid = userdb->getUserByCid(cid);
	printf("Connection %d to user %d closed, setting offline status and resetting cid.\n", cid, uid);
	userdb->setUserStatus(uid, STATUS_OFFLINE);
	userdb->setUserCId(uid, -1);
}

void ServerPacketHandler::startHandle(void){
	LPSECURITY_ATTRIBUTES lpThreadAttributes = NULL;
	SIZE_T threadStackSize = 0;
	this->run = true;
	this->handle_thread = CreateThread(lpThreadAttributes, threadStackSize, handleinc, (LPVOID)this, 0, NULL);

}

void ServerPacketHandler::stopHandle(void){
	this->run = false;
}

DWORD WINAPI ServerPacketHandler::handleinc(LPVOID lpParameter){
	ServerPacketHandler *sph = (ServerPacketHandler *)lpParameter;
	Packet *packet = NULL;

	while(sph->run){
		packet = (Packet *)sph->incoming->dequeue();
		if(packet == NULL)
			continue;
		switch(packet->getId()){
			case(PACKET_HELLO): sph->handlePacketHello((PacketHello *)packet);	break;
			case(PACKET_QUERY): sph->handlePacketQuery((PacketQuery *)packet); break;
			case(PACKET_LOOKUP): sph->handlePacketLookUp((PacketLookUp *)packet); break;
			case(PACKET_CONNECT): sph->handlePacketConnect((PacketConnect *)packet); break;
			case(PACKET_SET_STATUS): sph->handlePacketSetStatus((PacketSetStatus *)packet); break;
			default: break;
		}
		delete(packet);
		packet = NULL;
	}

	return(NULL);
}

void ServerPacketHandler::handlePacketHello(PacketHello *packet){
	int id = this->userdb->getUserId(packet->getName());
	int cid = packet->getCId();
	Packet *p = NULL;
	bool in = true;
	if(id != -1 && this->userdb->checkPasswd(id, packet->getPasswd())){
	//User types the correct password
		in = false;
		printf("User %d logged in.\n", id);
		p = (Packet *)new PacketWelcome();
		p->setCId(cid);
		if(this->userdb->setUserCId(id, cid)&&this->userdb->setUserAddr(id, this->ch->getAddresByCId(cid)))
			this->ch->sendPacket(p);
		else
			in = true;
	}
	else if(in){
	//User does not exist or types the incorrect password
		printf("incorrect username or password!\n");
		p = (Packet *)new PacketError(ERROR_PASSWORD);
		p->setCId(cid);
		this->ch->sendPacket(p);
		this->ch->closeConnection(cid);
		//verkar inte g

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -