﻿namespace Interpreter.Vm
{
    internal static class CoreFunctions
    {
        public static object[] NamedPipeCreate(string name)
        {
            System.IO.Pipes.NamedPipeClientStream pipe = new System.IO.Pipes.NamedPipeClientStream(name);
            pipe.Connect();
            return new object[] {
                pipe,
                new System.IO.StreamReader(pipe),
                new System.IO.StreamWriter(pipe),
            };
        }

        private static System.IO.StreamWriter GetStreamWriter(object pipeWrapper)
        {
            return (System.IO.StreamWriter)((object[])pipeWrapper)[2];
        }

        public static string NamedPipeWriteLine(object pipeWrapper, string value)
        {
            GetStreamWriter(pipeWrapper).WriteLine(value);
            return null;
        }

        public static string NamedPipeFlush(object pipeWrapper)
        {
            GetStreamWriter(pipeWrapper).Flush();
            return null;
        }
    }
}

﻿namespace Interpreter.Vm
{
    internal static class CoreFunctions
    {
        public static object[] NamedPipeCreate(string name)
        {
            System.IO.Pipes.NamedPipeClientStream pipe = new System.IO.Pipes.NamedPipeClientStream(name);
            pipe.Connect();
            return new object[] {
                pipe,
                new System.IO.StreamReader(pipe),
                new System.IO.StreamWriter(pipe),
            };
        }

        private static System.IO.StreamWriter GetStreamWriter(object pipeWrapper)
        {
            return (System.IO.StreamWriter)((object[])pipeWrapper)[2];
        }

        private static string NamedPipeConvertErrorMessage(string dotNetError)
        {
            if (dotNetError.StartsWith("Pipe is broken")) return "Pipe is broken.";

            return "An unknown error has occurred.";
        }

        public static string NamedPipeWriteLine(object pipeWrapper, string value)
        {
            try
            {
                GetStreamWriter(pipeWrapper).WriteLine(value);
            }
            catch (System.IO.IOException ioe)
            {
                return NamedPipeConvertErrorMessage(ioe.Message);
            }
            return null;
        }

        public static string NamedPipeFlush(object pipeWrapper)
        {
            try
            {
                GetStreamWriter(pipeWrapper).Flush();
            }
            catch (System.IO.IOException ioe)
            {
                return NamedPipeConvertErrorMessage(ioe.Message);
            }
            return null;
        }
    }
}

Make sure .NET error messages for NamedPipes are passed through a function to control consistency.
Make sure .NET error messages for NamedPipes are passed through a function to control consistency.
