Example #1
0
        private void m_ProcessUDPPacket(byte command, ref MemoryStream packet)
        {
            try
            {
                switch( (Protocol.ClientCommandExtUDP)command )
                {
                    case Protocol.ClientCommandExtUDP.ReaskFilePing:
                        if (!m_PortIsAccesible)
                            CLog.Log(Types.Constants.Log.Info,"UDP_PORT_IS_OPEN");
                        m_PortIsAccesible=true;
                        MemoryStream responseBuffer;
            //						if (packet.Length!=16) break;
            //						byte[] FilaHash=new byte[16];
            //						packet.Read(FilaHash,0,16);
                        CFileReaskUDP reaskMessage=new CFileReaskUDP(packet);
                        byte[] FilaHash=reaskMessage.FileHash;
                        //byte[] chunksAvaibility=reaskMessage.partes;
                        //TODO update chunksAvaibility in shared file

                        CElement requestedElement=CKernel.FilesList[FilaHash];
                        if (requestedElement==null)
                        {
                            responseBuffer=new MemoryStream(2);
                            CFileNotFoundUDP FicheroNoEncontradoUDP=new CFileNotFoundUDP(responseBuffer);
                        }
                        else
                        {
                            m_RemoteIPEndPoint=(IPEndPoint)m_RemoteEndPoint;
                            CClient foundClient=null;
                            ushort queuePosition=CKernel.Queue.RefreshClientUDP(BitConverter.ToUInt32(m_RemoteIPEndPoint.Address.GetAddressBytes(),0),(ushort)m_RemoteIPEndPoint.Port,ref foundClient);
                            if (queuePosition<=0)
                            {
                                responseBuffer=new MemoryStream(2);
                                CQueueFullUDP PaqueteColaLlenaUDP=new CQueueFullUDP(responseBuffer);
                            }
                            else
                            {
                                responseBuffer=new MemoryStream(4);
                                CQueuePositionUDP PaquetePosicionColaUDP=new CQueuePositionUDP(responseBuffer,queuePosition,requestedElement.File.ChunksStatus,foundClient.VersionUDP);
                            }
                        }
                        SendPacketUDP(responseBuffer.GetBuffer(),m_RemoteIPEndPoint);
                        break;
                    case Protocol.ClientCommandExtUDP.ReaskAck:
                        m_RemoteIPEndPoint=(IPEndPoint)m_RemoteEndPoint;
                        CClient client=CKernel.ClientsList.GetClient(BitConverter.ToUInt32(m_RemoteIPEndPoint.Address.GetAddressBytes(),0),0,0,null);
                        if (client!=null)
                        {
                            ushort queuePosition;

                            if ((packet.Length>2)&&(client.VersionUDP>3))
                            {
                                packet.Seek(0,SeekOrigin.Begin);
                                client.ProcessChunksStatus(packet,true);
                            }
                            BinaryReader reader=new BinaryReader(packet);
                            queuePosition=reader.ReadUInt16();
                            client.ProcessUDPReask(queuePosition);
                            packet.Close();
                            packet=null;
                        }
                        break;
                    case Protocol.ClientCommandExtUDP.QueueFull:
                        client=CKernel.ClientsList.GetClient(BitConverter.ToUInt32(m_RemoteIPEndPoint.Address.GetAddressBytes(),0),0,0,null);
                        if (client!=null)
                        {
                            client.ProcessUDPReask(0);
                        }
                        break;
                    case Protocol.ClientCommandExtUDP.FileNotFound:
                        client=CKernel.ClientsList.GetClient(BitConverter.ToUInt32(m_RemoteIPEndPoint.Address.GetAddressBytes(),0),0,0,null);
                        if ((client!=null)&&(client.DownloadElement!=null)&&(client.DownloadElement.SourcesList!=null))
                            client.DownloadElement.SourcesList.RemoveSource(client);
                        break;
                    default:
                        CLog.Log(Constants.Log.Verbose,"CLI_UNK_UDP_PACKET",command);
                        break;
                }
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.ToString());
                CLog.Log(Constants.Log.Verbose,"CLI_PAQ_UDP_ERROR",command);
            }
        }
Example #2
0
        public void TryUDPDownload()
        {
            m_AskingUDP=true;

            if ((m_EmuleProtocol)&&
                (m_VersionUDP>=1)&&
                (m_ID>Protocol.LowIDLimit)&&
                (m_UDPPort>0)&&
                (DownFileHash!=null)&&
                ((m_QRDownload>50)||(m_QRDownload==0))&&
                ((!m_SupportsPartner)||(!CKernel.Queue.AllowNewParner()))&&
                !m_AllowSourceExchangeRequest())
            {
            #if VERBOSE
                CKernel.LogClient.AddLog("Intentar Descarga UDP");
            #endif
                MemoryStream buffer=new MemoryStream();
                if (m_DownloadElement==null) m_DownloadElement=CKernel.FilesList[DownFileHash];
                if (m_DownloadElement==null) return;
                CFileReaskUDP PeticionUDP=new CFileReaskUDP(buffer,DownFileHash,m_VersionUDP,m_DownloadElement.File.ChunksStatus);

                IPEndPoint hostRemote=new IPEndPoint(m_ID,m_UDPPort);
                CKernel.UDPListener.SendPacketUDP(buffer.GetBuffer(),hostRemote);
            }
        }