/// <summary>
        /// 添加定时任务节点
        /// </summary>
        /// <param name="node"></param>
        internal void Append(SecondTimerTaskNode node)
        {
            long timeoutSeconds = node.TimeoutSeconds;

            TimerLinkLock.Enter();
            long index = timeoutSeconds - linkArrayBaseSeconds;

            if (index < linkArrayCapacity)
            {
                if (index >= linkArrayIndex)
                {
                    linkArray[(int)index].Append(node);
                    TimerLinkLock.Exit();
                    return;
                }
            }
            else
            {
                index = ((index - linkArrayCapacity) >> linkArrayBitSize) + nextLinkArrayIndex;
                if (index < linkArrayCapacity)
                {
                    nextLinkArray[(int)index].Append(node);
                }
                else
                {
                    timerLink.Append(node);
                }
                TimerLinkLock.Exit();
                return;
            }
            TimerLinkLock.Exit();
            node.AppendCall();
        }
Beispiel #2
0
 public void Dispose()
 {
     searcher.State = null;
     Unmanaged.Free(ref data);
     lastLock.Enter();
     Unmanaged.Free(ref lastDomain);
     lastLock.Exit();
 }
Beispiel #3
0
        /// <summary>
        /// 删除文件缓存
        /// </summary>
        /// <param name="path"></param>
        private void remove(ref FileCacheKey path)
        {
            FileCache file;

            fileLock.Enter();
            if (files.Remove(ref path, out file))
            {
                freeCacheSize += file.Size;
            }
            fileLock.Exit();
        }
Beispiel #4
0
        /// <summary>
        /// 获取用户长连接轮询验证
        /// </summary>
        /// <param name="userId">用户标识</param>
        /// <param name="sessionId">长连接轮询验证,0表示失败</param>
        internal void Get(int userId, out AutoCSer.Net.HttpDomainServer.SessionId sessionId)
        {
            int index = userId >> 8;

            if ((uint)index < (uint)sessions.Length)
            {
                sessionLock.Enter();
                sessionId = sessions[index].Get(timeoutTicks);
                sessionLock.Exit();
            }
            else
            {
                sessionId = default(AutoCSer.Net.HttpDomainServer.SessionId);
            }
        }
Beispiel #5
0
 /// <summary>
 /// 新建客户端命令池
 /// </summary>
 /// <param name="currentIndex">当前空闲命令位置</param>
 /// <returns></returns>
 private int create(int currentIndex)
 {
     if (bitSize == maxArrayBitSize)
     {
         if (arrayCount == arrays.Length)
         {
             if (arrayCount == 1 << (Server.CommandIndexBits - maxArrayBitSize))
             {
                 freeIndex = currentIndex;
                 if (isErrorLog == 0)
                 {
                     isErrorLog = 1;
                     client.Log.Error("TCP 客户端活动会话数量过多", LogLevel.Error | LogLevel.AutoCSer);
                 }
                 return(0);
             }
             arrays = arrays.copyNew(arrayCount << 1);
         }
         int           index = 1 << maxArrayBitSize;
         CommandLink[] array = new CommandLink[index];
         do
         {
             array[index - 1].Next = commandCount + index;
         }while (--index != 0);
         arrays[arrayCount++] = array;
         freeEndIndexLock.Enter();
         arrays[freeEndIndex >> bitSize][freeEndIndex & arraySizeAnd].Next = commandCount;
         freeEndIndex = (commandCount += 1 << maxArrayBitSize) - 1;
         freeEndIndexLock.Exit();
     }
     else
     {
         CommandLink[] array = new CommandLink[1 << ++bitSize];
         for (int index = commandCount, endIndex = commandCount << 1; index != endIndex; ++index)
         {
             array[index].Next = index + 1;
         }
         freeEndIndexLock.Enter();
         Array.CopyTo(array, 0);
         arrays[0] = Array = array;
         array[freeEndIndex].Next = commandCount;
         freeEndIndex             = arraySizeAnd = (commandCount <<= 1) - 1;
         freeEndIndexLock.Exit();
     }
     freeIndex = (currentIndex < (1 << maxArrayBitSize) ? Array : pushArray)[currentIndex & arraySizeAnd].Next;
     return(currentIndex);
 }
Beispiel #6
0
        /// <summary>
        /// 回调处理
        /// </summary>
        private void onReceive()
        {
            Callback <ReturnValue <outputParameterType> > callback = Callback;

            if (callback != null)
            {
                OutputLock.Enter();
                do
                {
                    currentOutputParameters.Exchange(ref outputParameters);
                    OutputLock.Exit();
                    ReturnValue <outputParameterType>[] outputParameterArray = currentOutputParameters.Array;
                    int index = 0, count = currentOutputParameters.Length;
                    do
                    {
                        try
                        {
                            do
                            {
                                callback.Call(ref outputParameterArray[index]);
                            }while (++index != count);
                            break;
                        }
                        catch (Exception error)
                        {
                            Socket.Log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer);
                        }
                    }while (++index != count);
                    if (AutoCSer.DynamicArray <outputParameterType> .IsClearArray)
                    {
                        System.Array.Clear(outputParameterArray, 0, count);
                    }
                    currentOutputParameters.Length = 0;

                    OutputLock.Enter();
                    if (outputParameters.Length == 0)
                    {
                        isOutput = 0;
                        OutputLock.Exit();
                        return;
                    }
                }while (true);
            }
        }
Beispiel #7
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();
        }
Beispiel #8
0
        /// <summary>
        /// 回调处理
        /// </summary>
        private void onReceive()
        {
            Action <ReturnValue> callback = Callback;

            if (callback != null)
            {
                OutputLock.Enter();
                do
                {
                    currentOutputParameters.Exchange(ref outputParameters);
                    OutputLock.Exit();
                    ReturnType[] outputParameterArray = currentOutputParameters.Array;
                    int          index = 0, count = currentOutputParameters.Length;
                    do
                    {
                        try
                        {
                            do
                            {
                                callback(new ReturnValue {
                                    Type = outputParameterArray[index]
                                });
                            }while (++index != count);
                            break;
                        }
                        catch (Exception error)
                        {
                            Socket.Log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer);
                        }
                    }while (++index != count);
                    currentOutputParameters.Length = 0;

                    OutputLock.Enter();
                    if (outputParameters.Length == 0)
                    {
                        isOutput = 0;
                        OutputLock.Exit();
                        return;
                    }
                }while (true);
            }
        }
Beispiel #9
0
        /// <summary>
        /// 获取服务端节点标识
        /// </summary>
        /// <param name="remoteTypes"></param>
        /// <returns></returns>
        internal unsafe static int[] Get(AutoCSer.Reflection.RemoteType[] remoteTypes)
        {
            int[] ids = new int[remoteTypes.Length];
            fixed(int *idFixed = ids)
            {
                int *idStart = idFixed;

                foreach (AutoCSer.Reflection.RemoteType remoteType in remoteTypes)
                {
                    Type type;
                    if (remoteType.TryGet(out type))
                    {
                        if (types.TryGetValue(type, out *idStart))
                        {
                            ++idStart;
                        }
                        else
                        {
                            typeLock.Enter();
                            if (types.TryGetValue(type, out *idStart))
                            {
                                typeLock.Exit();
                                ++idStart;
                            }
                            else
                            {
                                typeLock.SleepFlag = 1;
                                try
                                {
                                    Func <Node> createNode = (Func <Node>)Delegate.CreateDelegate(typeof(Func <Node>), createNodeMethod.MakeGenericMethod(type));
                                    int         id         = createNodes.Length;
                                    createNodes.Add(createNode);
                                    types.Add(type, id);
                                    *idStart++ = id;
                                }
                                finally { typeLock.ExitSleepFlag(); }
                            }
                        }
                    }
                    else
                    {
                        *idStart++ = 0;
                    }
                }
            }

            return(ids);
        }
Beispiel #10
0
        /// <summary>
        /// 检测短路径的有效性
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        private ReturnType check(MasterServer.TcpInternalClient client)
        {
            createLock.Enter();
            switch (Identity.Type)
            {
            case ReturnType.NotFoundShortPathNode: createLock.Exit(); return(ReturnType.NotFoundShortPathNode);

            case ReturnType.NotFoundShortPath: reCreate(client); return(Identity.Type);
            }
            if (socketIdentity == Client.SocketIdentity)
            {
                createLock.Exit();
            }
            else
            {
                reCreate(client);
            }
            return(Identity.Type);
        }
Beispiel #11
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);
        }