Beispiel #1
0
        public TcpClient(string host, int port)
        {
            this.Host = host;
            this.Port = port;

            UserTokenSession Session = new UserTokenSession();
            //初始化发送接套字
            SendEventArgs SendEvent = new SendEventArgs();

            SendEvent.Completed += AsyncDispatchCenter;
            SendEvent.UserToken  = Session;
            SendEvent.SendAction = ProcessSend;

            //初始化接收接套字
            ReceiveBuffer = new byte[BufferSize];
            ReceiveEventArgs ReceiveEvent = new ReceiveEventArgs();

            ReceiveEvent.Completed    += AsyncDispatchCenter;
            ReceiveEvent.UserToken     = Session;
            ReceiveEvent.ReceiveAction = ProcessReceive;
            Buffer.SetBuffer(ReceiveEvent, ReceiveBuffer);

            ShakeHandEvent               = new ShakeHandEventArgs(ReceiveEvent, SendEvent);
            ShakeHandEvent.Completed    += AsyncDispatchCenter;
            ShakeHandEvent.ConnectAction = StartConnect;
            Session.ShakeHandEvent       = ShakeHandEvent;
            Session.Mode             = ApplyMode.Client;
            ShakeHandEvent.UserToken = Session;
        }
Beispiel #2
0
        /// <summary>
        /// 接收池
        /// </summary>
        /// <param name="BufferSize"></param>
        /// <param name="PoolSize"></param>
        /// <param name="AcceptCompleted"></param>
        /// <param name="IOCompleted"></param>
        public ShakeHandEventArgsPool(int BufferSize, int PoolSize, EventHandler <SocketAsyncEventArgs> DispatchCenter)
        {
            ShakeHandPool     = new ConcurrentStack <ShakeHandEventArgs>();
            ReceiveBufferPool = new BufferPool(BufferSize, PoolSize);
            SendBufferPool    = new BufferPool(BufferSize, PoolSize);
            for (int i = 0; i < PoolSize; i++)
            {
                UserTokenSession userToken        = new UserTokenSession();
                ReceiveEventArgs receiveEventArgs = new ReceiveEventArgs();
                receiveEventArgs.UserToken  = userToken;
                receiveEventArgs.Completed += DispatchCenter;
                Buffer.SetBuffer(receiveEventArgs, ReceiveBufferPool.Pop());

                SendEventArgs senEventArgs = new SendEventArgs();
                senEventArgs.UserToken  = userToken;
                senEventArgs.Completed += DispatchCenter;
                Buffer.SetBuffer(senEventArgs, SendBufferPool.Pop());

                ShakeHandEventArgs acceptEventArgs = new ShakeHandEventArgs(receiveEventArgs, senEventArgs);
                acceptEventArgs.Completed += DispatchCenter;
                acceptEventArgs.UserToken  = userToken;

                ShakeHandPool.Push(acceptEventArgs);
            }
        }
Beispiel #3
0
        void ProcessConnect(SocketAsyncEventArgs e)
        {
            ShakeHandEventArgs eventArgs = (ShakeHandEventArgs)e;
            UserTokenSession   Session   = (UserTokenSession)e.UserToken;

            //结束连接中状态
            Session.Connecting = false;
            switch (eventArgs.SocketError)
            {
            case SocketError.Success:
                Session.OperationTime = DateTime.Now;

                //异步监听连接完成后的操作
                ThreadPool.QueueUserWorkItem((e) => OnConnect?.Invoke(Session));

                //接收服务端传来的流
                if (!Session.Channel.ReceiveAsync(eventArgs.ReceiveEventArgs))
                {
                    ProcessReceive(eventArgs.ReceiveEventArgs);
                }

                //如果存在发送失败的消息 在重连之后
                while (Session.NoSuccessMessage.Count > 0)
                {
                    if (Session.NoSuccessMessage.TryDequeue(out var message))
                    {
                        Session.Channel.Send(message);
                    }
                }
                break;

            case SocketError.ConnectionRefused:
                break;

            default:
                break;
            }
        }
Beispiel #4
0
        void ProcessAccept(SocketAsyncEventArgs e)
        {
            ShakeHandEventArgs eventArgs = (ShakeHandEventArgs)e;

            //接收用户成功
            if (eventArgs.LastOperation == SocketAsyncOperation.Accept && eventArgs.SocketError == SocketError.Success)
            {
                //创建会话信息(当前会话)
                UserTokenSession UserToken = eventArgs.UserToken as UserTokenSession;
                var EndPoint = (IPEndPoint)eventArgs.AcceptSocket.RemoteEndPoint;
                var AllHost  = Dns.GetHostEntry(EndPoint.Address).AddressList;
                UserToken.UserHost                       = string.Join("|", AllHost.Select(x => x.ToString()).ToArray());
                UserToken.UserPort                       = ((IPEndPoint)(eventArgs.AcceptSocket.RemoteEndPoint)).Port;
                UserToken.Mode                           = ApplyMode.Server;
                UserToken.OperationTime                  = DateTime.Now;
                eventArgs.SendEventArgs.SendAction       = ProcessSend;
                eventArgs.ReceiveEventArgs.ReceiveAction = ProcessReceive;
                UserToken.ShakeHandEvent                 = eventArgs;
                OnConnect?.Invoke(UserToken);

                //异步接收客户端行为
                //异步接收客户端消息
                if (!UserToken.Channel.ReceiveAsync(UserToken.ShakeHandEvent.ReceiveEventArgs))
                {
                    ProcessReceive(UserToken.ShakeHandEvent.ReceiveEventArgs);
                }
            }
            else
            {
                //接收用户失败就清理后送回池
                eventArgs.Clear();
                ShakeHandEventPool.Push(eventArgs);
            }
            //继续接收下一个
            ShakeHandAsync();
        }
Beispiel #5
0
 /// <summary>
 /// 推入池
 /// </summary>
 /// <param name="item"></param>
 public void Push(ShakeHandEventArgs item)
 {
     ShakeHandPool.Push(item);
 }