public bool Ping() { var commands = new[] { new EmptyPacketCommand() { Command = CommandType.Ping } }; var packet = new ScoreboardPacket { Commands = commands }; using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) { ReceiveTimeout = 1000 }) { try { socket.Connect(this.EndPoint); socket.Send(packet.ToBytes()); var buffer = new byte[100]; socket.Receive(buffer); var incoming = ScoreboardPacket.Parse(buffer); return(incoming.Commands[0].Command == CommandType.Pong); } catch (Exception) { // If anything fails, return false. return(false); } } }
public bool Ping() { var commands = new[] { new EmptyPacketCommand() { Command = CommandType.Ping } }; var packet = new ScoreboardPacket { Commands = commands }; using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) { ReceiveTimeout = 1000 }) { try { socket.Connect(this.EndPoint); socket.Send(packet.ToBytes()); var buffer = new byte[100]; socket.Receive(buffer); var incoming = ScoreboardPacket.Parse(buffer); return incoming.Commands[0].Command == CommandType.Pong; } catch (Exception) { // If anything fails, return false. return false; } } }
public void SendPacket(ScoreboardPacket packet) { using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) { ReceiveTimeout = 1000 }) { socket.Connect(this.EndPoint); socket.Send(packet.ToBytes()); } }
public void Listen(SettingsPanelViewModel sb) { if (this.listenThread != null) { if (this.listenThread.IsAlive) { this.StopListen = true; while (this.listenThread.IsAlive) { Thread.Sleep(20); } } } this.StopListen = false; IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); var endPoints = new List <IPEndPoint>(); foreach (var entry in hostEntry.AddressList) { if (entry.AddressFamily == AddressFamily.InterNetwork) { endPoints.Add(new IPEndPoint(entry, this.PortNumber)); break; } } if (endPoints.Count == 0) { MessageBox.Show("No valid networks were found to connect to."); return; } var socket = new Socket(endPoints[0].Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp) { ReceiveTimeout = 5000 }; foreach (var e in endPoints) { socket.Bind(e); } this.listenThread = new Thread( new ThreadStart( delegate { while (this.StopListen == false) { var inBuffer = new byte[1000]; EndPoint senderRemote = new IPEndPoint(IPAddress.Any, 0); try { socket.ReceiveFrom(inBuffer, ref senderRemote); } catch (SocketException ex) { int errorCode = ex.ErrorCode; continue; } var packet = ScoreboardPacket.Parse(inBuffer); foreach (var cmd in packet.Commands) { if (cmd.Command == CommandType.Ping) { var pongCmd = new EmptyPacketCommand { Command = CommandType.Pong }; var pongPacket = new ScoreboardPacket() { Commands = new[] { pongCmd } }; socket.SendTo(pongPacket.ToBytes(), senderRemote); } } this.dispatcher.BeginInvoke((Action)(() => sb.HandleRemoteCommands(packet, socket, senderRemote))); } socket.Close(); socket.Dispose(); })); this.listenThread.Start(); this.IsListening = true; }
public void Listen(SettingsPanelViewModel sb) { if (this.listenThread != null) { if (this.listenThread.IsAlive) { this.StopListen = true; while (this.listenThread.IsAlive) { Thread.Sleep(20); } } } this.StopListen = false; IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); IPEndPoint endPoint = null; foreach (var entry in hostEntry.AddressList) { if (entry.AddressFamily == AddressFamily.InterNetwork) { endPoint = new IPEndPoint(entry, this.PortNumber); } } if (endPoint == null) { MessageBox.Show("This application does not support IPv6 yet."); return; } var socket = new Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp) { ReceiveTimeout = 2000 }; socket.Bind(endPoint); // This ReceiveTimeout was causing issues with the Android app... we may need to change this? // A value of 0 worked with the app, but then the application couldn't abort the thread while waiting. socket.ReceiveTimeout = 5000; this.listenThread = new Thread( new ThreadStart( delegate { while (this.StopListen == false) { var inBuffer = new byte[1000]; EndPoint senderRemote = new IPEndPoint(IPAddress.Any, 0); try { socket.ReceiveFrom(inBuffer, ref senderRemote); } catch (SocketException ex) { int errorCode = ex.ErrorCode; continue; } var packet = ScoreboardPacket.Parse(inBuffer); foreach (var cmd in packet.Commands) { if (cmd.Command == CommandType.Ping) { var pongCmd = new EmptyPacketCommand { Command = CommandType.Pong }; var pongPacket = new ScoreboardPacket() { Commands = new[] { pongCmd } }; socket.SendTo(pongPacket.ToBytes(), senderRemote); } } this.dispatcher.BeginInvoke((Action)(() => sb.HandleRemoteCommands(packet, socket, senderRemote))); } socket.Close(); socket.Dispose(); })); this.listenThread.Start(); this.IsListening = true; }
/// <summary> Handles all incoming network packets. </summary> /// <param name="packet"> The packet. </param> /// <param name="socket"> The active socket. </param> /// <param name="senderRemote"> The sender where the packet originated. </param> /// <exception cref="System.Net.Sockets.SocketException"> Thrown if a response failed to send. </exception> public void HandleRemoteCommands(ScoreboardPacket packet, Socket socket, EndPoint senderRemote) { foreach (var command in packet.Commands) { switch (command.Command) { case CommandType.UpdatePlayerName: { var cmd = command as StringPacketCommand; if (cmd != null) { if (cmd.Player == 1) { this.controlViewModel.Scoreboard.Player1.Name = cmd.Data; } else if (cmd.Player == 2) { this.controlViewModel.Scoreboard.Player2.Name = cmd.Data; } } break; } case CommandType.UpdatePlayerScore: { var cmd = command as Int32PacketCommand; if (cmd != null) { if (cmd.Player == 1) { this.controlViewModel.Scoreboard.Player1.Score = cmd.Data; } else if (cmd.Player == 2) { this.controlViewModel.Scoreboard.Player2.Score = cmd.Data; } } break; } case CommandType.UpdatePlayerColor: { var cmd = command as BytePacketCommand; if (cmd != null) { if (cmd.Player == 1) { this.controlViewModel.Scoreboard.Player1.Color = (PlayerColor)cmd.Data; } else if (cmd.Player == 2) { this.controlViewModel.Scoreboard.Player2.Color = (PlayerColor)cmd.Data; } } break; } case CommandType.UpdatePlayerRace: { var cmd = command as BytePacketCommand; if (cmd != null) { if (cmd.Player == 1) { this.controlViewModel.Scoreboard.Player1.Race = (Race)cmd.Data; } else if (cmd.Player == 2) { this.controlViewModel.Scoreboard.Player2.Race = (Race)cmd.Data; } } break; } case CommandType.ToggleAnnouncement: this.controlViewModel.ToggleAnnouncement(); break; case CommandType.ToggleSubbar: this.controlViewModel.ToggleSubbar(); break; case CommandType.SwapPlayer: this.controlViewModel.SwapPlayers(); break; case CommandType.ShowScoreboard: this.controlViewModel.ToggleScoreboardVisible(); break; case CommandType.HideScoreboard: this.controlViewModel.ToggleScoreboardVisible(); break; case CommandType.ResetPlayer: { var cmd = command as EmptyPacketCommand; if (cmd != null) { if (cmd.Player == 1) { this.controlViewModel.Scoreboard.Player1.Reset(); } else if (cmd.Player == 2) { this.controlViewModel.Scoreboard.Player2.Reset(); } } break; } case CommandType.IncrementPlayerScore: { var cmd = command as EmptyPacketCommand; if (cmd != null) { if (cmd.Player == 1) { this.controlViewModel.Scoreboard.Player1.Score++; } else if (cmd.Player == 2) { this.controlViewModel.Scoreboard.Player2.Score++; } } break; } case CommandType.RetrievePlayerInformation: { var cmd = command as EmptyPacketCommand; if (cmd != null) { var commands = new List<IScoreboardPacketCommand>(); var player = cmd.Player == 1 ? this.controlViewModel.Scoreboard.Player1 : this.controlViewModel.Scoreboard.Player2; commands.Add( new StringPacketCommand { Command = CommandType.UpdatePlayerName, Player = cmd.Player, Data = player.Name }); commands.Add( new BytePacketCommand { Command = CommandType.UpdatePlayerRace, Data = (byte)player.Race, Player = cmd.Player }); commands.Add( new BytePacketCommand { Command = CommandType.UpdatePlayerColor, Data = (byte)player.Color, Player = cmd.Player }); commands.Add( new Int32PacketCommand { Command = CommandType.UpdatePlayerScore, Data = player.Score, Player = cmd.Player }); var p = new ScoreboardPacket { Commands = commands.ToArray() }; socket.SendTo(p.ToBytes(), senderRemote); } break; } } } }
public void Listen(SettingsPanelViewModel sb) { if (this.listenThread != null) { if (this.listenThread.IsAlive) { this.StopListen = true; while (this.listenThread.IsAlive) { Thread.Sleep(20); } } } this.StopListen = false; IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); var endPoints = new List<IPEndPoint>(); foreach (var entry in hostEntry.AddressList) { if (entry.AddressFamily == AddressFamily.InterNetwork) { endPoints.Add(new IPEndPoint(entry, this.PortNumber)); break; } } if (endPoints.Count == 0) { MessageBox.Show("No valid networks were found to connect to."); return; } var socket = new Socket(endPoints[0].Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp) { ReceiveTimeout = 5000 }; foreach (var e in endPoints) { socket.Bind(e); } this.listenThread = new Thread( new ThreadStart( delegate { while (this.StopListen == false) { var inBuffer = new byte[1000]; EndPoint senderRemote = new IPEndPoint(IPAddress.Any, 0); try { socket.ReceiveFrom(inBuffer, ref senderRemote); } catch (SocketException ex) { int errorCode = ex.ErrorCode; continue; } var packet = ScoreboardPacket.Parse(inBuffer); foreach (var cmd in packet.Commands) { if (cmd.Command == CommandType.Ping) { var pongCmd = new EmptyPacketCommand { Command = CommandType.Pong }; var pongPacket = new ScoreboardPacket() { Commands = new[] { pongCmd } }; socket.SendTo(pongPacket.ToBytes(), senderRemote); } } this.dispatcher.BeginInvoke((Action)(() => sb.HandleRemoteCommands(packet, socket, senderRemote))); } socket.Close(); socket.Dispose(); })); this.listenThread.Start(); this.IsListening = true; }