PasswordServiceClient.cs 3.32 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;

using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Client
{
15
    public class PasswordServiceClient:FObj, IPasswordService
潘栩锋's avatar
潘栩锋 committed
16 17 18
    {
        UInt32 mServerID;
        IFConn mConn;
19
        public PasswordServiceClient(UInt32 id)
潘栩锋's avatar
潘栩锋 committed
20
        {
潘栩锋's avatar
潘栩锋 committed
21
            mServerID = id;
潘栩锋's avatar
潘栩锋 committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        }

        #region IPasswordService 成员
        private List<PasswordCell> pws = new List<PasswordCell>();
        /// <summary>
        /// 密码列表。 储存在 服务器
        /// </summary>
        public List<PasswordCell> PWs
        {
            get
            {
                return pws;
            }
        }
        public void Apply()
        {
            PASSWORD_OBJ_INTERFACE.Pack_List p = new PASSWORD_OBJ_INTERFACE.Pack_List();
            p.list = PWs;

            CurrObjSys.SetValueEx(
               mConn, mServerID, ID, 
               PASSWORD_OBJ_INTERFACE.SET_PWLIST,
               p.ToBytes()
               );    
        }

        public AUTHORIZE_RESULT Authorize(string pw, int level)
        {
            if (pws.Count() == 0)
            { 
                //没有密码
                return AUTHORIZE_RESULT.OK;
            }

            var v = from p in pws where p.Password == pw select p;
            if (v.Count() > 0) 
            {
                if (v.First().Level >= level)
                    return AUTHORIZE_RESULT.OK;
                else
                    return AUTHORIZE_RESULT.ERR_LEVEL;
            }
            return AUTHORIZE_RESULT.ERR_PW;
        }
        #endregion


        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            if (from.IsConnected)
            {
                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID, 0xffffffff, SENSE_CONFIG.ADD);
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    PASSWORD_OBJ_INTERFACE.GET_PWLIST);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            PushInfo(from, srcid, memid, infodata);
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            switch (infoid)
            {
                case PASSWORD_OBJ_INTERFACE.PUSH_PWLIST:
                    {
                        PASSWORD_OBJ_INTERFACE.Pack_List p = new PASSWORD_OBJ_INTERFACE.Pack_List();
                        p.list = PWs;
                        if(!p.TryParse(infodata))
                            return;
                        NotifyPropertyChanged("PWs");
                    } break;
            }
        }
        
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        
        #endregion
    }
}