Ejemplo n.º 1
0
        /// <summary>
        /// 释放所有命令
        /// </summary>
        /// <param name="head"></param>
        /// <param name="end"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        internal ClientCommand.CommandBase Free(ClientCommand.CommandBase head, ClientCommand.CommandBase end, int startIndex)
        {
            DisposeTimeout();
            bool isNext = false;

            freeEndIndexLock.EnterSleepFlag();
            try
            {
                foreach (CommandLink[] array in arrays)
                {
                    if (isNext)
                    {
                        if (array == null)
                        {
                            break;
                        }
                        for (startIndex = array.Length; startIndex != 0;)
                        {
                            ClientCommand.Command command = array[--startIndex].Command;
                            if (command != null)
                            {
                                array[startIndex].Command = null;
                                if (head == null)
                                {
                                    head = end = command;
                                }
                                else
                                {
                                    end.LinkNext = command;
                                    end          = command;
                                }
                            }
                        }
                    }
                    else
                    {
                        isNext = true;
                        do
                        {
                            ClientCommand.Command command = array[startIndex].Command;
                            if (command != null)
                            {
                                array[startIndex].Command = null;
                                if (head == null)
                                {
                                    head = end = command;
                                }
                                else
                                {
                                    end.LinkNext = command;
                                    end          = command;
                                }
                            }
                        }while (++startIndex != array.Length);
                    }
                }
            }
            finally { freeEndIndexLock.ExitSleepFlag(); }
            return(head);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 输出错误状态
        /// </summary>
        /// <param name="state">错误状态</param>
        internal void ResponseError(ResponseState state)
        {
            if (DomainServer != null)
            {
                Response response = DomainServer.GetErrorResponseData(state, Header.IsGZip);
                if (response != null)
                {
                    if (state != ResponseState.NotFound404 || Header.Method != MethodType.GET)
                    {
                        Header.Flag &= HeaderFlag.All ^ HeaderFlag.IsKeepAlive;
                    }
                    if (responseHeader(ref response)) return;
                }
            }
            byte[] data = errorResponseDatas[(int)state];
            System.Net.Sockets.Socket socket = Socket;
            if (data != null && socket != null)
            {
                try
                {
                    SendType = state == ResponseState.NotFound404 && Header.Method == MethodType.GET ? SendType.Next : SendType.Close;
                    Data.Set(data, 0, data.Length);
                    Timeout = Config.GetTimeout(Data.Length);
#if DOTNET2
                    SocketError socketError;
                    IAsyncResult async = socket.BeginSend(data, 0, Data.Length, SocketFlags.None, out socketError, onSendAsyncCallback, socket);
                    if (socketError == SocketError.Success)
                    {
                        if (!async.CompletedSynchronously) Http.Header.ReceiveTimeout.Push(this, socket);
                        return;
                    }
#else
                    sendAsyncLock.EnterSleepFlag();
                    sendAsyncEventArgs.SetBuffer(data, 0, Data.Length);
                    if (socket.SendAsync(sendAsyncEventArgs))
                    {
                        sendAsyncLock.SleepFlag = 0;
                        Http.Header.ReceiveTimeout.Push(this, socket);
                        sendAsyncLock.Exit();
                        return;
                    }
                    sendAsyncLock.ExitSleepFlag();
                    if (onSend()) return;
#endif
                }
                catch (Exception error)
                {
                    Server.RegisterServer.TcpServer.Log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer);
                }
            }
            HeaderError();
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 设置文件缓存
 /// </summary>
 /// <param name="path"></param>
 /// <param name="fileCache"></param>
 /// <param name="fileSize"></param>
 private void set(ref FileCacheKey path, FileCache fileCache, int fileSize)
 {
     fileLock.EnterSleepFlag();
     try
     {
         fileCache.Size = fileSize;
         FileCache oldFileCache = files.Set(ref path, fileCache);
         freeCacheSize -= fileSize;
         if (oldFileCache == null)
         {
             while (freeCacheSize < 0 && files.Count > 1)
             {
                 freeCacheSize += files.UnsafePopValue().Size;
             }
         }
         else if (oldFileCache != fileCache)
         {
             freeCacheSize += oldFileCache.Size;
         }
     }
     finally { fileLock.ExitSleepFlag(); }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 输出 HTTP 响应数据
        /// </summary>
        /// <param name="response">HTTP 响应数据</param>
        private void response(ref Response response)
        {
            bool isHeaderError = false;
            try
            {
                CheckNotChanged304(ref response);
                if (Header.Method == MethodType.POST && (Flag & SocketFlag.IsLoadForm) == 0)
                {
                    Header.IgnoreContentLength();
                    if (Header.ContentLength == 0)
                    {
                        isHeaderError = true;
                        if (responseHeader(ref response)) return;
                    }
                    else
                    {
                        System.Net.Sockets.Socket socket = Socket;
                        if (socket == null) Http.Response.Push(ref response);
                        else
                        {
                            this.HttpResponse = response;
                            ReceiveType = ReceiveType.Response;
                            Data.Set(Buffer.Buffer, Buffer.StartIndex, Math.Min(Header.ContentLength, Buffer.Length));
                            response = null;
                            Timeout = Config.GetTimeout(Header.ContentLength);
#if DOTNET2
                                SocketError socketError;
                                IAsyncResult async = socket.BeginReceive(Buffer.Buffer, Buffer.StartIndex, Data.Length, SocketFlags.None, out socketError, onReceiveAsyncCallback, socket);
                                if (socketError == SocketError.Success)
                                {
                                    if (!async.CompletedSynchronously) Http.Header.ReceiveTimeout.Push(this, socket);
                                    return;
                                }
#else
                            receiveAsyncEventArgs.SocketError = SocketError.Success;
                            ReceiveAsyncLock.EnterSleepFlag();
                            receiveAsyncEventArgs.SetBuffer(Buffer.Buffer, Buffer.StartIndex, Data.Length);
                            if (socket.ReceiveAsync(receiveAsyncEventArgs))
                            {
                                ReceiveAsyncLock.SleepFlag = 0;
                                Http.Header.ReceiveTimeout.Push(this, socket);
                                ReceiveAsyncLock.Exit();
                                return;
                            }
                            ReceiveAsyncLock.ExitSleepFlag();
                            isHeaderError = true;
                            if (onReceive()) return;
#endif
                        }
                    }
                }
                else
                {
                    isHeaderError = true;
                    if (responseHeader(ref response)) return;
                }
            }
            catch (Exception error)
            {
                Server.RegisterServer.TcpServer.Log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer);
            }
            if (isHeaderError) HeaderError();
            else ResponseError(ResponseState.ServerError500);
        }