Example #1
0
        async Task UdpCheck()
        {
            healthLostCount = 0;

            while (IsOpened)
            {
                try
                {
                    await Task.Delay(100);

                    if (IsTcpConnected)
                    {
                        if (!udpAck)
                        {
                            if (serverNode != null && serverNode.AesEncrypter != null)
                            {
                                byte[] encrypted = serverNode.AesEncrypter.Encrypt(Global.UdpRawData);

                                IssueIdData data = new IssueIdData();
                                data.Id             = UserId;
                                data.encryptionData = encrypted;
                                SendInternal((short)PreservedChannelId.UdpNotify, data);
                            }
                        }
                    }
                }
                catch//(Exception e)
                {
                    //Util.Log("Health:" + e.Message);
                }
            }
        }
Example #2
0
        void OnConnectedInternal(string ip, TCPConnection connection)
        {
            if (connection != null)
            {
                connection.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                lock (serverNodeLocker)
                {
                    serverNode = new ComSnowballNode(connection);
                }

                udpTerminal.ReceiveStart();

                connection.OnDisconnected = OnDisconnectedInternal;
                connection.OnPoll         = OnPoll;

                IssueIdData ldata = new IssueIdData();

                if (UserId != 0)
                {
                    if (previousServerNode != null && previousServerNode.AesEncrypter != null)
                    {
                        ldata.Id             = UserId;
                        ldata.encryptionData = previousServerNode.AesEncrypter.Encrypt(Global.ReconnectRawData);
                    }
                    else
                    {
                        UserId = 0;
                    }
                }

                if (UserId == 0)
                {
                    ldata.Id = UserId;
                }

                SendInternal((short)PreservedChannelId.IssueId, ldata);

                healthLostCount = 0;

                //Util.Log("Client:Connected");

                isConnecting = false;
            }
            else
            {
                isConnecting = false;
                OnConnectError(ip);
            }
        }
Example #3
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;
            }
        }
Example #4
0
        void OnIssueId(ComNode node, IssueIdData data)
        {
            bool registerd = false;

            if (data.Id != 0 && data.encryptionData != null && data.encryptionData.Length > 0)
            {
                ComNode previousNode = null;
                try
                {
                    userNodeMapLock.AcquireReaderLock(1000);
                    ComNode n;
                    if (userNodeMap.TryGetValue(data.Id, out n))
                    {
                        previousNode = n;
                    }
                }
                finally
                {
                    userNodeMapLock.ReleaseReaderLock();
                }

                try
                {
                    disconnectedUserNodeMapLock.AcquireWriterLock(1000);
                    ComNode n;
                    if (previousNode == null && disconnectedUserNodeMap.TryGetValue(data.Id, out n))
                    {
                        previousNode = n;
                        disconnectedUserNodeMap.Remove(data.Id);
                    }
                }
                finally
                {
                    disconnectedUserNodeMapLock.ReleaseWriterLock();
                }

                if (previousNode != null && previousNode.AesDecrypter != null)
                {
                    try
                    {
                        byte[] decrypted = previousNode.AesDecrypter.Decrypt(data.encryptionData);
                        if (decrypted.SequenceEqual(Global.ReconnectRawData))
                        {
                            node.UserId = previousNode.UserId;

                            try
                            {
                                userNodeMapLock.AcquireWriterLock(1000);
                                userNodeMap.Add(node.UserId, node);
                            }
                            finally
                            {
                                userNodeMapLock.ReleaseWriterLock();
                            }
                            registerd = true;
                        }
                    }
                    catch
                    {
                    }
                }
            }

            if (!registerd)
            {
                GenerateId(ref node);
                registerd = true;
            }

            if (!registerd)
            {
                Disconnect(node);
            }
            else
            {
                IssueIdData ldata = new IssueIdData();
                ldata.Id = node.UserId;
                string xml = rsaDecrypter.ToPublicKeyXmlString();
                ldata.PublicKey      = xml;
                ldata.encryptionData = new byte[0];
                SendInternal(node, (short)PreservedChannelId.IssueId, ldata);
            }
        }