Exemple #1
0
        /// <summary>
        /// This method is the final delegate method given to the Network Controller to process the
        /// data from the server. It gets all server information about the world and updates both the
        /// view and the world.
        /// </summary>
        /// <param name="ss"></param>
        public void ProcessData(SocketState ss)
        {
            lock (this)
            {
                this.ss = ss;
                string   totalData = ss.sb.ToString();
                string[] parts     = Regex.Split(totalData, @"(?<=[\n])");
                JObject  blankJSonObject;

                string shipToken       = "thrust";
                string projectileToken = "owner";
                string starToken       = "mass";

                // Loop until we have processed all messages.
                // We may have received more than one.
                foreach (string p in parts)
                {
                    // Ignore empty strings added by the regex splitter
                    if (p.Length == 0)
                    {
                        continue;
                    }
                    // The regex splitter will include the last string even if it doesn't end with a '\n',
                    // So we need to ignore it if this happens.
                    if (p[p.Length - 1] != '\n')
                    {
                        break;
                    }

                    blankJSonObject = JObject.Parse(p);

                    JToken token = blankJSonObject[shipToken];

                    if (token != null)
                    {
                        Ship newShip = JsonConvert.DeserializeObject <Ship>(p);
                        theWorld.addShip(newShip);
                    }

                    token = blankJSonObject[projectileToken];

                    if (token != null)
                    {
                        Projectile newProjectile = JsonConvert.DeserializeObject <Projectile>(p);
                        if (!newProjectile.GetAlive())
                        {
                            theWorld.removeProjectile(newProjectile);
                        }
                        else
                        {
                            theWorld.addProjectile(newProjectile);
                        }
                    }

                    token = blankJSonObject[starToken];

                    if (token != null)
                    {
                        Star newStar = JsonConvert.DeserializeObject <Star>(p);
                        theWorld.addStar(newStar);
                    }
                    // Then remove it from the SocketState's growable buffer
                    ss.sb.Remove(0, p.Length);
                }

                // Call event to refresh view.
                UpdateArrived();
                if (stringToSend != null)
                {
                    UserInput(stringToSend);
                }
                Network.GetData(ss);
            }
        }