public static bool WriteFile(this MiraConnection p_Connection, string p_Path, byte[] p_Data)
        {
            if (!p_Connection.Connected)
            {
                return(false);
            }

            var s_Descriptor = p_Connection.Open(p_Path, 0x0002 | 0x0200 /* O_RDWR | O_CREAT*/, 0777);

            if (s_Descriptor < 0)
            {
                return(false);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_WriteFile,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferWriteFile>()
            }, p_Connection.SerializeObject(new FileTransferWriteFile
            {
                Handle = s_Descriptor,
                Size   = (ulong)p_Data.Length
            }));

            var s_BlockCount = p_Data.Length / 0x4000;
            var s_Leftover   = p_Data.Length % 0x4000;

            using (var s_Reader = new BinaryReader(new MemoryStream(p_Data)))
            {
                using (var s_Writer = new BinaryWriter(p_Connection.GetStream(), Encoding.ASCII, true))
                {
                    for (int i = 0; i < s_BlockCount; ++i)
                    {
                        s_Writer.Write(s_Reader.ReadBytes(0x4000));
                    }

                    s_Writer.Write(s_Reader.ReadBytes((int)s_Leftover));
                }
            }

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            p_Connection.Close(s_Descriptor);

            return(s_Result.ErrorType == 0);
        }
Exemplo n.º 2
0
        public void TestFileTransfer()
        {
            // Verify the connection is valid
            Assert.IsNotNull(m_Connection);

            // Verify that the connection is actually connected
            Assert.IsTrue(m_Connection.Connected);

            var s_Flags = (int)MiraFileTransferExtensions.FileTransferFlags.O_RDONLY;

            var s_FileHandle = m_Connection.Open("/user", s_Flags, 0);

            Assert.AreNotEqual(-1, s_FileHandle);

            m_Connection.Close(s_FileHandle);
        }
        public static byte[] ReadFile(this MiraConnection p_Connection, string p_Path)
        {
            if (!p_Connection.Connected)
            {
                return(null);
            }

            int s_Descriptor = p_Connection.Open(p_Path, 0, 0);

            if (s_Descriptor < 0)
            {
                return(null);
            }

            var s_HeaderData = p_Connection.SerializeObject(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_ReadFile,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferReadFile>()
            });

            var s_RequestData = p_Connection.SerializeObject(new FileTransferReadFile
            {
                Handle = s_Descriptor,
                Size   = 0
            });

            using (var s_Writer = new BinaryWriter(p_Connection.GetStream(), Encoding.ASCII, true))
            {
                s_Writer.Write(s_HeaderData);
                s_Writer.Write(s_RequestData);
            }

            var s_Response = p_Connection.ReceiveObject <FileTransferReadFile>();

            var s_BlockCount  = s_Response.Size / 0x4000;
            var s_Leftover    = s_Response.Size % 0x4000;
            var s_CurrentSize = 0;

            byte[] s_Data;
            using (var s_Reader = new BinaryReader(p_Connection.GetStream(), Encoding.ASCII, true))
            {
                using (var s_Writer = new BinaryWriter(new MemoryStream()))
                {
                    for (ulong i = 0; i < s_BlockCount; ++i)
                    {
                        s_CurrentSize += 0x4000;
                        s_Writer.Write(s_Reader.ReadBytes(0x4000));
                    }


                    s_Writer.Write(s_Reader.ReadBytes((int)s_Leftover));

                    s_Data = ((MemoryStream)s_Writer.BaseStream).ToArray();
                }
            }

            // Close the handle
            p_Connection.Close(s_Descriptor);

            return(s_Data);
        }