Exemple #1
0
 public virtual IEnumerable <WebsocketPipeMessageInfo> ReadMessages(Stream from)
 {
     return(new WebsocketPipeMessageInfo[] { WebsocketPipeMessageInfo.FromStream(from) });
 }
Exemple #2
0
        /// <summary>
        /// Reads the pending messages in the memory mapped file, where the memory mapped file name is in the stream from.
        /// mmf format: [wasread? 1 byte][datasize(int)][length(int)][msg][length(int)][msg]...
        /// If wasread=1, then ignores the read since the mmf was not written to, or was already read.
        /// If wasread=0, reads all pending messages and sets wasread to 1.
        /// </summary>
        /// <param name="wp"></param>
        /// <param name="from"></param>
        /// <returns></returns>
        public virtual IEnumerable <WebsocketPipeMessageInfo> ReadMessages(Stream from)
        {
            // reading the memory mapped file name or the msg bytes.
            if (from.ReadByte() == 1)
            {
                // internal message.
                return(WebsocketAsInternalDataSocket.ReadMessages(from));
            }

            // read the id from the from stram.
            StreamReader freader = new StreamReader(from, ASCIIEncoding.ASCII);
            string       id      = freader.ReadToEnd();

            // calling the mutex to verify reading.
            Mutex mu = new Mutex(false, id + "_mutex");

            if (!mu.WaitOne(MemoryMappedFileAccessTimeout))
            {
                throw new Exception("Wait timeout while attempting to read messages from mmf file with id: " + id);
            }

            MemoryMappedFile mmf    = MemoryMappedFile.OpenExisting(id);
            Stream           strm   = mmf.CreateViewStream(0, 0, MemoryMappedFileAccess.ReadWrite);
            BinaryReader     reader = new BinaryReader(strm);

            strm.Seek(0, SeekOrigin.Begin);

            BinaryReader msgreader       = null;
            int          totalDataLength = 0;

            if (strm.Length > MMFHeaderSize && reader.ReadByte() == 0)
            {
                // there is something we need to read.
                // reading all the contents.
                totalDataLength = reader.ReadInt32();

                if (totalDataLength > 0)
                {
                    msgreader = new BinaryReader(new MemoryStream(reader.ReadBytes(totalDataLength)));
                }

                // marking as read.
                strm.Seek(0, SeekOrigin.Begin);
                strm.WriteByte(1);
            }

            strm.Flush();
            strm.Close();
            strm.Dispose();

            // clearing the newly created mmf.
            mmf.Dispose();
            mmf = null;

            // release the mutex allowing others to write.
            mu.ReleaseMutex();
            mu.Dispose();
            mu     = null;
            reader = null;

            // return nothing...
            if (msgreader == null)
            {
                return(new WebsocketPipeMessageInfo[0]);
            }

            // reading the messages.
            strm = msgreader.BaseStream;
            msgreader.BaseStream.Seek(0, SeekOrigin.Begin);

            List <WebsocketPipeMessageInfo> msgs = new List <WebsocketPipeMessageInfo>();

            while (strm.Position < totalDataLength)
            {
                WebsocketPipeMessageInfo info = WebsocketPipeMessageInfo.FromStream(msgreader);
                if (info == null)
                {
                    break;  // something went wrong.
                }
                msgs.Add(info);
            }

            strm.Close();
            strm.Dispose();
            msgreader = null;
            strm      = null;
            return(msgs);
        }