Beispiel #1
0
        /// <summary>
        /// Receives and handles all server updates, by sending them forward to other handlers
        /// </summary>
        /// <param name="update">The serverUpdate to be handled, not null.</param>
        public void OnServerUpdate(AbstractUpdate update)
        {
            Assert.IsNotNull(update);
            switch (update.Type)
            {
            case UpdateType.UpdatePosition:
            case UpdateType.DeletePosition:
                this.SendMessage("OnPositionUpdate", update as PositionUpdate);
                break;

            case UpdateType.UpdateRotation:
                this.SendMessage("OnRotationUpdate", update as RotationUpdate);
                break;

            case UpdateType.UpdateLevel:
                this.BroadcastMessage("OnLevelUpdate", update as LevelUpdate);
                break;

            case UpdateType.UpdateARView:
                this.SendMessage("OnFollowPlayerInfo", update as ARViewUpdate);
                break;

            default:
                Assert.IsTrue(false, "Reached unreachable default case in MessageDistributer");
                break;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Reads all updates and sends the "OnPositionUpdate" message.
        /// </summary>
        /// <returns>The amount of updates parsed.</returns>
        public int ReadAllUpdates()
        {
            int count = 0;

            if (this.socket == null)
            {
                // This may sometimes happen without any explanation. Our only option is to restart
                // the socket connection.
                Debug.Log("Restarted broken socket connection");
                this.Start();
                return(0);
            }

            while (this.socket.Available >= MinPacketSize && count < MaxUpdates)
            {
                AbstractUpdate update = this.ReadMessage();
                if (update == null)
                {
                    return(count);
                }

                this.timestamp = DateTime.Now;
                if (update.Type != UpdateType.Ping)
                {
                    this.SendMessage("OnServerUpdate", update, SendMessageOptions.DontRequireReceiver);
                }

                count++;
            }

            long duration = (DateTime.Now - this.timestamp).Milliseconds;

            if (count == 0 && duration > Timeout)
            {
                // Assume we lost connection: Reset this ClientSocket instance.
                this.DisconnectSocket();
                this.Start();
            }

            return(count);
        }