Пример #1
0
        private void BeginReadCallback(IAsyncResult result)
        {
            string    pipeId = (string)result.AsyncState;
            EventPipe pipe   = _pipes[pipeId];

            int    readBytes = pipe.InPipe.EndRead(result);
            string msg       = ToString(pipe.InMsgBuf, readBytes);

            if (readBytes > 0 &&
                (_pipeStopMsg == null || (_pipeStopMsg != null && msg != _pipeStopMsg)))
            {
                try {
                    PipeMessage pmsg = JsonConvert.DeserializeObject <PipeMessage>(msg);
                    log.Debug($"Client message received, type: {pmsg.type}, msg: {pmsg.message} ");

                    if (OnMessage != null)
                    {
                        OnMessage(pmsg, _channelName, pipeId);
                    }
                } catch (Exception ex)
                {
                    log.Error($"Failed to process received message: {msg}, ex: {ex}");
                }

                pipe.InPipe.BeginRead(pipe.InMsgBuf, 0, BUFFER_SIZE, BeginReadCallback, pipe.Id);
            }
            else
            {
                log.Debug("Receive empty or stop message, client disconnected or broken.");
                RestartConnectionWaiting(pipe.Id);
            }
        }
Пример #2
0
 public void SendMsg(PipeMessage msg)
 {
     foreach (var pipeKV in _pipes)
     {
         EventPipe pipe = pipeKV.Value;
         try {
             if (pipe.OutPipe.IsConnected && pipe.OutPipe.CanWrite)
             {
                 log.Debug($"Sending to {pipe.Id}, message: {msg}");
                 byte[] msgBytes = Encoding.UTF8.GetBytes(msg.ToString());
                 pipe.OutPipe.Write(msgBytes, 0, msgBytes.Length);
                 pipe.OutPipe.Flush();
             }
         }
         catch (Exception ex)
         {
             log.Debug($"Write message failed. Pipe {pipe.Id} should have broken. ex: {ex}");
             if (OnDisconnected != null)
             {
                 OnDisconnected(pipe.Id);
             }
             RestartConnectionWaiting(pipe.Id);
         }
     }
 }
Пример #3
0
        static void CtrlMessageHandler(PipeMessage pmsg, string channelName, string pipeId)
        {
            VoiceCmdSetting.VoiceGrammar grammarToUpdate;
            try
            {
                switch (pmsg.type)
                {
                case EVENT_TYPE_ECHO:
                    if (pipeMap.ContainsKey(channelName))
                    {
                        pipeMap[channelName].SendMsg(pmsg);
                    }
                    break;

                case EVENT_TYPE_UPDATE_GRAMMAR:
                    grammarToUpdate = JsonConvert.DeserializeObject <VoiceCmdSetting.VoiceGrammar>(pmsg.message);
                    voiceCmdRecognizer.UpdateGrammar(channelName, grammarToUpdate);
                    break;

                default:
                    log.Debug($"no matched event type {pmsg.type}, msg: {pmsg.message}");
                    break;
                }
            }
            catch (Exception ex)
            {
                log.Error($"Failed in processing control message, msg: {pmsg.message}, ex: {ex}");
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            string input = "";

            byte[]      bytes2send;
            PipeMessage pmsg;

            XmlConfigurator.Configure(new FileInfo("logging.config"));

            // default config
            string confFile = "VoiceCmdClientSetting.json";

            try {
                if (args.Length >= 2)
                {
                    confFile = args[1];
                }

                // decide which configuration to use
                VoiceCmdSetting vs = JsonConvert.DeserializeObject <VoiceCmdSetting>(File.ReadAllText(confFile));

                StartListen(vs);

                Console.WriteLine("- Recognised voice messages will be listed below.");
                Console.WriteLine("- Enter anything to receive echo message.");
                Console.WriteLine("- Enter 'stop' to close program.\n\n");

                while (true)
                {
                    try
                    {
                        input = Console.ReadLine();
                        if (input == "stop")
                        {
                            Console.WriteLine("Stopping..");
                            StopListen();
                            break;
                        }
                        pmsg       = new PipeMessage(EVENT_TYPE_ECHO, input);
                        bytes2send = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(pmsg));
                        SendStream.Write(bytes2send, 0, bytes2send.Length);
                        pmsg       = null;
                        bytes2send = null;
                    }
                    catch (Exception ex)
                    {
                        log.Error("Namedpipe exception: ex: " + ex);
                    }
                }

                return;
            } catch (Exception ex)
            {
                log.Error("Failed to start test client, ex: " + ex);
                Console.WriteLine("enter to close program.");
                Console.ReadLine();
                return;
            }
        }
Пример #5
0
 static void MsgHandler(string msg)
 {
     try
     {
         PipeMessage pmsg = JsonConvert.DeserializeObject <PipeMessage>(msg);
         //log.Debug($"Message received, type: {pmsg.type}, msg: {pmsg.message}");
         Console.WriteLine($"Message received, type: {pmsg.type}, msg: {pmsg.message}");
     }
     catch (Exception ex)
     {
         log.Error("Failed to deserialize received message, ex: " + ex);
     }
 }
Пример #6
0
        private void OutConnectionCallBack(IAsyncResult result)
        {
            string    pipeId = (string)result.AsyncState;
            EventPipe pipe   = _pipes[pipeId];

            try
            {
                if (pipe.OutPipe != null && pipe.OutPipe.CanWrite)
                {
                    pipe.OutPipe.EndWaitForConnection(result);

                    if (_pipeStartMsg != null)
                    {
                        int    readBytes = pipe.OutPipe.Read(pipe.OutMsgBuf, 0, BUFFER_SIZE);
                        string msg       = ToString(pipe.OutMsgBuf, readBytes);

                        // Check start message
                        if (readBytes <= 0 || msg != _pipeStartMsg)
                        {
                            log.Error($"HandleMessage: empty content or invalid start message: " + msg);
                            RestartConnectionWaiting(pipe.Id);
                            return;
                        }
                    }

                    // Send ack message
                    log.Debug($"Pipe {pipe.Id} Connected.");
                    PipeMessage ackMsg      = new PipeMessage("Ack", $"Command pipe Connected! Id: {pipe.Id}, PipeName: {_outPipeName}");
                    byte[]      ackMsgBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ackMsg));
                    pipe.OutPipe.Write(ackMsgBytes, 0, ackMsgBytes.Length);

                    // Update status
                    pipe.Status = ConnStatus.Connected;

                    // Run user action
                    if (OnConnected != null)
                    {
                        OnConnected(pipeId);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("Exception while waiting for connection to OutPipe, restart waiting , ex: " + ex);
                RestartConnectionWaiting(pipe.Id);
            }
        }
Пример #7
0
        static void VoiceCmdHandler(string msg, string grammarName, float confidence)
        {
            log.Debug($"Voice message received: {grammarName}, {msg}, {confidence}");

            // dispatch by channel
            string channel     = grammarName.Split('.')[0];
            string grammarType = grammarName.Split('.')[1];

            PipeMessage pmsg = new PipeMessage(grammarType, msg);

            if (pipeMap.ContainsKey(channel))
            {
                pipeMap[channel].SendMsg(pmsg);
            }
            else
            {
                log.Debug($"Channel [{channel}] of recognised message doesn't exist anymore, ignore the message [{msg}]");
            }
        }