Example #1
0
        public InterProcessMessage GetMessage()
        {
            string cont = Console.ReadLine();

            if (cont == null)
            {
                return(null);
            }

            return(InterProcessMessage.Parse(cont));
        }
Example #2
0
        public void SendMessage(InterProcessMessage msg)
        {
            if (!_running)
            {
                return;
            }

            var msgStr    = msg.Serialize() + PackageSeparator;
            var outBuffer = TcpEncoding.GetBytes(msgStr);
            var len       = outBuffer.Length;

            Console.WriteLine(msgStr);

            _ns.Write(outBuffer, 0, len);
            _ns.Flush();
        }
Example #3
0
        /// <summary>Read a message.</summary>
        /// <returns>Message read or <c>null</c> when disconnected.</returns>
        public InterProcessMessage GetMessage()
        {
            if (!_running)
            {
                return(null);
            }
            if (_messageQueue.Count > 0)
            {
                return(_messageQueue.Dequeue());
            }

            while (_messageQueue.Count == 0)
            {
                int readLen = _ns.Read(_buffer, 0, BufferSize);
                if (readLen == 0)
                {
                    return(null);
                }

                _frag += TcpEncoding.GetString(_buffer, 0, readLen);

                int start = 0;
                int len   = _frag.Length;
                while (start < len)
                {
                    int sepIndex = _frag.IndexOf(PackageSeparator, start);
                    if (sepIndex < 0)
                    {
                        break;
                    }

                    _messageQueue.Enqueue(InterProcessMessage.Parse(_frag.Substring(start, sepIndex - start)));
                    start = sepIndex + 1;
                }

                _frag = _frag.Substring(start, len - start);
            }

            return(_messageQueue.Dequeue());
        }
Example #4
0
        public void Run()
        {
            _ipc.Start();

            AddModules();
            foreach (var m in _modules)
            {
                m.Value.SetDaemon(this);
                new Thread(m.Value.MessageLoop).Start();
            }

            while (true)
            {
                InterProcessMessage msg = _ipc.GetMessage();
                if (msg == null)
                {
                    Console.Error.WriteLine("null received. exit."); break;
                }

                // Console.WriteLine($"msg is module {msg.Module}, command {msg.Command}, args {msg.Args.ToStringForNetwork()}");

                if (InterProcessMessage.IsTerminator(msg))
                {
                    break;
                }

                if (_modules.TryGetValue(msg.Module, out var module))
                {
                    module.OnMessage(msg);
                }
            }

            foreach (var m in _modules)
            {
                m.Value.OnMessage(InterProcessMessage.Terminator);
            }

            _ipc.Close();
        }
Example #5
0
 public void SendMessage(InterProcessMessage msg)
 {
     Console.WriteLine(msg.Serialize());
 }
Example #6
0
 public void SendMessage(InterProcessMessage msg)
 {
     _ipc.SendMessage(msg);
 }
 public static bool IsTerminator(InterProcessMessage msg) => msg.Module == AnyModuleName && msg.Command == TerminatorCommandName;
 public static bool IsInvalid(InterProcessMessage msg) => msg.Module == InvalidModuleName;