Esempio n. 1
0
        private static void HandleGet(string pth, BinaryWriter writer)
        {
            var size = (int)new FileInfo(pth).Length;

            using (var fstream = new BinaryReader(File.OpenRead(pth)))
            {
                RespProtocol.WriteBulkString(writer, fstream, size);
            }
        }
Esempio n. 2
0
        static void HandleSocket(Socket sock)
        {
            var networkStream = new BufferedStream(new NetworkStream(sock));
            var writer        = new BinaryWriter(networkStream);
            var reader        = new BinaryReader(networkStream);

            var pth = TempFileUtil.GetTempFileName();

            void copyBulkStreamToNewFile(string fname)
            {
                using (var f = new BinaryWriter(File.OpenWrite(pth)))
                {
                    RespProtocol.ReadBulkString(reader, f);
                }
            }

            while (true)
            {
                // main command - handle loop
                var cmd      = RespProtocol.ReadSimpleString(reader);
                var cmdParts = cmd.Split(new char[] { ' ' }, 2);
                Console.WriteLine($"Command: {cmd}");
                // 1: run simple script
                if (cmd == "run")
                {
                    copyBulkStreamToNewFile(pth);
                    HandleStoredFile(pth, writer);
                    writer.Close();
                    break;
                }
                if (cmd == "ziprun")
                {
                    copyBulkStreamToNewFile(pth);
                    HandleZipFile(pth, writer);
                    writer.Close();
                    break;
                }
                if (cmdParts[0] == "save")
                {
                    copyBulkStreamToNewFile(cmdParts[1]);
                    writer.Close();
                    break;
                }

                if (cmdParts[0] == "get")
                {
                    HandleGet(cmdParts[1], writer);
                    writer.Close();
                    break;
                }

                Console.WriteLine($"Unknown command: {cmd}");
            }
        }