int switchState(int type) { if (state == type) /* 跟上次是同一状态? */ sendCount++; else { state = type; sendCount = 0; } if (sendCount>=MAX_SEND_COUNT && type!=ID_ECHO) /* 超时太多次? */ { switch (type) { case ID_START: printf(_(">> 找不到服务器,重启认证!\n")); break; case ID_IDENTITY: printf(_(">> 发送用户名超时,重启认证!\n")); break; case ID_CHALLENGE: printf(_(">> 发送密码超时,重启认证!\n")); break; case ID_WAITECHO: printf(_(">> 等候响应包超时,自行响应!\n")); return switchState(ID_ECHO); } return restart(); } switch (type) { case ID_DHCP: return renewIP(); case ID_START: return sendStartPacket(); case ID_IDENTITY: return sendIdentityPacket(); case ID_CHALLENGE: return sendChallengePacket(); case ID_WAITECHO: /* 塞尔的就不ping了,不好计时 */ return waitEchoPacket(); case ID_ECHO: if (pingHost && sendCount*echoInterval > 60) { /* 1分钟左右 */ if (isOnline() == -1) { printf(_(">> 认证掉线,开始重连!\n")); return switchState(ID_START); } sendCount = 1; } #ifndef NO_ARP if (gateMAC[0] != 0xFE) sendArpPacket(); #endif return sendEchoPacket(); case ID_DISCONNECT: return sendLogoffPacket(); } return 0; }
void ServerGame::receiveFromClients() { Packet packet; // go through all clients std::map<unsigned int, SOCKET>::iterator iter; for(iter = network->sessions.begin(); iter != network->sessions.end(); iter++) { int data_length = network->receiveData(iter->first, network_data); if (data_length <= 0) { //no data recieved continue; } int i = 0; while (i < (unsigned int)data_length) { // Deserialize and check the type of packet packet.deserialize(&(network_data[i])); switch (packet.hdr.packet_type) { case INIT_CONNECTION: // offset is i here since we want to parse the packet header // receive it and send it right back receiveInitPacket(i); //sendInitPacket(); //sendActionPackets(); break; case SET_USERNAME: receiveNamePacket(i); break; case JOIN_TEAM: receiveJoinPacket(i); break; case READY_GAME: ready_clients++; //printf("ready clients: %d\nclient_id: %d\n", ready_clients, client_id); break; case IND_SPAWN_EVENT: receiveIndSpawnPacket(i + sizeof(PacketHeader)); spawned_clients++; break; case START_GAME: receiveStartPacket(i); Sleep(7000); sendStartPacket(); break; case MOVE_EVENT: // sends a move packet based on if reception was valid receiveMovePacket(i); break; case JUMP_EVENT: receiveJumpPacket(i); break; case DANCE_EVENT: receiveDancePacket(i); break; case V_ROTATION_EVENT: receiveRotationPacket(i + sizeof(PacketHeader)); break; case ATTACK_EVENT: receiveAttackPacket(i); break; case DISCARD_EVENT: receiveDiscardPacket(i); break; default: printf("error in packet types\n"); break; } i += sizeof(Packet); } } }