⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messages.cs

📁 P2P (peer to peer) file sharing program in C#. Supports Gnutella, Gnutella2, eDonkey, and OpenNap. w
💻 CS
📖 第 1 页 / 共 3 页
字号:
											throw new Exception("eDonkey Login addr problem");										ms.Write(addr, 0, 4);										ms.Write(Endian.GetBytes((ushort)Sck.scks[servSockNum].port, false), 0, 2);										goto next;									}						}					ms.Write(Endian.GetBytes((uint)0, false), 0, 4);					ms.Write(Endian.GetBytes((ushort)0, false), 0, 2);				}			}			next:				int bufSize = (int)ms.Position;			ms.Close();			Packet pckt = new Packet(bufSize);			pckt.outPayload = ms.GetBuffer();			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing request to get some more servers.		/// </summary>		public static void GetServerList(int sockNum)		{			Packet pckt = new Packet(1);			pckt.outPayload = new byte[1];			pckt.outPayload[0] = 0x14;			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing search to all eDonkey servers for a file.		/// </summary>		public static void Search(string file)		{			MemoryStream ms = new MemoryStream();			//type			ms.WriteByte(0x16);			//rest			ms.WriteByte(0x01);			ms.Write(Endian.GetBytes((ushort)file.Length, false), 0, 2);			ms.Write(Encoding.ASCII.GetBytes(file), 0, file.Length);			//broadcast the search			int bufSize = (int)ms.Position;			ms.Close();			Packet pckt = new Packet(bufSize);			pckt.outPayload = ms.GetBuffer();			foreach(Sck sck in Sck.scks)				if(sck != null)					if(sck.state == Condition.Connected)						sck.SendPacket(pckt);		}		/// <summary>		/// Outgoing request to all eDonkey servers for a specific file.		/// </summary>		public static void FindSources(byte[] md4hash, bool globalUdp)		{			if(globalUdp)			{				byte[] msg = new byte[18];				msg[0] = 0xE3;				msg[1] = 0x9A;				Array.Copy(md4hash, 0, msg, 2, 16);				//find a random server				IPandPort ipap = new IPandPort();				GUIBridge.EGetRandomServer(ipap);				Listener.UdpSend(ref ipap.ip, ipap.port+4, msg, 0, msg.Length);			}			else			{				MemoryStream ms = new MemoryStream();				//type				ms.WriteByte(0x19);				//rest				ms.Write(md4hash, 0, 16);				int bufSize = (int)ms.Position;				ms.Close();				Packet pckt = new Packet(bufSize);				pckt.outPayload = ms.GetBuffer();				foreach(Sck sck in Sck.scks)					if(sck != null)						if(sck.server)							if(sck.state == Condition.Connected)								sck.SendPacket(pckt);			}		}		/// <summary>		/// Outgoing request for a callback connection.		/// </summary>		public static void SendCallback(ref string hostip, int sockNum)		{			byte[] msg = new byte[5];			msg[0] = 0x1C;			Array.Copy(Endian.BigEndianIP(hostip), 0, msg, 1, 4);			Packet pckt = new Packet(msg.Length);			pckt.outPayload = msg;			if(sockNum == -1)				return;			if(Sck.scks[sockNum].state != Condition.Connected)				return;			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing response to a client hello.		/// </summary>		public static void HelloResponse(int sockNum, string clientip)		{			MemoryStream ms = new MemoryStream();			//type			ms.WriteByte(0x4C);			//client info			Routines.ClientInfo(ms);			//server address			ms.Write(Endian.GetBytes((uint)0, false), 0, 4);			ms.Write(Endian.GetBytes((ushort)0, false), 0, 2);			int bufSize = (int)ms.Position;			ms.Close();			Packet pckt = new Packet(bufSize);			pckt.outPayload = ms.GetBuffer();			//also check if we need this host for downloading			int dmNum = Sck.scks[sockNum].dmNum;			int dlNum = Sck.scks[sockNum].dlNum;			if(dmNum == -1 && dlNum == -1)			{				//find a corresponding downloader				foreach(DownloadManager dMEr in DownloadManager.dms)					if(dMEr != null)						if(dMEr.active)							foreach(Downloader dler in dMEr.downloaders)								if(dler.qho.ip == clientip && dler.qho.networkType == NetworkType.EDonkey)								{									if(dler.queueState == 0)									{										if(dler.edsck != null)										{											System.Diagnostics.Debug.WriteLine("eDonkey HelloResponse incompatibility 1 " + dler.state.ToString());											return;										}										lock(dMEr.endpoints)										{											dler.edsck = Sck.scks[sockNum];											Sck.scks[sockNum].dlNum = dler.dlNum;											Sck.scks[sockNum].dmNum = dler.dmNumParent;											dler.state = DLState.Connecting;										}										Sck.scks[sockNum].SendPacket(pckt);										dler.EDonkeyReady();										return;									}									else									{										if(dler.edsck != null)										{											System.Diagnostics.Debug.WriteLine("eDonkey HelloResponse incompatibility 2 " + dler.state.ToString());											return;										}										lock(dMEr.endpoints)										{											dler.edsck = Sck.scks[sockNum];											Sck.scks[sockNum].dlNum = dler.dlNum;											Sck.scks[sockNum].dmNum = dler.dmNumParent;											dler.state = DLState.Connected;											dler.queueState = 0;										}										Sck.scks[sockNum].SendPacket(pckt);										return;									}								}			}			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing file request.		/// </summary>		public static void FileRequest(byte[] fileHash, int sockNum)		{			byte[] ba = new byte[17];			ba[0] = 0x58;			Array.Copy(fileHash, 0, ba, 1, 16);			Packet pckt = new Packet(ba.Length);			pckt.outPayload = ba;			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing file request response.		/// The found variable indicates whether or not we have the file.		/// </summary>		public static void FileRequestAnswer(int sockNum, bool found, byte[] md4, ref string filename)		{			byte[] ba;			if(!found)			{				ba = new byte[17];				ba[0] = 0x48;				Array.Copy(md4, 0, ba, 1, 16);			}			else			{				ba = new byte[17+2+filename.Length];				ba[0] = 0x59;				Array.Copy(md4, 0, ba, 1, 16);				ushort flen = (ushort)filename.Length;				Array.Copy(Endian.GetBytes(flen, false), 0, ba, 17, 2);				Array.Copy(System.Text.Encoding.ASCII.GetBytes(filename), 0, ba, 19, flen);			}			Packet pckt = new Packet(ba.Length);			pckt.outPayload = ba;			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing file status request.		/// </summary>		public static void FileStatusRequest(byte[] fileHash, int sockNum)		{			byte[] ba = new byte[17];			ba[0] = 0x4F;			Array.Copy(fileHash, 0, ba, 1, 16);			Packet pckt = new Packet(ba.Length);			pckt.outPayload = ba;			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing response to a file status packet.		/// </summary>		public static void FileStatusResponse(int sockNum)		{			//we don't support partial file uploading yet			MemoryStream ms = new MemoryStream();			ms.WriteByte(0x50);			ms.Write(lastRequestedFO.md4, 0, 16);			ms.Write(Endian.GetBytes((ushort)0, false), 0 , 2);			int bufSize = (int)ms.Position;			ms.Close();			Packet pckt = new Packet(bufSize);			pckt.outPayload = ms.GetBuffer();			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing request for part hashes.		/// </summary>		public static void HashSetRequest(byte[] fileHash, int sockNum)		{			byte[] ba = new byte[17];			ba[0] = 0x51;			Array.Copy(fileHash, 0, ba, 1, 16);			Packet pckt = new Packet(ba.Length);			pckt.outPayload = ba;			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing response to a hash set request packet.		/// </summary>		public static void HashSetResponse(int sockNum)		{			//we don't support partial file uploading yet			MemoryStream ms = new MemoryStream();			ms.WriteByte(0x52);			ms.Write(lastRequestedFO.md4, 0, 16);			ms.Write(Endian.GetBytes((ushort)0, false), 0 , 2);			int bufSize = (int)ms.Position;			ms.Close();			Packet pckt = new Packet(bufSize);			pckt.outPayload = ms.GetBuffer();			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing request for a download slot.		/// </summary>		public static void SlotRequest(int sockNum)		{			Packet pckt = new Packet(1);			pckt.outPayload = new byte[1] {0x54};			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing open slot is assigned.		/// </summary>		public static void SlotGiven(int sockNum)		{			Packet pckt = new Packet(1);			pckt.outPayload = new byte[1] {0x55};			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing release for a download slot.		/// </summary>		public static void SlotRelease(int sockNum)		{			Packet pckt = new Packet(1);			pckt.outPayload = new byte[1] {0x56};			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing request for file parts.		/// </summary>		public static void RequestParts(byte[] fileHash, int sockNum, uint start, uint stop)		{			//System.Diagnostics.Debug.WriteLine("REQUESTING OUR ed2k file");			byte[] msg = new byte[41];			msg[0] = 0x47;			Array.Copy(fileHash, 0, msg, 1, 16);			Array.Copy(Endian.GetBytes(start, false), 0, msg, 17, 4);			Array.Copy(Endian.GetBytes(0, false), 0, msg, 21, 4);			Array.Copy(Endian.GetBytes(0, false), 0, msg, 25, 4);			Array.Copy(Endian.GetBytes(stop, false), 0, msg, 29, 4);			Array.Copy(Endian.GetBytes(0, false), 0, msg, 33, 4);			Array.Copy(Endian.GetBytes(0, false), 0, msg, 37, 4);			Packet pckt = new Packet(41);			pckt.outPayload = msg;			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing file transfer.		/// </summary>		public static void SendingPart(byte[] md4hash, uint range1, uint range2, byte[] data, int sockNum)		{			if(range2 - range1 != data.Length)				System.Diagnostics.Debug.WriteLine("ed2k SendingPart size discrepancy");			byte[] wholepacket = new byte[1+16+8+data.Length];			wholepacket[0] = 0x46;			Array.Copy(md4hash, 0, wholepacket, 1, 16);			Array.Copy(Endian.GetBytes(range1, false), 0, wholepacket, 17, 4);			Array.Copy(Endian.GetBytes(range2, false), 0, wholepacket, 21, 4);			Array.Copy(data, 0, wholepacket, 25, data.Length);			Packet pckt = new Packet(wholepacket.Length);			pckt.outPayload = wholepacket;			Sck.scks[sockNum].SendPacket(pckt);			while(Sck.scks[sockNum].sendQueue.Count > 0)				System.Threading.Thread.Sleep(20);		}		/// <summary>		/// Outgoing info that we're done with the download.		/// </summary>		public static void EndOfDownload(byte[] fileHash, int sockNum)		{			byte[] ba = new byte[17];			ba[0] = 0x49;			Array.Copy(fileHash, 0, ba, 1, 16);			Packet pckt = new Packet(ba.Length);			pckt.outPayload = ba;			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing response to a view files request.		/// </summary>		public static void SendClientFiles(int sockNum)		{			MemoryStream ms = new MemoryStream();			ms.WriteByte(0x4B);			Routines.FileInfoList(ms);						int bufSize = (int)ms.Position;			ms.Close();			Packet pckt = new Packet(bufSize);			pckt.outPayload = ms.GetBuffer();			Sck.scks[sockNum].SendPacket(pckt);		}		/// <summary>		/// Outgoing udp request to see how our queued item is doing.		/// </summary>		public static void QueueStateCheck(Downloader elDLer)		{			byte[] msg = new byte[1+4+1+16];			msg[0] = 0xE3;			Array.Copy(Endian.GetBytes(17, false), 0, msg, 1, 4);			msg[5] = 0x90;			Array.Copy(elDLer.qho.md4sum, 0, msg, 6, 16);			//method:			//setup the header on that crap and send it udp-wise like in the udp-based find sources			//but what is the port?			//eMule finds the port by MuleInfoPacket in BaseClient			//also, it sets the port when receiving udp messages, obviously		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -