static EventLoop() { Instance = new EventLoop(); Instance.connectedToNetwork = false; Instance.eventQueueIncoming = new EventQueue(); Instance.eventQueueOutgoing = new EventQueueCollection(); Instance.addressBook = new AddressBook(); Instance.timeoutManager = new TimeoutManager(); // Register the event which should be triggered when the connection times out (i.e. a permanent failure, not one that we can resolve in a reasonable time) // Just remove the address from the addressbook, this will trigger the process that will properly disconnect the device from the network for us Instance.timeoutManager.ConnectionTimedOutEvent += (string targetAddress) => Instance.addressBook.Remove(targetAddress); Instance.timeoutManager.FutureTimedOutEvent += (string targetAddress, Guid futureIdentifier) => Instance.addressBook.Remove(targetAddress); Instance.eventQueueProcessor = new EventQueueProcessor(Instance.eventQueueIncoming, Instance.eventQueueOutgoing, Instance.timeoutManager); // Register callback for the incoming and outgoing messages event queue Instance.eventQueueIncoming.eventEnqueuedCallback += Instance.eventQueueProcessor.EventQueueIncoming_EventEnqueuedCallback; Instance.eventQueueOutgoing.eventEnqueuedCallback += Instance.eventQueueProcessor.EventQueueOutgoing_EventEnqueuedCallback; // Monitor all dequeued messages for the outgoing queue. When a message is dequeued, it is successfully sent. // If it is a remote method invocation, make sure we get a reply within the desired time. Instance.eventQueueOutgoing.eventDequeuedCallback += (string targetAddress, NetworkObject networkObject, int due) => { if (networkObject.GetType() == typeof(NetworkRemoteMethodInvocation)) Instance.timeoutManager.AddPendingFuture(targetAddress, ((NetworkRemoteMethodInvocation)networkObject).futureReference, due); }; Instance.discoveryManager = new DiscoveryManager(Instance.addressBook, Instance.eventQueueOutgoing); // Register callback for receiving objects from the network PeerConnector.Instance.networkObjectReceivedCallback += (string sender, NetworkObject networkObject) => { // If this is a new peer (or a previousy disconnected one), add him to the addressbook if (!Instance.addressBook.Contains(sender)) Instance.addressBook.Add(sender); if (networkObject.GetType() == typeof(NetworkMethodReturnValue)) if (!Instance.timeoutManager.IsPendingFuture(sender, ((NetworkMethodReturnValue)networkObject).futureReference)) // The future associated with the return value has already been ruined. The return value is sadly too late (past its due time). // We should just ignore it, because our application is not expecting it anymore. return; else Instance.timeoutManager.RemovePendingFuture(sender, ((NetworkMethodReturnValue)networkObject).futureReference); Instance.eventQueueIncoming.Enqueue(sender, networkObject); }; }
public virtual void UponReceiptBy(EventLoop eventLoop, string senderAddress) { return; }
public override void UponReceiptBy(EventLoop eventLoop, string senderAddress) { eventLoop.discoveryManager.OnNetworkDisconnectReceived(senderAddress); }
public override void UponReceiptBy(EventLoop eventLoop, string senderAddress) { eventLoop.discoveryManager.OnDevicePresenceReceived(senderAddress); }