public static ScoreboardPacket Parse(byte[] data)
        {
            using (Stream stream = new MemoryStream(data))
            {
                using (var reader = new BinaryReader(stream))
                {
                    var magicNumber = reader.ReadByte();
                    if (magicNumber != MagicNumber)
                    {
                        throw new NotSupportedException("Sanity check for received packet was not valid.");
                    }

                    byte numCommands = reader.ReadByte();

                    var commands = new List <IScoreboardPacketCommand>();

                    for (int i = 0; i < numCommands; i++)
                    {
                        var pt = (PacketCommandType)reader.ReadByte();

                        switch (pt)
                        {
                        case PacketCommandType.EmptyCommand:
                        {
                            var cmd = EmptyPacketCommand.Parse(reader);
                            commands.Add(cmd);
                            break;
                        }

                        case PacketCommandType.ByteCommand:
                        {
                            var cmd = BytePacketCommand.Parse(reader);
                            commands.Add(cmd);
                            break;
                        }

                        case PacketCommandType.Int32Command:
                        {
                            var cmd = Int32PacketCommand.Parse(reader);
                            commands.Add(cmd);
                            break;
                        }

                        case PacketCommandType.StringCommand:
                        {
                            var cmd = StringPacketCommand.Parse(reader);
                            commands.Add(cmd);
                            break;
                        }
                        }
                    }

                    var packet = new ScoreboardPacket {
                        Commands = commands.ToArray()
                    };

                    return(packet);
                }
            }
        }
        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 SendPacket(ScoreboardPacket packet)
        {
            using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            {
                ReceiveTimeout = 1000
            })
            {
                socket.Connect(this.EndPoint);

                socket.Send(packet.ToBytes());
            }
        }
        private void Player2ScoreChanged(Player obj)
        {
            var commands = new List<IScoreboardPacketCommand>
                {
                    new Int32PacketCommand
                        {
                            Command = CommandType.UpdatePlayerScore, Player = 2, Data = this.Player2.Score
                        }
                };

            var packet = new ScoreboardPacket { Commands = commands.ToArray() };

            this.scoreboardUpdater.SendPacket(packet);
        }
        private void SwapPlayers()
        {
            var packet = new ScoreboardPacket { Commands = new[] { new EmptyPacketCommand { Command = CommandType.SwapPlayer } } };

            this.scoreboardUpdater.SendPacket(packet);
        }
        private void Player2NameChanged(Player obj)
        {
            var commands = new List<IScoreboardPacketCommand>();

            commands.Add(new StringPacketCommand { Command = CommandType.UpdatePlayerName, Player = 2, Data = Player2.Name });

            var packet = new ScoreboardPacket { Commands = commands.ToArray() };

            this.scoreboardUpdater.SendPacket(packet);
        }
        private void Player2RaceChanged(Player obj)
        {
            var commands = new List<IScoreboardPacketCommand>();

            commands.Add(new BytePacketCommand { Command = CommandType.UpdatePlayerRace, Player = 2, Data = (byte)Player2.Race });

            var packet = new ScoreboardPacket { Commands = commands.ToArray() };

            this.scoreboardUpdater.SendPacket(packet);
        }
        private void Player1RaceChanged(Player obj)
        {
            var commands = new List<IScoreboardPacketCommand>
                {
                    new BytePacketCommand
                        {
                            Command = CommandType.UpdatePlayerRace, Player = 1, Data = (byte)this.Player1.Race
                        }
                };

            var packet = new ScoreboardPacket { Commands = commands.ToArray() };

            this.scoreboardUpdater.SendPacket(packet);
        }
        private void Player1ScoreChanged(Player obj)
        {
            var commands = new List<IScoreboardPacketCommand>();

            commands.Add(new Int32PacketCommand { Command = CommandType.UpdatePlayerScore, Player = 1, Data = Player1.Score });

            var packet = new ScoreboardPacket { Commands = commands.ToArray() };

            this.scoreboardUpdater.SendPacket(packet);
        }
        public static ScoreboardPacket Parse(byte[] data)
        {
            using (Stream stream = new MemoryStream(data))
            {
                using (var reader = new BinaryReader(stream))
                {
                    var magicNumber = reader.ReadByte();
                    if (magicNumber != MagicNumber)
                    {
                        throw new NotSupportedException("Sanity check for received packet was not valid.");
                    }

                    byte numCommands = reader.ReadByte();

                    var commands = new List<IScoreboardPacketCommand>();

                    for (int i = 0; i < numCommands; i++)
                    {
                        var pt = (PacketCommandType)reader.ReadByte();

                        switch (pt)
                        {
                            case PacketCommandType.EmptyCommand:
                                {
                                    var cmd = EmptyPacketCommand.Parse(reader);
                                    commands.Add(cmd);
                                    break;
                                }

                            case PacketCommandType.ByteCommand:
                                {
                                    var cmd = BytePacketCommand.Parse(reader);
                                    commands.Add(cmd);
                                    break;
                                }

                            case PacketCommandType.Int32Command:
                                {
                                    var cmd = Int32PacketCommand.Parse(reader);
                                    commands.Add(cmd);
                                    break;
                                }

                            case PacketCommandType.StringCommand:
                                {
                                    var cmd = StringPacketCommand.Parse(reader);
                                    commands.Add(cmd);
                                    break;
                                }
                        }
                    }

                    var packet = new ScoreboardPacket { Commands = commands.ToArray() };

                    return packet;
                }
            }
        }
        private void Player1NameChanged(Player obj)
        {
            var commands = new List<IScoreboardPacketCommand>
                {
                    new StringPacketCommand
                        {
                            Command = CommandType.UpdatePlayerName, Player = 1, Data = this.Player1.Name
                        }
                };

            var packet = new ScoreboardPacket { Commands = commands.ToArray() };

            this.scoreboardUpdater.SendPacket(packet);
        }
        private void ToggleScoreboardVisible()
        {
            var packet = new ScoreboardPacket { Commands = new[] { new EmptyPacketCommand { Command = CommandType.ShowScoreboard } } };

            this.scoreboardUpdater.SendPacket(packet);
        }
        /// <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 HandleRemoteCommands(ScoreboardPacket packet)
        {
            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.Scoreboard.Player1.Name = cmd.Data;
                                }
                                else if (cmd.Player == 2)
                                {
                                    this.Scoreboard.Player2.Name = cmd.Data;
                                }
                            }

                            break;
                        }

                    case CommandType.UpdatePlayerScore:
                        {
                            var cmd = command as Int32PacketCommand;

                            if (cmd != null)
                            {
                                if (cmd.Player == 1)
                                {
                                    this.Scoreboard.Player1.Score = cmd.Data;
                                }
                                else if (cmd.Player == 2)
                                {
                                    this.Scoreboard.Player2.Score = cmd.Data;
                                }
                            }

                            break;
                        }

                    case CommandType.UpdatePlayerColor:
                        {
                            var cmd = command as BytePacketCommand;

                            if (cmd != null)
                            {
                                if (cmd.Player == 1)
                                {
                                    this.Scoreboard.Player1.Color = (PlayerColor)cmd.Data;
                                }
                                else if (cmd.Player == 2)
                                {
                                    this.Scoreboard.Player2.Color = (PlayerColor)cmd.Data;
                                }
                            }

                            break;
                        }

                    case CommandType.UpdatePlayerRace:
                        {
                            var cmd = command as BytePacketCommand;

                            if (cmd != null)
                            {
                                if (cmd.Player == 1)
                                {
                                    this.Scoreboard.Player1.Race = (Race)cmd.Data;
                                }
                                else if (cmd.Player == 2)
                                {
                                    this.Scoreboard.Player2.Race = (Race)cmd.Data;
                                }
                            }

                            break;
                        }

                    case CommandType.ToggleAnnouncement:
                        this.ToggleAnnouncement();
                        break;

                    case CommandType.ToggleSubbar:
                        this.ToggleSubbar();
                        break;

                    case CommandType.SwapPlayer:
                        this.SwapPlayers();
                        break;

                    case CommandType.ShowScoreboard:
                        this.ToggleScoreboardVisible();
                        break;

                    case CommandType.HideScoreboard:
                        this.ToggleScoreboardVisible();
                        break;

                    case CommandType.ResetPlayer:
                        {
                            var cmd = command as EmptyPacketCommand;

                            if (cmd != null)
                            {
                                if (cmd.Player == 1)
                                {
                                    this.Scoreboard.Player1.Reset();
                                }
                                else if (cmd.Player == 2)
                                {
                                    this.Scoreboard.Player2.Reset();
                                }
                            }

                            break;
                        }
                }
            }
        }
Example #17
0
        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;
        }
        private void ToggleSubbar()
        {
            var packet = new ScoreboardPacket { Commands = new[] { new EmptyPacketCommand { Command = CommandType.ToggleSubbar } } };

            this.scoreboardUpdater.SendPacket(packet);
        }
        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;
        }