示例#1
0
 /// <summary>
 ///* Attribute to mark a method as a gRPC(Global Remote Procedure Call) service, the gRPC service must have a globally unique Id and cannot be declared in scripts that have multiple instances.<br/>
 ///* In gRPC, a client or server application can directly call a method on a server or client application on a different or local machine as if it were a local object, making it easier for you to create distributed applications and services.<br/>
 ///* gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters types.
 /// </summary>
 /// <param name="id">The globally unique Id of the gRPC service.</param>
 /// <param name="cacheMode">Defines how the service will be cached on the server side.</param>
 /// <param name="targetTo">These define which remote clients get your RPC call.</param>
 /// <param name="matchmakingTo">These define which matchmaking get your RPC call.</param>
 public gRPCAttribute(byte id, CacheMode cacheMode = CacheMode.None, TargetTo targetTo = TargetTo.All, MatchmakingTo matchmakingTo = MatchmakingTo.Auto)
 {
     Id            = id;
     CacheMode     = cacheMode;
     TargetTo      = targetTo;
     MatchmakingTo = matchmakingTo;
 }
示例#2
0
 protected void ChatHandler(NeutronPlayer player, ChatMode packet, MatchmakingTo matchmakingTo, int viewId, string message)
 {
     if (OnMessageReceived.Invoke(player, message))
     {
         using (NeutronStream stream = Neutron.PooledNetworkStreams.Pull())
         {
             NeutronStream.IWriter writer = stream.Writer;
             writer.WritePacket((byte)Packet.Chat);
             writer.Write(message);
             if (packet == ChatMode.Global)
             {
                 player.Write(writer, TargetTo.All, matchmakingTo, Protocol.Tcp);
             }
             else if (packet == ChatMode.Private)
             {
                 if (MatchmakingHelper.Server.GetPlayer(viewId, out NeutronPlayer playerFound))
                 {
                     playerFound.Write(player, writer, TargetTo.Me, MatchmakingTo.Me, Protocol.Tcp);
                 }
                 else
                 {
                     player.Error(Packet.Chat, "Player not found!", ErrorMessage.PLAYER_NOT_FOUND);
                 }
             }
         }
     }
 }
示例#3
0
#pragma warning disable IDE1006
        protected void iRPCHandler(NeutronPlayer owner, NeutronPlayer sender, short viewId, byte rpcId, byte instanceId, byte[] buffer, RegisterMode registerType, TargetTo targetTo, CacheMode cache, Protocol protocol)
#pragma warning restore IDE1006
        {
            void Run((int, int, RegisterMode) key)
            {
                bool Send()
                {
                    MatchmakingTo matchmakingTo = MatchmakingTo.Auto;

                    if (targetTo == TargetTo.Me)
                    {
                        matchmakingTo = MatchmakingTo.Me;
                    }
                    using (NeutronStream stream = Neutron.PooledNetworkStreams.Pull())
                    {
                        NeutronStream.IWriter writer = stream.Writer;
                        writer.WritePacket((byte)Packet.iRPC);
                        writer.WritePacket((byte)registerType);
                        writer.Write(viewId);
                        writer.Write(rpcId);
                        writer.Write(instanceId);
                        writer.WriteNext(buffer);
                        MatchmakingHelper.Internal.AddCache(rpcId, viewId, writer, owner, cache, CachedPacket.iRPC);
                        owner.Write(sender, writer, targetTo, matchmakingTo, protocol);
                    }
                    return(true);
                }

                if (MatchmakingHelper.Server.GetNetworkObject(key, owner, out NeutronView neutronView))
                {
                    if (neutronView.iRPCs.TryGetValue((rpcId, instanceId), out RPCInvoker remoteProceduralCall))
                    {
                        try
                        {
                            iRPCAttribute iRPCAttribute = remoteProceduralCall.iRPC;
                            if (ReflectionHelper.iRPC(buffer, remoteProceduralCall, owner))
                            {
                                Send();
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Stacktrace(ex);
                        }
                    }
                    else
                    {
                        Send();
                    }
                }
示例#4
0
            public static NeutronPlayer[] GetPlayersByMatchmakingTo(NeutronPlayer player, MatchmakingTo matchmakingTo)
            {
                switch (matchmakingTo)
                {
                case MatchmakingTo.Me:
                    return(null);

                case MatchmakingTo.Server:
                {
                    return(Neutron.Server.PlayersBySocket.Values.ToArray());
                }

                case MatchmakingTo.Channel:
                {
                    if (player.IsInChannel())
                    {
                        return(player.Channel.Players());
                    }
                    else
                    {
                        LogHelper.Error("Failed to direct packet, channel not found. Join a channel before sending the packet.");
                    }
                    return(default);
                }
示例#5
0
 public static void Write(this NeutronPlayer owner, NeutronPlayer sender, NeutronStream.IWriter writer, TargetTo targetTo, MatchmakingTo matchmakingTo, Protocol protocol)
 {
     MatchmakingHelper.Internal.Redirect(owner, sender, protocol, targetTo, writer.ToArray(), MatchmakingHelper.Internal.GetPlayersByMatchmakingTo(owner, matchmakingTo));
 }
示例#6
0
 protected virtual NeutronPlayer[] OnCustomMatchmakingTo(NeutronPlayer player, MatchmakingTo matchmakingTo)
 {
     switch (matchmakingTo)
     {
     default:
         LogHelper.Error("Broadcast Packet not implemented! " + matchmakingTo);
         return(null);
     }
 }