int db_thread_func(db_delay_helper<std::string, int>* db_delay) { unsigned int idle = 0; while(!glb_exit) { bool busy = false; if(!db_delay->pop_and_execute(nullptr)) idle++; else {idle = 0;} msg_ptr pmsg; if(sub_thread_process_msg_lst_.pop_front(pmsg)){ int ret = error_success; if (pmsg->head_.cmd_ == GET_CLSID(msg_user_login) || pmsg->head_.cmd_ == GET_CLSID(msg_user_login_channel)){ ret = handle_user_login((msg_user_login*)pmsg.get()); if (ret != error_success){ pmsg->from_sock_->is_login_ = false; } } else if (pmsg->head_.cmd_ == GET_CLSID(msg_user_register)){ ret = handle_user_register((msg_user_register*)pmsg.get()); pmsg->from_sock_->is_register_ = false; } else if (pmsg->head_.cmd_ == GET_CLSID(msg_get_verify_code)){ ret = handle_get_verify_code((msg_get_verify_code*) pmsg.get()); } if (ret != error_success){ msg_common_reply<nosocket> msg; msg.rp_cmd_ = pmsg->head_.cmd_; msg.err_ = ret; send_msg(pmsg->from_sock_, msg); } busy = true; } if (!busy){ boost::posix_time::milliseconds ms(20); boost::this_thread::sleep(ms); } } return 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); } }
// 处理包 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; }