예제 #1
0
        static void Main(string[] args)
        {
            bool end=false;

            while (!end)
            {
                using (System.IO.Pipes.NamedPipeServerStream pipeServer =
                    new System.IO.Pipes.NamedPipeServerStream("testpipe", System.IO.Pipes.PipeDirection.In))
                {
                    Console.WriteLine("NamedPipeServerStream object created.");

                    // Wait for a client to connect
                    Console.Write("Waiting for client connection...");
                    pipeServer.WaitForConnection();

                    Console.WriteLine("Client connected.");
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(pipeServer))
                    {
                        // Display the read text to the console
                        string temp;
                        while ((temp = sr.ReadLine()) != null)
                        {
                            Console.WriteLine("Received from client: {0}", temp);
                        }
                    }
                }
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Waiting for connection on named pipe mypipe");

            while (true)
            {
                System.IO.Pipes.NamedPipeServerStream namedPipeServerStream = new System.IO.Pipes.NamedPipeServerStream("mypipe");

                namedPipeServerStream.WaitForConnection();

                byte[] buffer = new byte[255];

                namedPipeServerStream.Read(buffer, 0, 255);

                string request = ASCIIEncoding.ASCII.GetString(buffer);

                Console.WriteLine(request);

                request = request.Trim('\0');

                if (request.ToLower() == "close")
                    break;

                namedPipeServerStream.Close();
            }
        }
예제 #3
0
파일: Program.cs 프로젝트: chanket/Worker
        protected static void StartScreenshot(string[] args)
        {
            if (args.Length < 1)
            {
                return;
            }

            string pipeName = args[0];

            using (System.IO.Pipes.NamedPipeServerStream pipeStream = new System.IO.Pipes.NamedPipeServerStream(pipeName))
            {
                try
                {
                    pipeStream.WaitForConnection();
                    if (pipeStream.IsConnected)
                    {
                        int    iWidth  = Screen.PrimaryScreen.Bounds.Width;
                        int    iHeight = Screen.PrimaryScreen.Bounds.Height;
                        Bitmap img     = new Bitmap(iWidth, iHeight);

                        Graphics gc = Graphics.FromImage(img);
                        gc.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
                        img.Save(pipeStream, ImageFormat.Jpeg);

                        pipeStream.WaitForPipeDrain();
                    }
                }
                catch { }
            }
        }
예제 #4
0
파일: Program.cs 프로젝트: chanket/Worker
        protected static void StartCamera(string[] args)
        {
            if (args.Length < 1)
            {
                return;
            }

            int    timeout  = 5000;
            string pipeName = args[0];

            if (args.Length >= 2)
            {
                int.TryParse(args[1], out timeout);
            }
            using (System.IO.Pipes.NamedPipeServerStream pipeStream = new System.IO.Pipes.NamedPipeServerStream(pipeName))
            {
                try
                {
                    pipeStream.WaitForConnection();
                    if (pipeStream.IsConnected)
                    {
                        Camera        camera = new Camera();
                        Task <Bitmap> task   = camera.CaptureImageAsync(timeout); //直接Wait这个task会产生死锁,因此异步开始、在后面循环判断执行结果。

                        while (task.Status != TaskStatus.RanToCompletion)
                        {
                            switch (task.Status)
                            {
                            case TaskStatus.Faulted: throw task.Exception;

                            case TaskStatus.Canceled: throw new TaskCanceledException();

                            default:
                            {
                                Thread.Sleep(1);
                                Application.DoEvents();
                            }
                            break;
                            }
                        }

                        Bitmap img = task.Result;
                        img.Save(pipeStream, ImageFormat.Jpeg);

                        pipeStream.WaitForPipeDrain();
                    }
                }
                catch { }
            }
        }
        public static ServerContext LaunchServer(string pipeName, string command, string[] args)
        {
            var serverPipe =
                new System.IO.Pipes.NamedPipeServerStream(
                    pipeName, System.IO.Pipes.PipeDirection.InOut);

            var startInfo = new System.Diagnostics.ProcessStartInfo(command);

            startInfo.Arguments = String.Join(" ", args);
            var childProc = System.Diagnostics.Process.Start(startInfo);

            serverPipe.WaitForConnection();

            return(new ServerContext(serverPipe, childProc));
        }
예제 #6
0
        public static void ipc()
        {
            // https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-use-named-pipes-for-network-interprocess-communication
            System.IO.Pipes.NamedPipeServerStream pipeServer =
                new System.IO.Pipes.NamedPipeServerStream("testpipe"
                                                          , System.IO.Pipes.PipeDirection.InOut, 1);

            // Wait for a client to connect
            pipeServer.WaitForConnection();

            // pipeServer.Write(
            System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpen("lul", 2);

            // System.IO.MemoryMappedFiles
        }
예제 #7
0
파일: Form1.cs 프로젝트: maziweis/studydemo
        private void StartPIPE()
        {
            while (true)
            {
                try
                {
                    //打开pipe管道,当易课程序启动时通过管道控制页面跳转
                    System.IO.Pipes.NamedPipeServerStream _pipeServer =
                        new System.IO.Pipes.NamedPipeServerStream("KSEXE", System.IO.Pipes.PipeDirection.InOut, 2);
                    _pipeServer.WaitForConnection();
                    StreamReader sr      = new StreamReader(_pipeServer);
                    string       recData = sr.ReadLine();
                    if (recData != string.Empty)
                    {
                        //MessageBox.Show("管道接收到的数据\n" + recData);
                        PIPEMod pm = new Helper().Deserialize <PIPEMod>(recData);
                        if (pm != null)
                        {
                            if (pm.FunName == "Load")
                            {
                                strArgs    = new string[1];
                                strArgs[0] = pm.FunData;//{"FunData":"{\"account\":\"mzw\",\"password\":\"CEDDDE06F9915A27\",\"webUrl\":\"http:\\/\\/192.168.3.187:6012\\/api\\/\",\"apiName\":\"Students\\/\",\"classID\":\"-1\",\"param\":\"AttLesson\\/Page\\/Teaching.aspx?BookID=707&UserID=49a92e48-1f39-4382-9273-0a0d521162c6&EditionID=18&SubjectID=3&GradeID=5&ClassID=-1&BookType=1&page=IndexPage&DXTP=true\",\"ClassName\":\"公共班级\",\"gradeName\":\"公共年级\",\"clsName\":\"公共班级\"}","FunName":"Load"}

                                wb.Load(ConfigurationManager.AppSettings["DefaultURL"] + "/DefaultW.aspx");
                            }
                            //从易课+客户端回调,返回学生ID,资源ID,题目下发编号,改变学生显示状态(点亮)
                            if (pm.FunName == "ResData")
                            {
                                wb.ExecuteScriptAsync("dianliangStu('" + pm.FunData + "');");
                            }
                        }
                    }
                    sr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
예제 #8
0
        private static void StartIpcPipe()
        {
            //todo: impl a set of IPC APIs to perform some cmds. eg.Pause/Resume, Quit...
            //todo: Temporay IPC mechanism.
            var synCtx     = new WindowsFormsSynchronizationContext();//note: won't work with `SynchronizationContext.Current`
            var pipeThread = new Thread(() =>
            {
                while (true)
                {
                    using (var server = new System.IO.Pipes.NamedPipeServerStream("WGestures_IPC_API"))
                    {
                        server.WaitForConnection();

                        Debug.WriteLine("Clien Connected");
                        using (var reader = new StreamReader(server))
                        {
                            var cmd = reader.ReadLine();
                            Debug.WriteLine("Pipe CMD=" + cmd);

                            if (cmd == "ShowSettings")
                            {
                                synCtx.Post((s) =>
                                {
                                    Debug.WriteLine("Thread=" + Thread.CurrentThread.ManagedThreadId);
                                    ShowSettings();
                                    //ToggleTrayIconVisibility();
                                }, null);
                            }
                        }
                    }
                }
            })
            {
                IsBackground = true
            };

            pipeThread.Start();
        }
예제 #9
0
        private static void StartIpcPipe()
        {
            //todo: impl a set of IPC APIs to perform some cmds. eg.Pause/Resume, Quit...
            //todo: Temporay IPC mechanism.
            var synCtx = new WindowsFormsSynchronizationContext();//note: won't work with `SynchronizationContext.Current`
            var pipeThread = new Thread(() =>
            {
                while (true)
                {
                    using (var server = new System.IO.Pipes.NamedPipeServerStream("WGestures_IPC_API"))
                    {
                        server.WaitForConnection();

                        Debug.WriteLine("Clien Connected");
                        using (var reader = new StreamReader(server))
                        {
                            var cmd = reader.ReadLine();
                            Debug.WriteLine("Pipe CMD=" + cmd);

                            if (cmd == "ShowSettings")
                            {
                                synCtx.Post((s) =>
                                {
                                    Debug.WriteLine("Thread=" + Thread.CurrentThread.ManagedThreadId);
                                    ShowSettings();
                                }, null);
                            }
                        }
                    }
                }
            }) { IsBackground = true };
            pipeThread.Start();
        }
예제 #10
0
        public static object NamedPipeServerCreate(VmContext vm, string name, Value startFn, Value onDataFn, Value closeFn)
        {
            System.IO.Pipes.NamedPipeServerStream namedPipe = new System.IO.Pipes.NamedPipeServerStream(name, System.IO.Pipes.PipeDirection.In);
            System.IO.StreamReader reader = new System.IO.StreamReader(namedPipe);
            object[] output = new object[] { namedPipe, reader, startFn, onDataFn, closeFn };

            System.ComponentModel.BackgroundWorker bgWorker = new System.ComponentModel.BackgroundWorker();
            bgWorker.DoWork += (sender, e) =>
            {
                try
                {
                    namedPipe.WaitForConnection();
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                    return;
                }

                // TODO: this is based on the MessageHub format and needs to be rewritten in a more generic way.
                // Sending individual characters is probably a bad idea performance-wise, and so some sort of
                // batching heuristic needs to be made. This "length@payload" format needs to go into the actual
                // MessageHub code directly.
                int stringLength = 0;
                System.Text.StringBuilder buffer = new System.Text.StringBuilder();
                bool isLengthParsing             = true;
                while (true)
                {
                    int b = namedPipe.ReadByte();
                    if (isLengthParsing)
                    {
                        if (b == '@')
                        {
                            stringLength = int.Parse(buffer.ToString());
                            buffer.Clear();
                            if (stringLength == 0)
                            {
                                TranslationHelper.GetEventLoop(vm).ExecuteFunctionPointerNativeArgs(onDataFn, new object[] { "" });
                            }
                            else
                            {
                                isLengthParsing = false;
                            }
                        }
                        else
                        {
                            buffer.Append((char)b);
                        }
                    }
                    else
                    {
                        buffer.Append((char)b);
                        stringLength--;
                        if (stringLength == 0)
                        {
                            TranslationHelper.GetEventLoop(vm).ExecuteFunctionPointerNativeArgs(onDataFn, new object[] { buffer.ToString() });
                            buffer.Clear();
                            isLengthParsing = true;
                        }
                    }
                }
            };
            bgWorker.RunWorkerAsync();

            return(output);
        }
예제 #11
0
        public void openConnection()
        {
            string  message = "";
            Boolean loop    = true;

            while (loop)
            {
                //set up the reciever pipe - this will listen for compute messages from the java plugin.
                using (System.IO.Pipes.NamedPipeServerStream _pipeServer = new System.IO.Pipes.NamedPipeServerStream(_recieverPipeName, System.IO.Pipes.PipeDirection.InOut, 5))
                {
                    Console.WriteLine("Named Pipe " + _recieverPipeName + " Started");
                    Console.WriteLine("Waiting for Client To Connect to " + _recieverPipeName);
                    //set up the broadcast pipe - this will send a message to the java plugin (preferably when the requested compute is complete)
                    using (System.IO.Pipes.NamedPipeServerStream _senderPipeServer = new System.IO.Pipes.NamedPipeServerStream(_senderPipeName, System.IO.Pipes.PipeDirection.InOut, 5))
                    {
                        //establish valid links for the recieving and sending pipes.
                        try
                        {
                            Console.WriteLine("Named Pipe " + _senderPipeName + " Started");
                            Console.WriteLine("Waiting for Client To Connect to " + _senderPipeName);
                            _pipeServer.WaitForConnection();
                            Console.WriteLine("Client Connected to " + _recieverPipeName);
                            _senderPipeServer.WaitForConnection();
                            Console.WriteLine("Client Connected to " + _senderPipeName);
                            //now that there are valid connections, establish stream readers and writers accordingly
                            StreamReader sr = new StreamReader(_pipeServer);
                            StreamWriter sw = new StreamWriter(_senderPipeServer);
                            sw.AutoFlush = true;
                            //this sr.ReadLine() will wait until a message has been sent from the java plugin.
                            message = sr.ReadLine();
                            //process the message
                            if (!Implementations.ParserFactory.ParserFactory.Instance.parse(message))
                            {
                                if (Implementations.ParserFactory.ParserFactory.Instance.isValid)
                                {
                                    sw.WriteLine("Compute Successful!");
                                }
                                else
                                {
                                    sw.WriteLine("Compute Failed! " + Implementations.ParserFactory.ParserFactory.Instance.message);
                                }
                            }

                            //ensure that the response
                            sw.WriteLine("Message Recieved: " + message);
                        }
                        catch (Exception ex)
                        {
                            try
                            {
                                _senderPipeServer.Close();
                            }
                            catch (Exception e1)
                            {
                            }
                            try
                            {
                                _pipeServer.Close();
                            }
                            catch (Exception e2)
                            {
                            }
                            Console.WriteLine(ex.InnerException);
                        }
                    }
                }
                if (message == "Exit")
                {
                    loop = false;
                }
            }
        }
예제 #12
0
파일: Editor.cs 프로젝트: scw000000/Engine
        public Editor()
        {
            InitializeComponent();
             this.WindowState = FormWindowState.Maximized;
             m_CurrentDirectory = Directory.GetCurrentDirectory();
             m_AssetsDirectory = m_CurrentDirectory + "\\Assets\\";

             m_SupportEditorPattern = new List<string>(){ "*.xml",
                                                "*.fragmentshader",
                                                "*.vertexshader",
                                                "*.lua"};
             for ( int i = 0; i < m_SupportEditorPattern.Count; i++)
            {
            m_SupportEditorPattern[i] = WildCardToRegular( m_SupportEditorPattern[i] );
            }

             // Setting all of splitter width in all comtainers
             int splitterWidth = 10;
             this.splitContainer1.SplitterWidth = splitterWidth;
             this.splitContainer2.SplitterWidth = splitterWidth;
             this.splitContainer_Left.SplitterWidth = splitterWidth;
             this.splitContainer_Mid.SplitterWidth = splitterWidth;
             this.splitContainer_Right.SplitterWidth = splitterWidth;

             int SDLWindowWidth = 1366;
             int SDLWindowHeight = 768;
             int lrPageWidth = ( this.splitContainer1.Width - SDLWindowWidth - 2 * splitterWidth - 8 ) / 2;
             this.splitContainer1.SplitterDistance = this.splitContainer1.Width - lrPageWidth - splitterWidth - 4;
             this.splitContainer2.SplitterDistance = this.splitContainer2.Width - SDLWindowWidth - splitterWidth;
             this.splitContainer_Mid.SplitterDistance = SDLWindowHeight;

             InitializeAssetTree();

             m_RedirectStringDelegate = new strDelegate( RedirectString );
             m_SetActorDataStringDelegate = new strDelegate( SetActorDataGrid );

             TextBoxWritter textBoxWritter = new TextBoxWritter( this.textBox_Console );
             Console.SetOut( textBoxWritter );

             Console.WriteLine( "Initializing output redirecting" );
             int id = System.Diagnostics.Process.GetCurrentProcess().Id; // make this instance unique
             m_ServerPipe = new System.IO.Pipes.NamedPipeServerStream( "consoleRedirect" + id, System.IO.Pipes.PipeDirection.In, 1 );
             m_ClientPipe = new System.IO.Pipes.NamedPipeClientStream( ".", "consoleRedirect" + id, System.IO.Pipes.PipeDirection.Out, System.IO.Pipes.PipeOptions.WriteThrough );
             m_ClientPipe.Connect();
             System.Runtime.InteropServices.HandleRef hr11 = new System.Runtime.InteropServices.HandleRef( m_ClientPipe, m_ClientPipe.SafePipeHandle.DangerousGetHandle() );
             SetStdHandle( -11, hr11.Handle ); // redirect stdout to my pipe
             SetStdHandle( -12, hr11.Handle ); // redirect error to my pipe
             Console.WriteLine( "Waiting for console connection" );
             m_ServerPipe.WaitForConnection(); //blocking
             Console.WriteLine( "Console connection complete." );

             m_DllRedirectThreadWorker = new DllRedirectThreadObject();
             m_DllRedirectThreadWorker.SetReader( m_ServerPipe );
             m_DllRedirectThread = new Thread( m_DllRedirectThreadWorker.Run );
             m_DllRedirectThread.Start();

             m_SDLThreadWorker = new SDLThreadObject( Handle, this.tabPageEX_World.Handle, this.splitContainer_Mid.Panel1.Width, this.splitContainer_Mid.Panel1.Height );
             m_SDLThread = new Thread( m_SDLThreadWorker.Run );
             m_SDLThread.Start();
        }