/// <summary> /// Writes the message to a memory mapped file, where the memory mapped file name is WebsocketPipe.Address + id. /// mmf format: [wasread? 1 byte][datasize(int)][length(int)][msg][length(int)][msg]... /// If wasread=0, then writes the new message to the end of the message list and advances the number of messages +1. /// If wasread=1, clears the mmf and then writes the new message. /// </summary> /// <param name="wp">The calling WebsocketPipe</param> /// <param name="msg">The message to write.</param> /// <param name="to">The stream to write the mmf filename to.</param> /// <param name="id">The id of the targer we are writing to, since there may be many we open a mmf for each</param> public virtual void WriteMessage(WebsocketPipeMessageInfo msg, Stream to) { if (msg.Data.Length < this.UseInternalPacketDataSendingIfMsgByteSizeIsLessThen) { // write that this is an internal message. to.WriteByte(1); WebsocketAsInternalDataSocket.WriteMessage(msg, to); return; } // write that this is a mmf msg. to.WriteByte(0); // make the id and write it to the stream. string id = MakeValidMmfID(msg.DataSocketId); byte[] mmfnamebuffer = ASCIIEncoding.ASCII.GetBytes(id); to.Write(mmfnamebuffer, 0, mmfnamebuffer.Length); Mutex mu = new Mutex(false, id + "_mutex"); if (!mu.WaitOne(MemoryMappedFileAccessTimeout)) { throw new Exception("Memory mapped file access timedout."); } // The data size for a single message. int totalDataSize = msg.Data.Length + sizeof(int) + 1; // Creating/Opening the stream. MemoryMappedViewStream strm = GetDataWritingMemoryMappedViewStream(id, ref totalDataSize); // we are at the position of the write, and are ready for the message write. msg.WriteToStream(strm); strm.Flush(); strm.Close(); strm.Dispose(); strm = null; // release the mutex. mu.ReleaseMutex(); }
public virtual void WriteMessage(WebsocketPipeMessageInfo msg, Stream to) { msg.WriteToStream(to); }