Ejemplo n.º 1
0
        private bool HandleMessage(NamedPipeMessage message)
        {
            if (_handlers.Count == 0)
            {
                return(false);
            }

            Action <NamedPipeMessage> handler;

            if (_handlers.TryGetValue(message.MessageObject.GetType(), out handler))
            {
                handler(message);
            }
            else
            {
                Logger.Info("No handler found for this message, storing in the cache.");
                if (_messageCache.Count >= ReceiveCacheSize)
                {
                    Logger.Info("Maximum receive cache size is reached, dropping RANDOM message from the cache.");
                    var key = _messageCache.Keys.FirstOrDefault();
                    if (key != null)
                    {
                        _messageCache.Remove(key);
                    }
                }
                _messageCache.AddOrUpdate(message, new object(), (deserializedMessage, o) => new object());
            }

            return(true);
        }
Ejemplo n.º 2
0
        private async Task <NamedPipeMessage> AwaitAnyMessageAsync(Type[] types, CancellationToken ct = default(CancellationToken))
        {
            var evt = new ManualResetEventSlim();
            NamedPipeMessage result = null;
            var set = types.ToHashSet();

            foreach (var type in types)
            {
                var handler = new Action <NamedPipeMessage>(msg =>
                {
                    result = msg;
                    evt.Set();
                });
                _handlers.AddOrUpdate(type, handler, (t, a) => handler);
            }

            if (_messageCache.Count > 0)
            {
                foreach (var message in _messageCache.Keys.ToList())
                {
                    if (set.Contains(message.MessageObject.GetType()))
                    {
                        _messageCache.Remove(message);
                        foreach (var type in types)
                        {
                            _handlers.Remove(type);
                        }

                        return(message);
                    }
                }
            }

            var cts = CombineWithInternalToken(ct);

            try
            {
                await Task.Run(() => evt.Wait(cts.Token), cts.Token);
            }
            catch (OperationCanceledException)
            {
                return(null);
            }

            return(result);
        }
Ejemplo n.º 3
0
 public NamedPipeMessageArgs(NamedPipeMessage message)
 {
     Message = message;
 }
Ejemplo n.º 4
0
        protected async Task ReadMessagesAsync(PipeStream stream, CancellationToken ct)
        {
            CheckDisposed();

            if (!stream.CanRead)
            {
                Logger.Warn($"Pipe stream doesn't support reading. Not listening to messages.");
                return;
            }

            while (!ct.IsCancellationRequested)
            {
                var buffer = new byte[2048];
                var ms     = new MemoryStream();

                do
                {
                    int read = 0;
                    try
                    {
                        read = await stream.ReadAsync(buffer, 0, buffer.Length, ct);
                    }
                    catch (OperationCanceledException e)
                    {
                        OnPipeDied(stream, true);
                        return;
                    }
                    catch (Exception ex) when(ex is IOException || ex is ObjectDisposedException)
                    {
                        OnPipeDied(stream, false);
                        return;
                    }
                    catch (Exception ex)
                    {
                        Logger.Error($"Unhandled stream exception: {ex.Message}. Disposing the pipe.", ex);
                        Dispose();
                        return;
                    }

                    if (read == 0) // Means that connection was closed.
                    {
                        OnPipeDied(stream, false);
                        return;
                    }

                    ms.Write(buffer, 0, read);
                } while (!stream.IsMessageComplete);

                if (ms.Length > 0)
                {
                    try
                    {
                        /*  PipeFrame frame = new PipeFrame();
                         * if (frame.ReadStream(ms))
                         * {
                         *    Console.ForegroundColor = ConsoleColor.White;
                         *    Console.WriteLine("NEW FRAME:");
                         *    Console.ForegroundColor = ConsoleColor.Gray;
                         *    Console.WriteLine("Opcode=" + frame.Opcode);
                         *    Console.WriteLine("Message=" + frame.Message);
                         *    Console.ForegroundColor = ConsoleColor.White;
                         * }*/

                        var message = new NamedPipeMessage(stream, this, ms.ToArray());
                        Logger.Debug($"Received a message of type {message.MessageObject.GetType()}");
                        HandleMessage(message);
                    }
                    catch (Exception e)
                    {
                        Logger.Warn("Failed to deserialize message!");
                    }
                }
            }
        }
 public NamedPipeMessage(NamedPipeMessage originalMessage) : base(originalMessage)
 {
 }