Пример #1
0
 /// <summary>
 /// 停止收发并关闭socket
 /// </summary>
 public void Close(DisconnectType e = DisconnectType.Manual)
 {
     lock (lockobj)
     {
         if (isRuning == true)
         {
             isRuning = false;
             try
             {
                 socket.Shutdown(SocketShutdown.Send);
             }
             catch (Exception) { }
             try
             {
                 socket.Shutdown(SocketShutdown.Receive);
             }
             catch (Exception) { }
             socket.Close();
             if (socketPool != null)
             {
                 if (readArgs != null)
                 {
                     readArgs.Completed -= readArgs_Completed;
                     socketPool.Push(readArgs);
                     readArgs.UserToken = null;
                 }
             }
             socket = null;
             OnSocketDisconnecting(new DisconnectEvent(e, this));
         }
         IsConnect = false;
     }
 }
Пример #2
0
        /// <summary>
        /// 初始化(必要)
        /// </summary>
        /// <param name="maxConn"></param>
        public virtual void Init(string id)
        {
            transfer      = new Transfer(maxConnect);
            registerWaite = new AutoResetEvent(false);
            this.Id       = id;
            YDataPacketManager.RegisterDataPacket(typeof(SplitDataPacket));
            YDataPacketManager.RegisterDataPacket(typeof(SocketRegisterPacket));
            YDataPacketManager.RegisterDataPacket(typeof(MessagePack));
            socketPool = new SocketAsyncEventArgsPool(maxConnect);
            SocketAsyncEventArgs readWriteEventArg;

            bufferManager = new BufferManager(maxConnect * bufferLenthg * 2, bufferLenthg);
            bufferManager.InitBuffer();
            for (int i = 0; i < maxConnect; i++)
            {
                readWriteEventArg = new SocketAsyncEventArgs();
                bufferManager.SetBuffer(readWriteEventArg);
                socketPool.Push(readWriteEventArg);
            }
            netTree.CreateRoot(id);
            AbsInit();
            isInit = true;
        }