Пример #1
0
 /// <summary>
 /// 文件流写入器
 /// </summary>
 /// <param name="cacheFile"></param>
 internal FileStreamWriter(FileStreamWriter cacheFile)
 {
     cache       = cacheFile.cache;
     Config      = cacheFile.Config;
     bufferPool  = SubBuffer.Pool.GetPool(Config.BufferSize);
     flushHandle = flush;
 }
Пример #2
0
        static SocketBase()
        {
            BufferPool = SubBuffer.Pool.GetPool((int)Config.BufferSize >= (int)Config.HeadSize ? Config.BufferSize : Config.HeadSize);

            int dateCacheSize = (Date.ToByteLength + 7) & (int.MaxValue - 7), AutoCSerServerSize = (AutoCSerServer.Length + 7) & (int.MaxValue - 7);

            dateCache = new Pointer {
                Data = Unmanaged.GetStatic64(dateCacheSize + AutoCSerServerSize, false)
            };
            responseServer = new Pointer {
                Data = dateCache.Byte + dateCacheSize
            };

            fixed(char *AutoCSerServerFixed = AutoCSerServer) StringExtension.WriteBytesNotNull(AutoCSerServerFixed, AutoCSerServer.Length, responseServer.Byte);

            byte[] responseServerEnd = (AutoCSerServer + @"Content-Length: 0

").getBytes();
            errorResponseDatas = new byte[EnumAttribute <ResponseState> .GetMaxValue(-1) + 1][];
            foreach (ResponseState type in System.Enum.GetValues(typeof(ResponseState)))
            {
                ResponseStateAttribute state = EnumAttribute <ResponseState, ResponseStateAttribute> .Array((int)type);

                if (state != null && state.IsError)
                {
                    string stateData    = state.Text;
                    byte[] responseData = new byte[httpVersionSize + stateData.Length + responseServerEnd.Length];
                    fixed(byte *responseDataFixed = responseData)
                    {
                        writeHttpVersion(responseDataFixed);
                        state.Write(responseDataFixed + httpVersionSize);
                    }

                    int index = httpVersionSize + stateData.Length;
                    System.Buffer.BlockCopy(responseServerEnd, 0, responseData, index, responseServerEnd.Length);
                    errorResponseDatas[(int)type] = responseData;
                    if (type == ResponseState.NotFound404)
                    {
                        byte[] html          = Encoding.UTF8.GetBytes("<html><head>" + AutoCSer.WebView.CharsetTypeAttribute.GetHtml(AutoCSer.WebView.CharsetType.Utf8) + "<title>404 Error, 请将链接中的 #! 或者 # 修改为 ?</title></head><body>404 Error, 请将链接中的 #! 或者 # 修改为 ?</body></html>");
                        byte[] contentLength = EncodingCache.Ascii.GetBytesNotEmpty(AutoCSerServer + "Content-Length: " + html.Length.toString() + @"

");
                        searchEngine404Data = new byte[httpVersionSize + stateData.Length + contentLength.Length + html.Length];
                        fixed(byte *responseDataFixed = searchEngine404Data)
                        {
                            writeHttpVersion(responseDataFixed);
                            state.Write(responseDataFixed + httpVersionSize);
                        }

                        System.Buffer.BlockCopy(contentLength, 0, searchEngine404Data, index = httpVersionSize + stateData.Length, contentLength.Length);
                        System.Buffer.BlockCopy(html, 0, searchEngine404Data, index         += contentLength.Length, html.Length);
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 /// 文件流写入器
 /// </summary>
 /// <param name="fileName">文件全名</param>
 /// <param name="mode">打开方式</param>
 /// <param name="fileShare">共享访问方式</param>
 /// <param name="fileOption">附加选项</param>
 /// <param name="bufferSize">缓冲区字节大小</param>
 /// <param name="log">日志处理</param>
 /// <param name="encoding">文件编码</param>
 internal FileStreamWriter(string fileName, FileMode mode, FileShare fileShare, FileOptions fileOption, SubBuffer.Size bufferSize, ILog log, EncodingCache encoding)
 {
     if (string.IsNullOrEmpty(fileName))
     {
         throw new ArgumentNullException("fileName is null");
     }
     FileName        = fileName;
     this.log        = log;
     this.fileShare  = fileShare;
     this.fileOption = fileOption;
     this.encoding   = encoding;
     bufferPool      = SubBuffer.Pool.GetPool(bufferSize);
     open(mode);
 }
Пример #4
0
        /// <summary>
        /// 队列数据 写文件
        /// </summary>
        /// <param name="node">消息队列节点</param>
        internal QueueWriter(QueueNode node)
        {
            this.node  = node;
            Config     = node.Cache.MasterConfig;
            bufferPool = SubBuffer.Pool.GetPool(Config.BufferSize);
            DirectoryInfo directory = new DirectoryInfo(node.FilePath);

            if (!directory.Exists)
            {
                directory.Create();
            }
            FilePath      = directory.fullName();
            disposeHandle = Dispose;
            isWrite       = 1;
        }
Пример #5
0
        /// <summary>
        /// 队列数据 写文件
        /// </summary>
        /// <param name="node">消息队列节点</param>
        internal FileWriter(Node node)
        {
            Node       = node;
            Config     = node.Cache.MasterConfig;
            bufferPool = SubBuffer.Pool.GetPool(Config.BufferSize);
            DirectoryInfo directory = new DirectoryInfo(node.FilePath);

            if (!directory.Exists)
            {
                directory.Create();
            }
            FilePath      = directory.fullName();
            disposeHandle = Dispose;
            onStartQueue  = new QueueTaskThread.Node.Queue(new QueueTaskThread.Null(this));
            isWrite       = 1;
        }
Пример #6
0
 /// <summary>
 /// TCP 服务客户端
 /// </summary>
 /// <param name="serviceName">服务名称</param>
 /// <param name="receiveBufferPool">接受数据缓存区池</param>
 /// <param name="sendBufferPool">发送接受数据缓存区池</param>
 /// <param name="sendBufferMaxSize">发送数据缓存区最大字节大小</param>
 /// <param name="minCompressSize">压缩启用最低字节数量</param>
 /// <param name="log">日志接口</param>
 internal CommandBuffer(string serviceName, SubBuffer.Pool receiveBufferPool, SubBuffer.Pool sendBufferPool, int sendBufferMaxSize, int minCompressSize, ILog log)
     : base(serviceName, sendBufferMaxSize, minCompressSize, log)
 {
     SendBufferPool    = sendBufferPool;
     ReceiveBufferPool = receiveBufferPool;
 }
Пример #7
0
 /// <summary>
 /// TCP 服务客户端
 /// </summary>
 /// <param name="attribute">TCP 服务调用配置</param>
 /// <param name="receiveBufferSize">接受数据缓冲区字节大小</param>
 /// <param name="sendBufferSize">发送数据缓冲区字节大小</param>
 /// <param name="sendBufferMaxSize">发送数据缓存区最大字节大小</param>
 /// <param name="log">日志接口</param>
 internal CommandBuffer(ServerBaseAttribute attribute, AutoCSer.Memory.BufferSize receiveBufferSize, AutoCSer.Memory.BufferSize sendBufferSize, int sendBufferMaxSize, ILog log)
     : base(attribute, sendBufferMaxSize, log)
 {
     SendBufferPool    = SubBuffer.Pool.GetPool(sendBufferSize);
     ReceiveBufferPool = SubBuffer.Pool.GetPool(receiveBufferSize);
 }
Пример #8
0
 internal void Get(SubBuffer.Pool bufferPool)
 {
     bufferPool.Get(ref Buffer);
     WriteIndex = Buffer.StartIndex;
     IsWait     = false;
 }
Пример #9
0
        /// <summary>
        /// 文件流写入器
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="config"></param>
        internal FileStreamWriter(CacheManager cache, MasterServerConfig config)
        {
            this.cache = cache;
            Config     = config;
            bufferPool = SubBuffer.Pool.GetPool(config.BufferSize);
            FileInfo file = new FileInfo(config.GetFileName);

            FileName   = file.FullName;
            IsDisposed = 1;
            FileMode    createMode = FileMode.CreateNew;
            FileBuffers buffer     = default(FileBuffers);

            try
            {
                if (file.Exists)
                {
                    if (file.Length == 0)
                    {
                        createMode = FileMode.Create;
                    }
                    else
                    {
                        fileStream = new FileStream(FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, bufferPool.Size, FileOptions.None);
                        bufferPool.Get(ref buffer.Buffer);
                        if (fileStream.Read(buffer.Buffer.Buffer, buffer.Buffer.StartIndex, fileHeaderSize) == fileHeaderSize)
                        {
                            fixed(byte *bufferFixed = buffer.Buffer.Buffer)
                            {
                                byte *bufferStart = bufferFixed + buffer.Buffer.StartIndex;

                                if (*(int *)bufferStart == FileHeader)
                                {
                                    Version = *(ulong *)(bufferStart + sizeof(int) * 2);
                                    FileInfo switchFile = new FileInfo(config.GetSwitchFileName);
                                    if (switchFile.Exists)
                                    {
                                        FileStream switchFileStream = new FileStream(switchFile.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, bufferPool.Size, FileOptions.None);
                                        if (switchFileStream.Read(buffer.Buffer.Buffer, buffer.Buffer.StartIndex, fileHeaderSize) == fileHeaderSize && *(int *)bufferStart == FileHeader &&
                                            *(ulong *)(bufferStart + sizeof(int) * 2) > Version)
                                        {
                                            fileStream.Dispose();
                                            Version      = *(ulong *)(bufferStart + sizeof(int) * 2);
                                            fileStream   = switchFileStream;
                                            FileName     = switchFile.FullName;
                                            isSwitchFile = true;
                                        }
                                    }
                                    if (Version > 0)
                                    {
                                        LoadData loadData = new LoadData {
                                            Buffer = BufferLink.Head
                                        };
                                        int  count = fileStream.Read(buffer.Buffer.Buffer, buffer.Buffer.StartIndex, buffer.Buffer.Length), index = 0, compressionDataSize;
                                        long nextLength = (FileLength = fileStream.Length) - fileHeaderSize - count;
                                        do
                                        {
                                            while (count >= sizeof(int) * 2)
                                            {
                                                byte *read = bufferStart + index;
                                                if ((compressionDataSize = *(int *)read) < 0)
                                                {
                                                    if (count >= (compressionDataSize = -compressionDataSize) + sizeof(int) * 2)
                                                    {
                                                        buffer.CompressionBuffer.StartIndex = *(int *)(read + sizeof(int));
                                                        AutoCSer.IO.Compression.DeflateDeCompressor.Get(buffer.Buffer.Buffer, buffer.Buffer.StartIndex + (index += sizeof(int) * 2), compressionDataSize, ref buffer.CompressionBuffer);
                                                        if (buffer.CompressionBuffer.Buffer != null)
                                                        {
                                                            fixed(byte *dataFixed = buffer.CompressionBuffer.Buffer)
                                                            {
                                                                loadData.Set(buffer.CompressionBuffer.Buffer, buffer.CompressionBuffer.StartIndex, *(int *)(read + sizeof(int)), dataFixed);
                                                                if (!cache.Load(ref loadData))
                                                                {
                                                                    throw new InvalidDataException();
                                                                }
                                                            }
                                                            index += compressionDataSize;
                                                            count -= compressionDataSize + sizeof(int) * 2;
                                                            buffer.CompressionBuffer.Free();
                                                        }
                                                        else
                                                        {
                                                            throw new InvalidDataException();
                                                        }
                                                    }
                                                    else if (count + nextLength >= compressionDataSize + sizeof(int) * 2)
                                                    {
                                                        if (compressionDataSize + sizeof(int) * 2 <= buffer.Buffer.StartIndex)
                                                        {
                                                            break;
                                                        }
                                                        if (bigBuffer.Length < compressionDataSize)
                                                        {
                                                            bigBuffer = new byte[Math.Max(compressionDataSize, bigBuffer.Length << 1)];
                                                        }
                                                        System.Buffer.BlockCopy(buffer.Buffer.Buffer, buffer.Buffer.StartIndex + (index + sizeof(int) * 2), bigBuffer, 0, count -= sizeof(int) * 2);
                                                        do
                                                        {
                                                            index       = fileStream.Read(bigBuffer, count, compressionDataSize - count);
                                                            nextLength -= index;
                                                            count      += index;
                                                        }while (count != compressionDataSize);
                                                        buffer.CompressionBuffer.StartIndex = *(int *)(read + sizeof(int));
                                                        AutoCSer.IO.Compression.DeflateDeCompressor.Get(bigBuffer, 0, compressionDataSize, ref buffer.CompressionBuffer);
                                                        if (buffer.CompressionBuffer.Buffer != null)
                                                        {
                                                            fixed(byte *dataFixed = buffer.CompressionBuffer.Buffer)
                                                            {
                                                                loadData.Set(buffer.CompressionBuffer.Buffer, buffer.CompressionBuffer.StartIndex, *(int *)(read + sizeof(int)), dataFixed);
                                                                if (!cache.Load(ref loadData))
                                                                {
                                                                    throw new InvalidDataException();
                                                                }
                                                            }
                                                            index = count = 0;
                                                            buffer.CompressionBuffer.Free();
                                                        }
                                                        else
                                                        {
                                                            throw new InvalidDataException();
                                                        }
                                                    }
                                                    else
                                                    {
                                                        endError(ref buffer.Buffer, nextLength, index, count);
                                                        return;
                                                    }
                                                }
                                                else if (count >= compressionDataSize + sizeof(int))
                                                {
                                                    loadData.Set(buffer.Buffer.Buffer, buffer.Buffer.StartIndex + (index += sizeof(int)), compressionDataSize, bufferFixed);
                                                    if (!cache.Load(ref loadData))
                                                    {
                                                        throw new InvalidDataException();
                                                    }
                                                    index += compressionDataSize;
                                                    count -= compressionDataSize + sizeof(int);
                                                }
                                                else if (count + nextLength >= compressionDataSize + sizeof(int))
                                                {
                                                    if (compressionDataSize + sizeof(int) <= buffer.Buffer.StartIndex)
                                                    {
                                                        break;
                                                    }
                                                    if (bigBuffer.Length < compressionDataSize)
                                                    {
                                                        bigBuffer = new byte[Math.Max(compressionDataSize, bigBuffer.Length << 1)];
                                                    }
                                                    System.Buffer.BlockCopy(buffer.Buffer.Buffer, buffer.Buffer.StartIndex + (index + sizeof(int)), bigBuffer, 0, count -= sizeof(int));
                                                    do
                                                    {
                                                        index       = fileStream.Read(bigBuffer, count, compressionDataSize - count);
                                                        nextLength -= index;
                                                        count      += index;
                                                    }while (count != compressionDataSize);
                                                    fixed(byte *dataFixed = bigBuffer)
                                                    {
                                                        loadData.Set(bigBuffer, 0, compressionDataSize, dataFixed);
                                                        if (!cache.Load(ref loadData))
                                                        {
                                                            throw new InvalidDataException();
                                                        }
                                                    }
                                                    index = count = 0;
                                                }
                                                else
                                                {
                                                    endError(ref buffer.Buffer, nextLength, index, count);
                                                    return;
                                                }
                                            }
                                            if (nextLength == 0)
                                            {
                                                if (count == 0)
                                                {
                                                    fileStream.Seek(0, SeekOrigin.End);
                                                    IsDisposed = 0;
                                                }
                                                else
                                                {
                                                    endError(ref buffer.Buffer, nextLength, index, count);
                                                }
                                                return;
                                            }
                                            if (count != 0)
                                            {
                                                System.Buffer.BlockCopy(buffer.Buffer.Buffer, buffer.Buffer.StartIndex + index, buffer.Buffer.Buffer, buffer.Buffer.StartIndex, count);
                                            }
                                            index       = fileStream.Read(buffer.Buffer.Buffer, buffer.Buffer.StartIndex + count, buffer.Buffer.Length - count);
                                            nextLength -= index;
                                            count      += index;
                                            index       = 0;
                                        }while (true);
                                    }
                                }
                            }
                        }
                        if (!config.IsMoveBakUnknownFile)
                        {
                            throw new InvalidDataException();
                        }
                        fileStream.Dispose();
                        AutoCSer.IO.File.MoveBak(FileName);
                    }
                }
                fileStream = new FileStream(FileName, createMode, FileAccess.Write, FileShare.Read, bufferPool.Size, FileOptions.None);
                create(ref buffer.Buffer, Version);
            }
            finally
            {
                buffer.Free();
                BufferLink.Head.Array.SetNull();
                if (IsDisposed == 0)
                {
                    fileFlushSeconds = config.FileFlushSeconds;
                    Writers.PushNotNull(this);
                    OnTime.Set(Date.NowTime.OnTimeFlag.CacheFile);
                }
                else if (fileStream != null)
                {
                    fileStream.Dispose();
                }
            }
        }