A game framework written with osu! in mind.
at master 39 lines 1.0 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using System.Threading.Tasks; 6 7namespace osu.Framework.Platform 8{ 9 public class IpcChannel<T> : IDisposable 10 { 11 private readonly IIpcHost host; 12 public event Action<T> MessageReceived; 13 14 public IpcChannel(IIpcHost host) 15 { 16 this.host = host; 17 this.host.MessageReceived += handleMessage; 18 } 19 20 public Task SendMessageAsync(T message) => host.SendMessageAsync(new IpcMessage 21 { 22 Type = typeof(T).AssemblyQualifiedName, 23 Value = message, 24 }); 25 26 private void handleMessage(IpcMessage message) 27 { 28 if (message.Type != typeof(T).AssemblyQualifiedName) 29 return; 30 31 MessageReceived?.Invoke((T)message.Value); 32 } 33 34 public void Dispose() 35 { 36 host.MessageReceived -= handleMessage; 37 } 38 } 39}