public void SendAndGetResponse()
        {
            "发送消息到另一个 IPC 服务,可以等待收到对方的返回值".Test(async() =>
            {
                var ipcAName         = Guid.NewGuid().ToString("N");
                var ipcBName         = Guid.NewGuid().ToString("N");
                var requestByteList  = new byte[] { 0xFF, 0xFE };
                var responseByteList = new byte[] { 0xF1, 0xF2 };

                using var ipcA = new IpcProvider(ipcAName);
                using var ipcB = new IpcProvider(ipcBName, new IpcConfiguration()
                {
                    DefaultIpcRequestHandler = new DelegateIpcRequestHandler(c =>
                    {
                        Assert.AreEqual(ipcAName, c.Peer.PeerName);
                        c.Handle = true;
                        var span = c.IpcBufferMessage.AsSpan();
                        Assert.AreEqual(true, span.SequenceEqual(requestByteList));

                        return(new IpcHandleRequestMessageResult(new IpcRequestMessage("Return",
                                                                                       new IpcBufferMessage(responseByteList))));
                    })
                });
                ipcA.StartServer();
                ipcB.StartServer();

                var bPeer = await ipcA.GetAndConnectToPeerAsync(ipcBName);
                // 从 A 发送消息给到 B 然后可以收到从 B 返回的消息
                var response =
                    await bPeer.GetResponseAsync(new IpcRequestMessage("发送", new IpcBufferMessage(requestByteList)));
                Assert.AreEqual(true, response.AsSpan().SequenceEqual(responseByteList));
            });
        }
Beispiel #2
0
        public IpcContext(IpcProvider ipcProvider, string pipeName)
        {
            IpcProvider = ipcProvider;
            PipeName    = pipeName;

            AckManager = new AckManager(this);
        }
Beispiel #3
0
        private static void WheehakawlucearHalwahewurlaiwhair()
        {
            var ipcProvider = new IpcProvider(IpcContext.DefaultPipeName);

            ipcProvider.PeerConnected += (sender, args) =>
            {
                Console.WriteLine($"连接上{args.Peer.PeerName}");
            };
            ipcProvider.StartServer();
        }
Beispiel #4
0
        private static async Task?BaiqealawbawKeqakeyawaw()
        {
            var ipcProvider = new IpcProvider();

            ipcProvider.PeerConnected += (sender, proxy) =>
            {
            };
            var peer = await ipcProvider.ConnectToPeerAsync(IpcContext.DefaultPipeName);

            Console.WriteLine($"连接上{peer.PeerName}");
        }
Beispiel #5
0
        /// <summary>
        /// 创建上下文
        /// </summary>
        /// <param name="ipcProvider"></param>
        /// <param name="pipeName">管道名,也将被做来作为服务器名或当前服务名</param>
        /// <param name="ipcConfiguration"></param>
        public IpcContext(IpcProvider ipcProvider, string pipeName, IpcConfiguration?ipcConfiguration = null)
        {
            IpcProvider = ipcProvider;
            PipeName    = pipeName;

            AckManager = new AckManager();
            IpcRequestHandlerProvider = new IpcRequestHandlerProvider(this);

            IpcConfiguration = ipcConfiguration ?? new IpcConfiguration();

            Logger = new IpcLogger(this);
        }
Beispiel #6
0
        private void StartServer(string serverName)
        {
            var ipcProvider = new IpcProvider(serverName);

            ipcProvider.StartServer();
            ipcProvider.PeerConnected += IpcProvider_PeerConnected;
            IpcProvider = ipcProvider;

            Log($"Start Server Name={serverName}");

            ServerPage.Visibility = Visibility.Collapsed;
            MainGrid.Visibility   = Visibility.Visible;

            ServerNameTextBox.Text = serverName;
        }
Beispiel #7
0
        private static async Task?FagurhaircerenaJawehefeljeane()
        {
            // 会断开的一端
            var ipcProvider = new IpcProvider();

            ipcProvider.PeerConnected += (sender, proxy) =>
            {
            };
            var peer = await ipcProvider.ConnectToPeerAsync(IpcContext.DefaultPipeName);

            await peer.IpcMessageWriter.WriteMessageAsync("林德熙是逗比");

            await Task.Delay(TimeSpan.FromSeconds(1));

            ipcProvider.Dispose();
        }
Beispiel #8
0
        public IpcCore(IServiceProvider serviceProvider)
        {
            IpcServer = new IpcProvider(IpcServerName, new IpcConfiguration()
            {
                DefaultIpcRequestHandler = new DelegateIpcRequestHandler(async context =>
                {
                    var server = (TestServer)serviceProvider.GetRequiredService <IServer>();

                    var requestMessage = HttpMessageSerializer.DeserializeToRequest(context.IpcBufferMessage.Buffer);

                    var clientHandler = (ClientHandler)server.CreateHandler();
                    var response      = await clientHandler.SendInnerAsync(requestMessage, CancellationToken.None);

                    var responseByteList = HttpMessageSerializer.Serialize(response);

                    return(new IpcHandleRequestMessageResult(new IpcRequestMessage($"[Response][{requestMessage.Method}] {requestMessage.RequestUri}", responseByteList)));
                })
            });
        }
Beispiel #9
0
        /// <summary>
        /// Creates an IPC client using the config from a ConfiguredCommand
        /// </summary>
        public static IClient CreateClient(ConfiguredCommand conf)
        {
            var daemonConfig = CreateDaemonConfig(conf);

            return(IpcProvider.GetClient(daemonConfig.Moniker, daemonConfig));
        }
Beispiel #10
0
        private async void ConnectToPeer(string peerName)
        {
            var peer = await IpcProvider.ConnectToPeerAsync(peerName);

            AddPeer(peer);
        }