Beispiel #1
0
void bank_process_remote_command(Bank *bank, char *command, size_t len)
{
    // TODO: Implement the bank side of the ATM-bank protocol

	/*
	 * The following is a toy example that simply receives a
	 * string from the ATM, prepends "Bank got: " and echoes 
	 * it back to the ATM before printing it to stdout.
	 */

	/*
    char sendline[1000];
    command[len]=0;
    sprintf(sendline, "Bank got: %s", command);
    bank_send(bank, sendline, strlen(sendline));
    printf("Received the following:\n");
    fputs(command, stdout);
	*/

    char cmd[64];
    char args1[2048];
    char args2[2048];

    bank_decrypt(bank, command, strlen(command));    
    sscanf(command, "%s%s%s", cmd, args1, args2);

    if (strcmp("user-login", cmd) == 0) {
        handle_user_login(bank, args1);
    } else if (strcmp("withdraw", cmd) == 0) {
        handle_withdraw(bank, args1, args2);
    } else if (strcmp("balance", cmd) == 0) {
        handle_balance_atm(bank, args1);
    } else if (strcmp("user-logout", cmd) == 0) {
        handle_user_logout(bank, args1);
    }

}
Beispiel #2
0
// 处理包
void ChatServer::handle_packet(IOComponent *ioc, Packet *packet)
{
	if (ioc == NULL || packet == NULL)
	{
		return;
	}

	if (packet->get_type() == IServerAdapter::CMD_DISCONN_PACKET)
	{
		std::string name = ioc->get_userid();
		if (name.empty())
		{
			return;
		}

		size_t index = _id_user.get_hash_index(name);
		_id_user.ct_write_lock(index);
		_id_user.remove_user(index, name);
		_id_user.ct_write_unlock(index);
		return;
	}

	std::string xml(packet->getData(), packet->getDataLen());

	Command *cmd = ChatXml::parse_command(xml);
	if (cmd == NULL)
	{
		return;
	}

	Response resp;
	std::string command = cmd->_cmd;
	if (command != "heartbeat")
	{
		printf("<request_xml-------------------------------------------------------\n");
		printf("%s\n", xml.c_str());
		printf("-------------------------------------------------------request_xml>\n");
	}

	if (command == "login")
	{
		handle_user_login(ioc, dynamic_cast<Login*>(cmd), &resp);
	}
	else if (command == "logout")
	{
		handle_user_logout(ioc, dynamic_cast<Logout*>(cmd), &resp);
	}
	else if (command == "send")
	{
		handle_user_send_msg(ioc, dynamic_cast<SendMsg*>(cmd), &resp);
	}
	else if (command == "groupsend")
	{
		handle_user_group_send_msg(ioc, dynamic_cast<GroupSendMsg*>(cmd), &resp);
	}
	else if (command == "heartbeat")
	{
		resp._result = "succ";
	}
	else
	{
		delete cmd;
		return;
	}

	// 返回通用应答
	std::string resp_xml = ChatXml::build_response_xml(cmd->_cmd, resp._result);
	if (command != "heartbeat")
	{
		printf("<response_xml-------------------------------------------------------\n");
		printf("%s\n", resp_xml.c_str());
		printf("-------------------------------------------------------response_xml>\n");
	}

	Packet *pack = _tp->encode_pack(resp_xml.c_str(), resp_xml.length());
	if (pack)
	{
		if (!ioc->post_packet(pack))
		{
			delete pack;
		}
	}

	// 释放空间
	delete cmd;
}