Example #1
0
 public EventLoop(int workerCount)
 {
     workers = new SingleEventLoop[workerCount];
     for (var i = 0; i < workers.Length; ++i)
     {
         workers[i] = new SingleEventLoop();
         workers[i].Start();
     }
 }
Example #2
0
        public SocketChannel(Socket socket, int readBufferSize)
        {
            this.socket         = socket;
            this.readBufferSize = readBufferSize;
            this.writeQueue     = new ConcurrentQueue <Holder>();
            this.pipeline       = new ChannelPipeline(this);
            _currentEventLoop   = EVENTLOOP.NextWorker();

            writeArgs            = new SocketAsyncEventArgs();
            writeArgs.Completed += (sender, e) =>
            {
                ProcessWriteCompleted(true);
            };
            receiveArgs            = new SocketAsyncEventArgs();
            receiveArgs.Completed += (sender, e) =>
            {
                ProcessReceiveCompleted();
            };

            //if allocate big buffer, then use pool
            if (readBufferSize > LOH)
            {
                _pool = new ByteBufferPool(readBufferSize, POOL_SIZE);
            }
            else
            {
                _pool = new MockPool(readBufferSize);
            }

            _receiveTask = () =>
            {
                if (close == 1)
                {
                    return;
                }
                if (Interlocked.CompareExchange(ref reading, READING, IDLE) == IDLE)
                {
                    DoReceive();
                }
            };

            _closeTask = () =>
            {
                if (Interlocked.CompareExchange(ref close, 1, 0) != 0)
                {
                    return;
                }

                Dequeue();
                try
                {
                    if (socket != null)
                    {
                        socket.Close();
                    }
                }
                catch (Exception e)
                {
                    logger.Warn(e, "close socket error");
                }
                finally
                {
                    TriggerInActive();
                }
            };

            _flushTask = () =>
            {
                Flush();
            };
        }