Пример #1
0
        /// <summary>
        /// 弹出节点
        /// </summary>
        /// <returns></returns>
        internal static SocketAsyncEventArgs Get()
        {
            popLock.EnterYield();
            SocketAsyncEventArgs value;

            do
            {
                if ((value = head) == null)
                {
                    popLock.Exit();
                    value                       = new SocketAsyncEventArgs();
                    value.SocketFlags           = System.Net.Sockets.SocketFlags.None;
                    value.DisconnectReuseSocket = false;
                    return(value);
                }
                if (Interlocked.CompareExchange(ref head, new UnionType.SocketAsyncEventArgs {
                    Object = value.UserToken
                }.Value, value) == value)
                {
                    popLock.Exit();
                    System.Threading.Interlocked.Decrement(ref count);
                    value.UserToken = null;
                    return(value);
                }
                AutoCSer.Threading.ThreadYield.Yield();
            }while (true);
        }
Пример #2
0
 /// <summary>
 /// 添加任务
 /// </summary>
 /// <param name="value"></param>
 internal void Add(LinkQueueTaskNode value)
 {
     queueLock.EnterYield();
     if (head == null)
     {
         head = end = value;
     }
     else
     {
         end.LinkNext = value;
         end          = value;
     }
     queueLock.Exit();
     if (System.Threading.Interlocked.CompareExchange(ref isThread, 1, 0) == 0)
     {
         try
         {
             threadPool.FastStart(run);
         }
         catch (Exception error)
         {
             AutoCSer.LogHelper.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer);
             run();
         }
     }
 }
Пример #3
0
        /// <summary>
        /// 获取下一个随机字节
        /// </summary>
        /// <returns></returns>
        public byte NextByte()
        {
START:
            byteLock.EnterYield();
            if (byteCount == 0)
            {
                byteCount = -1;
                byteLock.Exit();
                byte value = (byte)(bytes = NextULong());
                bytes >>= 8;
                byteLock.EnterYield();
                byteCount = 7;
                byteLock.Exit();
                return(value);
            }
            else if (byteCount > 0)
            {
                byte value = (byte)bytes;
                --byteCount;
                bytes >>= 8;
                byteLock.Exit();
                return(value);
            }
            else
            {
                byteLock.Exit();
                while (byteCount < 0)
                {
                    AutoCSer.Threading.ThreadYield.Yield();
                }
                goto START;
            }
        }
Пример #4
0
        /// <summary>
        /// 获取下一个随机双字节
        /// </summary>
        /// <returns></returns>
        public ushort NextUShort()
        {
START:
            ushortLock.EnterYield();
            if (ushortCount == 0)
            {
                ushortCount = -1;
                ushortLock.Exit();
                ushort value = (ushort)(ushorts = NextULong());
                ushorts >>= 16;
                ushortLock.EnterYield();
                ushortCount = 3;
                ushortLock.Exit();
                return(value);
            }
            else if (ushortCount > 0)
            {
                ushort value = (ushort)ushorts;
                --ushortCount;
                ushorts >>= 16;
                ushortLock.Exit();
                return(value);
            }
            else
            {
                ushortLock.Exit();
                while (ushortCount < 0)
                {
                    AutoCSer.Threading.ThreadYield.Yield();
                }
                goto START;
            }
        }
Пример #5
0
 /// <summary>
 /// 获取成员位图内存池
 /// </summary>
 /// <param name="size">成员位图字节数量</param>
 /// <returns></returns>
 internal static MemberMapPool GetPool(int size)
 {
     if (size > 0)
     {
         int index = size >> 3;
         if (index < pools.Length)
         {
             MemberMapPool pool = pools[index];
             if (pool != null)
             {
                 return(pool);
             }
             poolLock.EnterSleep();
             if ((pool = pools[index]) == null)
             {
                 try
                 {
                     pools[index] = pool = new MemberMapPool(size);
                 }
                 finally { poolLock.Exit(); }
             }
             else
             {
                 poolLock.Exit();
             }
             return(pool);
         }
     }
     return(null);
 }
Пример #6
0
 /// <summary>
 /// 加载成员处理
 /// </summary>
 /// <param name="memberIndex"></param>
 public void LoadMember(int memberIndex)
 {
     if ((uint)memberIndex < waitMap.Length << 3)
     {
         waitMapLock.EnterYield();
         if ((waitMap[memberIndex >> 3] & (1 << (int)(memberIndex & 7))) == 0)
         {
             waitMapLock.Exit();
         }
         else
         {
             waitMap[memberIndex >> 3] ^= (byte)(1 << (int)(memberIndex & 7));
             if (--waitCount == 0)
             {
                 waitMapLock.Exit();
                 loaded();
                 waitMap = null;
             }
             else
             {
                 waitMapLock.Exit();
             }
         }
     }
 }
Пример #7
0
        /// <summary>
        /// 获取成员位图
        /// </summary>
        /// <returns>成员位图</returns>
        public byte *Get()
        {
            byte *value;

            freeLock.EnterYield();
            if (free != null)
            {
                value = free;
                free  = *(byte **)free;
                freeLock.Exit();
                return(value);
            }
            freeLock.Exit();

            memoryLock.Enter();
            value = memoryStart;
            if ((memoryStart += size) <= memoryEnd)
            {
                memoryLock.Exit();
                return(value);
            }
            memoryLock.SleepFlag = 1;
            try
            {
                value       = (byte *)AutoCSer.Memory.Unmanaged.GetStatic(memorySize, false);
                memoryStart = value + size;
                memoryEnd   = value + memorySize;
            }
            finally
            {
                ++memoryCount;
                memoryLock.ExitSleepFlag();
            }
            return(value);
        }
Пример #8
0
        /// <summary>
        /// 获取数据缓冲区
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        internal static Buffer GetBuffer(int size)
        {
            if (size > (int)BufferSize)
            {
                return(new Buffer(size));
            }
            Buffer buffer;

            bufferCountLock.EnterYield();
            try
            {
                buffer = bufferCount.get(size);
            }
            finally { bufferCountLock.Exit(); }

            if (buffer == null)
            {
                byte        step = 0xff;
                BufferCount newBufferCount = null, oldBufferCount;
                Monitor.Enter(newBufferCountLock);
                oldBufferCount = bufferCount;
                try
                {
                    bufferCountLock.EnterYield();
                    step   = 0;
                    buffer = oldBufferCount.get(size);
                    bufferCountLock.Exit();
                    step = 0xff;

                    if (buffer == null)
                    {
                        newBufferCount = new BufferCount();
                        step           = 1;
                        buffer         = newBufferCount.get(size);
                        if (newBufferCount.Size > oldBufferCount.Size)
                        {
                            step        = 0xff;
                            bufferCount = newBufferCount;
                            oldBufferCount.Free();
                        }
                    }
                }
                finally
                {
                    switch (step)
                    {
                    case 0: bufferCountLock.Exit(); break;

                    case 1: newBufferCount.Free(); break;
                    }
                    Monitor.Exit(newBufferCountLock);
                }
            }
            return(buffer);
        }
Пример #9
0
        /// <summary>
        /// 继续接收数据
        /// </summary>
        /// <returns>是否接收成功</returns>
        private unsafe bool receive()
        {
#if DOTNET2
            if (buffer == null)
            {
                buffer         = new BufferCount();
                bufferIndex    = buffer.Buffer.StartIndex;
                bufferEndIndex = bufferIndex + BufferPool.Size;
            }
            socket.BeginReceive(buffer.Buffer.Buffer, bufferIndex, bufferEndIndex - bufferIndex, SocketFlags.None, out socketError, onReceiveAsyncCallback, socket);
            return(socketError == SocketError.Success);
#else
START:
            if (buffer == null)
            {
                buffer         = new BufferCount();
                bufferIndex    = buffer.Buffer.StartIndex;
                bufferEndIndex = bufferIndex + BufferPool.Size;
#if !DotNetStandard
                receiveAsyncLock.EnterYield();
#endif
                async.SetBuffer(buffer.Buffer.Buffer, bufferIndex, BufferPool.Size);
                if (socket.ReceiveAsync(async))
                {
#if !DotNetStandard
                    receiveAsyncLock.Exit();
#endif
                    return(true);
                }
            }
            else
            {
#if !DotNetStandard
                receiveAsyncLock.EnterYield();
#endif
                async.SetBuffer(bufferIndex, bufferEndIndex - bufferIndex);
                if (socket.ReceiveAsync(async))
                {
#if !DotNetStandard
                    receiveAsyncLock.Exit();
#endif
                    return(true);
                }
            }
#if !DotNetStandard
            receiveAsyncLock.Exit();
#endif
            if (async.SocketError == SocketError.Success)
            {
                onReceive(async.BytesTransferred);
                goto START;
            }
            return(false);
#endif
        }
Пример #10
0
        /// <summary>
        /// 添加清除缓存数据委托
        /// </summary>
        /// <param name="clearCache"></param>
        /// <returns>false 表示已经添加过,无需重复添加</returns>
        public static bool AddClearCache(Action <int> clearCache)
        {
            ReferenceHashKey <Action <int> > hashKey = new ReferenceHashKey <Action <int> >(clearCache);

            clearCacheLock.EnterSleep();
            try
            {
                return(clearCountCaches.Add(hashKey));
            }
            finally { clearCacheLock.Exit(); }
        }
Пример #11
0
 internal DbConnection Pop()
 {
     arrayLock.EnterYield();
     if (index != 0)
     {
         DbConnection value = array[--index];
         arrayLock.Exit();
         return(value);
     }
     arrayLock.Exit();
     return(null);
 }
Пример #12
0
 /// <summary>
 /// 检测当前时间戳
 /// </summary>
 /// <param name="ticks"></param>
 /// <param name="senderTimeVerifyTicks"></param>
 /// <returns></returns>
 public bool Check(ref long ticks, ref long senderTimeVerifyTicks)
 {
     if (ticks <= lastVerifyTicks && ticks != senderTimeVerifyTicks)
     {
         if (senderTimeVerifyTicks == 0)
         {
             lastVerifyTickLock.EnterYield();
             senderTimeVerifyTicks = ++lastVerifyTicks;
             lastVerifyTickLock.Exit();
         }
         ticks = senderTimeVerifyTicks;
         return(false);
     }
     return(true);
 }
Пример #13
0
 /// <summary>
 /// 添加任务
 /// </summary>
 /// <param name="node"></param>
 private void add(TaskLinkNode node)
 {
     if (head == null)
     {
         head = end = node;
         queueLock.Exit();
         queue.Add(this);
     }
     else
     {
         end.LinkNext = node;
         end          = node;
         queueLock.Exit();
     }
 }
Пример #14
0
 internal void PushNotNull(T value)
 {
     linkLock.EnterYield();
     if (End == null)
     {
         End = value;
         linkLock.Exit();
     }
     else
     {
         End.DoubleLinkNext       = value;
         value.DoubleLinkPrevious = End;
         End = value;
         linkLock.Exit();
     }
 }
Пример #15
0
        private SendState send()
        {
#if !DOTNET2
        START:
#endif
            if (IsSocket)
            {
                if (onSendAsyncCallback == null) onSendAsyncCallback = onSend;
#if DOTNET2
                SocketError socketError;
                Socket.BeginSend(sendData.Array, sendData.Start, sendData.Length, SocketFlags.None, out socketError, onSendAsyncCallback, Socket);
                if (socketError == SocketError.Success) return SendState.Asynchronous;
#else
                if (sendAsyncEventArgs == null)
                {
                    sendAsyncEventArgs = SocketAsyncEventArgsPool.Get();
                    sendAsyncEventArgs.Completed += onSendAsyncCallback;
                }
#if !DotNetStandard
                sendAsyncLock.EnterYield();
#endif
                sendAsyncEventArgs.SetBuffer(sendData.Array, sendData.Start, sendData.Length);
                if (Socket.SendAsync(sendAsyncEventArgs))
                {
#if !DotNetStandard
                    sendAsyncLock.Exit();
#endif
                    return SendState.Asynchronous;
                }
                if (sendAsyncEventArgs.SocketError == SocketError.Success)
                {
                    sendData.MoveStart(sendAsyncEventArgs.BytesTransferred);
#if !DotNetStandard
                    sendAsyncLock.Exit();
#endif
                    if (sendData.Length == 0)
                    {
                        freeCopyBuffer();
                        return SendState.Synchronize;
                    }
                    goto START;
                }
#endif
            }
            return SendState.Error;
        }
Пример #16
0
 private bool freeNotEnd(ref AutoCSer.Threading.SpinLock linkLock)
 {
     if (DoubleLinkNext != null)
     {
         DoubleLinkNext.DoubleLinkPrevious = DoubleLinkPrevious;
         if (DoubleLinkPrevious != null)
         {
             DoubleLinkPrevious.DoubleLinkNext = DoubleLinkNext;
             DoubleLinkPrevious = null;
         }
         DoubleLinkNext = null;
         linkLock.Exit();
         return(true);
     }
     linkLock.Exit(); //已经被释放的不做处理
     return(false);
 }
Пример #17
0
 /// <summary>
 /// 添加待检测类型
 /// </summary>
 /// <param name="type"></param>
 internal static void Add(Type type)
 {
     typeLock.EnterSleep();
     try
     {
         types.Add(type);
     }
     finally { typeLock.Exit(); }
 }
Пример #18
0
        /// <summary>
        /// 加载程序集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private static void loadAssembly(object sender, AssemblyLoadEventArgs args)
        {
            AssemblyCache assemblyCache = new AssemblyCache(args.LoadedAssembly);

            cacheLock.EnterSleep();
            try
            {
                cache[assemblyCache.fullName] = assemblyCache;
            }
            finally { cacheLock.Exit(); }
        }
Пример #19
0
            public T Pop()
            {
                T headValue;

                popLock.EnterYield();
                do
                {
                    if ((headValue = Head) == null)
                    {
                        popLock.Exit();
                        return(null);
                    }
                    if (System.Threading.Interlocked.CompareExchange(ref Head, headValue.LinkNext, headValue) == headValue)
                    {
                        popLock.Exit();
                        headValue.LinkNext = null;
                        return(headValue);
                    }
                    AutoCSer.Threading.ThreadYield.Yield();
                }while (true);
            }
Пример #20
0
        /// <summary>
        /// 获取随机种子位置
        /// </summary>
        /// <returns></returns>
        private int nextIndex64()
        {
            currentLock.EnterYield();
            int index = current64;

            if ((current64 -= 2) < 0)
            {
                current64 = (5 * 11 - 4) - current64;
            }
            currentLock.Exit();
            return(index);
        }
Пример #21
0
        /// <summary>
        /// 获取数据分配缓冲区
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        internal static AutoCSer.CacheServer.BufferCount GetBufferCount(ref SubArray <byte> data)
        {
            if (data.Length > (int)AutoCSer.CacheServer.BufferCount.BufferSize)
            {
                data.Set(new byte[data.Length], 0, data.Length);
                return(null);
            }
            AutoCSer.CacheServer.BufferCount buffer;
            bufferCountLock.EnterYield();
            buffer = bufferCount.Get(ref data);
            bufferCountLock.Exit();

            if (buffer == null)
            {
                Monitor.Enter(newBufferCountLock);
                AutoCSer.CacheServer.BufferCount oldBufferCount = bufferCount;
                try
                {
                    bufferCountLock.EnterYield();
                    buffer = oldBufferCount.Get(ref data);
                    bufferCountLock.Exit();
                    if (buffer == null)
                    {
                        AutoCSer.CacheServer.BufferCount newBufferCount = new AutoCSer.CacheServer.BufferCount();
                        buffer = newBufferCount.Get(ref data);
                        if (newBufferCount.Size > oldBufferCount.Size)
                        {
                            bufferCount = newBufferCount;
                            oldBufferCount.Free();
                        }
                        else
                        {
                            newBufferCount.Free();
                        }
                    }
                }
                finally { Monitor.Exit(newBufferCountLock); }
            }
            return(buffer);
        }
Пример #22
0
        internal static valueType Pop()
        {
            popLock.EnterYield();
            valueType headValue;

            do
            {
                if ((headValue = head) == null)
                {
                    popLock.Exit();
                    return(null);
                }
                if (System.Threading.Interlocked.CompareExchange(ref head, headValue.MemberMapValueLink, headValue) == headValue)
                {
                    popLock.Exit();
                    System.Threading.Interlocked.Decrement(ref count);
                    headValue.MemberMapValueLink = null;
                    return(headValue);
                }
                AutoCSer.Threading.ThreadYield.Yield();
            }while (true);
        }
Пример #23
0
        //[AutoCSer.IOS.Preserve(Conditional = true)]
        public static AutoWaitReturnValue Pop()
        {
            popLock.EnterYield();
            AutoWaitReturnValue headValue;

            do
            {
                if ((headValue = poolHead) == null)
                {
                    popLock.Exit();
                    return(new AutoWaitReturnValue());
                }
                if (System.Threading.Interlocked.CompareExchange(ref poolHead, headValue.next, headValue) == headValue)
                {
                    popLock.Exit();
                    System.Threading.Interlocked.Decrement(ref poolCount);
                    headValue.next = null;
                    return(headValue);
                }
                AutoCSer.Threading.ThreadYield.Yield();
            }while (true);
        }
        internal static MemberMapValueJsonSerializeConfig Pop()
        {
            popLock.EnterYield();
            MemberMapValueJsonSerializeConfig headValue;

            do
            {
                if ((headValue = head) == null)
                {
                    popLock.Exit();
                    return(null);
                }
                if (System.Threading.Interlocked.CompareExchange(ref head, headValue.LinkNext, headValue) == headValue)
                {
                    popLock.Exit();
                    System.Threading.Interlocked.Decrement(ref count);
                    headValue.LinkNext = null;
                    return(headValue);
                }
                AutoCSer.Threading.ThreadYield.Yield();
            }while (true);
        }
Пример #25
0
 /// <summary>
 /// 默认线程静态变量
 /// </summary>
 /// <returns></returns>
 private static ThreadStaticSerializer get()
 {
     createLock.EnterSleep();
     try
     {
         if (value == null)
         {
             value = new ThreadStaticSerializer();
         }
     }
     finally { createLock.Exit(); }
     return(value);
 }
Пример #26
0
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            if (isDisposed == 0)
            {
                int clientIndex = 0;
                Uri uri         = null;
                clientLock.EnterYield();
                if (isDisposed == 0)
                {
                    isDisposed       = 1;
                    clientIndex      = this.clientIndex;
                    this.clientIndex = 0;
                    uri = uris.GetClear();
                }
                clientLock.Exit();

                if (clientIndex != 0)
                {
                    foreach (HttpClient client in clients)
                    {
                        client.Free();
                        if (--clientIndex == 0)
                        {
                            break;
                        }
                    }
                }
                Array.Clear(clients, 0, clients.Length);
                if (uri != null)
                {
                    uri.CancelQueue(Log);
                }

                SocketTimeoutLink.Free(ref socketTimeout);
            }
        }
Пример #27
0
 internal bool SetCommandIndex(int commandIndex)
 {
     keepLock.EnterYield();
     if (isDisposed == 0)
     {
         this.commandIndex = commandIndex;
         keepLock.Exit();
         return(true);
     }
     keepLock.Exit();
     return(false);
 }
Пример #28
0
 ///// <summary>
 ///// 添加超时套接字
 ///// </summary>
 ///// <param name="value"></param>
 //[MethodImpl(AutoCSer.MethodImpl.AggressiveInlining)]
 //internal void Push(SocketTimeoutLink value)
 //{
 //    value.setTimeout(currentSeconds + seconds);
 //    push(value);
 //}
 /// <summary>
 /// 添加超时套接字
 /// </summary>
 /// <param name="value"></param>
 /// <param name="socket"></param>
 /// <param name="count"></param>
 internal void Push(SocketTimeoutNode value, Socket socket, ushort count = 0)
 {
     value.SetTimeout(AutoCSer.Threading.SecondTimer.CurrentSeconds + seconds, socket, count);
     queueLock.EnterYield();
     if (End == null)
     {
         End = Head = value;
         queueLock.Exit();
     }
     else
     {
         End.NextTimeout       = value;
         value.PreviousTimeout = End;
         End = value;
         queueLock.Exit();
     }
     value.IsSetReceiveTimeout = 1;
 }
Пример #29
0
 public void Set(DateTime time)
 {
     timeLock.EnterYield();
     minTime = time.AddTicks(ticks);
     timeLock.Exit();
 }
Пример #30
0
            internal T GetClear()
            {
                queueLock.EnterYield();
                T value = Head.LinkNext;

                end           = Head;
                Head.LinkNext = null;
                queueLock.Exit();
                return(value);
            }