コード例 #1
0
ファイル: Instance.cs プロジェクト: drewomix/fivem-1
        private void StartIPC()
        {
            Task.Run(async() =>
            {
                // TODO: cleaner way?
                var nngFactory = new nng.Tests.TestFactory();

                while (!m_hadPipe)
                {
                    try
                    {
                        if (m_process == null)
                        {
                            return;
                        }

                        var pid = m_process.Id;

                        // c# isnt f# or rust
                        // why is this api trying to be both at once
                        var pairResult = nngFactory
                                         .PairOpen()
                                         .ThenListen($"ipc:///tmp/fxs_instance_{pid}");

                        if (pairResult.TryError(out var error))
                        {
                            await Task.Delay(500);
                            continue;
                        }

                        m_pair = pairResult.Unwrap();

                        m_peer = new PipePeer(m_pair);
                        m_peer.Start();

                        m_peer.CommandReceived += async cmd =>
                        {
                            await HandleCommand(cmd);
                        };

                        m_peer.TargetUnreachable += () =>
                        {
                            m_pair = null;
                        };

                        m_hadPipe = true;

                        while (m_pair != null && m_process != null && pid == m_process.Id)
                        {
                            await Task.Delay(500);
                        }

                        m_peer.Stop();
                    }
                    catch (Exception e) { Debug.WriteLine(e.ToString()); }
                }
            });
        }