Beispiel #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);
        }
Beispiel #2
0
        public static PipeProtoMessage ReadFromStream(Stream stream)
        {
            int headerLengthExpected = 1 + 1 + 4;

            byte[] nextHeader      = new byte[headerLengthExpected];
            int    headerBytesRead = stream.Read(nextHeader, 0, nextHeader.Length);

            if (headerBytesRead == headerLengthExpected)
            {
                byte index     = 0;
                byte firstByte = nextHeader[index++];

                if (firstByte == PipeProto.PACKET_START_MARKER)
                {
                    byte packetOpcode = nextHeader[index++];

                    UInt32 payloadLength = BitConverter.ToUInt32(new byte[] { nextHeader[index++], nextHeader[index++],
                                                                              nextHeader[index++], nextHeader[index++] }, 0);

                    var packetParsed = new PipeProtoMessage
                    {
                        Opcode  = packetOpcode,
                        Payload = null
                    };

                    if (payloadLength > 0)
                    {
                        packetParsed.Payload = new byte[payloadLength];
                        stream.Read(packetParsed.Payload, 0, (int)payloadLength);
                    }

                    return(packetParsed);
                }
            }

            if (headerBytesRead == 0)
            {
                throw new InvalidOperationException("Connection is now closed, cannot read data.");
            }

            return(null);
        }
Beispiel #3
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();
                            }
                        }
                    }
                }
            }));
        }