Пример #1
0
        static void commandHandler(Socket socket)
        {
            var ctx = new Context();

            if (socket == null)
            {
                ctx.InitPipeServer(Fd.FromPosixFd(0), Fd.FromPosixFd(1));
            }
            else
            {
                ctx.InitSocketServer(socket, SocketServerFlags.Accepted);
            }
            ctx.RegisterCommand("FOO", foo);
            ctx.RegisterCommand("BAR", bar);
            ctx.RegisterCommand("INPUT", null);
            ctx.RegisterCommand("OUTPUT", null);

            for (; ;)
            {
                try {
                    ctx.Accept();
                }
                catch (ErrorException ex) when(ex.Error.Code == ErrorCode.EOF)
                {
                    break;
                }
                try {
                    ctx.Process();
                }
                catch (Exception ex) {
                    Console.Error.WriteLine("Processing failed: {}", ex.Message);
                    continue;
                }
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                if (RuntimeInformation.ProcessArchitecture != Architecture.X86)
                {
                    throw new NotSupportedException("Must run as 32-bit process");
                }
                var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
                foreach (var dir in new[] { @"Gpg4win\bin", @"GnuPG\bin" })
                {
                    var path = Path.Combine(programFiles, dir);
                    AddDllDirectory(path);
                }
            }

            Runtime.Init();

            if (args.Length > 0)
            {
                // if an argument is given, create a unix socket file
                using (var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified)) {
                    // FIXME: we should be deleting the socket file when the program exits, not when it starts
                    if (File.Exists(args[0]))
                    {
                        File.Delete(args[0]);
                    }
                    var address = Socket.CreateSocketAddress(args[0], out var redirected);
                    socket.Bind(address);
                    socket.Listen(0);
                    commandHandler(socket.Accept());
                }
            }
            else
            {
                // when no command line args, use stdin/stdout pipes
                commandHandler(null);
            }
        }