Example #1
0
        private void Init(ServerSetting serverSetting)
        {
            this.bufferManager.InitBuffer();

            SocketAsyncEventArgs eventArgObjectForPool;

            for (int i = 0; i < serverSetting.maxSimultaneousConnectOps; i++)
            {
                eventArgObjectForPool = new SocketAsyncEventArgs();

                eventArgObjectForPool.Completed += new EventHandler <SocketAsyncEventArgs>(AcceptEventArg_Completed);

                this.poolOfAcceptEventArgs.Push(eventArgObjectForPool);
            }

            for (int i = 0; i < serverSetting.numberOfSaeaForRecSend; i++)
            {
                eventArgObjectForPool = new SocketAsyncEventArgs();

                this.bufferManager.SetBuffer(eventArgObjectForPool);

                eventArgObjectForPool.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);

                ServerUserToken userToken = new ServerUserToken(eventArgObjectForPool.Offset, eventArgObjectForPool.Offset + serverSetting.bufferSize, serverSetting.receivePrefixLength, serverSetting.sendPrefixLength);

                eventArgObjectForPool.UserToken = userToken;

                this.poolOfRecSendEventArgs.Push(eventArgObjectForPool);
            }

            simplePerf = new SimplePerformanceCounter(true, true);
        }
Example #2
0
        private void StartListen(ServerSetting serverSetting)
        {
            listenSocket = new Socket(serverSetting.localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            listenSocket.Bind(serverSetting.localEndPoint);
            listenSocket.Listen(1000000);

            LogManager.Log(string.Format("listen on {0}", serverSetting.localEndPoint));

            StartAccept();
        }
Example #3
0
        public SocketListener(Func <byte[], byte[]> dataProcessor)
        {
            this.dataProcessor = dataProcessor;

            ServerSetting setting = ReadConfigFile.GetServerSetting();

            this.receivePrefixLength = setting.receivePrefixLength;
            this.bufferSize          = setting.bufferSize;
            this.supportKeepAlive    = setting.useKeepAlive;

            this.bufferManager           = new BufferManager(setting.bufferSize * setting.numberOfSaeaForRecSend * setting.opsToPreAllocate, setting.bufferSize * setting.opsToPreAllocate);
            this.poolOfAcceptEventArgs   = new SocketAsyncEventArgPool();
            this.poolOfRecSendEventArgs  = new SocketAsyncEventArgPool();
            this.maxConcurrentConnection = new Semaphore(setting.numberOfSaeaForRecSend, setting.numberOfSaeaForRecSend);

            Init(setting);
            StartListen(setting);
        }