private void Awake() { JSONNodeReader jsonReader = new JSONNodeReader(fileItems); JSONNamedEventReader itemReader = new JSONNamedEventReader("identifier"); itemReader.AddCallbackInt("health bonus", player.AddMaxHealth, "+{0} health"); itemReader.AddCallbackInt("tongue damage bonus", player.AddTongueDamage, "+{0} tongue damage"); itemReader.AddCallbackInt("headbutt damage bonus", player.AddHeadbuttDamage, "+{0} headbutt damage"); itemReader.AddCallbackInt("laser damage bonus", player.AddLaserDamage, "+{0} laser damage"); itemReader.AddCallbackInt("contact damage per second bonus", player.AddContactDamagePerSecond, "+{0} contact damage"); itemReader.AddCallbackInt("damaged explosion damage bonus", player.AddEggExplosionDamage, "+{0} egg damage...?"); itemReader.AddCallbackInt("headbutt explosion damage bonus", player.AddHeadbuttExplosionDamage, "+{0} headbutt explosion damage"); itemReader.AddCallbackFloat("jump velocity bonus", player.AddJumpVelocity, "+{0} jump height"); itemReader.AddCallbackFloat("horizontal acceleration bonus", player.AddHorizontalAcceleration, "+{0} acceleration"); itemReader.AddCallbackFloat("max horizontal speed bonus", player.AddMaxHorizontalSpeed, "+{0} max speed"); itemReader.AddCallbackBool("press down for shield", player.SetHasSoap, "Press down for shield"); JSONArrayReader itemsReader = jsonReader.Get <JSONArrayReader>("items"); JSONNodeReader itemNodeReader; while (itemsReader.GetNextNode(out itemNodeReader)) { bool blacklist = itemNodeReader.Get("BLACKLIST", false); if (!blacklist) { NamedEvent item = itemReader.Read(itemNodeReader); item.AddCallback(() => Claim(item)); pool.AddUnclaimed(item); } } }
/// <summary> /// Tries to open a named event. /// </summary> /// <param name="globalMemoryRegion"></param> /// <param name="memoryRegionId"></param> /// <param name="namedEvent"></param> /// <returns></returns> public static bool TryOpenExisting( this GlobalMemoryRegion globalMemoryRegion, MlosInternal.MemoryRegionId memoryRegionId, out NamedEvent namedEvent) { MlosInternal.RegisteredNamedEventConfig.CodegenKey namedEventLookupKey = default; namedEventLookupKey.MemoryRegionId = memoryRegionId; // Locate named event config. // SharedConfig <MlosProxyInternal.RegisteredNamedEventConfig> registeredNamedEventSharedConfig = SharedConfigManager.Lookup(globalMemoryRegion.SharedConfigDictionary, namedEventLookupKey); if (!registeredNamedEventSharedConfig.HasSharedConfig) { namedEvent = null; return(false); } // Config exists, create a named event. // MlosProxyInternal.RegisteredNamedEventConfig registeredNamedEventConfig = registeredNamedEventSharedConfig.Config; namedEvent = NamedEvent.CreateOrOpen(registeredNamedEventConfig.EventName.Value); return(true); }
public static void HandleSupportsEventInheritance() { var queue = new ManualEventQueue(); var subscriber = new AncestorEventHandler(); queue.Subscribers.AddAll(subscriber); // subscriber does not handle events not in it's inheritance tree Assert.True(queue.Enqueue(new TestEvent())); Assert.True(queue.HandleNext()); Assert.Null(subscriber.LastEventHandled); // subscriber accepts event type it explicitly stated it would handle var namedEvent = new NamedEvent("name"); Assert.True(queue.Enqueue(namedEvent)); Assert.True(queue.HandleNext()); Assert.AreSame(namedEvent, subscriber.LastEventHandled); // subscriber accepts event that inherit what it declared it would handle var derivedEvent = new DescendantEvent(); Assert.True(queue.Enqueue(derivedEvent)); Assert.True(queue.HandleNext()); Assert.AreSame(derivedEvent, subscriber.LastEventHandled); }
// Callback for when the player collects the item. private void Claim(NamedEvent item) { string identifier = item.GetName(); itemText.Appear(identifier); player.AddItemVisualEffect(identifier); pool.Claim(item); }
/// <summary> /// Executes the <see cref="NamedEvent"/> /// </summary> /// <param name="sender">The object initiating the execution</param> /// <param name="name">The name of the <see cref="NamedEvent"/> to execute</param> /// <param name="parameters">Collection of <see cref="NamedEventParameter"/> items</param> public void Execute(object sender, string name, params NamedEventParameter[] parameters) { NamedEvent subscription = subscriptions.FirstOrDefault(s => s.Name == name); if (subscription != null) { subscription?.Execute(sender, parameters); } }
public static void EnqueueUpdatesMetadata() { var queue = new ManualEventQueue(); var evnt = new NamedEvent("test"); Assert.Null(evnt.EventEnqueuePos); Assert.True(queue.Enqueue(evnt)); Assert.True(evnt.EventEnqueuePos.Contains(nameof(EnqueueUpdatesMetadata))); }
/// <summary> /// Locates a <see cref="NamedEvent"/> and unsubscribes a consumer /// </summary> /// <param name="subscriber">Identifier of the subscriber class</param> /// <param name="name">The name of the <see cref="NamedEvent"/> to unsubscribes from</param> /// <param name="handler">The <see cref="NamedEventHandler"/> containing the link to the consuming method</param> public void Unsubscribe(string subscriber, string name, NamedEventHandler handler) { NamedEvent subscription = subscriptions.FirstOrDefault(s => s.Name == name); if (subscription != null) { subscription.Subscribers.Remove(subscriber); subscription.Handler -= handler; } }
/// <summary> /// Handle requests. /// </summary> public void HandleRequests() { File.Delete(socketName); serverSocket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified); // Start listening on the Unix socket. // serverSocket.Bind(new UnixDomainSocketEndPoint(socketName)); serverSocket.Listen(backlog: 1); // Signal the target process, the Agent is ready. // using NamedEvent namedEvent = NamedEvent.CreateOrOpen(semaphoreName); namedEvent.Signal(); { // Accept the connection and obtain the list of the shared memory regions. // using Socket acceptedSocket = serverSocket.Accept(); HandleAcceptedRequest(acceptedSocket); } Thread handlerThread = new Thread( start: () => { while (!isDisposed) { Socket socket = serverSocket; if (socket == null) { // Stop processing the request after we disposed the socket. // break; } try { using Socket acceptedSocket = socket.Accept(); HandleAcceptedRequest(acceptedSocket); } catch (SocketException) { // Ignore the exception. // } catch (ObjectDisposedException) { // Ignore the exception. // } } }); handlerThread.Start(); }
private void SpawnItem(Transform parent, NamedEvent itemPoolItem) { GameObject obj = Instantiate(prefabUpgradeItem, parent); obj.transform.localPosition = Vector3.zero; UpgradeItem item = obj.GetComponent <UpgradeItem>(); item.Collected += itemPoolItem.OnInvoked; item.Collected += DestroyItems; item.SetSprite(itemPoolItem.GetName()); }
/// <summary> /// Executes the <see cref="NamedEvent"/> /// </summary> /// <param name="sender">The object initiating the execution</param> /// <param name="name">The name of the <see cref="NamedEvent"/> to execute</param> /// <param name="parameters">Collection of <see cref="NamedEventParameter"/> items</param> public object ExecuteWithReturn(object sender, string name, params NamedEventParameter[] parameters) { NamedEvent subscription = subscriptions.FirstOrDefault(s => s.Name == name); if (subscription != null) { return(subscription?.ExecuteWithReturn(sender, parameters)); } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="AnonymousMemoryMlosContext"/> class. /// </summary> /// <param name="globalMemoryRegionView"></param> /// <param name="controlChannelMemoryMapView"></param> /// <param name="feedbackChannelMemoryMapView"></param> /// <param name="sharedConfigMemoryRegionView"></param> /// <param name="controlChannelNamedEvent"></param> /// <param name="feedbackChannelNamedEvent"></param> /// <param name="fileDescriptorExchangeServer"></param> private AnonymousMemoryMlosContext( SharedMemoryRegionView <MlosProxyInternal.GlobalMemoryRegion> globalMemoryRegionView, SharedMemoryMapView controlChannelMemoryMapView, SharedMemoryMapView feedbackChannelMemoryMapView, SharedMemoryRegionView <MlosProxyInternal.SharedConfigMemoryRegion> sharedConfigMemoryRegionView, NamedEvent controlChannelNamedEvent, NamedEvent feedbackChannelNamedEvent, FileDescriptorExchangeServer fileDescriptorExchangeServer) { this.globalMemoryRegionView = globalMemoryRegionView ?? throw new ArgumentNullException(nameof(globalMemoryRegionView)); this.controlChannelMemoryMapView = controlChannelMemoryMapView ?? throw new ArgumentNullException(nameof(controlChannelMemoryMapView)); this.feedbackChannelMemoryMapView = feedbackChannelMemoryMapView ?? throw new ArgumentNullException(nameof(feedbackChannelMemoryMapView)); this.controlChannelNamedEvent = controlChannelNamedEvent ?? throw new ArgumentNullException(nameof(controlChannelNamedEvent)); this.feedbackChannelNamedEvent = feedbackChannelNamedEvent ?? throw new ArgumentNullException(nameof(feedbackChannelNamedEvent)); this.fileDescriptorExchangeServer = fileDescriptorExchangeServer; // Create the shared config manager. // SharedConfigManager = new SharedConfigManager(); // Register shared config memory region. // SharedConfigManager.RegisterSharedConfigMemoryRegion(sharedConfigMemoryRegionView); MlosProxyInternal.GlobalMemoryRegion globalMemoryRegion = globalMemoryRegionView.MemoryRegion(); // Increase the usage counter. When closing global shared memory, we will decrease the counter. // If there is no process using the shared memory, we will clean the OS resources. On Windows OS, // this is no-op; on Linux, we unlink created files. // globalMemoryRegion.AttachedProcessesCount.FetchAdd(1); // Create the control channel instance. // ControlChannel = new SharedChannel <InterProcessSharedChannelPolicy, SharedChannelSpinPolicy>( buffer: controlChannelMemoryMapView.Buffer, size: (uint)controlChannelMemoryMapView.MemSize, sync: globalMemoryRegion.ControlChannelSynchronization) { ChannelPolicy = { NotificationEvent = controlChannelNamedEvent }, }; // Create the feedback channel instance. // FeedbackChannel = new SharedChannel <InterProcessSharedChannelPolicy, SharedChannelSpinPolicy>( buffer: feedbackChannelMemoryMapView.Buffer, size: (uint)feedbackChannelMemoryMapView.MemSize, sync: globalMemoryRegion.FeedbackChannelSynchronization) { ChannelPolicy = { NotificationEvent = feedbackChannelNamedEvent }, }; }
public void CreateSharedEvents() { IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(sa)); Marshal.StructureToPtr(sa, pnt, false); SharedEvent_dataarv = new NamedEvent(sa.lpSecurityDescriptor, "Global\\DataArrived", true); SharedEvent_trigger = new NamedEvent(pnt, "Global\\trigger", true); SharedEvent_ready2read = new NamedEvent(pnt, "Global\\ReadyRead", true); Marshal.FreeHGlobal(pnt); //NamedEvent SharedEvent_trigger = new NamedEvent(pnt,"Global\\SharedMem",true); //SharedEvent_dataarv = CreateEventA(&sa, TRUE, FALSE, "Global\\DataArrived"); }
/// <summary> /// Tries to open an existing semaphore. /// </summary> /// <param name="name"></param> /// <param name="namedEvent"></param> /// <returns></returns> public static new bool TryOpenExisting(string name, out NamedEvent namedEvent) { var semaphore = new NamedSemaphore(name, Native.OpenFlags.O_RDWR); if (semaphore.semaphoreHandle.IsInvalid) { namedEvent = null; return(false); } namedEvent = semaphore; return(true); }
/// <summary> /// Determines if the subscriber class is subscribed to the <see cref="NamedEvent"/> /// </summary> /// <param name="subscriber">Identifier of the subscriber class</param> /// <param name="name">The name of the <see cref="NamedEvent"/></param> /// <returns>If the cubscriber is subscribed to the <see cref="NamedEvent"/>, true; otherwise, false</returns> public bool IsSubscribed(string subscriber, string name) { NamedEvent subscription = subscriptions.FirstOrDefault(s => s.Name == name); if (subscription != null) { return(subscription.Subscribers.Contains(subscriber)); } else { return(false); } }
/// <summary> /// Creates InterProcessMlosContext. /// </summary> /// <returns></returns> public static AnonymousMemoryMlosContext Create() { var fdExchangeServer = new FileDescriptorExchangeServer(FdUnitDomainSocketName, FdExchangeSemaphoreName); fdExchangeServer.HandleRequests(); MemoryRegionId memoryRegionId = new MemoryRegionId { Type = MemoryRegionType.Global }; SharedMemoryRegionView <MlosProxyInternal.GlobalMemoryRegion> globalMemoryRegionView = SharedMemoryRegionView.OpenAnonymousFromFileDescriptor <MlosProxyInternal.GlobalMemoryRegion>( fdExchangeServer.GetSharedMemoryFd(memoryRegionId), fdExchangeServer.GetSharedMemorySize(memoryRegionId)); memoryRegionId = new MemoryRegionId { Type = MemoryRegionType.ControlChannel }; SharedMemoryMapView controlChannelMemoryMapView = SharedMemoryMapView.OpenAnonymousFromFileDescriptor( fdExchangeServer.GetSharedMemoryFd(memoryRegionId), fdExchangeServer.GetSharedMemorySize(memoryRegionId)); memoryRegionId = new MemoryRegionId { Type = MemoryRegionType.FeedbackChannel }; SharedMemoryMapView feedbackChannelMemoryMapView = SharedMemoryMapView.OpenAnonymousFromFileDescriptor( fdExchangeServer.GetSharedMemoryFd(memoryRegionId), fdExchangeServer.GetSharedMemorySize(memoryRegionId)); memoryRegionId = new MemoryRegionId { Type = MemoryRegionType.SharedConfig }; SharedMemoryRegionView <MlosProxyInternal.SharedConfigMemoryRegion> sharedConfigMemoryRegionView = SharedMemoryRegionView.OpenAnonymousFromFileDescriptor <MlosProxyInternal.SharedConfigMemoryRegion>( fdExchangeServer.GetSharedMemoryFd(memoryRegionId), fdExchangeServer.GetSharedMemorySize(memoryRegionId)); // Create channel synchronization primitives. // NamedEvent controlChannelNamedEvent = NamedEvent.CreateOrOpen(ControlChannelSemaphoreName); NamedEvent feedbackChannelNamedEvent = NamedEvent.CreateOrOpen(FeedbackChannelSemaphoreName); return(new AnonymousMemoryMlosContext( globalMemoryRegionView, controlChannelMemoryMapView, feedbackChannelMemoryMapView, sharedConfigMemoryRegionView, controlChannelNamedEvent, feedbackChannelNamedEvent, fdExchangeServer)); }
private void SpawnItem(Transform parent, NamedEvent itemPoolItem) { GameObject obj = Instantiate(prefabUpgradeItem, parent); obj.transform.localPosition = Vector3.zero; UpgradeItem item = obj.GetComponent <UpgradeItem>(); item.Collected += itemPoolItem.OnInvoked; item.Collected += DestroyItems; string itemName = itemPoolItem.GetName(); item.SetSprite(itemName); item.SetItemName(UtilTranslate.ItemName(itemName)); item.SetItemProperties(itemPoolItem.GetDescriptionList("\n")); }
// Reads from the given node and returns the resulting NamedEvent. public NamedEvent Read(JSONNodeReader nodeReader) { this.nodeReader = nodeReader; string identifier = nodeReader.Get(nameKey, "ERROR"); currentEvent = new NamedEvent(identifier); // Read all callback JSON node data. foreach (KeyValuePair <string, EventCallback <int> > pair in nameToCallbackInt) { TryAddCallback(pair.Key, pair.Value); } return(currentEvent); }
protected static bool CheckRunning() { //return false; string name = "SCOUT NS"; s_runSignal = new NamedEvent(name, false); if (s_runSignal.WaitOne(0, false)) { MessageBox.Show("A copy of SCOUT is already running.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); return(true); } s_runSignal.Set(); return(false); }
private void Awake() { JSONNodeReader jsonReader = new JSONNodeReader(fileItems); JSONNamedEventReader itemReader = new JSONNamedEventReader("identifier"); itemReader.AddCallbackInt("health bonus", player.AddMaxHealth); itemReader.AddCallbackInt("tongue damage bonus", player.AddTongueDamage); itemReader.AddCallbackInt("headbutt damage bonus", player.AddHeadbuttDamage); JSONArrayReader itemsReader = jsonReader.Get <JSONArrayReader>("items"); JSONNodeReader itemNodeReader; while (itemsReader.GetNextNode(out itemNodeReader)) { NamedEvent item = itemReader.Read(itemNodeReader); item.AddCallback(() => Claim(item)); pool.AddUnclaimed(item); } }
/// <summary> /// Locates or creates a <see cref="NamedEvent"/> and subscribes a consumer /// </summary> /// <param name="subscriber">Identifier of the subscriber class</param> /// <param name="name">The name of the <see cref="NamedEvent"/> to subscribe to</param> /// <param name="handler">The <see cref="NamedEventReturnHandler"/> containing the link to the consuming method</param> public void SubscribeWithReturn(string subscriber, string name, NamedEventReturnHandler handler) { NamedEvent subscription = subscriptions.FirstOrDefault(s => s.Name == name); if (subscription != null) { if (!subscription.Subscribers.Contains(subscriber)) { subscription.Subscribers.Add(subscriber); } subscription.ReturnHandler -= handler; subscription.ReturnHandler += handler; } else { subscription = new NamedEvent(name, handler); subscription.Subscribers.Add(subscriber); subscriptions.Add(subscription); } }
public AudioPlayer() { connectedEvent = new NamedEvent(IPCConsts.CONNECTED_EVENT_NAME, true); errorEvent = new NamedEvent(IPCConsts.ERROR_EVENT_NAME, true); }
/// <summary> /// Initialize shared channel. /// </summary> public static void InitializeSharedChannel() { // Create or open the memory mapped files. // globalMemoryRegionView = SharedMemoryRegionView.CreateOrOpen <MlosProxyInternal.GlobalMemoryRegion>(GlobalMemoryMapName, SharedMemorySize); controlChannelMemoryMapView = SharedMemoryMapView.CreateOrOpen(ControlChannelMemoryMapName, SharedMemorySize); feedbackChannelMemoryMapView = SharedMemoryMapView.CreateOrOpen(FeedbackChannelMemoryMapName, SharedMemorySize); sharedConfigMemoryMapView = SharedMemoryRegionView.CreateOrOpen <MlosProxyInternal.SharedConfigMemoryRegion>(SharedConfigMemoryMapName, SharedMemorySize); // Create channel synchronization primitives. // controlChannelNamedEvent = NamedEvent.CreateOrOpen(ControlChannelSemaphoreName); feedbackChannelNamedEvent = NamedEvent.CreateOrOpen(FeedbackChannelSemaphoreName); // Setup feedback channel. // MlosProxyInternal.GlobalMemoryRegion globalMemoryRegion = globalMemoryRegionView.MemoryRegion(); // Enable channels. // globalMemoryRegion.ControlChannelSynchronization.TerminateChannel.Store(false); globalMemoryRegion.FeedbackChannelSynchronization.TerminateChannel.Store(false); var feedbackChannel = new SharedChannel <InterProcessSharedChannelPolicy, SharedChannelSpinPolicy>( buffer: feedbackChannelMemoryMapView.Buffer, size: (uint)feedbackChannelMemoryMapView.MemSize, sync: globalMemoryRegion.FeedbackChannelSynchronization); feedbackChannel.ChannelPolicy.NotificationEvent = feedbackChannelNamedEvent; // Set SharedConfig memory region. // SharedConfigManager.SetMemoryRegion(new MlosProxyInternal.SharedConfigMemoryRegion() { Buffer = sharedConfigMemoryMapView.MemoryRegion().Buffer }); // Setup MlosContext. // MlosContext.FeedbackChannel = feedbackChannel; MlosContext.SharedConfigManager = SharedConfigManager; // Initialize callbacks. // MlosProxyInternal.RegisterAssemblyRequestMessage.Callback = RegisterAssemblyCallback; MlosProxyInternal.RegisterMemoryRegionRequestMessage.Callback = RegisterMemoryRegionMessageCallback; MlosProxyInternal.RegisterSharedConfigMemoryRegionRequestMessage.Callback = RegisterSharedConfigMemoryRegionRequestMessageCallback; MlosProxy.TerminateReaderThreadRequestMessage.Callback = TerminateReaderThreadRequestMessageCallback; // Register Mlos.Core assembly. // RegisterAssembly(typeof(MlosContext).Assembly, dispatchTableBaseIndex: 0); // Register assemblies from the shared config. // Assembly Mlos.NetCore does not have a config, as it is always registered first. // for (uint index = 1; index < globalMemoryRegion.RegisteredSettingsAssemblyCount.Load(); index++) { RegisterSettingsAssembly(assemblyIndex: index); } }
public AudioTrackStreamer() : base() { connectedEvent = new NamedEvent(IPCConsts.CONNECTED_EVENT_NAME, true); }
public static ForbiddenTransitionDefinition Ignore(NamedEvent @event) => new ForbiddenTransitionDefinition(@event);
private void Claim(NamedEvent item) { itemText.Appear(item.GetName()); pool.Claim(item); }
internal WithNamedEvent(NamedEvent @event) => _event = @event;