void PacketBuilder::SendFunctionResponse(Client* client,FunctionRequest* function)
{
	if (client == NULL) return;
	Packet* sendpak = new Packet();
	sendpak->Opcode = Packet::FunctionReply;
	sendpak->AddString(function->guid);
	sendpak->AddString(function->name);
	sendpak->AddInt32(function->response);
	sendpak->AddString(function->params);
	
	sendpak->AddData(&function->data->Data[0],function->data->Length);

	      
        /*char* qq = (char*)malloc(127);
        sprintf(qq,"requestpacketsize: %d",sendpak->Length);
        Log::Debug(qq);
        free(qq);*/
	//Log::Debug("SendFunctionResponse");
	Server::Instance->SendPacket(client,sendpak);
	delete(sendpak);
}
void PacketProcessor::ProcessPacket(Client* client,Packet* pak)
{
	if (!client->IsConnected) return;
	//Log::Debug("Packet received");
	if (pak->Opcode == Packet::Auth)
	{ // authentication request
		//Log::Debug("Auth received");
		char* auth = (char*)calloc(32,1);
		auth = pak->ReadString(auth,32);
		//int a = strcmp(auth,Server::Instance->AuthKey);
		//logprintf("Auth compare: %s / %s / %d",auth,Server::Instance->AuthKey, a);
		if (strcmp(auth,Server::Instance->AuthKey) == 0)
		{
			client->IsAuthenticated = true;
			Server::Instance->PakSender->SendAuthReply(client,true);
			Log::Line("Client Authentication Successful");
		}
		else
		{
			client->IsAuthenticated = false;
			Server::Instance->PakSender->SendAuthReply(client,false);
			Log::Warning("Client Authentication Failed");
		}
		free(auth);
		return;
	}
	if (!client->IsAuthenticated) return;
	//Log::Debug("Packet received2");
	
	if (pak->Opcode == Packet::Ping)
	{
		client->Timeout = 0;
		return;
	}

	if (pak->Opcode == Packet::FunctionRequest)
	{
		//Log::Debug("FunctionRequest received");
		FunctionRequest* function = new FunctionRequest();
		function->_Client = client;
		function->guid = pak->ReadString(function->guid,FunctionRequest::STRING_SIZE);
		function->name = pak->ReadString(function->name,FunctionRequest::STRING_SIZE);
		function->response = pak->ReadInt32();
		function->params = pak->ReadString(function->params,FunctionRequest::STRING_SIZE);
		int datastart = pak->pos;
		int datalength = pak->Length - datastart;
		function->data->AddData(&(pak->Data[datastart]),datalength);

		Server::Instance->FuncProcessor->AddFunctionRequestToQue(function);

		//function = _Server->FuncProcessor->ProcessFunctionRequest(function);
		//_Server->PakSender->SendFunctionResponse(client,function);
		//delete(function);
		return;
	}

	if (pak->Opcode == Packet::Test)
	{
		Log::Debug("Test packet received");
		char* str1 = (char*)calloc(32,1);
		str1 = pak->ReadString(str1,32);
		int i = pak->ReadInt32();
		char b = pak->ReadByte();
		float f = pak->ReadFloat32();

		char* str2 = (char*)calloc(32,1);
		str2 = pak->ReadString(str2,32);


		Packet* sp = new Packet();
		sp->Opcode = Packet::Test;
		sp->AddString(str1);
		sp->AddInt32(i);
		sp->AddByte(b);
		sp->AddFloat32(f);
		sp->AddString(str2);
		Server::Instance->SendPacket(client,sp);
		free(str1);
		free(str2);
		return;
	}


}