public void Handle(Disconnect message) { _clientsIds.Remove(message.Username); foreach (var client in _clients) { client.Tell(new DisconnectResponse() { Username = message.Username }, Self); } }
// Mở ra form Disconnect private void btnDisconnect_Click(object sender, EventArgs e) { Disconnect disconnectForm = new Disconnect(); foreach (string key in clientList.Keys) { disconnectForm.inputList.Add(key); } this.Hide(); disconnectForm.ShowDialog(); this.Show(); if (disconnectForm.outputList.Count > 0) { foreach (string name in disconnectForm.outputList) { TcpClient workerSocket = null; workerSocket = (TcpClient)clientList.FirstOrDefault(x => x.Key == name).Value; workerSocket.Close(); } } }
public void ProcessDatagram(byte[] datagram, Player source) { switch ((DatagramID)datagram[0]) { case DatagramID.entityUpdate: #region entityUpdate var entityUpdate = new EntityUpdate(datagram); string ACmessage = AntiCheat.Inspect(entityUpdate); if (ACmessage != "ok") { //var kickMessage = new ChatMessage() { // message = "illegal " + ACmessage //}; //kickMessage.Write(player.writer, true); //Console.WriteLine(player.entityData.name + " kicked for illegal " + kickMessage.message); //Thread.Sleep(100); //thread is about to run out anyway so np //Kick(player); //return; } if (entityUpdate.name != null) { //Announce.Join(entityUpdate.name, player.entityData.name, players); } entityUpdate.entityFlags |= 1 << 5; //enable friendly fire flag for pvp if (!source.entityData.IsEmpty) //dont filter the first packet //entityUpdate.Filter(player.entityData); { } if (!entityUpdate.IsEmpty) { //entityUpdate.Broadcast(players, 0); BroadcastUDP(entityUpdate.Data, source); if (entityUpdate.HP == 0 && source.entityData.HP > 0) { BroadcastUDP(Tomb.Show(source).Data); } else if (source.entityData.HP == 0 && entityUpdate.HP > 0) { BroadcastUDP(Tomb.Hide(source).Data); } entityUpdate.Merge(source.entityData); } break; #endregion case DatagramID.attack: #region attack var attack = new Attack(datagram); source.lastTarget = attack.Target; if (players.ContainsKey(attack.Target)) //in case the target is a tombstone { SendUDP(datagram, players[attack.Target]); } break; #endregion case DatagramID.shoot: #region shoot var shoot = new Resources.Datagram.Shoot(datagram); BroadcastUDP(datagram, source); //pass to all players except source break; #endregion case DatagramID.proc: #region proc var proc = new Proc(datagram); switch (proc.Type) { case ProcType.bulwalk: SendUDP(new Chat(string.Format("bulwalk: {0}% dmg reduction", 1.0f - proc.Modifier)).data, source); break; case ProcType.poison: var poisonTickDamage = new Attack() { Damage = proc.Modifier, Target = proc.Target }; var target = players[poisonTickDamage.Target]; Func <bool> tick = () => { bool f = players.ContainsKey(poisonTickDamage.Target); if (f) { SendUDP(poisonTickDamage.data, target); } return(!f); }; Tools.DoLater(tick, 500, 7); //Poison(players[proc.Target], poisonTickDamage); break; case ProcType.manashield: SendUDP(new Chat(string.Format("manashield: {0}", proc.Modifier)).data, source); break; case ProcType.warFrenzy: case ProcType.camouflage: case ProcType.fireSpark: case ProcType.intuition: case ProcType.elusivenes: case ProcType.swiftness: break; default: break; } BroadcastUDP(datagram, source); //pass to all players except source break; #endregion case DatagramID.chat: #region chat var chat = new Chat(datagram); if (chat.Text.StartsWith("/")) { Command.Server(chat.Text, source, this); //wip } else { Console.ForegroundColor = ConsoleColor.Cyan; Console.Write(players[chat.Sender].entityData.name + ": "); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(chat.Text); BroadcastUDP(datagram, null, true); //pass to all players } break; #endregion case DatagramID.interaction: #region interaction var interaction = new Interaction(datagram); BroadcastUDP(datagram, source); //pass to all players except source break; #endregion case DatagramID.connect: #region connect var connect = new Connect(datagram) { Guid = (ushort)source.entityData.guid, Mapseed = Database.mapseed }; SendUDP(connect.data, source); foreach (Player player in players.Values) { if (player.playing) { SendUDP(player.entityData.Data, source); } } source.playing = true; //Task.Delay(100).ContinueWith(t => Load_world_delayed(source)); //WIP, causes crash when player disconnects before executed Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(source.IpEndPoint.Address + " is now playing"); break; #endregion case DatagramID.disconnect: #region disconnect var disconnect = new Disconnect(datagram); source.playing = false; BroadcastUDP(datagram, source, true); source.entityData = new EntityUpdate() { guid = source.entityData.guid }; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(source.IpEndPoint.Address + " is now lurking"); break; #endregion case DatagramID.specialMove: #region specialMove var specialMove = new SpecialMove(datagram); switch (specialMove.Id) { case SpecialMoveID.taunt: var targetGuid = specialMove.Guid; specialMove.Guid = (ushort)source.entityData.guid; SendUDP(specialMove.data, players[targetGuid]); break; case SpecialMoveID.cursedArrow: case SpecialMoveID.arrowRain: case SpecialMoveID.shrapnel: case SpecialMoveID.smokeBomb: case SpecialMoveID.iceWave: case SpecialMoveID.confusion: case SpecialMoveID.shadowStep: BroadcastUDP(specialMove.data, source); break; default: break; } break; #endregion case DatagramID.dummy: break; default: Console.WriteLine("unknown DatagramID: " + datagram[0]); break; } }
public void ListenTCP() { Player player = new Player(tcpListener.AcceptTcpClient()); new Thread(new ThreadStart(ListenTCP)).Start(); if (player.reader.ReadInt32() != Database.bridgeVersion) { player.writer.Write(false); return; } player.writer.Write(true); string username = player.reader.ReadString(); if (!accounts.ContainsKey(username)) { player.writer.Write((byte)AuthResponse.unknownUser); return; } string password = player.reader.ReadString(); if (accounts[username] != password) { player.writer.Write((byte)AuthResponse.wrongPassword); return; } player.writer.Write((byte)AuthResponse.success); player.admin = username == "BLACKROCK"; player.MAC = player.reader.ReadString(); var banEntry = bans.FirstOrDefault(x => x.Mac == player.MAC || x.Ip == player.IpEndPoint.Address.ToString()); if (banEntry != null) { player.writer.Write(true); player.writer.Write(banEntry.Reason); return; } player.writer.Write(false);//not banned ushort newGuid = 1; while (players.ContainsKey(newGuid)) //find lowest available guid { newGuid++; } player.entityData.guid = newGuid; players.Add(newGuid, player); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(player.IpEndPoint.Address + " connected"); while (true) { try { byte packetID = player.reader.ReadByte(); ProcessPacket(packetID, player); } catch (IOException) { players.Remove((ushort)player.entityData.guid); var disconnect = new Disconnect() { Guid = (ushort)player.entityData.guid }; BroadcastUDP(disconnect.data); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(newGuid + " disconnected"); break; } } }