Beispiel #1
0
 private void error()
 {
     Buffer.DisposeLink(currentQueue);
     snapshot     = null;
     currentQueue = null;
     step         = CacheGetStep.Error;
 }
Beispiel #2
0
 /// <summary>
 /// 重建文件流
 /// </summary>
 /// <param name="cacheFile"></param>
 /// <returns></returns>
 internal ReturnType Start(FileStreamWriter cacheFile)
 {
     IsDisposed = 1;
     SubBuffer.PoolBufferFull buffer = default(SubBuffer.PoolBufferFull);
     try
     {
         snapshot = new Snapshot.Cache(cache, true);
         FileInfo file = new FileInfo(cacheFile.isSwitchFile ? Config.GetFileName : Config.GetSwitchFileName);
         FileName = file.FullName;
         if (file.Exists)
         {
             AutoCSer.IO.File.MoveBak(FileName);
         }
         fileStream = new FileStream(FileName, FileMode.CreateNew, FileAccess.Write, FileShare.Read, bufferPool.Size, FileOptions.None);
         create(ref buffer, 0);
         return(ReturnType.Success);
     }
     catch (Exception error)
     {
         cache.TcpServer.AddLog(error);
     }
     finally
     {
         buffer.Free();
         if (IsDisposed == 0)
         {
             Interlocked.Exchange(ref fileFlushSeconds, Config.FileFlushSeconds);
             AutoCSer.Threading.ThreadPool.TinyBackground.Start(writeCache);
         }
         else
         {
             cache.NextGetter();
             if (fileStream != null)
             {
                 fileStream.Dispose();
             }
         }
     }
     return(ReturnType.SnapshotFileStartError);
 }
Beispiel #3
0
        /// <summary>
        /// 缓存加载完毕开始获取数据
        /// </summary>
        internal void Start()
        {
            bool isStart = false;

            try
            {
                snapshot     = new Snapshot.Cache(Cache, false);
                snapshotSize = snapshot.NextSize();
                timeout      = Date.Now.AddTicks(timeoutTicks);
                if (snapshotSize != 0)
                {
                    step = CacheGetStep.Snapshot;
                    if (onCache.Callback(new CacheReturnParameter {
                        Getter = this
                    }))
                    {
                        isStart = true;
                    }
                }
                else
                {
                    step    = CacheGetStep.TcpQueue;
                    isStart = true;
                    GetQueue();
                }
            }
            finally
            {
                if (!isStart)
                {
                    error();
                    onCache.Callback(default(CacheReturnParameter));
                    Cache.NextGetter();
                }
            }
        }
Beispiel #4
0
        /// <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;
            }
        }
Beispiel #5
0
        /// <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();
            }
        }