Exemplo n.º 1
0
 protected override void _processCmd(List <byte> data)
 {
     if (data.Count >= 4)
     {
         uint cmd = BitConverter.ToUInt32(data.GetRange(0, 4).ToArray(), 0);
         if (cmd == RemoteFile.Constants.RMF_CMD_FILE_INFO)
         {
             RemoteFile.File remoteFile = RemoteFileUtil.unPackFileInfo(data);
             for (int i = 0; i < requestedFiles.Count; i++)
             {
                 if (requestedFiles[i].name == remoteFile.name)
                 {
                     requestedFiles[i].address    = remoteFile.address;
                     requestedFiles[i].fileType   = remoteFile.fileType;
                     requestedFiles[i].digestType = remoteFile.digestType;
                     requestedFiles[i].digestData = remoteFile.digestData;
                     requestedFiles[i].open();
                     remoteFileMap.insert(requestedFiles[i]);
                     Msg msg = new Msg(RemoteFile.Constants.RMF_MSG_FILEOPEN, 0, 0, requestedFiles[i].address);
                     requestedFiles.RemoveAt(i);
                     msgQueue.Add(msg);
                 }
                 else
                 {
                     remoteFileMap.insert(remoteFile);
                 }
             }
         }
         else if (cmd == RemoteFile.Constants.RMF_CMD_FILE_OPEN)
         {
             uint address = RemoteFileUtil.unPackFileOpen(data, "<");
             Console.WriteLine("received open command, address: " + address);
             Apx.File file = localFileMap.findByAddress(address);
             if (file != null)
             {
                 Console.WriteLine("received open command, name: " + file.name);
                 file.open();
                 List <byte> fileContent = file.read(0, (int)file.length);
                 if (fileContent.Count > 0)
                 {
                     Msg msg = new Msg(RemoteFile.Constants.RMF_MSG_WRITE_DATA, file.address, 0, fileContent);
                     msgQueue.Add(msg);
                 }
             }
         }
         else if (cmd == RemoteFile.Constants.RMF_CMD_FILE_CLOSE)
         {
             throw new NotImplementedException();
         }
         else
         {
             throw new ArgumentException("Unknown command, cannot process");
         }
     }
     else
     {
         throw new ArgumentException("too short command to proccess");
     }
 }
Exemplo n.º 2
0
        public void Test_unPackFileInfo()
        {
            // Header content
            List <byte> data = new List <byte> {
                0x03, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };
            // Digest data
            List <byte> ddata = Enumerable.Repeat((byte)0, 32).ToList();

            data.AddRange(ddata);
            // Name
            List <byte> ndata = ASCIIEncoding.ASCII.GetBytes("test.txt").ToList();

            data.AddRange(ndata);
            // Null termination
            data.Add(0);
            RemoteFile.File file = RemoteFileUtil.unPackFileInfo(data, "<");

            Assert.AreEqual(file.address, (uint)10000);
            Assert.IsTrue(file.digestData.SequenceEqual(ddata));
            Assert.AreEqual(file.digestType, RemoteFile.Constants.RMF_DIGEST_TYPE_NONE);
            Assert.AreEqual(file.name, "test.txt");
        }