Example #1
0
        protected override Response ProcessHttpReq(EndPoint endpoint,Hashtable headers, string body)
        {
            FileResponse response = new FileResponse(@"c:\devtemp\allfeed.xml");
            /*
            string s;

            using (FileStream fs = new FileStream(@"c:\devtemp\allfeed.xml", FileMode.Open, FileAccess.Read))
            using(StreamReader sr = new StreamReader(fs))
            {

                s = sr.ReadToEnd();
            }

            //string s = "DFKSFSLJL";

            StringResponse response = new StringResponse(s);
            */
            /*
            object value = headers["Range"];
            if (value == null)
            {
                return null;
            }
            string range = value.ToString();
            if(!string.IsNullOrEmpty(range))
            {

                string[] lr = range.Split(new string[] {"="}, StringSplitOptions.RemoveEmptyEntries);
                if(lr.Length == 2)
                {
                    string left = lr[0];
                    string right = lr[1];

                    string[] be = right.Split(new[] {"-"}, StringSplitOptions.RemoveEmptyEntries);

                    string begin = be[0];
                    string end = be[1];

                    long iBegin,iEnd;
                    if(long.TryParse(begin, out iBegin) && long.TryParse(end, out iEnd))
                    {
                        FileResponse response = new FileResponse(@"c:\devtemp\allfeed.xml");
                        response.HasRange = true;
                        response.RangeBegin = iBegin;
                        response.RangeEnd = iEnd;
                        return response;
                    }
                }
            }
            */

            return response;
        }
Example #2
0
File: CSServer.cs Project: vls/hp2p
        //public void TestGetHasFilBlocks()
        //{
        //    HPPClient.HashFullNameDict.Add("88", @"D:\test\abc.txt");
        //    HPPClient.HashDownloadDict.Add("88", new byte[] { 80, 80 });
        //    Console.WriteLine(GetHasFileBlocks("8"));
        //}
        /// <summary>
        /// 根据文件的hash值及要找的块的编号,得到一个FileResponse的对象
        /// </summary>
        /// <param name="fileHash">文件的hash值</param>
        /// <param name="blockNum">要找的哪一块</param>
        /// <returns></returns>
        protected FileResponse GetDownLoadFile(string fileHash,int blockNum)
        {
            if (!BitArrayHelper.IsHasBlock(HPPClient.DownloadJobDict[fileHash].Mine, blockNum))
            {
                Console.WriteLine("没有找到所需的块!");
                return null;
            }
            int lastBlockSize;
            int blockAmount;
            long fileLen = HPPClient.DownloadJobDict[fileHash].FileLen;
            if (fileLen % BLOCKSIZE == 0)
            {
                blockAmount = (int)fileLen / BLOCKSIZE;
                lastBlockSize = BLOCKSIZE;
            }
            else
            {
                blockAmount = (int)fileLen / BLOCKSIZE + 1;
                lastBlockSize = (int)fileLen % BLOCKSIZE;
            }

            string fileName = HPPClient.HashFullNameDict[fileHash];
            long beginPos;
            long endPos;
            if (blockNum != blockAmount)
            {
                beginPos = (blockNum - 1)*BLOCKSIZE;
                endPos = beginPos + BLOCKSIZE - 1;
            }
            else
            {
                if (lastBlockSize == BLOCKSIZE)
                {
                     beginPos = (blockNum - 1)*BLOCKSIZE;
                     endPos = beginPos + BLOCKSIZE - 1;
                }
                else
                {
                    beginPos = (blockNum - 1) * BLOCKSIZE;
                    endPos = beginPos + lastBlockSize - 1;
                }
            }

            FileResponse fileResponse = new FileResponse(fileName);
            fileResponse.HasRange = true;
            fileResponse.RangeBegin = beginPos;
            fileResponse.RangeEnd = endPos;
            return fileResponse;
        }