示例#1
0
        public override int GetDataSize(BytePacker packer)
        {
            int s;

            VarintBitConverter.ToUShort(packer, out s);
            return(s);
        }
示例#2
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;
            }
        }
示例#3
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);
        }
示例#4
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);
        }
示例#5
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);
        }
示例#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.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;
            }
        }
示例#7
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));
        }
示例#8
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);
        }
示例#9
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
                IDataChannel channel;

                if (dataChannelMap.TryGetValue(channelId, out channel))
                {
                    if (channel.CheckMode == CheckMode.Sequre)
                    {
                        ComNode node;
                        if (nodeUdpMap.TryGetValue(endPoint.Address.ToString(), out node))
                        {
                            object container = channel.FromStream(ref packer, null);

                            channel.Received(node, container);
                        }
                    }

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

                head += datasize + 4;
            }
        }
示例#10
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));
        }
示例#11
0
        public override void Serialize(BytePacker packer, object data)
        {
            int s;

            VarintBitConverter.SerializeInt((int)data, packer, out s);
        }
示例#12
0
        public override object Deserialize(BytePacker packer)
        {
            int s;

            return(VarintBitConverter.ToUShort(packer, out s));
        }
示例#13
0
        public override void Serialize(BytePacker packer, object data)
        {
            int s;

            VarintBitConverter.SerializeULong((ulong)data, packer, out s);
        }
示例#14
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

                IDataChannel channel;

                if (dataChannelMap.TryGetValue(channelId, out channel))
                {
                    if (channelId == (short)PreservedChannelId.Beacon)
                    {
                    }
                    else if (channelId == (short)PreservedChannelId.UdpNotify)
                    {
                        IssueIdData idData = (IssueIdData)channel.FromStream(ref packer);

                        try
                        {
                            userNodeMapLock.AcquireReaderLock(1000);

                            ComNode n;
                            if (userNodeMap.TryGetValue(idData.Id, out n))
                            {
                                //Util.Log("UdpUserName:" + userName);
                                ComSnowballNode node = (ComSnowballNode)n;

                                if (node.UdpEndPoint == null && node.AesDecrypter != null)
                                {
                                    byte[] decrypted = node.AesDecrypter.Decrypt(idData.encryptionData);

                                    if (decrypted.SequenceEqual(Global.UdpRawData))
                                    {
                                        node.UdpEndPoint = endPoint;

                                        if (nodeUdpMap.ContainsKey(endPoint))
                                        {
                                            nodeUdpMap.Remove(endPoint);
                                        }

                                        nodeUdpMap.Add(endPoint, node);
                                        SendInternal(node, (short)PreservedChannelId.UdpNotifyAck, endPoint.Port);

                                        byte[] key = GenerateTmpKey();
                                        node.TmpKey = key;
                                        SendInternal(node, (short)PreservedChannelId.Health, key);

                                        node.IsConnected = true;
                                        if (OnConnected != null)
                                        {
                                            OnConnected(node);
                                        }
                                    }
                                }
                            }
                        }
                        finally
                        {
                            userNodeMapLock.ReleaseReaderLock();
                        }
                    }
                    else
                    {
                        if (channel.CheckMode == CheckMode.Sequre || channel.Encryption == Encryption.Aes)
                        {
                            ComNode node;
                            if (nodeUdpMap.TryGetValue(endPoint, out node))
                            {
                                IDecrypter decrypter = null;
                                if (channel.Encryption == Encryption.Rsa)
                                {
                                    decrypter = rsaDecrypter;
                                }
                                else if (channel.Encryption == Encryption.Aes)
                                {
                                    decrypter = node.AesDecrypter;
                                }

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

                                channel.Received(node, container);
                            }
                        }
                        else
                        {
                            IDecrypter decrypter = null;
                            if (channel.Encryption == Encryption.Rsa)
                            {
                                decrypter = rsaDecrypter;
                            }

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

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

                head += datasize + 4;
            }
        }
示例#15
0
        public async Task Start()
        {
            cancelToken.Token.Register(() => client.Close());
            int   resSize   = 0;
            short channelId = 0;

            bool isRent = false;

            byte[] buffer = null;

            do
            {
                try
                {
                    resSize = await nStream.ReadAsync(receiveBuffer, 0, 2, cancelToken.Token).ConfigureAwait(false);

                    if (resSize != 0)
                    {
                        receivePacker.Position = 0;
                        resSize = receivePacker.ReadShort();
#if DISABLE_CHANNEL_VARINT
                        await nStream.ReadAsync(receiveBuffer, 0, 2, cancelToken.Token).ConfigureAwait(false);

                        receivePacker.Position = 0;
                        channelId = receivePacker.ReadShort();
                        await nStream.ReadAsync(receiveBuffer, 0, resSize, cancelToken.Token).ConfigureAwait(false);
#else
                        int s = 0;
                        channelId = VarintBitConverter.ToInt16(nStream, out s);
                        await nStream.ReadAsync(receiveBuffer, 0, resSize, cancelToken.Token).ConfigureAwait(false);
#endif


                        buffer = arrayPool.Rent(resSize);
                        if (buffer != null)
                        {
                            isRent = true;
                        }
                        else
                        {
                            buffer = new byte[resSize];
                            isRent = false;
                        }

                        Array.Copy(receiveBuffer, buffer, resSize);

                        //Util.Log("TCP:" + resSize);
                    }
                }
                catch//(Exception e)
                {
                    //Util.Log("TCP:" + e.Message);
                    break;
                }

                if (resSize == 0)
                {
                    break;
                }

                if (cancelToken.IsCancellationRequested)
                {
                    break;
                }

                if (Global.SyncContext != null)
                {
                    Global.SyncContext.Post((state) => {
                        if (cancelToken.IsCancellationRequested)
                        {
                            return;
                        }
                        CallbackParam param = (CallbackParam)state;
                        if (OnReceive != null)
                        {
                            OnReceive(param.Ip, param.channelId, param.buffer, param.size);
                        }
                        if (isRent)
                        {
                            arrayPool.Return(buffer);
                        }
                    }, new CallbackParam(IP, channelId, buffer, resSize, isRent));
                }
                else
                {
                    if (OnReceive != null)
                    {
                        OnReceive(IP, channelId, receiveBuffer, resSize);
                    }
                }
            } while (client.Connected);

            Disconnect();
        }
示例#16
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;
            }
        }
示例#17
0
        async Task <bool> OnPoll(
            TCPConnection connection,
            NetworkStream nStream,
            byte[] receiveBuffer,
            BytePacker receivePacker,
            CancellationTokenSource cancelToken
            )
        {
            int   resSize   = 0;
            int   tmpSize   = 0;
            short channelId = 0;

            bool isRent = false;

            byte[] buffer = null;

            try
            {
                resSize = await ReadAsync(nStream, receiveBuffer, 2, cancelToken).ConfigureAwait(false);

                if (resSize != 0)
                {
                    receivePacker.Position = 0;
                    resSize = receivePacker.ReadShort();
#if DISABLE_CHANNEL_VARINT
                    tmpSize = await ReadAsync(nStream, receiveBuffer, 2, cancelToken).ConfigureAwait(false);

                    if (tmpSize == 0)
                    {
                        throw new EndOfStreamException();
                    }

                    receivePacker.Position = 0;
                    channelId = receivePacker.ReadShort();
#else
                    int s = 0;
                    channelId = VarintBitConverter.ToShort(nStream, out s);
#endif
                    tmpSize = await ReadAsync(nStream, receiveBuffer, resSize, cancelToken).ConfigureAwait(false);

                    if (tmpSize == 0)
                    {
                        throw new EndOfStreamException();
                    }

                    buffer = arrayPool.Rent(resSize);
                    if (buffer != null)
                    {
                        isRent = true;
                    }
                    else
                    {
                        buffer = new byte[resSize];
                        isRent = false;
                    }

                    Array.Copy(receiveBuffer, buffer, resSize);

                    //Util.Log("TCP:" + resSize);
                }
            }
            catch//(Exception e)
            {
                //Util.Log("TCP:" + e.Message);
                return(false);
            }

            if (resSize == 0)
            {
                return(false);
            }

            if (cancelToken.IsCancellationRequested)
            {
                return(false);
            }

            if (syncContext != null)
            {
                syncContext.Post((state) =>
                {
                    if (cancelToken.IsCancellationRequested)
                    {
                        return;
                    }
                    CallbackParam param = (CallbackParam)state;
                    OnReliableReceived(param.endPoint, param.channelId, param.buffer, param.size);
                    if (isRent)
                    {
                        arrayPool.Return(buffer);
                    }
                }, new CallbackParam((IPEndPoint)connection.Client.Client.RemoteEndPoint, channelId, buffer, resSize, isRent));
            }
            else
            {
                OnReliableReceived((IPEndPoint)connection.Client.Client.RemoteEndPoint, channelId, buffer, resSize);
            }

            return(true);
        }