示例#1
0
        void OnTCPReceived(string endPointIp, short channelId, byte[] data, int size)
        {
            if (channelId == (short)PreservedChannelId.Beacon)
            {
            }
            else if (channelId == (short)PreservedChannelId.Health)
            {
                if (nodeMap.ContainsKey(endPointIp))
                {
                    ComNode node = nodeMap[endPointIp];
                    node.HealthLostCount = 0;
                }
            }
            else if (!dataChannelMap.ContainsKey(channelId))
            {
            }
            else
            {
                BytePacker packer = new BytePacker(data);

                if (nodeMap.ContainsKey(endPointIp))
                {
                    ComNode node = nodeMap[endPointIp];

                    node.HealthLostCount = 0;

                    IDataChannel channel = dataChannelMap[channelId];

                    object container = channel.FromStream(ref packer);

                    channel.Received(node, container);
                }
            }
        }
示例#2
0
        public override object Deserialize(BytePacker packer)
        {
            object data;

            int length = packer.ReadInt();

            if (length < 0)
            {
                return(null);
            }
            else if (length > 0)
            {
                byte[] strData;
                packer.ReadByteArray(out strData, 0, length);

                string text = System.Text.Encoding.UTF8.GetString(strData);

                data = (string)text;
            }
            else
            {
                data = "";
            }
            return(data);
        }
示例#3
0
        void OnTCPReceived(string endPointIp, short channelId, byte[] data, int size)
        {
            if (channelId == (short)PreservedChannelId.Beacon)
            {
            }
            else if (!dataChannelMap.ContainsKey(channelId))
            {
            }
            else
            {
                BytePacker packer = new BytePacker(data);

                if (serverNode == null)
                {
                    ;
                }
                if (endPointIp == serverNode.IP)
                {
                    healthLostCount = 0;

                    IDataChannel channel   = dataChannelMap[channelId];
                    object       container = channel.FromStream(ref packer);

                    channel.Received(serverNode, container);
                }
            }
        }
示例#4
0
        public object FromStream(ref BytePacker packer, IDecrypter decrypter)
        {
            BytePacker p = packer;

            if (Compression == Compression.LZ4)
            {
                byte[] encoded = (byte[])byteArrayConverter.Deserialize(p);
                byte[] data    = LZ4Pickler.Unpickle(encoded);

                if (decrypter != null)
                {
                    data = decrypter.Decrypt(data);
                }

                p = new BytePacker(data);
            }
            else if (decrypter != null)
            {
                try
                {
                    byte[] data = (byte[])encArrayConverter.Deserialize(p);
                    data = decrypter.Decrypt(data);
                    p    = new BytePacker(data);
                }
                catch (Exception e)
                {
                    Util.Log("FromStream:" + e.Message);
                }
            }

            return(converter.Deserialize(p));
        }
示例#5
0
        public override int GetDataSize(BytePacker packer)
        {
            int s;

            VarintBitConverter.ToUShort(packer, out s);
            return(s);
        }
示例#6
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)
                {
                }
                else if (channelId == (short)PreservedChannelId.Health)
                {
                    if (nodeMap.ContainsKey(endPointIp))
                    {
                        ComNode node = nodeMap[endPointIp];
                        node.HealthLostCount = 0;
                    }
                }
                else if (!dataChannelMap.ContainsKey(channelId))
                {
                }
                else
                {
                    IDataChannel channel = dataChannelMap[channelId];

                    if (channel.CheckMode == CheckMode.Sequre)
                    {
                        if (nodeMap.ContainsKey(endPointIp))
                        {
                            ComNode node = nodeMap[endPointIp];

                            node.HealthLostCount = 0;

                            object container = channel.FromStream(ref packer);

                            channel.Received(node, container);
                        }
                    }
                    else
                    {
                        object container = channel.FromStream(ref packer);

                        channel.Received(null, container);
                    }
                }

                head += datasize + 4;
            }
        }
示例#7
0
        public override int GetDataSize(object data)
        {
            int s;

            byte[]     array  = new byte[16];
            BytePacker packer = new BytePacker(array);

            VarintBitConverter.SerializeLong((long)data, packer, out s);
            return(s);
        }
示例#8
0
        public override int GetDataSize(object data)
        {
            int s;

            byte[]     array  = new byte[4];
            BytePacker packer = new BytePacker(array);

            VarintBitConverter.SerializeUShort((ushort)data, packer, out s);
            return(s);
        }
示例#9
0
        public async Task <bool> Send <T>(ComNode node, short channelId, T data)
        {
            if (!nodeMap.ContainsKey(node.IP))
            {
                return(false);
            }

            if (!dataChannelMap.ContainsKey(channelId))
            {
                return(false);
            }

            IDataChannel channel = dataChannelMap[channelId];

            bool isRent  = true;
            int  bufSize = channel.GetDataSize(data);

            byte[] buf = arrayPool.Rent(bufSize + 6);
            if (buf == null)
            {
                isRent = false;
                buf    = new byte[bufSize + 6];
            }

            BytePacker packer = new BytePacker(buf);

            packer.Write((short)bufSize);

#if DISABLE_CHANNEL_VARINT
            packer.Write(channelId);
#else
            int s = 0;
            VarintBitConverter.SerializeShort(channelId, packer, out s);
#endif
            channel.ToStream(data, ref packer);

            int maxpos = (int)packer.Position;

            if (channel.Qos == QosType.Reliable)
            {
                await node.Connection.Send(maxpos, buf);
            }
            else if (channel.Qos == QosType.Unreliable)
            {
                await udpSender.Send(node.IP, maxpos, buf);
            }

            if (isRent)
            {
                arrayPool.Return(buf);
            }

            return(true);
        }
示例#10
0
        public TCPConnection(TcpClient client, int receiveBufferSize = DefaultBufferSize)
        {
            receiveBuffer = new byte[receiveBufferSize];
            receivePacker = new BytePacker(receiveBuffer);


            UpdateClient(client);
            nStream = client.GetStream();
            nStream.WriteTimeout = sendTimeOut;
            nStream.ReadTimeout  = receiveTimeOut;
        }
示例#11
0
 public int GetDataSize(object data)
 {
     if (Compression == Compression.LZ4)
     {
         return(lz4converter.GetDataSize(data));
     }
     else
     {
         BytePacker packer = new BytePacker((byte[])data);
         return(converter.GetDataSize(packer));
     }
 }
示例#12
0
 public void ToStream(object data, ref BytePacker packer)
 {
     if (Compression == Compression.LZ4)
     {
         lz4converter.Serialize(packer, data);
     }
     else
     {
         byte[] arr = (byte[])data;
         packer.Write(arr, 0, arr.Length);
     }
 }
        public override int GetDataSize(BytePacker packer)
        {
            byte isNull = packer.ReadByte();

            if (isNull == 0)
            {
                return(sizeof(byte));
            }

            packer.Position += sizeof(float) * 2;
            return(sizeof(byte) + sizeof(float) * 2);
        }
示例#14
0
        public override int GetDataSize(BytePacker packer)
        {
            int length = packer.ReadInt();

            if (length < 0)
            {
                return(sizeof(int));
            }

            packer.Position += length;
            return(sizeof(int) + length);
        }
示例#15
0
        public override object Deserialize(BytePacker packer)
        {
            byte isNull = packer.ReadByte();

            if (isNull == 0)
            {
                return(null);
            }
            else
            {
                return(TimeSpan.FromTicks(packer.ReadLong()));
            }
        }
示例#16
0
        public override void Serialize(BytePacker packer, object data)
        {
            if (data == null)
            {
                packer.Write((byte)0);
            }
            else
            {
                packer.Write((byte)1);

                packer.Write(((TimeSpan)data).Ticks);
            }
        }
示例#17
0
        public override object Deserialize(BytePacker packer)
        {
            byte isNull = packer.ReadByte();

            if (isNull == 0)
            {
                return(null);
            }
            else
            {
                return(DateTime.FromBinary(packer.ReadLong()));
            }
        }
示例#18
0
 public object FromStream(ref BytePacker packer)
 {
     if (Compression == Compression.LZ4)
     {
         byte[]     encoded   = (byte[])lz4converter.Deserialize(packer);
         BytePacker lz4packer = new BytePacker(encoded);
         return(converter.Deserialize(lz4packer));
     }
     else
     {
         return(converter.Deserialize(packer));
     }
 }
示例#19
0
        public static void Serialize <T>(BytePacker packer, T data)
        {
            if (!setup)
            {
                Setup();
            }

            if (!converterMap.ContainsKey(typeof(T)))
            {
                converterMap.Add(typeof(T), GetConverter(typeof(T)));
            }
            converterMap[typeof(T)].Serialize(packer, data);
        }
示例#20
0
        public static T Deserialize <T>(BytePacker packer)
        {
            if (!setup)
            {
                Setup();
            }

            if (!converterMap.ContainsKey(typeof(T)))
            {
                converterMap.Add(typeof(T), GetConverter(typeof(T)));
            }
            return((T)converterMap[typeof(T)].Deserialize(packer));
        }
示例#21
0
        public override void Serialize(BytePacker packer, object data)
        {
            if (data == null)
            {
                packer.Write((byte)0);
            }
            else
            {
                packer.Write((byte)1);

                packer.Write(((DateTime)data).ToBinary());
            }
        }
示例#22
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.ToInt16(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 (!dataChannelMap.ContainsKey(channelId))
                {
                }
                else
                {
                    if (serverNode == null)
                    {
                        break;
                    }
                    if (endPointIp == serverNode.IP)
                    {
                        healthLostCount = 0;

                        IDataChannel channel   = dataChannelMap[channelId];
                        object       container = channel.FromStream(ref packer);

                        channel.Received(serverNode, container);
                    }
                }

                head += datasize + 4;
            }
        }
示例#23
0
        public override void Serialize(BytePacker packer, object data)
        {
            if (data == null)
            {
                packer.Write(-1);
            }
            else
            {
                byte[] array = (byte[])data;

                packer.Write(array.Length);

                packer.Write(array, 0, array.Length);
            }
        }
        public override void Serialize(BytePacker packer, object data)
        {
            if (data == null)
            {
                packer.Write((byte)0);
            }
            else
            {
                packer.Write((byte)1);

                Vector2 vector = (Vector2)data;

                packer.Write(vector.x);
                packer.Write(vector.y);
            }
        }
示例#25
0
 public void ToStream(object data, ref BytePacker packer)
 {
     if (Compression == Compression.LZ4)
     {
         int        size      = lz4converter.GetDataSize(data);
         byte[]     buf       = new byte[size];
         BytePacker lz4packer = new BytePacker(buf);
         lz4converter.Serialize(lz4packer, data);
         byte[] encoded = LZ4Pickler.Pickle(buf);
         converter.Serialize(packer, encoded);
     }
     else
     {
         converter.Serialize(packer, data);
     }
 }
示例#26
0
        public static void Serialize <T>(BytePacker packer, T data)
        {
            if (!setup)
            {
                Setup();
            }

            Converter converter;

            if (!converterMap.TryGetValue(typeof(T), out converter))
            {
                converter = GetConverter(typeof(T));
                converterMap.Add(typeof(T), converter);
            }
            converter.Serialize(packer, data);
        }
示例#27
0
        public static T Deserialize <T>(BytePacker packer)
        {
            if (!setup)
            {
                Setup();
            }

            Converter converter;

            if (!converterMap.TryGetValue(typeof(T), out converter))
            {
                converter = GetConverter(typeof(T));
                converterMap.Add(typeof(T), converter);
            }
            return((T)converter.Deserialize(packer));
        }
示例#28
0
        public override void Serialize(BytePacker packer, object data)
        {
            if (data == null)
            {
                packer.Write((byte)0);
            }
            else
            {
                packer.Write((byte)1);

                for (int i = 0; i < parameters.Count; i++)
                {
                    converters[i].Serialize(packer, parameters[i].GetValue(data));
                }
            }
        }
示例#29
0
        public override object Deserialize(BytePacker packer)
        {
            int length = packer.ReadInt();

            if (length < 0)
            {
                return(null);
            }
            else
            {
                byte[] array;

                packer.ReadByteArray(out array, 0, length);

                return(array);
            }
        }
示例#30
0
        void BuildBuffer <T>(
            IDataChannel channel, T data, ref byte[] buffer, ref int bufferSize, ref bool isRent, IEncrypter encrypter
            )
        {
            isRent = true;

            if (channel.Encryption == Encryption.Rsa)
            {
                throw new InvalidOperationException("Server cant send data via RSA channel.");
            }

            int bufSize = channel.GetDataSize(data);
            int lz4ext  = 0;

            if (channel.Compression == Compression.LZ4)
            {
                lz4ext = 4;
            }

            buffer = arrayPool.Rent(bufSize + 6 + lz4ext);
            if (buffer == null)
            {
                isRent = false;
                buffer = new byte[bufSize + 6 + lz4ext];
            }

            BytePacker packer = new BytePacker(buffer);

            packer.Write((short)bufSize);

#if DISABLE_CHANNEL_VARINT
            packer.Write(channel.ChannelID);
#else
            int s = 0;
            VarintBitConverter.SerializeShort(channel.ChannelID, packer, out s);
#endif
            int start = packer.Position;

            channel.ToStream(data, ref packer, encrypter);

            bufferSize = (int)packer.Position;

            packer.Position = 0;
            packer.Write((short)(bufferSize - start));
        }