Ejemplo n.º 1
0
        public override int GetDataSize(object data)
        {
            int s;

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

            VarintBitConverter.SerializeShort((short)data, packer, out s);
            return(s);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
0
        int CreateBeaconData(out BytePacker packer)
        {
            string data     = BeaconDataCreate();
            int    dataSize = beaconConverter.GetDataSize(data);

            byte[] beaconBuf = new byte[dataSize + 4];
            packer = new BytePacker(beaconBuf);

            packer.Write((short)dataSize);

#if DISABLE_CHANNEL_VARINT
            packer.Write((short)PreservedChannelId.Beacon);
#else
            int s = 0;
            VarintBitConverter.SerializeShort((short)PreservedChannelId.Beacon, packer, out s);
#endif
            beaconConverter.Serialize(packer, data);

            return(packer.Position);
        }
Ejemplo n.º 5
0
        public void BuildBuffer <T>(IDataChannel channel, T data, ref byte[] buffer, ref int bufferSize, ref bool isRent)
        {
            isRent = true;
            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(channelId);
#else
            int s = 0;
            VarintBitConverter.SerializeShort(channel.ChannelID, packer, out s);
#endif
            int start = packer.Position;

            channel.ToStream(data, ref packer);

            bufferSize = (int)packer.Position;

            packer.Position = 0;
            packer.Write((short)(bufferSize - start));
        }
Ejemplo n.º 6
0
        public override void Serialize(BytePacker packer, object data)
        {
            int s;

            VarintBitConverter.SerializeShort((short)data, packer, out s);
        }