/// <summary>
        /// Look up the IObserver for the registered IPEndPoint or event type
        /// and execute the IObserver.
        /// </summary>
        /// <param name="transportEvent">The incoming remote event</param>
        public void OnNext(TransportEvent <IWritableRemoteEvent <T> > transportEvent)
        {
            IWritableRemoteEvent <T> remoteEvent = transportEvent.Data;

            remoteEvent.LocalEndPoint  = transportEvent.Link.LocalEndpoint;
            remoteEvent.RemoteEndPoint = transportEvent.Link.RemoteEndpoint;
            T    value   = remoteEvent.Value;
            bool handled = false;

            IObserver <T> observer1;
            IObserver <IRemoteMessage <T> > observer2;

            if (_universalObserver != null)
            {
                _universalObserver.OnNext(value);
                handled = true;
            }
            if (_endpointMap.TryGetValue(remoteEvent.RemoteEndPoint, out observer1))
            {
                // IObserver was registered by IPEndpoint
                observer1.OnNext(value);
                handled = true;
            }
            else if (_typeMap.TryGetValue(value.GetType(), out observer2))
            {
                // IObserver was registered by event type
                IRemoteIdentifier  id            = new SocketRemoteIdentifier(remoteEvent.RemoteEndPoint);
                IRemoteMessage <T> remoteMessage = new DefaultRemoteMessage <T>(id, value);
                observer2.OnNext(remoteMessage);
                handled = true;
            }

            if (!handled)
            {
                throw new WakeRuntimeException("Unrecognized Wake RemoteEvent message");
            }
        }
Example #2
0
        /// <summary>
        /// Receives event from connected TcpClient and invokes handler on the event.
        /// </summary>
        /// <param name="client">The connected client</param>
        private async Task ProcessClient(TcpClient client)
        {
            // Keep reading messages from client until they disconnect or timeout
            CancellationToken token = _cancellationSource.Token;

            using (ILink <T> link = new Link <T>(client, _codec))
            {
                while (!token.IsCancellationRequested)
                {
                    T message = await link.ReadAsync(token);

                    TransportEvent <T> transportEvent = new TransportEvent <T>(message, link);

                    _remoteObserver.OnNext(transportEvent);

                    if (message == null)
                    {
                        LOGGER.Log(Level.Error, "ProcessClient, no message received, break." + link.RemoteEndpoint + " - " + link.LocalEndpoint);
                        break;
                    }
                }
                LOGGER.Log(Level.Error, "ProcessClient close the Link. IsCancellationRequested: " + token.IsCancellationRequested);
            }
        }