예제 #1
0
        /// <summary>
        /// Registers all <see cref="Packet"/> inheritors in the given <see cref="Assembly"/> with this <see cref="Connection"/>. Should this method be called manually, it must be called on both the server and client so that all <see cref="Packet"/> s in use are known to all parties (avoids incompatible states; exception thrown otherwise).
        /// <para> 使用此 <see cref="Connection"/> 注册给定 <see cref="Assembly"/> 中的所有 <see cref="Packet"/> 继承者。如果应该手动调用此方法,则必须在服务器和客户端上都调用此方法,以便各方都知道正在使用的所有 <see cref="Packet"/>(避免出现不兼容的状态;否则抛出异常)。 </para>
        /// </summary>
        /// <param name="assembly"> The <see cref="Assembly"/> to search in for inheritors of <see cref="Packet"/>. </param>

        /// <remarks>
        /// All packets in the network lib are included by default. A manual call is not essential, even if the used packets are not included, as the library will attempt to synchronise known <see cref="Packet"/> s between the server and client automatically.
        /// <para> 默认情况下包括网络库中的所有数据包。即使不包括使用的数据包,手动调用也不是必需的,因为库将尝试在服务器和客户端之间同步已知的 <see cref="Packet"/> </para>
        /// </remarks>

        internal void AddExternalPackets(Assembly assembly)
        {
            assembly.GetTypes().ToList().Where(c => c.IsSubclassOf(typeof(Packet))).ToList().ForEach(p =>
            {
                if (typeByte.ContainsKey(p))
                {
                    return;                          //Already in the dictionary.
                }
                ushort packetId = (ushort)Interlocked.Increment(ref currentTypeByteIndex);
                Attribute packetTypeAttribute = p.GetCustomAttribute(typeof(PacketTypeAttribute));
                //Apply the local ID if there exist any.
                if (packetTypeAttribute != null)
                {
                    packetId = ((PacketTypeAttribute)packetTypeAttribute).Id;
                }
                typeByte[p] = packetId;
            });

            //检索应用于指定成员的指定类型的自定义属性的集合。
            assembly.GetTypes().ToList().Where(c => c.GetCustomAttributes(typeof(PacketRequestAttribute)).Count() > 0).ToList().
            ForEach(c =>
            {
                PacketRequestAttribute requestAttribute = ((PacketRequestAttribute)c.GetCustomAttribute(typeof(PacketRequestAttribute)));
                // TryAdd will fail if another thread investigates the object. However, it turned out, that somehow the RequestType has been already added to the requestResponseMap. Hence, we can ignore the failure.
                if (!requestResponseMap.ContainsKey(requestAttribute.RequestType))
                {
                    requestResponseMap.Add(requestAttribute.RequestType, c);
                }
            });
        }
예제 #2
0
        /// <summary>
        /// External packets which also should be known by the network lib can be added with this function.
        /// All packets in the network lib are included automatically. A call is not essential, even if the used packets
        /// are not included in the network library. Manuell calls have to be invoked on the client and server side to avaid incompatible states.
        /// </summary>
        /// <param name="assembly">The assembly to search for included packets.</param>
        internal void AddExternalPackets(Assembly assembly)
        {
            assembly.GetTypes().ToList().Where(c => c.IsSubclassOf(typeof(Packet))).ToList().ForEach(p =>
            {
                if (typeByte.ContainsKeyA(p))
                {
                    return;                           //Already in the dictionary.
                }
                ushort packetId = (ushort)Interlocked.Increment(ref currentTypeByteIndex);
                Attribute packetTypeAttribute = p.GetCustomAttribute(typeof(PacketTypeAttribute));
                //Apply the local ID if there exist any.
                if (packetTypeAttribute != null)
                {
                    packetId = ((PacketTypeAttribute)packetTypeAttribute).Id;
                }
                typeByte.Add(p, packetId);
            });

            assembly.GetTypes().ToList().Where(c => c.GetCustomAttributes(typeof(PacketRequestAttribute)).Count() > 0).ToList().
            ForEach(c =>
            {
                PacketRequestAttribute requestAttribute = ((PacketRequestAttribute)c.GetCustomAttribute(typeof(PacketRequestAttribute)));
                if (!requestResponseMap.ContainsKey(requestAttribute.RequestType))
                {
                    requestResponseMap.Add(requestAttribute.RequestType, c);
                }
            });
        }