Inheritance: IDisposable
Example #1
0
        /// <summary>
        /// 服务端初始化
        /// </summary>
        public void Init()
        {
            this.WasteClientMonitor = new System.Threading.Timer(WasteClientMonitorHandler, null, 1000 * 5, 1000 * 5);//Clean waste client period is 1 min.
            //this.bufferManager.InitBuffer();
            NSocketSAEAItem clientItem;

            for (Int32 i = 0; i < this.numConcurrence; i++)
            {
                clientItem = new NSocketSAEAItem();
                clientItem.ReceiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(OnReceiveCompleted);
                clientItem.SendSAEA.Completed    += new EventHandler <SocketAsyncEventArgs>(OnSendCompleted);
                //只给接收的SocketAsyncEventArgs设置缓冲区
                //this.bufferManager.SetBuffer(readWriteEventArgWithId.ReceiveSAEA);
                this.clientPool.Push(clientItem);
            }
            //serverstate = ServerState.Inited;
        }
Example #2
0
 internal NSocketSAEAItem Pop(string uid, Socket socket)
 {
     if (uid == string.Empty || uid == "")
         return null;
     NSocketSAEAItem si = null;
     lock (this.idlePool)
     {
         si = this.idlePool.Dequeue();
         si.UseTimes++;
     }
     si.UID = uid;
     si.ReceiveSAEA.UID = uid;
     si.SendSAEA.UID = uid;
     si.ReceiveSAEA.Socket = socket;
     si.SendSAEA.Socket = socket;
     busyPool.Add(uid, si);
     return si;
 }
Example #3
0
        internal void Push(NSocketSAEAItem item)
        {
            if (item == null)
                throw new ArgumentNullException("SocketAsyncEventArgsWithId对象为空");


            if (busyPool.Keys.Contains(item.UID))
            {
                lock (busyPool)
                {
                    busyPool.Remove(item.UID);
                }
            }

            item.UID = "-1";
            item.Disconnect();
            lock (this.idlePool)
            {
                this.idlePool.Enqueue(item);
            }
        }
Example #4
0
        /// <summary>
        /// 客户端连接请求处理方法
        /// </summary>
        /// <param name="e"></param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            if (e.LastOperation != SocketAsyncOperation.Accept)    //检查上一次操作是否是Accept,不是就返回
            {
                return;
            }
            if (e.BytesTransferred <= 0)    //检查发送的长度是否大于0,不是就返回
            {
                return;
            }

            string UID = Guid.NewGuid().ToString();                                         //使用Guid作为客户端请求的ID, 不使用Client IP的原因是允许同一个IP建立多个连接.

            NSocketSAEAItem readEventArgsWithId = this.clientPool.Pop(UID, e.AcceptSocket); // new NSocketSAEAItem(UID, e.AcceptSocket);

            //readEventArgsWithId.ReceiveSAEA.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceiveCompleted);
            //readEventArgsWithId.SendSAEA.Completed += new EventHandler<SocketAsyncEventArgs>(OnSendCompleted);
            byte[] acceptBuffer = new byte[1024];
            readEventArgsWithId.ReceiveSAEA.SetBuffer(acceptBuffer, 0, acceptBuffer.Length);

            ReceiveListen(readEventArgsWithId.ReceiveSAEA);
            this.StartAccept(e);
            ClientConnected(UID);
        }
Example #5
0
        internal void Push(NSocketSAEAItem item)
        {
            if (item == null)
                throw new ArgumentNullException("SocketAsyncEventArgsWithId对象为空");

            if (busyPool.Keys.Contains(item.UID))
            {
                lock (busyPool)
                {
                    busyPool.Remove(item.UID);
                }
            }

            item.UID = "-1";
            item.Disconnect();
            lock (this.idlePool)
            {
                this.idlePool.Enqueue(item);
            }
        }
Example #6
0
 /// <summary>
 /// 服务端初始化
 /// </summary>
 public void Init()
 {
     this.WasteClientMonitor = new System.Threading.Timer(WasteClientMonitorHandler, null, 1000 * 5, 1000 * 5);//Clean waste client period is 1 min.
     //this.bufferManager.InitBuffer();
     NSocketSAEAItem clientItem;
     for (Int32 i = 0; i < this.numConcurrence; i++)
     {
         clientItem = new NSocketSAEAItem();
         clientItem.ReceiveSAEA.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceiveCompleted);
         clientItem.SendSAEA.Completed += new EventHandler<SocketAsyncEventArgs>(OnSendCompleted);
         //只给接收的SocketAsyncEventArgs设置缓冲区
         //this.bufferManager.SetBuffer(readWriteEventArgWithId.ReceiveSAEA);
         this.clientPool.Push(clientItem);
     }
     //serverstate = ServerState.Inited;
 }