示例#1
0
		public static void SendHandshake(IO.Stream stream, ByteField20 infoDigest)
		{
			stream.WriteByte((byte)protocolString.Length);
			stream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(protocolString), 0, protocolString.Length);

			// 8 zeros
			stream.Write(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, 0, 8);

			// SHA digest
			stream.Write(infoDigest.Data, 0, infoDigest.Data.Length);
		}
示例#2
0
		public static void SendPeerId(IO.Stream netStream, ByteField20 peerId)
		{
			netStream.Write(peerId.Data, 0, peerId.Data.Length);
		}
示例#3
0
		public static void SendBitfieldMessage(IO.Stream stream, DownloadFile downloadFile)
		{
			int bitfieldLength = downloadFile.Bitfield.Count/8 + ((downloadFile.Bitfield.Count % 8) > 0 ? 1 : 0);
			byte[] bitfield = new byte[bitfieldLength];
			downloadFile.Bitfield.CopyTo(bitfield, 0);
			SendMessageHeader(stream, PeerMessage.Bitfield, bitfieldLength);
			stream.Write(bitfield, 0, bitfield.Length);
		}
示例#4
0
		// i would use IO.BinaryWriter but that caused problems
		private static void WriteInt(IO.Stream stream, int val)
		{
			byte[] b = new byte[4];
			b[3] = (byte)(val & 0x000000FF);
			b[2] = (byte)((val & 0x0000FF00) >> 8);
			b[1] = (byte)((val & 0x00FF0000) >> 16);
			b[0] = (byte)((val & 0xFF000000) >> 24);
			stream.Write(b, 0, b.Length);
//			stream.Write(System.BitConverter.GetBytes(val), 0, 4);
		}
示例#5
0
		/// <summary>
		/// Loads a piece from the torrent. The torrent piece must already have been downloaded (ie exists in the bitfield)
		/// </summary>
		/// <param name="pieceId">Piece index to load from</param>
		/// <param name="ostream">Stream to save to</param>
		/// <returns>True if the piece was loaded succesfully, false otherwise</returns>
		public bool LoadFromFile(int pieceId, IO.Stream ostream)
		{
			if (!this.piecesDownloaded.Get(pieceId))
				return false;
			else
			{
				int dataRead = 0;
				int positionInFile = 0;
				int fileNum = 0;
				
				WhichFileIsPieceIn(pieceId, out fileNum, out positionInFile);

				while (dataRead < this.infofile.GetPieceLength(pieceId) && fileNum < this.infofile.FileCount)
				{
					int fileLength = this.infofile.GetFileLength(fileNum);
					int dataToRead = System.Math.Min(fileLength - positionInFile, this.infofile.GetPieceLength(pieceId) - dataRead);

					IO.FileStream fstream = new IO.FileStream(this.infofile.GetFileName(fileNum), IO.FileMode.Open);

					// read data from the file
					fstream.Seek(positionInFile, IO.SeekOrigin.Begin);
					
					byte[] data = new byte[ dataToRead ];
					fstream.Read(data, 0, data.Length);
					ostream.Write(data, 0, data.Length);
					dataRead += dataToRead;

					fstream.Close();

					fileNum++; // move onto next file
					positionInFile = 0;
				}
				
				return true;
			}
		}
示例#6
0
		/// <summary>Reads data from the piece</summary>
		/// <param name="stream">Data to read into</param>
		/// <param name="length">Amount of data to read</param>
		/// <param name="poffset">Offset within the piece to read data from</param>
		public void Read(IO.Stream stream, int length, int poffset)
		{
			byte[] tdata = new byte[length];
			this.Read(tdata, 0, length, poffset);
			stream.Write(tdata, 0, length);
		}