Exemple #1
0
        void Server_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 0) {
                // 0: Started.
                if (null != this.Started)
                    this.Started(this, new EventArgs());

            } else if (e.ProgressPercentage == 1) {
                // 1: Client connected
                RemoteInformation ri = e.UserState as RemoteInformation;
                if (null == ri)
                    return;

                NetworkEventArgs arg = new NetworkEventArgs(ri,
                     new NetworkContent(EnumNetworkContentType.String, ri.Name, this.Encoding));

                if (null != this.ClientConnecting)
                    this.ClientConnecting(this, arg);

                // cancelled or reach max connections
                if (arg.Cancelled) {
                    Host.SendData(ri.Connection, new NetworkContent(EnumNetworkContentType.String,
                        "BLOCKED: Connection is cancelled by server.").GetBinary());
                    ri.Connection.Close();

                } else if (this.ClientCount >= this.MaxConnections) {
                    Host.SendData(ri.Connection, new NetworkContent(EnumNetworkContentType.String,
                        string.Format("BLOCKED: Reached limitation of max connections (now it's {0}).",
                        this.MaxConnections)).GetBinary());
                    ri.Connection.Close();

                } else {
                    ri.Worker = new BackgroundWorker();
                    ri.Worker.DoWork += new DoWorkEventHandler(Client_DoWork);
                    ri.Worker.ProgressChanged += new ProgressChangedEventHandler(Client_ProgressChanged);
                    ri.Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Client_RunWorkerCompleted);
                    ri.Worker.WorkerReportsProgress = true;
                    ri.Worker.WorkerSupportsCancellation = true;
                    ri.Worker.RunWorkerAsync(ri);

                    // the name could be changed in event by caller.
                    m_clients.Add(ri.Name, ri);

                    if (null != this.ClientConnected) {
                        arg.Cancelled = false;
                        this.ClientConnected(this, arg);
                    }
                }
            }
        }
Exemple #2
0
        void OnClientDisconnected(object sender, NetworkEventArgs e)
        {
            RemoteInformation ri = e.RemoteInformation;
            if (null == ri)
                return;
            Player player = this.GetPlayerByHostName(ri.Name);
            if (null != player) {
                this.RemovePlayer(player);

                this.CallClients(player.HostName, "QUIT");

                if (null != this.PlayerLeaved)
                    this.PlayerLeaved(this, new PlayerEventArgs(player));
            }
        }
Exemple #3
0
 void OnClientConnecting(object sender, NetworkEventArgs e)
 {
     if (this.Players.Count == this.MaxPlayers)
         e.Cancelled = true;
 }
Exemple #4
0
        void OnClientConnected(object sender, NetworkEventArgs e)
        {
            RemoteInformation ri = e.RemoteInformation;
            if (null == ri)
                return;

            Player player = new Player();
            player.HostName = ri.Name;
            player.EndPoint = ri.EndPoint;
            this.AddPlayer(player);

            // tell new player how many players here
            foreach (Player p in this.Players.Values) {
                if (p != player) {
                    this.CallClient(player.HostName, p.HostName, "JOIN");
                    this.CallClient(player.HostName, p.HostName, "NAME," + p.Name);
                }
            }

            // tell all players the new player joined
            this.CallClients(player.HostName, "JOIN");
            // Get client player name
            this.CallClient(player.HostName, player.HostName, "NAME");
            // tell client the max players
            this.CallClient(player.HostName, player.HostName, "MAX," + this.MaxPlayers.ToString());

            if (null != this.PlayerJoined)
                this.PlayerJoined(this, new PlayerEventArgs(player));
        }
Exemple #5
0
        void OnClientCalled(object sender, NetworkEventArgs e)
        {
            if (null == e.Content)
                return;
            string hostName = e.RemoteInformation.Name;
            string tmp = e.Content.GetString();
            if (string.IsNullOrEmpty(tmp))
                return;

            string[] commands = tmp.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string command in commands) {

                string[] cmd = this.ParseCommand(command);

                if (cmd.Length < 2)
                    break;
                cmd[1] = cmd[1].ToUpper();
                //string hostName = cmd[0];
                //string action = cmd[1];
                //string arg =
                if (cmd.Length > 2 && cmd[1].Equals("NAME")) {
                    string playerName = cmd[2];
                    this.ChangePlayerName(hostName, playerName);
                    // notify client
                    //this.CallClients(hostName, "NAME," + playerName);
                    this.DispatchCommand(e.Content);
                } else if (cmd.Length > 1 && cmd[1].Equals("NEXT")) {
                    ICommand commander = null;
                    if (this.Commands.TryGetValue(cmd[1], out commander)) {
                        commander.Parameters = cmd;
                        if (commander.Execute()) {
                            string result = commander.Result as string;
                            this.CallClients(hostName, string.Format("NEXT,{0}", result));
                        } else
                            Trace.TraceWarning(commander.ErrorMessage);
                    }
                } else if (cmd.Length > 2 && cmd[1].Equals("READY")) {
                    m_clientReady[hostName] = Convert.ToInt32(cmd[2]);
                    if (this.PlayerPrepared != null) {
                        Player player = this.GetPlayerByHostName(hostName);
                        this.PlayerPrepared(this, new PlayerEventArgs(player));
                    }
                } else
                    this.DispatchCommand(e.Content);
            }
        }
Exemple #6
0
 void OnServerCalled(object sender, NetworkEventArgs e)
 {
     string tmp = e.Content.GetString();
     string[] commands = tmp.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
     for (int i = 0; i < commands.Length; i++) {
         this.ExecuteCommand(this.ParseCommand(commands[i]));
     }
 }