예제 #1
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);
        }
예제 #2
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);
            }
        }
예제 #3
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());
            }
        }
예제 #4
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);
            }
        }
예제 #5
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));
                }
            }
        }
        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);
            }
        }
예제 #7
0
파일: ComServer.cs 프로젝트: nakky/Snowball
        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));
        }
예제 #8
0
        public override void Serialize(BytePacker packer, object data)
        {
            string strData = (string)data;

            if (strData == null)
            {
                packer.Write(-1);
            }
            else
            {
                byte[] strbuf = System.Text.Encoding.UTF8.GetBytes(strData);
                packer.Write(strbuf.Length);
                if (strbuf.Length > 0)
                {
                    packer.Write(strbuf, 0, strbuf.Length);
                }
            }
        }
예제 #9
0
        public override void Serialize(BytePacker packer, object data)
        {
            if (data == null)
            {
                packer.Write(-1);
            }
            else
            {
                System.Collections.IList list = (System.Collections.IList)data;

                packer.Write(list.Count);

                for (int i = 0; i < list.Count; i++)
                {
                    converter.Serialize(packer, list[i]);
                }
            }
        }
예제 #10
0
        public override void Serialize(BytePacker packer, object data)
        {
            if (data == null)
            {
                packer.Write(-1);
            }
            else
            {
                Array array = (Array)data;

                packer.Write(array.Length);

                for (int i = 0; i < array.Length; i++)
                {
                    converter.Serialize(packer, array.GetValue(i));
                }
            }
        }
예제 #11
0
        public override void Serialize(BytePacker packer, object data)
        {
            if (data == null)
            {
                packer.Write(-1);
            }
            else
            {
                System.Collections.IDictionary dictionary = (System.Collections.IDictionary)data;

                packer.Write(dictionary.Count);

                foreach (System.Collections.DictionaryEntry kvp in dictionary)
                {
                    keyConverter.Serialize(packer, kvp.Key);
                    valueConverter.Serialize(packer, kvp.Value);
                }
            }
        }
예제 #12
0
파일: ComServer.cs 프로젝트: nakky/Snowball
        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);
        }
예제 #13
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);
     }
 }
예제 #14
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));
        }
예제 #15
0
        public static void GetVarintBytes(ulong value, BytePacker packer, out int size)
        {
            size = 0;
            ulong byteVal;

            byte[] byteval = new byte[1];
            do
            {
                byteVal = value & 0x7f;
                value >>= 7;

                if (value != 0)
                {
                    byteVal |= 0x80;
                }

                byteval[0] = (byte)byteVal;
                packer.Write((byte[])byteval, 0, 1);
                size++;
            } while (value != 0);
        }
        public override void Serialize(BytePacker packer, object data)
        {
            if (data == null)
            {
                packer.Write((byte)0);
            }
            else
            {
                packer.Write((byte)1);

                Quaternion quaternion = (Quaternion)data;

                packer.Write(quaternion.x);
                packer.Write(quaternion.y);
                packer.Write(quaternion.z);
                packer.Write(quaternion.w);
            }
        }
예제 #17
0
        public override void Serialize(BytePacker packer, object data)
        {
            if (data == null)
            {
                packer.Write((byte)0);
            }
            else
            {
                packer.Write((byte)1);

                Color32 color = (Color32)data;

                packer.Write(color.r);
                packer.Write(color.g);
                packer.Write(color.b);
                packer.Write(color.a);
            }
        }
예제 #18
0
 public override void Serialize(BytePacker packer, object data)
 {
     packer.Write((byte)data);
 }