Example #1
0
        internal void Send(int id, NetConnection connection, NetStream stream)
        {
            NetMessage message = NetMessage.Create((ushort)id, 0, 1, true);

            message.Parameters[0] = stream;
            connection.Send(message);
        }
Example #2
0
        internal bool Dispatch(NetMessage message, NetConnection connection, MethodInfo method, object instance)
        {
            List <Type> paramTypes = RpcInfoCache.ParamTypes(method.Name);

            if (paramTypes.Count >= message.Parameters.Length)
            {
                return(false);
            }

            if (method.ReturnType == typeof(IEnumerator) || method.ReturnType == typeof(void))
            {
                return(false);
            }

            ushort requestId = (ushort)message.Parameters[message.Parameters.Length - 1];

            object[] culledParams = new object[paramTypes.Count];

            for (int i = 0; i < paramTypes.Count; i++)
            {
                culledParams[i] = message.Parameters[i];
            }

            message.Parameters = culledParams;

            object result = method.Invoke(instance, message.Parameters);

            connection.Send(NetMessage.Create((ushort)Cmd.RequestResponse, message.ViewId,
                                              new object[] { requestId, true, result }, true));

            return(true);
        }
Example #3
0
        internal Request <T> Send <T>(uint viewId, string methodName, NetConnection target, object[] parameters)
        {
            if (!Socket.Rpc.HasId(methodName))
            {
                throw new Exception("Cannot create request. Id does not exist for RPC: " + methodName);
            }

            if (requestIdIndex == ushort.MaxValue)
            {
                requestIdIndex = 1;
            }

            ushort requestId = requestIdIndex++;
            uint   key       = CreateKey(viewId, requestId);

            if (requests.ContainsKey(key))
            {
                throw new Exception(
                          "RequestID already exists. Requests either not completing or being sent too quickly.");
            }

            object[] newParams = new object[parameters.Length + 1];
            parameters.CopyTo(newParams, 0);
            newParams[newParams.Length - 1] = requestId;

            NetMessage netMessage = NetMessage.Create(Socket.Rpc.NameToId(methodName), viewId, newParams, true);

            NetRequest <T> request = new NetRequest <T>(Socket, viewId, requestId);

            requests.Add(key, request);
            target.Send(netMessage);

            return(request);
        }
Example #4
0
 private void ResponseSet()
 {
     if (requestor == null)
     {
         return;
     }
     object[] responseParams = { requestId, true, result };
     requestor.Send(NetMessage.Create((ushort)Cmd.RequestResponse, viewId, responseParams, true));
 }
Example #5
0
        private void SendOwnerData(NetConnection connection)
        {
            var msg = NetMessage.Create((ushort)ViewCmd.CreateOwnerView, (uint)Id, 4, true);

            msg.Parameters[0] = Id;
            msg.Parameters[1] = Group;
            msg.Parameters[2] = PrefabRoot;
            msg.Parameters[3] = TriggerGetOwnerData();
            connection.Send(msg);
        }
Example #6
0
        /// <summary> Sends a reliable RPC that does not target a specific view. </summary>
        public void Send(string methodName, NetConnection target, params object[] parameters)
        {
            if (!Rpc.HasId(methodName))
            {
                NetLog.Error("Remote RPC does not have an assigned ID: " + methodName);
                return;
            }
            var message = NetMessage.Create(Rpc.NameToId(methodName), 0, parameters, true);

            target.Send(message);
        }
Example #7
0
        /// <summary> Send overload that creates the NetMessage for the RPC. </summary>
        internal void Send(int viewId, bool reliable, string methodName, NetConnection target, params object[] parameters)
        {
            if (!Socket.Rpc.HasId(methodName))
            {
                NetLog.Error("Send failed: RPC method name has not been assigned an ID.");
                return;
            }
            var netMessage = NetMessage.Create(Socket.Rpc.NameToId(methodName), (uint)viewId, parameters, reliable);

            target.Send(netMessage);
        }
Example #8
0
 /// <summary>
 /// Sends the NetStream to the RPC at the supplied target.
 /// </summary>
 /// <param name="target"> Who to send the RPC to. For a client, this should always be Server. </param>
 private void SendSync(RpcTarget target)
 {
     if (target == RpcTarget.None)
     {
         return;
     }
     if (syncMessage == null)
     {
         syncMessage = NetMessage.Create((ushort)ViewCmd.Sync, (uint)Id, 1, false);
     }
     syncMessage.Parameters[0] = syncStream;
     ViewManager.Send(this, syncMessage, target);
 }
Example #9
0
        private void SendPeerData(NetConnection connection)
        {
            var msg = NetMessage.Create((ushort)ViewCmd.CreatePeerView, (uint)Id, 5, true);

            msg.Parameters[0] = Id;
            msg.Parameters[1] = Group;
            msg.Parameters[2] = PrefabRoot;
            msg.Parameters[3] = TriggerGetPeerData();
            if (Controllers.Count > 0)
            {
                msg.Parameters[4] = Controllers[0].Endpoint;
            }
            connection.Send(msg);
        }
Example #10
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);
        }
Example #11
0
        /// <summary> Send overload that creates the NetMessage for the RPC. </summary>
        internal void Send(int viewId, bool reliable, string methodName, List <NetConnection> targets, params object[] parameters)
        {
            if (!Socket.Rpc.HasId(methodName))
            {
                NetLog.Error("Send failed: RPC method name has not been assigned an ID.");
                return;
            }

            var message = NetMessage.Create(Socket.Rpc.NameToId(methodName), (uint)viewId, parameters, reliable);

            for (int i = 0; i < targets.Count; i++)
            {
                targets[i].Send(message);
            }
        }
Example #12
0
        /// <summary> Send overload that creates the NetMessage for the RPC. </summary>
        internal void Send(int viewId, bool reliable, string methodName, RpcTarget target, params object[] parameters)
        {
            if (!Socket.Rpc.HasId(methodName))
            {
                NetLog.Error("Send failed: RPC method name has not been assigned an ID.");
                return;
            }
            if (!ViewLookup.ContainsKey(viewId))
            {
                return;
            }
            NetView view       = ViewLookup[viewId];
            var     netMessage = NetMessage.Create(Socket.Rpc.NameToId(methodName), (uint)viewId, parameters, reliable);

            Send(view, netMessage, target);
        }
Example #13
0
        internal void SendCreatorData(NetConnection connection)
        {
            if (!HasCreatorData())
            {
                return;
            }
            var msg = NetMessage.Create((ushort)ViewCmd.CreateCreatorView, (uint)Id, 5, true);

            msg.Parameters[0] = Id;
            msg.Parameters[1] = Group;
            msg.Parameters[2] = PrefabRoot;
            msg.Parameters[3] = TriggerGetCreatorData();
            if (Controllers.Count > 0)
            {
                msg.Parameters[4] = Controllers[0].Endpoint;
            }
            connection.Send(msg);
        }
Example #14
0
        /// <summary> Sends command to all connected clients to destroy the provided view. </summary>
        private void SendDestroyView(NetView view)
        {
            var destroyViewMessage = NetMessage.Create((ushort)ViewCmd.DestroyView, 0, 1, true);

            destroyViewMessage.Parameters[0] = view.Id;

            for (int i = 0; i < Socket.Connections.Count; i++)
            {
                var connection = Socket.Connections[i];
                if (connection.IsServer)
                {
                    continue;
                }
                if (view.Group == 0 || connection.InGroup(view.Group))
                {
                    connection.Send(destroyViewMessage);
                }
            }
        }
Example #15
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));
        }
Example #16
0
        /// <summary> Sends a command to all controllers of the View that we are the new Server. </summary>
        internal void SendChangeViewServer(int viewId)
        {
            if (!ViewLookup.ContainsKey(viewId))
            {
                return;
            }

            var view = ViewLookup[viewId];

            if (view.Controllers.Count == 0)
            {
                return;
            }

            var message = NetMessage.Create((ushort)ViewCmd.ChangeServer, 0, new object[] { viewId }, true);

            for (int i = 0; i < view.Controllers.Count; i++)
            {
                view.Controllers[i].Send(message);
            }
        }
Example #17
0
 /// <summary> Creates and sends a reliable command to a single connection. </summary>
 internal void Send(int id, NetConnection connection, params object[] parameters)
 {
     connection.Send(NetMessage.Create((ushort)id, 0, parameters, true));
 }