Пример #1
0
        /// <summary>
        /// Send a state object to the UI via a named pipe.
        /// </summary>
        /// <param name="sbStateObject"></param>
        public void SendSbStateObject(SbStateObject sbStateObject)
        {
            //Reference for message
            byte[] message = null;
            //Serialize our object into a memorystream
            using (MemoryStream memStream = new MemoryStream())
            {
                Serializer.Serialize <SbStateObject>(memStream, sbStateObject);

                //Set the message
                message = memStream.ToArray();
            }

            //Send that message, yo.
            _sendMessage(message);
        }
Пример #2
0
        /// <summary>
        /// End the asynchronous read operation on a connected named pipe.
        /// </summary>
        /// <param name="iAsyncResult"></param>
        private void _endRead(IAsyncResult iAsyncResult)
        {
            //Get the instance of the connection we're ending for
            AsyncPipeStateWrapper wrapper    = iAsyncResult.AsyncState as AsyncPipeStateWrapper;
            NamedPipeServerStream connection = wrapper.NamedPipeServerStream;

            if (connection != null && connection.IsConnected)
            {
                try
                {
                    //Get the length read
                    int    length     = connection.EndRead(iAsyncResult);
                    byte[] readBuffer = wrapper.Buffer;

                    //If we have somethign read...
                    if (length > 0)
                    {
                        byte[] destinationArray = new byte[length];
                        Array.Copy(readBuffer, 0, destinationArray, 0, length);
                        //Deserialize the read object
                        using (MemoryStream memStream = new MemoryStream(readBuffer))
                        {
                            //Still figuring out why this excepts
                            try
                            {
                                SbStateObject stateObject = Serializer.Deserialize <SbStateObject>(memStream);
                                //Cool, we have ourselves our very own SbStateObject. Fire the event.
                                if (SbStateObjectReceived != null)
                                {
                                    SbStateObjectReceived(this, new SbStateObjectEventArgs(stateObject, connection));
                                }
                            }
                            catch (ProtoException) { }
                        }
                    }
                    //lock the connection and begin reading
                    lock (connection)
                    {
                        //Sanitize the read buffer. FFS, examples...
                        readBuffer = new byte[SIZE_BUFFER];
                        connection.BeginRead(readBuffer, 0, SIZE_BUFFER,
                                             new AsyncCallback(this._endRead), new AsyncPipeStateWrapper(connection, readBuffer));
                    }
                }
                catch (Exception) { }
            }
        }
Пример #3
0
 public SbStateObjectEventArgs(SbStateObject sbStateObject, NamedPipeServerStream sendingStream)
 {
     SbStateObject = sbStateObject;
     SendingStream = sendingStream;
 }