Beispiel #1
0
        /// <summary>
        /// End an asynchronous read operation, fire off the StateObjectReceived event if we read
        /// the whole object, and start a new asynchronous read operation.
        /// </summary>
        /// <param name="iAsyncResult"></param>
        private void _endRead(IAsyncResult iAsyncResult)
        {
            //Get the length of everything read in the stream, blocking 'til we're done reading
            int length = _pipeClientStream.EndRead(iAsyncResult);

            //Get the buffer from the async result
            byte[] previousBuffer = (byte[])iAsyncResult.AsyncState;

            //If we've read -something-
            if (length > 0)
            {
                //copy the start buffer into the new buffer
                byte[] endBuffer = new byte[length];
                Array.Copy(previousBuffer, 0, endBuffer, 0, length);

                //Woohoo. We just received a state object.
                using (MemoryStream memStream = new MemoryStream(endBuffer))
                {
                    //Deserialize it and fire the event if possible
                    UiStateObject stateObject = Serializer.Deserialize <UiStateObject>(memStream);
                    if (UiStateObjectReceived != null)
                    {
                        UiStateObjectReceived(this, new UiStateObjectEventArgs(stateObject));
                    }
                }
            }

            //Get the connection lock and start another asynch read
            lock (_connectionLock)
            {
                //Sanitize the read buffer.
                previousBuffer = new byte[SIZE_BUFFER];
                _pipeClientStream.BeginRead(previousBuffer, 0, SIZE_BUFFER, new AsyncCallback(this._endRead), previousBuffer);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Send a UiStateObject to a specified session via named pipe.
        /// </summary>
        /// <param name="uiStateObject"></param>
        public void SendUiStateObject(string session, UiStateObject uiStateObject)
        {
            //If we've got a connection for the passed session and it's an active connection...
            if (_sessions_namedPipes.ContainsKey(session))
            {
                //Get lock object on the connection
                NamedPipeServerStream connection = _sessions_namedPipes[session];
                lock (connection)
                {
                    //Check if it's connected, if so begin the write.
                    if (connection.IsConnected)
                    {
                        byte[] message;
                        //Get the bytes from the object...
                        using (MemoryStream memStream = new MemoryStream())
                        {
                            Serializer.Serialize <UiStateObject>(memStream, uiStateObject);
                            message = memStream.ToArray();
                        }

                        //Start the write
                        connection.BeginWrite(message, 0, message.Length,
                                              new AsyncCallback(_endWrite), new AsyncPipeStateWrapper(connection, null));
                    }
                }
            }
        }
Beispiel #3
0
 public UiStateObjectEventArgs(UiStateObject sbStateObject)
 {
     UiStateObject = sbStateObject;
 }