예제 #1
0
        internal static NetMessage ReadNetMessage(NetStream stream)
        {
            List <Type> paramTypes;
            ushort      messageId = stream.ReadUShort(11);
            uint        viewId    = 0;

            if (stream.ReadBool())
            {
                viewId = stream.ReadUInt(20);
            }

            if (messageId == (int)Cmd.RequestResponse)
            {
                return(CreateResponseMessage(stream, messageId, viewId));
            }

            if (messageId > 1800)
            {
                if (!stream.Socket.Command.Exists(messageId))
                {
                    NetLog.Error("Cannot deserialize message, Command ID not found: " + messageId);
                    return(null);
                }
                paramTypes = stream.Socket.Command.ParamTypes(messageId);
            }
            else
            {
                if (!stream.Socket.Rpc.Exists(messageId))
                {
                    NetLog.Error("Cannot deserialize message, RPC ID not found: " + messageId);
                    return(null);
                }
                paramTypes = stream.Socket.Rpc.ParamTypes(messageId);
            }

            NetMessage netMessage = NetMessage.Create(messageId, viewId, paramTypes.Count, false);

            if (stream.Socket.Rpc.TakesRequests(messageId))
            {
                return(CreateRequestMessage(stream, netMessage, paramTypes));
            }

            for (int i = 0; i < paramTypes.Count; i++)
            {
                netMessage.Parameters[i] = ReadParam(stream, paramTypes[i]);
            }

            return(netMessage);
        }
예제 #2
0
        private static NetMessage CreateResponseMessage(NetStream stream, ushort messageId, uint viewId)
        {
            ushort requestId = stream.ReadUShort();

            if (!stream.Socket.Request.Exists(viewId, requestId))
            {
                return(null);
            }
            bool   isSuccessful = stream.ReadBool();
            object result       = null;

            if (isSuccessful)
            {
                Type resultType = stream.Socket.Request.Type(viewId, requestId);
                result = ReadParam(stream, resultType);
            }
            object[] requestParams = { requestId, isSuccessful, result };
            return(NetMessage.Create(messageId, viewId, requestParams, true));
        }
예제 #3
0
        private static NetMessage CreateRequestMessage(NetStream stream, NetMessage netMessage, List <Type> paramTypes)
        {
            int requestIndex = -1;

            for (int i = 0; i < paramTypes.Count; i++)
            {
                if (paramTypes[i].IsGenericType && paramTypes[i].GetGenericTypeDefinition() == typeof(NetRequest <>))
                {
                    requestIndex             = i;
                    netMessage.Parameters[i] = null;
                    continue;
                }
                netMessage.Parameters[i] = ReadParam(stream, paramTypes[i]);
            }

            if (!stream.CanRead(16))
            {
                return(netMessage);
            }

            ushort requestId = stream.ReadUShort();

            if (requestIndex != -1)
            {
                netMessage.Parameters[requestIndex] = Activator.CreateInstance(paramTypes[requestIndex],
                                                                               netMessage.ViewId, requestId, stream.Connection);
            }
            else
            {
                object[] newParams = new object[paramTypes.Count + 1];
                netMessage.Parameters.CopyTo(newParams, 0);
                newParams[newParams.Length - 1] = requestId;
                netMessage.Parameters           = newParams;
            }
            return(netMessage);
        }
예제 #4
0
 internal static NetHeader FromStream(NetStream stream) {
     return new NetHeader(NetMath.Trim(stream.ReadUShort()), NetMath.Trim(stream.ReadUShort()),
         stream.ReadULong(), stream.ReadUShort());
 }
예제 #5
0
 internal static NetHeader FromStream(NetStream stream)
 {
     return(new NetHeader(NetMath.Trim(stream.ReadUShort()), NetMath.Trim(stream.ReadUShort()),
                          stream.ReadULong(), stream.ReadUShort()));
 }
예제 #6
0
        private static object ReadParam(NetStream stream, Type type)
        {
            if (type == typeof(bool))
            {
                return(stream.ReadBool());
            }
            if (type == typeof(byte))
            {
                return(stream.ReadByte());
            }
            if (type == typeof(short))
            {
                return(stream.ReadShort());
            }
            if (type == typeof(ushort))
            {
                return(stream.ReadUShort());
            }
            if (type == typeof(int))
            {
                return(stream.ReadInt());
            }
            if (type == typeof(uint))
            {
                return(stream.ReadUInt());
            }
            if (type == typeof(float))
            {
                return(stream.ReadFloat());
            }
            if (type == typeof(long))
            {
                return(stream.ReadLong());
            }
            if (type == typeof(ulong))
            {
                return(stream.ReadULong());
            }
            if (type == typeof(double))
            {
                return(stream.ReadDouble());
            }
            if (type == typeof(string))
            {
                return(stream.ReadString());
            }
            if (type == typeof(Vector2))
            {
                return(stream.ReadVector2());
            }
            if (type == typeof(Vector3))
            {
                return(stream.ReadVector3());
            }
            if (type == typeof(Quaternion))
            {
                return(stream.ReadQuaternion());
            }

            if (type == typeof(bool[]))
            {
                ushort length = stream.ReadUShort();
                bool[] arr    = new bool[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadBool();
                }
                return(arr);
            }
            if (type == typeof(byte[]))
            {
                ushort length = stream.ReadUShort();
                byte[] arr    = new byte[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadByte();
                }
                return(arr);
            }
            if (type == typeof(ushort[]))
            {
                ushort   length = stream.ReadUShort();
                ushort[] arr    = new ushort[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadUShort();
                }
                return(arr);
            }
            if (type == typeof(int[]))
            {
                ushort length = stream.ReadUShort();
                int[]  arr    = new int[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadInt();
                }
                return(arr);
            }
            if (type == typeof(uint[]))
            {
                ushort length = stream.ReadUShort();
                uint[] arr    = new uint[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadUInt();
                }
                return(arr);
            }
            if (type == typeof(float[]))
            {
                ushort  length = stream.ReadUShort();
                float[] arr    = new float[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadFloat();
                }
                return(arr);
            }
            if (type == typeof(string[]))
            {
                ushort   length = stream.ReadUShort();
                string[] arr    = new string[length];
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = stream.ReadString();
                }
                return(arr);
            }
            if (type == typeof(char[]))
            {
                ushort length = stream.ReadUShort();
                char[] arr    = new char[length];
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = stream.ReadChar();
                }
                return(arr);
            }
            if (type == typeof(NetMessage))
            {
                return(ReadNetMessage(stream));
            }
            if (type == typeof(NetMessage[]))
            {
                byte         length = stream.ReadByte();
                NetMessage[] arr    = new NetMessage[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = ReadNetMessage(stream);
                }
                return(arr);
            }
            if (type == typeof(NetConnection))
            {
                return(stream.Connection);
            }
            if (type == typeof(IPAddress))
            {
                byte[] array = new byte[4];
                stream.ReadByteArray(array);
                return(new IPAddress(array));
            }
            if (type == typeof(IPEndPoint))
            {
                if (!stream.ReadBool())
                {
                    return(null);
                }
                return(new IPEndPoint((IPAddress)ReadParam(stream, typeof(IPAddress)), stream.ReadUShort()));
            }
            if (type == typeof(NetStream))
            {
                return(stream.ReadNetStream());
            }
            if (type == typeof(NetZone))
            {
                return(new NetZone {
                    Id = stream.ReadUInt(),
                    ServerEndpoint =
                        stream.ReadBool()
                            ? (IPEndPoint)ReadParam(stream, typeof(IPEndPoint))
                            : null,
                    Position = stream.ReadVector3(),
                    ViewIdMin = stream.ReadInt(),
                    ViewIdMax = stream.ReadInt()
                });
            }
            if (HasType(type))
            {
                return(Read(stream, type));
            }

            // We don't know how to deserialize this type
            throw new Exception("Deserializer not implemented for type: " + type);
        }