/// <summary> /// 序列化数据 /// </summary> /// <param name="stream"></param> internal unsafe void Serialize(UnmanagedStream stream) { int startIndex; switch (step) { case CacheGetStep.Snapshot: startIndex = stream.AddSize(sizeof(int)); snapshot.CopyTo(stream); do { snapshotSize = snapshot.NextSize(); }while (snapshotSize != 0 && snapshot.TryCopyTo(stream)); timeout = Date.Now.AddTicks(timeoutTicks); *(int *)(stream.Data.Byte + (startIndex - sizeof(int))) = stream.ByteSize - startIndex; if (snapshotSize != 0) { if (!onCache.Callback(new CacheReturnParameter { Getter = this })) { error(); *(int *)(stream.Data.Byte + (startIndex - sizeof(int))) = AutoCSer.BinarySerialize.Serializer.NullValue; stream.ByteSize = startIndex; } return; } snapshot = null; step = CacheGetStep.TcpQueue; Cache.TcpServer.CallQueue.Add(new ServerCall.CacheGetterGetQueue(this)); return; case CacheGetStep.TcpQueue: startIndex = stream.AddSize(sizeof(int)); currentQueue.CopyTo(stream); do { currentQueue = currentQueue.LinkNext; }while (currentQueue != null && currentQueue.TryCopyTo(stream)); timeout = Date.Now.AddTicks(timeoutTicks); *(int *)(stream.Data.Byte + (startIndex - sizeof(int))) = stream.ByteSize - startIndex; if (currentQueue != null) { if (!onCache.Callback(new CacheReturnParameter { Getter = this })) { error(); *(int *)(stream.Data.Byte + (startIndex - sizeof(int))) = AutoCSer.BinarySerialize.Serializer.NullValue; stream.ByteSize = startIndex; } return; } Cache.TcpServer.CallQueue.Add(new ServerCall.CacheGetterGetQueue(this)); return; case CacheGetStep.Loaded: stream.Write(0); return; case CacheGetStep.Error: stream.Write(AutoCSer.BinarySerialize.Serializer.NullValue); return; } }
/// <summary> /// 写缓存数据 /// </summary> private void writeCache() { bool isError = true; FileBuffers buffer = default(FileBuffers); try { bufferPool.Get(ref buffer.Buffer); fixed(byte *bufferFixed = buffer.Buffer.Buffer) { byte *bufferStart = bufferFixed + buffer.Buffer.StartIndex; int index = sizeof(int), snapshotSize; long writeLength = 0; while ((snapshotSize = snapshot.NextSize()) != 0) { CHECK: if (snapshotSize + index <= buffer.Buffer.Length) { snapshot.CopyTo(bufferStart + index); index += snapshotSize; } else if (index == sizeof(int)) { if (bigBuffer.Length < (index = snapshotSize + sizeof(int))) bigBuffer = new byte[Math.Max(index, bigBuffer.Length << 1)]; fixed(byte *bigBufferFixed = bigBuffer) { snapshot.CopyTo(bigBufferFixed + sizeof(int)); if (snapshotSize >= Config.MinCompressSize) { if (AutoCSer.IO.Compression.DeflateCompressor.Get(bigBuffer, sizeof(int), snapshotSize, ref buffer.CompressionBuffer, ref buffer.CompressionData, sizeof(int) * 2, sizeof(int) * 2)) { writeCompression(ref buffer.CompressionData, snapshotSize); buffer.CompressionBuffer.TryFree(); writeLength += buffer.CompressionData.Length; index = sizeof(int); continue; } buffer.CompressionBuffer.TryFree(); } *(int *)bigBufferFixed = snapshotSize; } fileStream.Write(bigBuffer, 0, index); writeLength += index; index = sizeof(int); } else { if (index > Config.MinCompressSize) { if (AutoCSer.IO.Compression.DeflateCompressor.Get(buffer.Buffer.Buffer, buffer.Buffer.StartIndex + sizeof(int), index - sizeof(int), ref buffer.CompressionBuffer, ref buffer.CompressionData, sizeof(int) * 2, sizeof(int) * 2)) { writeCompression(ref buffer.CompressionData, index - sizeof(int)); buffer.CompressionBuffer.TryFree(); writeLength += buffer.CompressionData.Length; index = sizeof(int); goto CHECK; } buffer.CompressionBuffer.TryFree(); } *(int *)bufferStart = index - sizeof(int); fileStream.Write(buffer.Buffer.Buffer, buffer.Buffer.StartIndex, index); writeLength += index; index = sizeof(int); goto CHECK; } } if (index != sizeof(int)) { if (index > Config.MinCompressSize) { if (AutoCSer.IO.Compression.DeflateCompressor.Get(buffer.Buffer.Buffer, buffer.Buffer.StartIndex + sizeof(int), index - sizeof(int), ref buffer.CompressionBuffer, ref buffer.CompressionData, sizeof(int) * 2, sizeof(int) * 2)) { writeCompression(ref buffer.CompressionData, index - sizeof(int)); buffer.CompressionBuffer.TryFree(); writeLength += buffer.CompressionData.Length; goto FLUSH; } buffer.CompressionBuffer.TryFree(); } *(int *)bufferStart = index - sizeof(int); fileStream.Write(buffer.Buffer.Buffer, buffer.Buffer.StartIndex, index); writeLength += index; } FLUSH: fileStream.Flush(true); FileLength += writeLength; isError = false; } } finally { buffer.Free(); snapshot = null; if (isError) { cache.TcpServer.CallQueue.Add(new ServerCall.CacheManager(cache, ServerCall.CacheManagerServerCallType.CreateNewFileStreamError)); if (fileStream != null) { fileStream.Dispose(); } } } if (!isError) { writeQueue(); } }