Ejemplo n.º 1
0
        /// <summary>
        /// 确认连接包
        /// </summary>
        /// <param name="session"></param>
        private void ValiditySession(SessionProviderContext session)
        {
            var  ack       = GetMessageEntity <AckPack>(session);
            long accessKey = ack.AccessKey;

            if (accessKey != int.Parse(AppConfiguration.ConnectPassWord))
            {
                session.SessionClose();
                return;
            }
            else
            {
                //连接密码验证通过,设置成为主连接,正式接收处理数据包
                session.AppTokens[SysConstants.INDEX_WORKTYPE] = ConnectionWorkType.MAINCON;
                //告诉服务端一切就绪
                SendTo(session, MessageHead.S_GLOBAL_OK);
            }
        }
Ejemplo n.º 2
0
        private void OnMessage(SessionProviderContext session)
        {
            object[]           ars             = session.AppTokens;
            ConnectionWorkType sessionWorkType = (ConnectionWorkType)ars[0];

            if (sessionWorkType == ConnectionWorkType.WORKCON)
            {
                //消息扔给消息提供器,由提供器提供消息给所在窗口进行消息处理
                //((MessageAdapter)ars[1]).OnSessionNotify(session, SessionNotifyType.Message);
            }
            else if (sessionWorkType == ConnectionWorkType.MAINCON)
            {
                MessageHead cmd = session.CompletedBuffer.GetMessageHead <MessageHead>();
                switch (cmd)
                {
                case MessageHead.C_MAIN_LOGIN:    //上线包
                    this.ProcessLogin(session);
                    break;

                case MessageHead.C_MAIN_DESKTOPVIEW_FRAME:    //屏幕视图数据
                    this.ProcessDesktopViewData(session, session.CompletedBuffer.GetMessagePayload());
                    break;
                }
            }
            else if (sessionWorkType == ConnectionWorkType.NONE) //初始消息只能进入该方法块处理,连接密码验证正确才能正式处理消息
            {
                switch (session.CompletedBuffer.GetMessageHead <MessageHead>())
                {
                case MessageHead.C_GLOBAL_CONNECT:    //连接确认包
                                                      //CheckSessionValidity(session);

                    session.AppTokens[0] = ConnectionWorkType.MAINCON;

                    byte[] data = Core.MessageHelper.CopyMessageHeadTo(MessageHead.S_GLOBAL_OK);
                    session.SendAsync(data);

                    break;

                default:
                    session.SessionClose();    //伪造包断掉
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        private void OnReceiveComplete(SessionProviderContext session)
        {
            // Tokens参数说明
            // [0]为该连接工作类型,MainWork为主连接,Work工作连接,NONE为未知连接
            // [1]如果连接为Work类型,则是消息处理器,否则是主连接上下文对象
            var appTokens       = session.AppTokens;
            var sessionWorkType = appTokens[SysConstants.INDEX_WORKTYPE].ConvertTo <ConnectionWorkType>();

            if (sessionWorkType == ConnectionWorkType.WORKCON)
            {
                //消息传给消息处理器,由消息处理器所在App进行处理
                var app = appTokens[SysConstants.INDEX_WORKER].ConvertTo <ApplicationAdapterHandler>();
                if (app.WhetherClose)
                {
                    return;
                }
                app.HandlerBinder.InvokePacketHandler(session, GetMessageHead(session), app);
            }
            else if (sessionWorkType == ConnectionWorkType.MAINCON)
            {
                this.HandlerBinder.InvokePacketHandler(session, GetMessageHead(session), this);
            }
            else if (sessionWorkType == ConnectionWorkType.NONE) //未经过验证的连接的消息只能进入该方法块处理,连接密码验证正确才能正式处理消息
            {
                switch (GetMessageHead(session))
                {
                case MessageHead.C_GLOBAL_CONNECT:    //连接确认包
                    this.ValiditySession(session);
                    break;

                default:                    //接收到其他数据包的处理
                    session.SessionClose(); //伪造包,断开连接
                    break;
                }
            }
        }
Ejemplo n.º 4
0
        private void OnActivateStartApp(SessionProviderContext session)
        {
            var    openControl = GetMessageEntity <ActivateApplicationPack>(session);
            string originName  = openControl.OriginName;
            string appKey      = openControl.ServiceKey;
            string identifyId  = openControl.IdentifyId;
            //查找离线任务队列,如果有对应的任务则继续工作
            var task = FindOfSuspendTaskContext(identifyId);

            if (!task.IsNull())
            {
                //再发出重连命令后,如果使用者主动关闭消息处理器将不再建立连接
                if (task.AdapterHandler.WhetherClose)
                {
                    //通知远程释放资源
                    SendTo(session, MessageHead.S_GLOBAL_ONCLOSE);
                    return;
                }

                //将消息处理器与会话关联
                var tokens = session.AppTokens;
                tokens[SysConstants.INDEX_WORKTYPE] = ConnectionWorkType.WORKCON;
                tokens[SysConstants.INDEX_WORKER]   = task.AdapterHandler;
                task.AdapterHandler.SetSession(session);
                task.AdapterHandler.ContinueTask(session);//继续任务

                RemoveSuspendTaskContext(identifyId);
            }
            else
            {
                var context = SysUtil.ApplicationTypes.FirstOrDefault(x => x.ApplicationKey.Equals(appKey));
                if (!context.IsNull())
                {
                    var appHandlerType = context.Type.GetAppAdapterHandlerType();
                    ApplicationAdapterHandler appHandlerBase = Activator.CreateInstance(appHandlerType).ConvertTo <ApplicationAdapterHandler>();
                    IApplication app = Activator.CreateInstance(context.Type).ConvertTo <IApplication>();

                    appHandlerBase.App            = app;
                    appHandlerBase.IdentifyId     = identifyId;
                    appHandlerBase.OriginName     = originName;
                    appHandlerBase.ApplicationKey = context.Type.GetApplicationKey();
                    appHandlerBase.SetSession(session);

                    //每个应用至少标记一个应用处理器属性
                    var handlerFieder = context
                                        .Type
                                        .GetProperties()
                                        .Single(c => !c.GetCustomAttribute <ApplicationAdapterHandlerAttribute>(true).IsNull());
                    handlerFieder.SetValue(app, appHandlerBase);

                    this.OnApplicationCreatedEventHandler?.Invoke(app);

                    //app.HandlerAdapter = handlerBase;
                    app.Start();

                    session.AppTokens[SysConstants.INDEX_WORKTYPE] = ConnectionWorkType.WORKCON;
                    session.AppTokens[SysConstants.INDEX_WORKER]   = appHandlerBase;
                }
                else
                {
                    session.SessionClose();
                    LogHelper.WriteErrorByCurrentMethod("A working connection was closed because the control whose controlkey is :{0} could not be found!".FormatTo(appKey));
                    return;
                }
            }
        }