示例#1
0
        /// <summary>
        /// Reads an <see cref="ARViewUpdate"/> message from the given byte array.
        /// </summary>
        /// <param name="bytes">The byte array to read from.</param>
        /// <param name="length">The maximum amount of bytes to read.</param>
        /// <returns>The created <see cref="ARViewUpdate"/>.</returns>
        public static ARViewUpdate ReadARViewUpdate(byte[] bytes, int length)
        {
            Assert.IsNotNull(bytes);
            Assert.IsFalse(length < 28, "ARViewUpdate length needs to be at least 28");
            Assert.IsFalse(bytes.Length < length, "byte array length is insufficient");

            int     id       = MessageProcessor.ReadInt32(bytes, 0);
            Vector3 position = new Vector3(
                MessageProcessor.ReadSingle(bytes, 4),
                MessageProcessor.ReadSingle(bytes, 8),
                MessageProcessor.ReadSingle(bytes, 12));
            Vector3 rotation = new Vector3(
                MessageProcessor.ReadSingle(bytes, 16),
                MessageProcessor.ReadSingle(bytes, 20),
                MessageProcessor.ReadSingle(bytes, 24));

            return(new ARViewUpdate(id, position, rotation));
        }
示例#2
0
        internal void AcceptReaderConnection(Socket readingSocket,
                                             MessageProcessor messageProcessor)
        {
            try
            {
                if (IsClosed)
                {
                    throw new NodeException("AcceptConnection: node is disconnected " + FullDescription());
                }

                if (readerStatus != ReadStatus.READY)
                {
                    throw new NodeException("AcceptConnection: reader is aready initialized " + FullDescription());
                }

                reader = new SocketReader(sync,
                                          (stm) =>
                {
                    //string sentMsg = mtp.ToString();
                    //if (MasterFileLog.LogLevel > 2)
                    //    sentMsg += new ChunkDebug(stm).GetData() + "\n\n";

                    //Log.EntryVerbose(LogR, sentMsg);

                    messageProcessor(stm, this);
                },
                                          (ioex) =>
                {
                    readerStatus = ReadStatus.DISCONNECTED;
                    Close(ioex, DisconnectType.READ);
                },
                                          OnSoftDisconnectReader,
                                          readingSocket);

                readerStatus = ReadStatus.READING;
            }
            catch (Exception)
            {
                readingSocket.Close();
                throw;
            }
        }
示例#3
0
        /// <summary>
        /// Reads a single PositionUpdate message from the Socket.
        /// <para>
        /// If not enough data is available, this method may return null.
        /// </para>
        /// </summary>
        /// <returns>The PositionUpdate.</returns>
        public AbstractUpdate ReadMessage()
        {
            int received = this.socket.Receive(this.buffer, 1, SocketFlags.None);

            if (received < 1)
            {
                Debug.Log("Received not enough bytes: " + received);
                return(null);
            }

            byte type = this.buffer[0];

            switch ((UpdateType)type)
            {
            case UpdateType.DeletePosition:
                received = this.socket.Receive(this.buffer, 4, SocketFlags.None);
                return(MessageProcessor.ReadDelete(this.buffer, received));

            case UpdateType.UpdatePosition:
                received = this.socket.Receive(this.buffer, 16, SocketFlags.None);
                return(MessageProcessor.ReadUpdatePosition(this.buffer, received));

            case UpdateType.Ping:
                return(new PingUpdate());

            case UpdateType.UpdateRotation:
                received = this.socket.Receive(this.buffer, 8, SocketFlags.None);
                return(MessageProcessor.ReadUpdateRotation(this.buffer, received));

            case UpdateType.UpdateLevel:
                received = this.socket.Receive(this.buffer, 16, SocketFlags.None);
                return(MessageProcessor.ReadUpdateLevel(this.buffer, received));

            case UpdateType.UpdateARView:
                received = this.socket.Receive(this.buffer, 28, SocketFlags.None);
                return(MessageProcessor.ReadARViewUpdate(this.buffer, received));

            default:
                return(null);
            }
        }
示例#4
0
        /// <summary>
        /// Reads a <c>Delete</c> type PositionUpdate message.
        /// </summary>
        /// <param name="buffer">The byte array containing data.</param>
        /// <param name="length">The length of the message.</param>
        /// <returns>The PositionUpdate.</returns>
        public static PositionUpdate ReadDelete(byte[] buffer, int length)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (buffer.Length < length)
            {
                throw new ArgumentOutOfRangeException("buffer", buffer, "The buffer is not long enough to contain a message of the specified length.");
            }

            if (length < 4)
            {
                return(null);
            }

            int id = MessageProcessor.ReadInt32(buffer, 0);

            return(new PositionUpdate(UpdateType.DeletePosition, new Vector2(0, 0), 0, id));
        }
示例#5
0
        /// <summary>
        /// Reads a <c>UpdateRotation</c> type RotationUpdate message.
        /// </summary>
        /// <param name="buffer">The byte array containing data.</param>
        /// <param name="length">The length of the message.</param>
        /// <returns>The RotationUpdate.</returns>
        public static RotationUpdate ReadUpdateRotation(byte[] buffer, int length)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (buffer.Length < length)
            {
                throw new ArgumentOutOfRangeException("buffer", buffer, "The buffer is not long enough to contain a message of the specified length.");
            }

            if (length < 8)
            {
                return(null);
            }

            int   id       = MessageProcessor.ReadInt32(buffer, 0);
            float rotation = MessageProcessor.ReadSingle(buffer, 4);

            return(new RotationUpdate(UpdateType.UpdateRotation, rotation, id));
        }
示例#6
0
        /// <summary>
        /// Reads a <c>UpdateRotation</c> type RotationUpdate message.
        /// </summary>
        /// <param name="buffer">The byte array containing data.</param>
        /// <param name="length">The length of the message.</param>
        /// <returns>The RotationUpdate.</returns>
        public static LevelUpdate ReadUpdateLevel(byte[] buffer, int length)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (buffer.Length < length)
            {
                throw new ArgumentOutOfRangeException("buffer", buffer, "The buffer is not long enough to contain a message of the specified length.");
            }

            if (length < 16)
            {
                return(null);
            }

            int   index  = MessageProcessor.ReadInt32(buffer, 0);
            int   time   = MessageProcessor.ReadInt32(buffer, 4);
            float width  = MessageProcessor.ReadSingle(buffer, 8);
            float height = MessageProcessor.ReadSingle(buffer, 12);

            return(new LevelUpdate(index, new Vector2(width, height), time));
        }
示例#7
0
 /// <summary>
 /// Called whenever a local player changes its position.
 /// </summary>
 /// <param name="update">The <see cref="ARViewUpdate"/> to send.</param>
 public void OnSendPosition(ARViewUpdate update)
 {
     this.socket.Send(MessageProcessor.WriteARViewUpdate(update));
 }
示例#8
0
 /// <summary>
 /// Called whenever one local player manages to complete the level.
 /// </summary>
 /// <param name="update">The LevelUpdate describing the change.</param>
 public void OnLevelCompleted(LevelUpdate update)
 {
     this.socket.Send(MessageProcessor.WriteLevelUpdate(update));
 }
示例#9
0
 /// <summary>
 /// Called whenever the remote player rotates an object.
 /// </summary>
 /// <param name="update">The RotationUpdate describing the change.</param>
 public void OnRotationChanged(RotationUpdate update)
 {
     this.socket.Send(MessageProcessor.WriteRotationUpdate(update));
 }