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

📄 messages.cs

📁 P2P (peer to peer) file sharing program in C#. Supports Gnutella, Gnutella2, eDonkey, and OpenNap. w
💻 CS
📖 第 1 页 / 共 3 页
字号:
// Messages.cs// Copyright (C) 2002 Matt Zyzik (www.FileScope.com)// // This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.// // This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.// // You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USAusing System;using System.IO;using System.Text;namespace FileScope.EDonkey{	/// <summary>	/// This class has any reusable static routines for dealing with messages.	/// </summary>	public class Routines	{		public static void ClientInfo(MemoryStream ms)		{			//client hash			ms.Write(Stats.settings.myGUID, 0, 16);			//client id			ms.Write(Endian.GetBytes(Sck.clientID, false), 0, 4);			//port			ms.Write(Endian.GetBytes((ushort)Stats.settings.port, false), 0, 2);			//meta tag list			ms.Write(Endian.GetBytes((uint)3, false), 0, 4);			//meta 1 (nickname)			ms.WriteByte(0x02);			ms.Write(Endian.GetBytes((ushort)1, false), 0, 2);			ms.WriteByte(0x01);			byte[] bytesNick = Encoding.ASCII.GetBytes(Sck.nick);			ms.Write(Endian.GetBytes((ushort)bytesNick.Length, false), 0, 2);			ms.Write(bytesNick, 0, bytesNick.Length);			//meta 2 (version)			ms.WriteByte(0x03);			ms.Write(Endian.GetBytes((ushort)1, false), 0, 2);			ms.WriteByte(0x11);			ms.Write(Endian.GetBytes((uint)0x3c, false), 0, 4);			//meta 3 (port)			ms.WriteByte(0x03);			ms.Write(Endian.GetBytes((ushort)1, false), 0, 2);			ms.WriteByte(0x0F);			ms.Write(Endian.GetBytes((uint)Stats.settings.port, false), 0, 4);		}		public static void FileInfoList(MemoryStream ms)		{			int fileCount = Stats.fileList.Count;			ms.Write(Endian.GetBytes(fileCount, false), 0, 4);			for(int x = 0; x < fileCount; x++)			{				FileObject elFile = (FileObject)Stats.fileList[x];				//file hash				ms.Write(elFile.md4, 0, 16);				//client id				ms.Write(Endian.GetBytes(Sck.clientID, false), 0, 4);				//port				ms.Write(Endian.GetBytes((ushort)Stats.settings.port, false), 0, 2);				//meta stuff				ms.Write(Endian.GetBytes((uint)4, false), 0, 4);				string elFname = Path.GetFileName(elFile.location);				//meta 1				ms.WriteByte(0x02);				ms.Write(Endian.GetBytes((ushort)1, false), 0, 2);				ms.WriteByte(0x01);				byte[] bytesName = Encoding.ASCII.GetBytes(elFname);				ms.Write(Endian.GetBytes((ushort)bytesName.Length, false), 0, 2);				ms.Write(bytesName, 0, bytesName.Length);				//meta 2				ms.WriteByte(0x02);				ms.Write(Endian.GetBytes((ushort)1, false), 0, 2);				ms.WriteByte(0x03);				byte[] bytesType = Encoding.ASCII.GetBytes(QHOStuff.GetType(elFname));				ms.Write(Endian.GetBytes((ushort)bytesType.Length, false), 0, 2);				ms.Write(bytesType, 0, bytesType.Length);				//meta 3				ms.WriteByte(0x02);				ms.Write(Endian.GetBytes((ushort)1, false), 0, 2);				ms.WriteByte(0x04);				byte[] bytesFormat = Encoding.ASCII.GetBytes(QHOStuff.GetFormat(elFname));				ms.Write(Endian.GetBytes((ushort)bytesFormat.Length, false), 0, 2);				ms.Write(bytesFormat, 0, bytesFormat.Length);				//meta 4				ms.WriteByte(0x03);				ms.Write(Endian.GetBytes((ushort)1, false), 0, 2);				ms.WriteByte(0x02);				ms.Write(Endian.GetBytes(elFile.b, false), 0, 4);			}		}	}	/// <summary>	/// This class contains all static functions for handling messages.	/// </summary>	public class Messages	{		//keep track of the last FileObject we deal with, as most following messages deal with the same one		public static FileObject lastRequestedFO;		/// <summary>		/// Incoming id change message.		/// </summary>		public static void IDChange(Packet pckt, int sockNum)		{			uint newid = Endian.ToUInt32(pckt.payload, 1, false);			Sck.clientID = newid;			GUIBridge.EMessage(sockNum, "ID: " + newid.ToString());		}		/// <summary>		/// Incoming server string-based message.		/// </summary>		public static void ServerMessage(Packet pckt, int sockNum)		{			ushort len = Endian.ToUInt16(pckt.payload, 1, false);			GUIBridge.EMessage(sockNum, Encoding.ASCII.GetString(pckt.payload, 3, len));		}		/// <summary>		/// Incoming list of other eDonkey servers.		/// </summary>		public static void ServerList(Packet pckt, int sockNum)		{			int numServers = pckt.payload[1];			for(int x = 0; x < numServers; x++)			{				int offset = 2 + (x * 6);				string server = Endian.BigEndianIP(pckt.payload, offset) + ":" + Endian.ToUInt16(pckt.payload, offset+4, false).ToString();				if(!IPfilter.Private(server))					GUIBridge.ENewServer(ref server);			}		}		/// <summary>		/// Incoming info about the server.		/// </summary>		public static void ServerInfoData(Packet pckt, int sockNum)		{			uint metaCount = Endian.ToUInt32(pckt.payload, 23, false);			int offset = 27;			string name = "";			string desc = "";			for(int x = 0; x < metaCount; x++)			{				if(pckt.payload[offset] == 0x02)				{					offset++;					ushort len = Endian.ToUInt16(pckt.payload, offset, false);					offset += 2;					if(len == 1)					{						byte specialTag = pckt.payload[offset];						offset++;						ushort strLen = Endian.ToUInt16(pckt.payload, offset, false);						offset += 2;						switch(specialTag)						{							case 0x01:								name = Encoding.ASCII.GetString(pckt.payload, offset, strLen);								offset += strLen;								break;							case 0x0B:								desc = Encoding.ASCII.GetString(pckt.payload, offset, strLen);								offset += strLen;								break;							default:								System.Diagnostics.Debug.WriteLine("ServerInfoData unexpected 3: " + specialTag.ToString());								return;						}					}					else					{						System.Diagnostics.Debug.WriteLine("ServerInfoData unexpected 2");						return;					}				}				else				{					System.Diagnostics.Debug.WriteLine("ServerInfoData unexpected 1 " + pckt.payload[offset].ToString());					return;				}			}			//update gui			if(name.Length > 0 && desc.Length > 0)			{				string server = Sck.scks[sockNum].address + ":" + Sck.scks[sockNum].port.ToString();				Stats.EDonkeyServerInfo edsi = (Stats.EDonkeyServerInfo)Stats.edonkeyHosts[server];				edsi.servName = name;				edsi.servDesc = desc;				GUIBridge.EUpdateStats(sockNum);			}		}		/// <summary>		/// Incoming info on the number of files and users available on the server.		/// </summary>		public static void ServerStatus(Packet pckt, int sockNum)		{			uint users = Endian.ToUInt32(pckt.payload, 1, false);			uint files = Endian.ToUInt32(pckt.payload, 5, false);			//make the update			string server = Sck.scks[sockNum].address + ":" + Sck.scks[sockNum].port.ToString();			Stats.EDonkeyServerInfo edsi = (Stats.EDonkeyServerInfo)Stats.edonkeyHosts[server];			edsi.curUsers = users;			edsi.curFiles = files;			//inform gui			GUIBridge.EUpdateStats(sockNum);		}		/// <summary>		/// Incoming search results for the file we searched for.		/// </summary>		public static void SearchResults(Packet pckt, int sockNum)		{			int offset = 1;			uint count = Endian.ToUInt32(pckt.payload, offset, false);			offset += 4;			for(uint x = 0; x < count; x++)			{				byte[] bytesHash = new byte[16];				//uint clientID = 0;				ushort port = 0;				string name = "";				uint size = 0;				uint sources = 0;				string type = "";				uint lastseencomplete = 0;				uint priority = 0;				uint bitrate = 0;				uint ulpriority = 0;				string format = "";				string codec = "";				string length = "";				string artist = "";				string album = "";				string title = "";				Array.Copy(pckt.payload, offset, bytesHash, 0, 16);				offset += 16;				//clientID = Endian.ToUInt32(pckt.payload, offset, false);				int clientIDoffset = offset;				offset += 4;				port = Endian.ToUInt16(pckt.payload, offset, false);				offset += 2;				uint metaCount = Endian.ToUInt32(pckt.payload, offset, false);				offset += 4;				for(uint y = 0; y < metaCount; y++)				{					if(pckt.payload[offset] == 0x02)					{						offset++;						ushort len = Endian.ToUInt16(pckt.payload, offset, false);						offset += 2;						if(len == 1)						{							byte specialTag = pckt.payload[offset];							offset++;							ushort strLen = Endian.ToUInt16(pckt.payload, offset, false);							offset += 2;							switch(specialTag)							{								case 0x01:									name = Encoding.ASCII.GetString(pckt.payload, offset, strLen);									offset += strLen;									//System.Diagnostics.Debug.WriteLine("name: " + name.ToString());									break;								case 0x03:									type = Encoding.ASCII.GetString(pckt.payload, offset, strLen);									offset += strLen;									//System.Diagnostics.Debug.WriteLine("type: " + type.ToString());									break;								case 0x04:									format = Encoding.ASCII.GetString(pckt.payload, offset, strLen);									offset += strLen;									//System.Diagnostics.Debug.WriteLine("format: " + format.ToString());									break;								default:									System.Diagnostics.Debug.WriteLine("SearchResults unexpected 3: " + specialTag.ToString());									offset += strLen;									break;							}						}						else						{							string stuff = Encoding.ASCII.GetString(pckt.payload, offset, len);							offset += len;							ushort strLen = Endian.ToUInt16(pckt.payload, offset, false);							offset += 2;							if(stuff.ToLower().IndexOf("codec") != -1)							{								codec = Encoding.ASCII.GetString(pckt.payload, offset, strLen);								offset += strLen;								//System.Diagnostics.Debug.WriteLine("codec: " + codec);							}							else if(stuff.ToLower().IndexOf("length") != -1)							{								length = Encoding.ASCII.GetString(pckt.payload, offset, strLen);								offset += strLen;								//System.Diagnostics.Debug.WriteLine("length: " + length);							}							else if(stuff.ToLower().IndexOf("artist") != -1)							{								artist = Encoding.ASCII.GetString(pckt.payload, offset, strLen);								offset += strLen;								//System.Diagnostics.Debug.WriteLine("artist: " + artist);							}							else if(stuff.ToLower().IndexOf("album") != -1)							{								album = Encoding.ASCII.GetString(pckt.payload, offset, strLen);								offset += strLen;								//System.Diagnostics.Debug.WriteLine("album: " + album);							}							else if(stuff.ToLower().IndexOf("title") != -1)							{								title = Encoding.ASCII.GetString(pckt.payload, offset, strLen);								offset += strLen;								//System.Diagnostics.Debug.WriteLine("title: " + title);							}							else							{								System.Diagnostics.Debug.WriteLine("(harmless) SearchResults unknown metaname1: " + stuff);								offset += strLen;							}						}					}					else if(pckt.payload[offset] == 0x03)					{						offset++;						ushort len = Endian.ToUInt16(pckt.payload, offset, false);						offset += 2;						if(len == 1)						{							byte specialTag = pckt.payload[offset];							offset++;							switch(specialTag)							{								case 0x02:									size = Endian.ToUInt32(pckt.payload, offset, false);									offset += 4;									//System.Diagnostics.Debug.WriteLine("size: " + size.ToString());									break;								case 0x05:									lastseencomplete = Endian.ToUInt32(pckt.payload, offset, false);									offset += 4;									//System.Diagnostics.Debug.WriteLine("lastseencomplete: " + lastseencomplete.ToString());									break;								case 0x13:									priority = Endian.ToUInt32(pckt.payload, offset, false);									offset += 4;									//System.Diagnostics.Debug.WriteLine("priority: " + priority.ToString());									break;								case 0x15:									sources = Endian.ToUInt32(pckt.payload, offset, false);									offset += 4;									//System.Diagnostics.Debug.WriteLine("sources: " + sources.ToString());									break;								case 0x17:									ulpriority = Endian.ToUInt32(pckt.payload, offset, false);									offset += 4;									//System.Diagnostics.Debug.WriteLine("ulpriority: " + ulpriority.ToString());									break;								default:									System.Diagnostics.Debug.WriteLine("(harmless) SearchResults unexpected 5: " + specialTag.ToString());									offset += 4;									break;							}						}						else						{							string stuff = Encoding.ASCII.GetString(pckt.payload, offset, len);							offset += len;							if(stuff.ToLower().IndexOf("bitrate") != -1)							{								bitrate = Endian.ToUInt32(pckt.payload, offset, false);								offset += 4;								//System.Diagnostics.Debug.WriteLine("bitrate: " + bitrate.ToString());							}							else							{								System.Diagnostics.Debug.WriteLine("SearchResults unknown metaname2: " + stuff);								offset += 4;							}						}					}					else					{						System.Diagnostics.Debug.WriteLine("SearchResults unexpected 1: " + pckt.payload[offset].ToString());						offset++;						ushort len = Endian.ToUInt16(pckt.payload, offset, false);						offset += 2;						System.Diagnostics.Debug.WriteLine("the length: " + len.ToString());						System.Diagnostics.Debug.WriteLine("what: " + Encoding.ASCII.GetString(pckt.payload, offset, len));						offset += len;						return;					}

⌨️ 快捷键说明

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