Example #1
0
        public ClassConverter(Type type)
        {
            this.type = type;

            object[] attributes = type.GetCustomAttributes(typeof(TransferableAttribute), true);

            foreach (TransferableAttribute attr in attributes)
            {
                PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

                var props = properties.Where(x => Attribute.GetCustomAttributes(x, typeof(DataAttribute)).Length > 0)
                            .OrderBy(x => ((DataAttribute)Attribute.GetCustomAttributes(x, typeof(DataAttribute))[0]).Index);

                foreach (PropertyInfo p in props)
                {
                    //Util.Log("name:" + f.Name);

                    var dattrs = Attribute.GetCustomAttributes(p, typeof(DataAttribute));

                    foreach (DataAttribute datt in dattrs)
                    {
                        parameters.Add(p);
                        if (datt.ConverterType == null)
                        {
                            converters.Add(DataSerializer.GetConverter(p.PropertyType));
                        }
                        else
                        {
                            converters.Add((Converter)Activator.CreateInstance(datt.ConverterType));
                        }
                        break;
                    }
                }

                return;
            }

            throw new InvalidDataException("The class " + type.Name + " is not supported.");
        }
Example #2
0
 public ArrayConverter(Type type)
 {
     this.type = type;
     converter = DataSerializer.GetConverter(type.GetElementType());
 }
Example #3
0
        void OnUnreliableReceived(IPEndPoint endPoint, byte[] data, int size)
        {
            int head = 0;

            while (head < size)
            {
                BytePacker packer   = new BytePacker(data);
                short      datasize = packer.ReadShort();
#if DISABLE_CHANNEL_VARINT
                short channelId = packer.ReadShort();
#else
                int   s         = 0;
                short channelId = VarintBitConverter.ToShort(packer, out s);
#endif
                if (channelId == (short)PreservedChannelId.Beacon)
                {
                    if (acceptBeacon && !isConnecting && !IsTcpConnected)
                    {
                        string beaconData = (string)DataSerializer.Deserialize <string>(packer);

                        if (BeaconAccept(beaconData))
                        {
                            Connect(endPoint.Address.ToString());
                        }
                    }
                }
                else
                {
                    IDataChannel channel;

                    if (dataChannelMap.TryGetValue(channelId, out channel))
                    {
                        if (channelId == (short)PreservedChannelId.Health)
                        {
                            if (serverNode == null)
                            {
                                break;
                            }

                            if (serverNode.UdpEndPoint == null && serverNode.TcpEndPoint.Address.Equals(endPoint.Address))
                            {
                                serverNode.UdpEndPoint = endPoint;
                            }
                        }

                        if (channel.CheckMode == CheckMode.Sequre)
                        {
                            IDecrypter decrypter = null;
                            if (channel.Encryption == Encryption.Rsa)
                            {
                                throw new InvalidOperationException("Client cant receive data via RSA channel.");
                            }
                            else if (channel.Encryption == Encryption.Aes)
                            {
                                decrypter = serverNode.AesDecrypter;
                            }

                            if (serverNode == null)
                            {
                                break;
                            }
                            if (endPoint.Address.Equals(serverNode.UdpEndPoint.Address))
                            {
                                object container = channel.FromStream(ref packer, decrypter);

                                channel.Received(serverNode, container);
                            }
                        }
                        else
                        {
                            IDecrypter decrypter = null;
                            if (channel.Encryption == Encryption.Rsa)
                            {
                                throw new InvalidOperationException("Client cant receive data via RSA channel.");
                            }
                            else if (channel.Encryption == Encryption.Aes)
                            {
                                decrypter = serverNode.AesDecrypter;
                            }

                            object container = channel.FromStream(ref packer, decrypter);

                            channel.Received(serverNode, container);
                        }
                    }
                }

                head += datasize + 4;
            }
        }
Example #4
0
        void OnUDPReceived(string endPointIp, byte[] data, int size)
        {
            int head = 0;

            while (head < size)
            {
                BytePacker packer   = new BytePacker(data);
                short      datasize = packer.ReadShort();
#if DISABLE_CHANNEL_VARINT
                short channelId = packer.ReadShort();
#else
                int   s         = 0;
                short channelId = VarintBitConverter.ToShort(packer, out s);
#endif

                if (channelId == (short)PreservedChannelId.Beacon)
                {
                    if (acceptBeacon && !isConnecting && !IsConnected)
                    {
                        string beaconData = (string)DataSerializer.Deserialize <string>(packer);

                        if (BeaconAccept(beaconData))
                        {
                            Connect(endPointIp);
                        }
                    }
                }
                else if (channelId == (short)PreservedChannelId.Health)
                {
                    if (serverNode == null)
                    {
                        break;
                    }
                    if (endPointIp == serverNode.IP)
                    {
                        healthLostCount = 0;
                    }
                }
                else if (!dataChannelMap.ContainsKey(channelId))
                {
                }
                else
                {
                    IDataChannel channel = dataChannelMap[channelId];

                    if (channel.CheckMode == CheckMode.Sequre)
                    {
                        if (serverNode == null)
                        {
                            break;
                        }
                        if (endPointIp == serverNode.IP)
                        {
                            healthLostCount = 0;

                            object container = channel.FromStream(ref packer);

                            channel.Received(serverNode, container);
                        }
                    }
                    else
                    {
                        healthLostCount = 0;

                        object container = channel.FromStream(ref packer);

                        channel.Received(serverNode, container);
                    }
                }

                head += datasize + 4;
            }
        }