Example #1
0
        public bool SendPacket(Packet packet, uint dwIP, ushort nPort, bool bEncrypt,
                               byte[] pachTargetClientHashORKadID, bool bKad, uint nReceiverVerifyKey)
        {
            UDPPack newpending = new UDPPack();

            newpending.dwIP     = dwIP;
            newpending.nPort    = nPort;
            newpending.packet   = packet;
            newpending.dwTime   = MpdUtilities.GetTickCount();
            newpending.bEncrypt =
                bEncrypt &&
                (pachTargetClientHashORKadID != null ||
                 (bKad && nReceiverVerifyKey != 0));
            newpending.bKad = bKad;
            newpending.nReceiverVerifyKey = nReceiverVerifyKey;

            if (newpending.bEncrypt && pachTargetClientHashORKadID != null)
            {
                MpdUtilities.Md4Cpy(newpending.pachTargetClientHashORKadID, pachTargetClientHashORKadID);
            }
            else
            {
                MpdUtilities.Md4Clr(newpending.pachTargetClientHashORKadID);
            }
            lock (sendLocker_)
            {
                controlpacketQueue_.Add(newpending);
            }// sendLocker.Unlock();

            MuleApplication.Instance.UploadBandwidthThrottler.QueueForSendingControlPacket(this);
            return(true);
        }
Example #2
0
        public SocketSentBytes SendControlData(uint maxNumberOfBytesToSend, uint minFragSize)
        {
            uint sentBytes = 0;

            lock (sendLocker_)
            {
                while (controlpacketQueue_.Count > 0 &&
                       sentBytes < maxNumberOfBytesToSend)
                {
                    UDPPack cur_packet = controlpacketQueue_[0];
                    if (MpdUtilities.GetTickCount() - cur_packet.dwTime < MuleConstants.UDPMAXQUEUETIME)
                    {
                        uint   nLen       = cur_packet.packet.Size + 2;
                        byte[] sendbuffer = new byte[nLen];
                        Array.Copy(cur_packet.packet.UDPHeader, sendbuffer, 2);
                        Array.Copy(cur_packet.packet.Buffer, 0, sendbuffer, 2, cur_packet.packet.Size);

                        if (cur_packet.bEncrypt && (MuleApplication.Instance.PublicIP > 0 || cur_packet.bKad))
                        {
                            nLen = (uint)EncryptSendClient(ref sendbuffer, (int)nLen,
                                                           cur_packet.pachTargetClientHashORKadID, cur_packet.bKad,
                                                           cur_packet.nReceiverVerifyKey,
                                                           (cur_packet.bKad ? MuleApplication.Instance.KadEngine.Preference.GetUDPVerifyKey(cur_packet.dwIP) : 0));
                        }

                        if (UDPSendTo(sendbuffer, (int)nLen, cur_packet.dwIP, cur_packet.nPort) == 0)
                        {
                            sentBytes += nLen;

                            controlpacketQueue_.RemoveAt(0);
                        }
                    }
                    else
                    {
                        controlpacketQueue_.RemoveAt(0);
                    }
                }

                if (controlpacketQueue_.Count > 0)
                {
                    MuleApplication.Instance.UploadBandwidthThrottler.QueueForSendingControlPacket(this);
                }
            }//sendLocker.Unlock();

            SocketSentBytes returnVal = new SocketSentBytes(true, 0, sentBytes);

            return(returnVal);
        }