Exemplo n.º 1
0
        public FileInfoMessage(byte[] buffer)
        {
            type = P2PMessageType.FILE_INFO;
            var index_now = 4;
            count = BitConverter.ToInt32(buffer, 0);
            locateInfos = new FileLocateInfo[count];
            int l_count = 0;
            while(index_now < buffer.Length)
            {
                var locate = new byte[4];
                Array.Copy(buffer, 0 + index_now, locate, 0, 4);
                index_now += 4;
                var countOfIndexs = 0;
                countOfIndexs = BitConverter.ToInt32(buffer, index_now);
                index_now += 4;
                var indexs = new Int32[countOfIndexs];
                var i = 0;
                for(; i < countOfIndexs; ++i)
                {
                    indexs[i] = BitConverter.ToInt32(buffer, index_now);
                    index_now += 4;
                }
                var hash = new byte[32];
                _logger.Info($"i is {i}, countOfIndexs is {countOfIndexs}, index_now is {index_now}");
                Array.Copy(buffer, index_now, hash, 0, 32);

                index_now += 32;
                locateInfos[l_count] = new FileLocateInfo(locate, hash, countOfIndexs, indexs);
                l_count++;
            }
            length = index_now;
            Debug.Assert(index_now == buffer.Length);
        }
Exemplo n.º 2
0
        public static void Test(string[] args)
        {
            Int32[] temp = { 1, 2, 3, 4};
            var hash = SHA256.Create().ComputeHash(new byte[1]);
            GetDataMessage getData = new GetDataMessage(hash, temp);
            serializeAndWrite(getData);
            TestParse(getData);

            GetFileInfoMessage getFileInfo = new GetFileInfoMessage(hash);
            serializeAndWrite(getFileInfo);
            TestParse(getFileInfo);

            GetBlockInfoMessage getBlockInfo = new GetBlockInfoMessage(hash, 10);
            serializeAndWrite(getBlockInfo);
            TestParse(getBlockInfo);

            var block = new byte[4096];
            block[1] = 0; block[2] = 2;
            DataMessage data = new DataMessage(block, hash, 10);
            serializeAndWrite(data);
            TestParse(data);

            var error = new ErrorMessage(hash, 10);
            serializeAndWrite(error);
            TestParse(error);

            IPAddress a = IPAddress.Parse("192.168.1.190");
            int[] array = { 1, 3, 4 };
            FileLocateInfo fileLocateInfo = new FileLocateInfo(a, hash, array);
            FileLocateInfo[] fileLocateInfos = new FileLocateInfo[1];
            fileLocateInfos[0] = fileLocateInfo;
            var fileInfo = new FileInfoMessage(fileLocateInfos);
            serializeAndWrite(fileInfo);
            TestParse(fileInfo);

            BlockInfoMessage blockInfo = new BlockInfoMessage(hash, 10);
            serializeAndWrite(blockInfo);
            TestParse(blockInfo);

            OKMessage oK = new OKMessage(hash, 10);
            serializeAndWrite(oK);
            TestParse(oK);
        }
Exemplo n.º 3
0
        public void SendFileInfoMessage(List <LocateInfo> fileInfos, byte[] hash, TcpClient client)
        {
            FileLocateInfo[] locateInfos = new FileLocateInfo[fileInfos.Count];
            var count = 0;

            foreach (var i in fileInfos)
            {
                lock (i.Indexs)
                {
                    var            array = i.Indexs.ToArray();
                    FileLocateInfo info  = new FileLocateInfo(i.IP.GetAddressBytes(),
                                                              hash, array.Count(), array);
                    locateInfos[count++] = info;
                }
            }
            var FileInfoMessage = new FileInfoMessage(locateInfos);

            _logger.Info($"send FILE_INFO message, count is {count}");
            send(FileInfoMessage, client);
        }