示例#1
0
        /// <summary>
        /// Shorthand to send a request for a default instance of this protocol's data from a client.
        /// Requires `SetClientDefaults()` to be implemented.
        /// </summary>
        /// <param name="toWho">Main.player index of player (client) being requested for this data. -1 for all clients.</param>
        /// <param name="ignoreWho">Main.player index of player (client) being ignored. -1 for no client.</param>
        /// <param name="retries"></param>
        protected static void QuickRequestToClient <T>(int toWho, int ignoreWho, int retries) where T : PacketProtocol
        {
            if (Main.netMode != 2)
            {
                throw new ModHelpersException("Not server.");
            }

            T t = (T)StreamProtocol.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            t.SendRequestToClient(toWho, ignoreWho, retries);
        }
示例#2
0
        /// <summary>
        /// Shorthand to send a request for a default instance of this protocol's data from the server.
        /// Requires `SetServerDefaults()` to be implemented.
        /// </summary>
        protected static void QuickRequestToServer <T>(int retries) where T : PacketProtocol             //, new()
        {
            if (Main.netMode != 1)
            {
                throw new ModHelpersException("Not a client.");
            }

            T t = (T)StreamProtocol.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            t.SendRequestToServer(retries);
        }
示例#3
0
        private static void QuickSendToServerBase <T>(bool syncToClients)
            where T : PacketProtocol                        //, new()
        {
            if (Main.netMode != 1)
            {
                throw new ModHelpersException("Can only send as client.");
            }

            T t = (T)StreamProtocol.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );
            t.SetClientDefaults();
            t.OnClone();

            t.SendToServer(syncToClients);
        }
示例#4
0
        /// <summary>
        /// Shorthand to send a default instance of this protocol's data to a client. Requires `SetServerDefaults()`
        /// to be implemented.
        /// </summary>
        /// <param name="toWho">Main.player index of player (client) receiving this data. -1 for all clients.</param>
        /// <param name="ignoreWho">Main.player index of player (client) being ignored. -1 for no client.</param>
        protected static void QuickSendToClient <T>(int toWho, int ignoreWho)
            where T : PacketProtocol                        //, new()
        {
            if (Main.netMode != 2)
            {
                throw new ModHelpersException("Can only send as client.");
            }

            T t = (T)StreamProtocol.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            t.SetServerDefaults(toWho);
            t.OnClone();

            t.SendToClient(toWho, ignoreWho);
        }
示例#5
0
        internal static void HandlePacketOnServer(int protocolCode, BinaryReader reader, int playerWho)
        {
            var  mymod = ModHelpersMod.Instance;
            bool isRequest, isSyncedToClients;

            Type protocolType = mymod.PacketProtocolMngr.GetProtocolType(protocolCode);

            if (protocolType == null)
            {
                throw new ModHelpersException("Invalid protocol (hash: " + protocolCode + ")");
            }

            try {
                isRequest         = reader.ReadBoolean();
                isSyncedToClients = reader.ReadBoolean();
            } catch (Exception e) {
                throw new ModHelpersException("Could not read data for protocol " + protocolType.Namespace + "." + protocolType.Name + " - " + e.ToString());
            }

            try {
                var protocol = (PacketProtocol)StreamProtocol.CreateInstance(protocolType);
                //var protocol = (PacketProtocol)Activator.CreateInstance( protocolType, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

                if (isRequest)
                {
                    protocol.ReceiveRequestWithServerBase(playerWho);
                    protocol.OnClone();
                }
                else
                {
                    if (protocol.IsAsync)
                    {
                        ThreadPool.QueueUserWorkItem(_ => {
                            protocol.HandleServer_Core(reader, playerWho, isSyncedToClients);
                        });
                    }
                    else
                    {
                        protocol.HandleServer_Core(reader, playerWho, isSyncedToClients);
                    }
                }
            } catch (Exception e) {
                throw new ModHelpersException("Error handling " + protocolType.Namespace + "." + protocolType.Name + " - " + e.ToString());
            }
        }