Пример #1
0
        public bool SendMessage(PipeProtoMessage msg)
        {
            if (Connected)
            {
                try
                {
                    msg.WriteToClientStream(stream, true);
                    statMsgsSent++;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Send message failure: " + ex.Message + " " + ex.StackTrace.ToString());

                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }

                    return(false);
                }

                return(true);
            }

            return(false);
        }
Пример #2
0
#pragma warning disable CS4014
        public void Connect()
        {
            if (Connected)
            {
                return;
            }

            stream = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

            try
            {
                stream.Connect(3000);
            }
            catch (Exception ex)
            {
                Disconnect();

                if (ConnectionStateChanged != null)
                {
                    ConnectionStateChanged.Invoke(this, Connected);
                }

                throw ex;
            }

            Task.Run(new Action(() =>
            {
                if (ConnectionStateChanged != null)
                {
                    ConnectionStateChanged.Invoke(this, Connected);
                }

                // Send ping message
                var pingMessage = new PipeProtoMessage
                {
                    Opcode = PipeProto.OPCODE_PING
                };

                try
                {
                    pingMessage.WriteToClientStream(stream, true);
                    statMsgsSent++;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Send message failure: " + ex.Message + " " + ex.StackTrace.ToString());

                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }

                    Disconnect();
                }

                // Ask for a repaint
                SendRepaintRequest();

                while (Connected)
                {
                    // Read next message
                    PipeProtoMessage incomingMessage = null;

                    try
                    {
                        incomingMessage = PipeProtoMessage.ReadFromStream(stream);
                    }
                    catch (Exception ex) { }

                    if (incomingMessage != null)
                    {
                        try
                        {
                            if (MessageReceived != null)
                            {
                                MessageReceived.Invoke(this, incomingMessage);
                                statMsgsReceived++;
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            Debug.WriteLine(ex.Message + " @ " + ex.StackTrace);

                            if (Debugger.IsAttached)
                            {
                                Debugger.Break();
                            }
                        }
                    }
                }
            }));
        }