Buffer_OBJProxy.cs 6.37 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FLY.OBJComponents.OBJ_INTERFACE;
using FLY.Thick.RemoteHistory;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.OBJComponents.Server.OBJProxy
{
    public class Buffer_OBJProxy<T> : FObj
    {
潘栩锋's avatar
潘栩锋 committed
17 18
        const int MARKNO_PUSH_STATUS = 1;
        const int MARKNO_PUSH_PARAMS = 2;
潘栩锋's avatar
潘栩锋 committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
        IBuffer<T> buffer;

        public Buffer_OBJProxy(int objsys_idx, UInt32 id, IBuffer<T> buffer)
            : base(objsys_idx)
        {
            ID = id;
            this.buffer = buffer;
            buffer.BufferChanged += Buffer_BufferChanged;
            buffer.PropertyChanged += Buffer_PropertyChanged;
        }

        private void Buffer_BufferChanged(object sender, NotifyBufferChangedEventArgs<T> e)
        {
            string j = JsonConvert.SerializeObject(e);

            CurrObjSys.PushObjInfoEx(
                this, BUFFER_OBJ_INTERFACE.PUSH_BUFFERCHANGED,
                Misc.Converter.StringToBytes(j));
        }

        private void Buffer_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
41 42
            if ((e.PropertyName == "NewestID") ||
                (e.PropertyName == "Count"))
43
            {
潘栩锋's avatar
潘栩锋 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
                FObjBase.PollModule.Current.Poll_JustOnce(() =>
                {
                    byte[] buf;
                    GetValue(null, 0, BUFFER_OBJ_INTERFACE.GET_STATUS, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, BUFFER_OBJ_INTERFACE.GET_STATUS,
                        buf);
                }, this, MARKNO_PUSH_STATUS);
            }
            if (e.PropertyName == "Capacity")
            {
                FObjBase.PollModule.Current.Poll_JustOnce(() =>
                {
                    byte[] buf;
                    GetValue(null, 0, BUFFER_OBJ_INTERFACE.GET_PARAMS, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, BUFFER_OBJ_INTERFACE.GET_PARAMS,
                        buf);
                }, this, MARKNO_PUSH_PARAMS);
            }
潘栩锋's avatar
潘栩锋 committed
64 65 66 67 68 69 70 71 72
        }

        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case BUFFER_OBJ_INTERFACE.SET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
潘栩锋's avatar
潘栩锋 committed
73 74
                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<BUFFER_OBJ_INTERFACE.Pack_Params>(json);
                        buffer.Capacity = p.capcity;
潘栩锋's avatar
潘栩锋 committed
75 76 77 78 79 80 81 82 83 84 85
                    }
                    break;
            }
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case BUFFER_OBJ_INTERFACE.GET_PARAMS:
                    {
潘栩锋's avatar
潘栩锋 committed
86 87 88 89 90 91 92 93 94 95 96
                        var p = new BUFFER_OBJ_INTERFACE.Pack_Params()
                        {
                            capcity = buffer.Capacity
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                case BUFFER_OBJ_INTERFACE.GET_STATUS:
                    {
                        var p = new BUFFER_OBJ_INTERFACE.Pack_Status()
潘栩锋's avatar
潘栩锋 committed
97
                        {
潘栩锋's avatar
潘栩锋 committed
98 99 100
                            newestId = buffer.NewestID,
                            count = buffer.Count
                        };
潘栩锋's avatar
潘栩锋 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                default:
                    infodata = null;
                    break;
            }
        }

        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case BUFFER_OBJ_INTERFACE.CALL_GETRECORD:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        BUFFER_OBJ_INTERFACE.Pack_GetRecordRequest request = JsonConvert.DeserializeObject<BUFFER_OBJ_INTERFACE.Pack_GetRecordRequest>(json);
                        buffer.GetRecord(request.last_id, request.count,
                            (asyncContext, retData) =>
                            {
                                ConnContext connContext = asyncContext as ConnContext;
潘栩锋's avatar
潘栩锋 committed
123
                                string j = JsonConvert.SerializeObject(retData);
潘栩锋's avatar
潘栩锋 committed
124 125 126
                                CurrObjSys.PushCallFunctionEx(
                                    connContext.from, 
                                    connContext.srcid, ID, 
潘栩锋's avatar
潘栩锋 committed
127
                                    connContext.magic, BUFFER_OBJ_INTERFACE.CALL_GETRECORD, Misc.Converter.StringToBytes(j));
潘栩锋's avatar
潘栩锋 committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

                            }, new ConnContext(from, srcid, magic));
                        
                    }
                    break;
                case BUFFER_OBJ_INTERFACE.CALL_GETRECORD_NEWEST:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        BUFFER_OBJ_INTERFACE.Pack_GetRecordNewestRequest request = JsonConvert.DeserializeObject<BUFFER_OBJ_INTERFACE.Pack_GetRecordNewestRequest>(json);
                        buffer.GetRecord(request.count,
                            (asyncContext, retData) =>
                            {
                                ConnContext connContext = asyncContext as ConnContext;
                                GetRecordReponse<T> reponse = retData as GetRecordReponse<T>;
                                string j = JsonConvert.SerializeObject(reponse);
                                CurrObjSys.PushCallFunctionEx(
                                    connContext.from,
                                    connContext.srcid, ID,
                                    connContext.magic, funcid, Misc.Converter.StringToBytes(j));

                            }, new ConnContext(from, srcid, magic));

                    }
                    break;
                case BUFFER_OBJ_INTERFACE.CALL_RESET:
                    {
                        buffer.Reset();
                    }break;
            }
        }
    }
}