Ejemplo n.º 1
0
        /// <summary>
        /// Get a Vector3 out of a Networking Stream
        /// </summary>
        /// <param name="stream">Networking Stream to be used</param>
        /// <returns>A Vector3 out of the Networking Stream</returns>
        public static object MapVector3(NetworkingStream stream)
        {
            x = BitConverter.ToSingle(stream.Read(sizeof(float)), 0);
            y = BitConverter.ToSingle(stream.Read(sizeof(float)), 0);
            z = BitConverter.ToSingle(stream.Read(sizeof(float)), 0);

            return(new Vector3(x, y, z));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get a Vector4 out of a Networking Stream
        /// </summary>
        /// <param name="type">Type of object to be mapped</param>
        /// <param name="stream">Networking Stream to be used</param>
        /// <returns>A type of Vector4 (Vector4/Color/Quaternion) out of the Networking Stream</returns>
        public static object MapVector4(Type type, NetworkingStream stream)
        {
            x = BitConverter.ToSingle(stream.Read(sizeof(float)), 0);
            y = BitConverter.ToSingle(stream.Read(sizeof(float)), 0);
            z = BitConverter.ToSingle(stream.Read(sizeof(float)), 0);
            w = BitConverter.ToSingle(stream.Read(sizeof(float)), 0);

            if (type == typeof(Vector4))
            {
                return(new Vector4(x, y, z, w));
            }
            else if (type == typeof(Color))
            {
                return(new Color(x, y, z, w));
            }
            else            // if (type == typeof(Quaternion))
            {
                return(new Quaternion(x, y, z, w));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Map a string from the Networking Stream
        /// </summary>
        /// <param name="stream">Networking Stream to be used</param>
        /// <returns>Returns a string out of the Networking Stream</returns>
        public static object MapString(NetworkingStream stream)
        {
            int length = BitConverter.ToInt32(stream.Read(sizeof(int)), 0);

            if (length <= 0)
            {
                return(string.Empty);
            }

#if NetFX_CORE
            return(Encoding.UTF8.GetString(stream.Read(length), 0, length));
#else
            if (length > stream.Bytes.Size - sizeof(int))
            {
                return(string.Empty);
                //throw new NetworkException(12, "Attempted to read a string that doesn't exist");
            }

            return(Encoding.UTF8.GetString(stream.Read(length), 0, length));
#endif
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get a byte array of a Networking Stream
        /// </summary>
        /// <param name="type">Type of object to be mapped</param>
        /// <param name="stream">Networking Stream to be used</param>
        /// <returns>A byte array that was read from the Networking Stream</returns>
        public static object MapByteArray(NetworkingStream stream)
        {
            byteArrSize = Map <int>(stream);
            byte[] readBytes = stream.Read(byteArrSize);

            byte[] value = new byte[byteArrSize];
            for (int i = 0; i < value.Length; i++)
            {
                value[i] = readBytes[i];
            }

            return(value);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Get a BMSByte out of a Networking Stream
 /// </summary>
 /// <param name="type">Type of object to be mapped</param>
 /// <param name="stream">Networking Stream to be used</param>
 /// <returns>A BMSByte that was read from the Networking Stream</returns>
 public static object MapBMSByte(NetworkingStream stream)
 {
     size = Map <int>(stream);
     return(new BMSByte().Clone(stream.Read(size), size));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Get a mapped basic type of object from the Networking Stream
 /// </summary>
 /// <param name="type">Type of object to be mapped</param>
 /// <param name="stream">Networking Stream to be used</param>
 /// <returns>Returns a mapped object of the given type</returns>
 public static object MapBasicType(Type type, NetworkingStream stream)
 {
     if (type == typeof(sbyte))
     {
         return((sbyte)stream.Read(sizeof(sbyte))[0]);
     }
     else if (type == typeof(byte))
     {
         return((byte)stream.Read(sizeof(byte))[0]);
     }
     else if (type == typeof(char))
     {
         return((byte)stream.Read(sizeof(byte))[0]);
     }
     else if (type == typeof(short))
     {
         return(BitConverter.ToInt16(stream.Read(sizeof(short)), 0));
     }
     else if (type == typeof(ushort))
     {
         return(BitConverter.ToUInt16(stream.Read(sizeof(short)), 0));
     }
     else if (type == typeof(bool))
     {
         return(BitConverter.ToBoolean(stream.Read(sizeof(bool)), 0));
     }
     else if (type == typeof(int))
     {
         return(BitConverter.ToInt32(stream.Read(sizeof(int)), 0));
     }
     else if (type == typeof(uint))
     {
         return(BitConverter.ToUInt32(stream.Read(sizeof(int)), 0));
     }
     else if (type == typeof(float))
     {
         return(BitConverter.ToSingle(stream.Read(sizeof(float)), 0));
     }
     else if (type == typeof(long))
     {
         return(BitConverter.ToInt64(stream.Read(sizeof(long)), 0));
     }
     else if (type == typeof(ulong))
     {
         return(BitConverter.ToUInt64(stream.Read(sizeof(long)), 0));
     }
     else if (type == typeof(double))
     {
         return(BitConverter.ToDouble(stream.Read(sizeof(double)), 0));
     }
     else
     {
         throw new NetworkException(11, "The type " + type.ToString() + " is not allowed to be sent over the Network (yet)");
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Get a Vector2 out of a Networking Stream
 /// </summary>
 /// <param name="stream">Networking Stream to be used</param>
 /// <returns>A Vector2 out of the Networking Stream</returns>
 public static object MapVector2(NetworkingStream stream)
 {
     x = BitConverter.ToSingle(stream.Read(ref readBytes, sizeof(float)), 0);
     y = BitConverter.ToSingle(stream.Read(ref readBytes, sizeof(float)), 0);
     return(new Vector2(x, y));
 }