DataService.cs 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
//==============================================================
//  Copyright (C) 2020  Inc. All rights reserved.
//
//==============================================================
//  Create by 种道洋 at 2020/5/27 9:02:43.
//  Version 1.0
//  种道洋
//==============================================================

using Cdy.Tag;
using DotNetty.Buffers;
using System;
using System.Collections.Generic;
using System.Text;

namespace SpiderDriver
{
    /// <summary>
    /// 
    /// </summary>
    public class DataService: SocketServer
    {

        #region ... Variables  ...
        private TagInfoServerProcess mInfoProcess;
        private RealDataServerProcess mRealProcess;
27
        private HisDataServerProcess mHisProcess;
28
        //private IByteBuffer mAsyncCalldata;
29

30 31 32 33 34 35 36
        #endregion ...Variables...

        #region ... Events     ...

        #endregion ...Events...

        #region ... Constructor...
37 38 39 40 41 42 43
        /// <summary>
        /// 
        /// </summary>
        public DataService()
        {
            RegistorInit();
        }
44 45 46
        #endregion ...Constructor...

        #region ... Properties ...
47 48 49 50
        /// <summary>
        /// 
        /// </summary>
        public override string Name => "SpiderDriveDataService";
51 52 53 54 55 56 57 58 59
        #endregion ...Properties...

        #region ... Methods    ...
        /// <summary>
        /// 
        /// </summary>
        private void RegistorInit()
        {
            this.RegistorFunCallBack(APIConst.TagInfoRequestFun, TagInfoRequest);
60 61
            this.RegistorFunCallBack(APIConst.RealValueFun, RealDataRequest);
            this.RegistorFunCallBack(APIConst.HisValueFun, HisDataRequest);
62 63
        }

cdy816's avatar
cdy816 已提交
64 65 66 67 68 69 70 71
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        protected override void OnClientConnected(string id)
        {
            mRealProcess.OnClientConnected(id);
            mInfoProcess.OnClientConnected(id);
72
            mHisProcess.OnClientConnected(id);
cdy816's avatar
cdy816 已提交
73 74 75 76 77 78 79 80 81 82 83
            base.OnClientConnected(id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        protected override void OnClientDisConnected(string id)
        {
            mRealProcess.OnClientDisconnected(id);
            mInfoProcess.OnClientDisconnected(id);
84
            mHisProcess.OnClientDisconnected(id);
cdy816's avatar
cdy816 已提交
85 86 87
            base.OnClientDisConnected(id);
        }

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        /// <summary>
        /// 
        /// </summary>
        public void PushRealDatatoClient(string clientId, byte[] value)
        {
            this.SendData(clientId, APIConst.PushDataChangedFun, value, value.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="value"></param>
        public void PushRealDatatoClient(string clientId, IByteBuffer value)
        {
            this.SendData(clientId, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="fun"></param>
        /// <param name="value"></param>
        public void AsyncCallback(string clientId, byte fun, byte[] value, int len)
        {
            this.SendData(clientId, fun, value, len);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="data"></param>
        public void AsyncCallback(string clientId, IByteBuffer data)
        {
            this.SendData(clientId, data);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="fun"></param>
        /// <param name="value"></param>
        /// <param name="len"></param>
        public void AsyncCallback(string clientId, byte fun, IntPtr value, int len)
        {
            this.SendData(clientId, fun, value, len);
        }

141 142 143 144 145 146 147 148 149 150
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //private IByteBuffer GetAsyncData()
        //{
        //    mAsyncCalldata = BufferManager.Manager.Allocate(APIConst.AysncReturn, 4);
        //    mAsyncCalldata.WriteInt(0);
        //    return mAsyncCalldata;
        //}
151 152 153 154 155 156

        /// <summary>
        /// 
        /// </summary>
        private IByteBuffer TagInfoRequest(string clientId, IByteBuffer memory)
        {
cdy816's avatar
cdy816 已提交
157
            mInfoProcess?.ProcessData(clientId, memory);
158
            return null;
159 160 161 162 163 164 165 166
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="memory"></param>
        /// <returns></returns>
167
        private IByteBuffer RealDataRequest(string clientId, IByteBuffer memory)
168
        {
cdy816's avatar
cdy816 已提交
169
            this.mRealProcess?.ProcessData(clientId, memory);
170
            return null;
171 172
        }

173 174 175 176 177 178 179 180
        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientid"></param>
        /// <param name="memory"></param>
        /// <returns></returns>
        private IByteBuffer HisDataRequest(string clientid,IByteBuffer memory)
        {
cdy816's avatar
cdy816 已提交
181
            this.mHisProcess?.ProcessData(clientid, memory);
182 183 184
            return null;
        }

185 186 187 188 189 190 191 192

        /// <summary>
        /// 
        /// </summary>
        /// <param name="port"></param>
        protected override void StartInner(int port)
        {
            mRealProcess = new RealDataServerProcess() { Parent = this };
cdy816's avatar
cdy816 已提交
193
            mRealProcess.Init();
194
            mInfoProcess = new TagInfoServerProcess() { Parent = this };
195
            mHisProcess = new HisDataServerProcess() { Parent = this };
196 197
            mRealProcess.Start();
            mInfoProcess.Start();
198
            mHisProcess.Start();
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
            base.StartInner(port);

        }

        /// <summary>
        /// 
        /// </summary>
        public override void Stop()
        {
            base.Stop();
            if (mRealProcess != null)
            {
                mRealProcess.Stop();
                mRealProcess.Dispose();
                mRealProcess = null;
            }
            if (mInfoProcess != null)
            {
                mInfoProcess.Stop();
                mInfoProcess.Dispose();
                mInfoProcess = null;
            }
221 222 223 224 225 226 227

            if (mHisProcess != null)
            {
                mHisProcess.Stop();
                mHisProcess.Dispose();
                mHisProcess = null;
            }
228 229 230 231 232 233 234 235
        }
        #endregion ...Methods...

        #region ... Interfaces ...

        #endregion ...Interfaces...
    }
}