コード例 #1
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;
            }
        }
コード例 #2
0
ファイル: ComClient.cs プロジェクト: nakky/Snowball
        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;
            }
        }