public bool AddSource(CClient source) { bool notMaxSources; notMaxSources=(((m_Element.File!=null)&&(m_ArrayList.Count<m_Element.File.MaxSources))|| (RemoveNoNeededSource())); if ((notMaxSources) &&(!m_ArrayList.Contains(source)) &&((source.UserID!=CKernel.Preferences.GetUInt("ID")) ||(source.Port!=CKernel.Preferences.GetUShort("TCPPort")))) { //this is an additional check to avoid duplicated sources //is it really needed, does it solve the problem at 100% ? CClient existingSource=CKernel.GetClient(m_ArrayList,source.UserID,source.Port,source.ServerIP,source.UserHash); if (existingSource!=null) { #if DEBUG //CLog.Log(Types.Constants.Log.Verbose,"Duplicated source detected: "+ source.UserName + "-" + source.UserID.ToString() + "-" + m_ArrayList.Contains(source).ToString()); #endif //attach the client to the existing one source=existingSource; return false; } // m_ArrayList.Add(source); CKernel.NewSource(source, CKernel.HashToString(source.DownFileHash)); return true; } else CKernel.ClientsList.IsClientNeeded(source); return false; }
public CSourceOld(CClient client) { FileHash=client.DownFileHash; UserHash=client.UserHash; FileChunks=client.DownFileChunks; DeletedTime=DateTime.Now; }
public bool AddClient(CClient client) { if (client.DownFileChunks==null) return false; CSourceOld source=GetSourceOld(client.UserHash); if (source==null) { CSourceOld sourceOld=new CSourceOld(client); m_ArrayList.Add(sourceOld); return true; } else source.DeletedTime=DateTime.Now; return false; }
internal InterfaceClient ClientToInterfaceClient(CClient Client) { if (apw) { InterfaceClient response=new InterfaceClient(); response.Name=Client.UserName; #if DEBUG if (Client.IsPartner) response.Name="(partner) "+response.Name; #endif response.DownFileName=Client.ClientFileName; if ((Client.UploadElement!=null)&&(Client.UploadElement.File!=null)) response.UpFileName=Client.UploadElement.File.FileName; else response.UpFileName=""; response.ID=Client.UserID; response.Port=Client.Port; response.ServerIP=Client.ServerIP; response.Software=Client.Software; response.Version=Client.Version; response.DownloadState=(byte)Client.DownloadState; response.UploadState=(byte)Client.UploadState; response.UploadRequests=Client.UploadRequests; response.UploadedBytes=Client.UploadedBytes; response.DownloadedBytes=Client.DownloadedBytes; response.LastUploadRequest=Client.LastUploadRequest; response.QueueTime=Client.QueueInTime; response.DownloadSpeed=(float)Math.Round((Client.DownloadSpeed/1024F),1); response.UploadSpeed=Client.UploadSpeed; response.DownQR=Client.QRDownload; response.DownFileChunks=Client.DownFileChunks; response.UpFileChunks=Client.UpFileChunks; response.UserHash=Client.UserHash; return response; } else return null; }
/// <summary> /// Create a new connection. /// </summary> /// <param name="nIP">IP</param> /// <param name="Port">Port</param> /// <param name="in_client">Client</param> internal CConnection(uint nIP,int Port,CClient in_client) { CreateConnection(nIP,Port,in_client); }
internal static void NewSource(CClient client, string strDownFileHash) { InterfaceClient interfaceClient=InterfaceGateway[0].ClientToInterfaceClient(client); for (int i=0;i<=InterfaceGateway.Length-1;i++) { if (CKernel.InterfaceGateway[i]!=null) { InterfaceGateway[i].NewSource(interfaceClient,strDownFileHash); } } }
private ushort m_GetQueuePosition(CClient Client) { if (!m_QueueList.Contains(Client)) return 0; ushort position=1; double rating=GetClientRating(Client); foreach (CClient compareClient in m_QueueList) { if (compareClient==Client) continue; if (GetClientRating(compareClient)<rating) { position++; } } return position; }
public void RemoveUpload(CClient Client, bool enqueue) { if (!UploadList.Contains(Client)) return; if ((enqueue)&&((float)CKernel.Queue.UploadList.Count-1>=m_GetNumberOfSlots())) { // si no hay nadie en la cola yo no lo he tirado así que se ha salido de transferencias por que // ha desconectado,si no hay más gente en la cola que slots no lo metemos para prevenir un bucle Client.QueueInTime=DateTime.Now; UploadList.Remove(Client); AddClient(Client); Client.UploadState=Protocol.UploadState.OnQueue; if ((Client.IsPartner)&&(m_Partners>0)) m_Partners--; Debug.WriteLine("Client "+Client.UserName+" moved to queue"); } else { UploadList.Remove(Client); if (Client.UploadElement!=null) Client.UploadElement.Statistics.RemoveUploadChunksAvaibility(Client.UpFileChunks); if ((Client.IsPartner)&&(m_Partners>0)) m_Partners--; Client.UploadState=Protocol.UploadState.None; Debug.WriteLine("Client "+Client.UserName+" removed from transfer"); } }
public ushort RefreshClientUDP(uint IP,ushort UDPPort, ref CClient foundClient) { foundClient=null; lock(m_QueueList.SyncRoot) { foreach (CClient client in m_QueueList) { if ((client.UserID==IP)&&(client.UDPPort==UDPPort)) { foundClient=client; break; } } } if (foundClient!=null) { foundClient.IncUploadRequests(); foundClient.LastUploadRequest=DateTime.Now; return m_GetQueuePosition(foundClient); } else { foundClient=CKernel.ClientsList.GetClient(IP,UDPPort); if (foundClient!=null) { if (foundClient.UpFileHash!=null) return AddClient(foundClient); else return 0; } else return 0; } }
public void AddPartner(CClient client) { if (!AllowNewParner()) return; //select a client and kick to allow slot for a partner m_AddingPartner=true; CClient kickClient; if ((UploadList.Count>=this.m_GetNumberOfSlots())) { for (int i=0;i<UploadList.Count;i++) { kickClient=(CClient)UploadList[i]; if (!kickClient.IsPartner) { kickClient.CancelUploadTransfer(true); CLog.Log(Constants.Log.Verbose,"kicked "+kickClient.UserName+" to allow new partner"); break; } } } if (client!=null) { client.IsPartner=true; m_Partners++; client.StartUpload(); m_LastAcceptedClientTime=DateTime.Now; UploadList.Add(client); m_QueueList.Remove(client); CKernel.NewUpload(client); } m_AddingPartner=false; }
public bool Contains(CClient client) { return m_ArrayList.Contains(client); }
public void AddSource(byte[] FileHash,ref CClient client) { CElement Element=(CElement)this[FileHash]; if ((Element!=null)&& (Element!=null)&&(!Element.File.Completed)) Element.SourcesList.AddSource(client); }
/// <summary> /// Create a new connection. /// </summary> /// <param name="nIP">IP</param> /// <param name="Port">Port</param> /// <param name="in_client">Client</param> private void CreateConnection(uint nIP,int Port,CClient in_client) { m_ReceptionStream=new MemoryStream(); m_StreamPosition=0; m_ntimeOuts=0; m_HeaderPending=true; m_DownLimit=(int)(CKernel.Preferences.GetFloat("MaxDownloadRate")*1024F*0.20F); m_SendPacketList=new ArrayList(); // m_TimerTimeout=new Timer(new TimerCallback(OnTimeOut),null,Timeout.Infinite,Timeout.Infinite); m_socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); m_socket.Blocking=false; //m_socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendBuffer,1500); IPAddress DireccionIP=new IPAddress(nIP); m_EPremote=new IPEndPoint(DireccionIP,Port); if (in_client!=null) { m_Client=in_client; } }
/// <summary> /// Process incomming eDonkey packet. /// </summary> /// <param name="packet">Packet which need to be processed.</param> protected virtual void ProcessDonkeyPacket(ref byte[] packet) { try { switch ((Protocol.ClientCommand)m_DonkeyHeader.Command) { case Protocol.ClientCommand.Hello: if (m_Client==null) { CReceiveHello HelloPacket=new CReceiveHello(false,new MemoryStream(packet)); m_Client=CKernel.ClientsList.GetClient(HelloPacket.UserID,HelloPacket.UserPort,HelloPacket.ServerIP,HelloPacket.Hash); if (m_Client==null) { m_Client=new CClient(this); CKernel.ClientsList.AddClient(m_Client); } else { if ((m_Client.connection!=null)&&(m_Client.connection.Connected)&&(m_Client.connection!=this)) { m_Client.connection.CloseConnection(); Debug.WriteLine("Client conexion!=null"); } m_Client.connection=this; } } CClient cant=m_Client; CConnection conant=m_Client.connection; bool NeedExtendRequest=!m_Client.ProcessHello(false,packet); if ((NeedExtendRequest)&&(m_Client.EmuleProtocol)&&(CKernel.Preferences.GetBool("UseEmuleProtocol"))) m_Client.SendEmuleHello(false); m_Client.ResponseHello(); break; case Protocol.ClientCommand.FileRequest: m_Client.ProcessFileRequest(packet); break; case Protocol.ClientCommand.HelloAnswer: m_Client.ProcessHello(true,packet); break; case Protocol.ClientCommand.SetRequestFileID: m_Client.ProcessRequestFileID(packet); break; case Protocol.ClientCommand.FileRequestAnswer: m_Client.ProcessFileInfo(packet); break; case Protocol.ClientCommand.FileState: m_Client.ProcessChunksStatus(packet,false/*tcp*/); break; case Protocol.ClientCommand.HashSetAnswer: m_Client.ProcessHashSet(packet); break; case Protocol.ClientCommand.HashSetRequest: m_Client.SendHashSet(); break; case Protocol.ClientCommand.AcceptUploadRequest: if (m_Client.DownloadState==Protocol.DownloadState.OnQueue) { m_Client.RequestBlocks(); } else { if (m_Client.DownFileHash!=null) { // CLog.Log(Constants.Log.Verbose,"Trying to resume download {2}: {0},hashdescarga=null? {1}",m_Client.DownloadState,(m_Client.DownFileHash==null),m_Client.UserName); m_Client.RequestBlocks(); } else { if (m_Client.UpFileHash!=null) { #if DEBUG CLog.Log(Constants.Log.Verbose,"Recovering source to download "+m_Client.UserName); #endif m_Client.DownFileHash=m_Client.UpFileHash; m_Client.RequestBlocks(); } else { CSourceOld sourceRecovered=CKernel.SourcesOld.GetSourceOld(m_Client.UserHash); if (sourceRecovered!=null) { m_Client.DownFileHash=sourceRecovered.FileHash; m_Client.DownFileChunks=sourceRecovered.FileChunks; m_Client.RequestBlocks(); #if DEBUG CLog.Log(Constants.Log.Verbose,"RECOVERED SOURCE from sources cache "+m_Client.UserName); #endif } #if DEBUG else CLog.Log(Constants.Log.Verbose,"download lost: {0}",m_Client.UserName); #endif } } } break; case Protocol.ClientCommand.OutOfPartRequest: m_Client.ProcessOutOfParts(); break; case Protocol.ClientCommand.StartUploadRequest: m_Client.ProcessStartUploadRequest(packet); break; case Protocol.ClientCommand.RequestParts: m_Client.ProcessRequestParts(packet); break; case Protocol.ClientCommand.EndOfDownload: case Protocol.ClientCommand.CancelTransfer: m_Client.CancelUploadTransfer(false); break; case Protocol.ClientCommand.SendingPart: m_Client.ReceiveBlock(packet); break; case Protocol.ClientCommand.QueueRank: m_Client.ProcessQR(packet); break; case Protocol.ClientCommand.FileRequestAnswerNoFile: m_Client.FileNotFound(packet); break; case Protocol.ClientCommand.ChangeClientID: m_Client.ProcessIDChange(packet); break; case Protocol.ClientCommand.ChangeSlot: break; case Protocol.ClientCommand.AskSharedFiles: Constants.AllowViewShared allow=(Constants.AllowViewShared)CKernel.Preferences.GetEnum("AllowViewShared",Constants.AllowViewShared.Nobody); if (allow==Constants.AllowViewShared.All) { m_Client.ProcessSharedListRequest(); } break; case Protocol.ClientCommand.AskSharedFilesAnswer: m_Client.ProcessSharedListResponse(packet); break; case Protocol.ClientCommand.Message: m_Client.ProcessChatMessage(packet); break; case Protocol.ClientCommand.CrumbSetResponse: m_Client.ProcessCrumbSetResponse(packet); break; default: CLog.Log(Constants.Log.Verbose,"CLI_UNK_DONK_TCP_PACKET",m_DonkeyHeader.Command); break; } } catch(Exception e) { Debug.WriteLine(e.ToString()); #if DEBUG if (m_DonkeyHeader!=null) CLog.Log(Constants.Log.Verbose,"CLI_PAQ_DONK_ERROR",m_DonkeyHeader.Command); #endif } packet=null; return; }
/// <summary> /// Close connection. /// </summary> public void CloseConnection() { m_ntimeOuts=0; // if (m_TimerTimeout!=null) // { // m_TimerTimeout.Dispose(); // m_TimerTimeout=null; // } m_SendPacketList.Clear(); m_SendPacket=null; m_ReceptionPacket=null; CloseSocket(); CKernel.ConnectionsList.Remove(this); m_Client=null; #if VERBOSE if (m_Client!=null) CKernel.LogClient.AddLog("closedConnection",m_Client.UserID); #endif }
internal virtual void SubmitOnNewChatMessage(CClient client, string message) { if (OnNewChatMessage!=null) OnNewChatMessage(ClientToInterfaceClient(client),message); }
public ushort AddClient(CClient Client) { Client.IncUploadRequests(); if (m_QueueList.Contains(Client)) { TimeSpan diff=DateTime.Now-Client.LastUploadRequest; if ((diff<new TimeSpan(0,Protocol.MinClientReask-11,0))&& (diff>new TimeSpan(0,3,0))) { RemoveClient(Client); Debug.WriteLine("Client kicked from queue, fast reask "+Client.UserName+" "+Client.Software+" "+diff.TotalMinutes); CKernel.ClientsList.IsClientNeeded(Client); return 0; } else { Client.LastUploadRequest=DateTime.Now; return m_GetQueuePosition(Client); } } Client.LastUploadRequest=DateTime.Now; if (UploadList.Contains(Client)) return 1; //is a new client int queueLength=CKernel.Preferences.GetInt("QueueLength"); int queuedForRequestedFile=0; if (Client.UploadElement!=null) queuedForRequestedFile=Client.UploadElement.Statistics.QueuedClients; if ((m_QueueList.Count>=queueLength)&& (!Client.IsFriend)&& ((queuedForRequestedFile>0)||((queuedForRequestedFile==0)&&(m_QueueList.Count>=queueLength+15)))) { if ((Client.UploadElement==null)|| (Client.UploadElement.File==null)|| (Client.UploadElement.File.UpPriority!=Constants.Priority.VeryHigh)|| (m_QueueList.Count>=queueLength+50)) if (Client.SoftwareID==(uint)Protocol.Client.eLePhant) return (ushort)m_QueueList.Count; else return 0; } //there is room in queue or is a very high priority file and we can use additional 50 queue positions //or is the first client requesting a file //#if DEBUG // if (queuedForRequestedFile==0) // CLog.Log(Constants.Log.Verbose,"Added to queue "+ m_QueueList.Count.ToString()+" "+Client.UserName +" for rare file "+Client.UploadElement.File.FileName); //#endif m_QueueList.Add(Client); if (Client.UploadElement!=null) Client.UploadElement.Statistics.AddUploadChunksAvaibility(Client.UpFileChunks); Client.QueueInTime=DateTime.Now; return m_GetQueuePosition(Client); }
public void RemoveSource(CClient Client) { if (m_ArrayList.Contains(Client)) { RemoveChunksAvaibility(Client.DownFileChunks); m_ArrayList.Remove(Client); Client.RemoveDownload(); Client.SwapToOtherFile(); } CKernel.ClientsList.IsClientNeeded(Client); }
public bool ContainsClient(CClient cliente) { return ((m_QueueList.Contains(cliente))||(UploadList.Contains(cliente))); }
/* public CClient GetClient(uint ID,ushort port,uint serverIP,byte[] ClientHash) { CClient resClient=null; byte[] HashComp; bool isSame; lock (m_List.SyncRoot) { foreach (CClient Client in m_List) { HashComp=Client.UserHash; if ((ClientHash!=null)&&(HashComp!=null)) { isSame=true; for (int i=0;i!=16;i++) { if (HashComp[i]!=ClientHash[i]) { isSame=false; break; } } if (isSame) { resClient=Client; break; } else { continue; } } if (ID>Protocol.LowIDLimit) { if (port>0) { if ((Client.UserID==ID)&&(Client.Port==port)) { resClient=Client; break; } } else { if (Client.UserID==ID) { resClient=Client; break; } } } else { if ((Client.UserID==ID)&&(Client.Port==port)&&(Client.ServerIP==serverIP)) { resClient=Client; break; } } } } return resClient; } */ public bool AddClient(CClient Client) { CClient ClientBuscado=GetClient(Client.UserID,Client.Port,Client.ServerIP,Client.UserHash); if (ClientBuscado==null) { m_List.Add(Client); return true; } else { return false; } }
public void RemoveClient(CClient client) { if (client.UploadElement!=null) client.UploadElement.Statistics.RemoveUploadChunksAvaibility(client.UpFileChunks); m_QueueList.Remove(client); }
public CClient AddClient(uint IP,ushort port,uint ServerIP,byte[] HashClient,ushort ServerPort,byte[] FileHashDescarga, Constants.FriendLevel friendLevel) { CClient client=GetClient(IP,port,ServerIP,HashClient); if (client==null) { client=new CClient(port,IP,ServerIP,ServerPort,FileHashDescarga); client.FriendStatus=friendLevel; if (m_LastConnection<DateTime.Now-new TimeSpan(0,Protocol.MinClientReask,0)) { m_LastConnection=DateTime.Now-new TimeSpan(0,0,Protocol.MinClientReask-1,59,700); client.LastDownloadRequest=DateTime.Now-new TimeSpan(0,Protocol.MinClientReask,0); } else { m_LastConnection += new TimeSpan(0,0,0,0,300); client.LastDownloadRequest=m_LastConnection; } m_List.Add(client); } else { if (!client.IsFriend) client.FriendStatus=friendLevel; else { CKernel.FriendsList.Add(IP,port,ServerIP,HashClient,ServerPort,"",client.UserName,client.Software,client.Version); } }; return client; }
private double GetClientRating(CClient client) { double priority=1F; double credit=1F; TimeSpan timeInQueue=DateTime.Now-client.QueueInTime; if ((client.UploadElement!=null)&&(client.UploadElement.File!=null)) { switch(client.UploadElement.File.UpPriority) { case Constants.Priority.VeryLow: priority=1; break; case Constants.Priority.Low: priority=3; break; case Constants.Priority.Normal: priority=8; break; case Constants.Priority.High: priority=18; break; case Constants.Priority.VeryHigh: priority=37; break; } } //instant credit if ((client.DownloadState==Protocol.DownloadState.Downloading)) { credit=1F+client.DownloadSpeed*20F; } // friend bonus if (client.IsFriend) { credit=5.0F; } // emule credits systems benefit sources to us by probabilities due to the credit system,we emulate it with a littel benefit to get similar downloads speeds if ((credit==1F)&&(client.DownFileHash!=null)&&(client.DownloadState!=Protocol.DownloadState.NoNeededParts)) { if ((client.QRDownload<100)&&(client.QRDownload>0)) { credit=3.5F; } else { credit=2.5F; } } return timeInQueue.TotalSeconds*priority*credit; }
public void AddClientToFile(CClient client,byte[] FileHash) { if (client.DownFileHash==null) { client.DownFileHash=FileHash; CKernel.FilesList.AddSource(FileHash,ref client); } else { if (CKernel.SameHash(ref FileHash,ref client.DownFileHash)) { CKernel.FilesList.AddSource(FileHash,ref client); } else { client.AddOtherDownloadFile(CKernel.HashToString(FileHash)); } } }
internal static void NewChatMessage(CClient client,string message) { for (int i=0;i<= InterfaceGateway.Length-1;i++) { if (CKernel.InterfaceGateway[i]!=null) { InterfaceGateway[i].SubmitOnNewChatMessage(client, message); } } }
public void IsClientNeeded(CClient client) { if ((client.DownFileHash==null) ||((client.DownFileHash!=null)&&((CKernel.FilesList[client.DownFileHash]==null)) ||((client.DownFileHash!=null)&&(CKernel.FilesList[client.DownFileHash].SourcesList==null)) ||((client.DownFileHash!=null)&&(CKernel.FilesList[client.DownFileHash].SourcesList!=null) &&(!CKernel.FilesList[client.DownFileHash].SourcesList.Contains(client))))) { if ((!client.DownloadVariousFiles) &&(!CKernel.Queue.ContainsClient(client)) &&((client.connection==null)||(!client.connection.Connected))) { #if VERBOSE CKernel.LogClient.AddLog("Client not needed",client.UserID); #endif if (!client.IsFriend) { //Debug.WriteLine(m_List.Count.ToString()+" Client not needed"); m_List.Remove(client); } } } }
internal static void NewUpload(CClient client) { InterfaceClient interfaceClient=InterfaceGateway[0].ClientToInterfaceClient(client); for (int i=0;i<=InterfaceGateway.Length-1;i++) { if (CKernel.InterfaceGateway[i]!=null) { InterfaceGateway[i].NewUpload(interfaceClient); } } }
/// <summary> /// Create a new connection. /// </summary> /// <param name="strIP">IP</param> /// <param name="Port">Port</param> /// <param name="in_client">Client</param> internal CConnection(string strIP,int Port,CClient in_client) { IPAddress DirectionIP=IPAddress.Parse(strIP); CreateConnection(BitConverter.ToUInt32(DirectionIP.GetAddressBytes(),0),Port,in_client); }