internal void SetArchorInfo(UDPArchor archor, string key) { if (key == "id") { } ScanArchors(UDPCommands.GetAll().ToArray(), new string[] { archor.GetClientIP() }); }
private void BtnSearch_OnClick(object sender, RoutedEventArgs e) { SetArchorList(null, null); List <string> cmds = UDPCommands.GetAll(); Clear(); archorManager.ScanArchors(GetScanArg(cmds.ToArray()), DataGrid3.subArchorList); }
internal void ScanArchor(params UDPArchor[] archors) { var ips = new List <string>(); foreach (UDPArchor item in archors) { ips.Add(item.GetClientIP()); } ScanArchors(UDPCommands.GetAll().ToArray(), ips.ToArray()); }
static void OnUdpData(IAsyncResult result) { UdpClient socket = result.AsyncState as UdpClient; IPEndPoint source = new IPEndPoint(IPAddress.Any, 51600); try { byte[] message = socket.EndReceive(result, ref source); bool conn = false; foreach (var item in list) { if (source.Equals(item.ip)) { conn = true; } } string rec_name = Encoding.Default.GetString(message, 0, 8).TrimEnd(); UDPCommands rec_command = (UDPCommands)BitConverter.ToInt32(message, 8); Console.WriteLine("Received data from..."); Console.WriteLine("Name=" + rec_name); Console.WriteLine("Command=" + rec_command.ToString()); if (!conn) { var a = new User("test", source); list.AddLast(a); Console.WriteLine("Adding connection to list..."); } if (rec_command == UDPCommands.MESSAGE) { byte[] rec_message = new byte[rec_name.Length + 2 + message.Length - 12]; byte[] padd = Encoding.Default.GetBytes(": "); Buffer.BlockCopy(message, 0, rec_message, 0, rec_name.Length); Buffer.BlockCopy(padd, 0, rec_message, rec_name.Length, 2); Buffer.BlockCopy(message, 12, rec_message, rec_name.Length + 2, message.Length - 12); Console.WriteLine(Encoding.Default.GetString(rec_message) + " (from " + source.Address + ":" + source.Port + ")"); foreach (var item in list) { socket.Send(rec_message, rec_message.Length, item.ip); } } } catch { } socket.BeginReceive(new AsyncCallback(OnUdpData), socket); }
private void _analyzeUDPCommand(byte[] data, IPEndPoint ipep) { for (int i = 0; i < _connections.Count; i++) { if (_connections[i].Address.Address == ipep.Address.Address && _connections[i].Port == ipep.Port) { _requestTime[i] = DateTime.Now; } } int pos = 0; int id = BinaryHelper.ReadInt32(data, ref pos); //check if id is id and not a server command if (id >= 0) { //if id is id //load current pos and direction of player float x = BinaryHelper.ReadFloat(data, ref pos); float y = BinaryHelper.ReadFloat(data, ref pos); Directions dir = (Directions)BinaryHelper.ReadInt32(data, ref pos); //if player shooted bool shooting = BinaryHelper.ReadBool(data, ref pos); int team = BinaryHelper.ReadInt32(data, ref pos); //selected inv item byte curInvItem = BinaryHelper.ReadByte(data, ref pos); InvType invType = (InvType)BinaryHelper.ReadInt32(data, ref pos); //check if bonus was removed int removedBonus = BinaryHelper.ReadInt32(data, ref pos); if (removedBonus != -1) { if (OnBonusRemoved != null) { OnBonusRemoved(removedBonus); } } //load statistics information int Deaths = BinaryHelper.ReadInt32(data, ref pos); int Kills = BinaryHelper.ReadInt32(data, ref pos); //check if invItem was used lock (Players[id]) { Players[id].CurrentInvItemAKey = curInvItem; Players[id].X = x; Players[id].Y = y; Players[id].Direction = dir; Players[id].SetTeam((Teams)team); Players[id].Kills = Kills; Players[id].Deaths = Deaths; if (shooting) { Players[id].Shoot(true); } if (invType != InvType.Unknown && invType != InvType.Cannon) { GameLevel.CreateEntity(_createInvItem(invType), id); } } //check if new bonus was created if (pos != data.Length) { int bId = BinaryHelper.ReadInt32(data, ref pos); int bx = (int)BinaryHelper.ReadFloat(data, ref pos); int by = (int)BinaryHelper.ReadFloat(data, ref pos); BonusType type = (BonusType)BinaryHelper.ReadByte(data, ref pos); Bonus b = _createBonus(bId, type, bx, by); GameLevel.AddBonus(b); } } else { //if id is a server command UDPCommands command = (UDPCommands)id; switch (command) { case UDPCommands.AddPlayer: { Player t = new Player(Players.Count, 0, 0); t.Id = BinaryHelper.ReadInt32(data, ref pos); t.Removed = true; if (t.Id == Players.Count) { Players.Add(t); _connections.Add(ipep); _requestTime.Add(DateTime.Now); } else { Players[t.Id] = t; _connections[t.Id] = ipep; _requestTime[t.Id] = DateTime.Now; } break; } case UDPCommands.AddPlayerComplete: { id = BinaryHelper.ReadInt32(data, ref pos); lock (Players[id]) { Players[id].Removed = false; } break; } case UDPCommands.ClientDisconnected: { id = BinaryHelper.ReadInt32(data, ref pos); lock (Players[id]) { Players[id] = null; _connections[id] = null; _requestTime[id] = new DateTime(); } //MessageBox.Show("Client " + id + " disconnected"); break; } case UDPCommands.ConnectToGame: { _connectToGame(ipep); break; } case UDPCommands.ServerChanged: { long ip = BinaryHelper.ReadLong(data, ref pos); if (ip == CIp.Address) { _isMain = true; } GameLevel.BonusId = BinaryHelper.ReadInt32(data, ref pos); break; } } } }