//Pacote de Login (SelChar -> TMSRV -> ToWord) public static DBResult Exec_MSG_DBCharacterLogin2(DBController gs, pServer GameServer) { _MSG_DBCharacterLogin2 sm = W2Marshal.GetStructure <_MSG_DBCharacterLogin2>(GameServer.RecvPacket.RawBuffer); if (sm.Slot < 0 || sm.Slot >= 4) { W2Log.Write("err,charlogin slot illegal"); return(DBResult.NO_ERROR); } int idx = gs.GetIndex(sm.Header.ClientId); gs.AccountList[idx].Slot = (short)sm.Slot; _MSG_CNFCharacterLogin ret = W2Marshal.CreatePacket <_MSG_CNFCharacterLogin>(BaseDef._MSG_CNFCharacterLogin, sm.Header.ClientId); STRUCT_ACCOUNTFILE File = gs.AccountList[idx].Account; if (String.IsNullOrEmpty(File.Info.AccountName)) { W2Log.Write(String.Format("err,charlogin mobname {0} empty", File.Info.AccountName)); return(DBResult.NO_ERROR); } ret.Slot = (short)sm.Slot; ret.Mob = File.Mob[sm.Slot]; ret.Mob.CurrentScore = ret.Mob.BaseScore; ret.ShortSkill = File.Skillbar[sm.Slot].ShortSkill; ret.Affects = File.Affects[sm.Slot].Affects; ret.mobExtra = File.MobExtra[sm.Slot]; ret.mobExtra.ClassMaster = File.MobExtra[sm.Slot].ClassMaster; gs.AccountList[idx].State = EServerStatus.INGAME; GameServer.SendPacket(ret); return(DBResult.NO_ERROR); }
/// <summary> /// Process the newly connected client. /// </summary> /// <param name="client">The newly connected client.</param> private async void ProcessClient_Channel1(TcpClient client) { using (client) using (NetworkStream stream = client.GetStream()) { pServer GameServer = new pServer(stream); CCompoundBuffer packet = GameServer.RecvPacket; try { // Iterate processing incoming GameServer packets until he disconnects. while (GameServer.State != EServerStatus.CLOSED) { int readCount = 0; try { readCount = await stream.ReadAsync(packet.RawBuffer, 0, BaseDef.MAXL_PACKET); } catch (Exception e) { break; } if (readCount != 4 && (readCount < 12 || readCount > BaseDef.MAXL_PACKET)) // Invalid game packet. { // gameController.DisconnectPlayer(GameServer); break; } else // Possible valid game packet chunk. { unsafe { packet.Offset = 0; fixed(byte *PinnedPacketChunk = &packet.RawBuffer[packet.Offset]) { // Check for the init code. if (*(uint *)&PinnedPacketChunk[packet.Offset] == BaseDef.INIT_CODE) { packet.Offset = 4; // If a valid index can't be assigned to the GameServer, disconnect him if (!gameController.TryInsertPlayer(GameServer)) { GameServer.SendPacket(MTextMessagePacket.Create("O servidor está lotado. Tente novamente mais tarde.")); // gameController.DisconnectPlayer(GameServer); continue; } // If all the received chunk resumes to the INIT_CODE, read the next packet. if (readCount == 4) { continue; } } // Process all possible packets that were possibly sent together. while (packet.Offset < readCount && GameServer.State != EServerStatus.CLOSED) { // Check if the game packet size is bigger than the remaining received chunk. if (packet.ReadNextUShort(0) > readCount - packet.Offset || packet.ReadNextUShort(0) < 12) { throw new Exception("Pacote recebido inválido.O pacote de leitura é maior que o restante do pacote."); //continue; } // Tries to decrypt the packet. if (!PacketSecurity.Decrypt(packet)) { throw new Exception($"Não é possível descriptografar um pacote recebido de {client.Client.RemoteEndPoint}."); } // W2Log.Write(String.Format("Em processamento recv packet {{0x{0:X}/{1}}} a partir de {2}.", packet.ReadNextUShort(4), packet.ReadNextUShort(0), client.Client.RemoteEndPoint), ELogType.NETWORK); // Process the incoming packet. DBResult requestResult = gameController.ProcessPlayerRequest(GameServer); // Treat the processing packet return. switch (requestResult) { //case DBResult.PACKET_NOT_HANDLED: //{ // W2Log.Write(String.Format("Recv packet {{0x{0:X}/{1}}} de {2} não foi processado.", // packet.ReadNextUShort(4), packet.ReadNextUShort(0), client.Client.RemoteEndPoint), ELogType.NETWORK); // byte[] rawPacket = new byte[packet.ReadNextUShort(0)]; // for (int i = 0; i < rawPacket.Length; i++) // rawPacket[i] = PinnedPacketChunk[i + packet.Offset]; // File.WriteAllBytes($@"..\..\{packet.ReadNextUShort(4)}.bin", // rawPacket); // break; //} case DBResult.CHECKSUM_FAIL: { W2Log.Write($"Recibo de pacote de { client.Client.RemoteEndPoint} tem soma de verificação inválida.", ELogType.CRITICAL_ERROR); //gameController.DisconnectPlayer(GameServer); break; } case DBResult.PLAYER_INCONSISTENT_STATE: { W2Log.Write($"Um GameServer foi desconectado devido ao DBResult inconsistente.", ELogType.CRITICAL_ERROR); //gameController.DisconnectPlayer(GameServer); break; } case DBResult.WAIT: { break; } } // Correct the offset to process the next packet in the received chunk. PlayersCount.Text = gameController.PlayerCount.ToString(); GameServer.RecvPacket.Offset += packet.ReadNextUShort(0); } } } } } } catch (Exception ex) { W2Log.Write($"Uma exceção não tratada aconteceu processando o GameServer {GameServer.Index}. MSG: {ex.Message}", ELogType.CRITICAL_ERROR); } finally { // gameController.DisconnectPlayer(GameServer); } } }