REMOTEHISTORY_OBJ_INTERFACE.cs 8.56 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
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 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 120 121 122 123 124 125 126 127 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;

namespace FLY.Thick.RemoteHistory
{
    public class REMOTEHISTORY_OBJ_INTERFACE
    {
        #region Pack
        public class Pack_Params : IPack 
        {
            public int keeyday;
            public int saverows;

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(keeyday));
                buf.AddRange(BitConverter.GetBytes(saverows));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 8)
                    return false;
                int idx = 0;
                keeyday = BitConverter.ToInt32(value, idx);
                idx += 4;
                saverows = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;

            }
        }
        public class Pack_State : IPack 
        {
            public int currrows;
            public string currpath;
            public int currdays;
            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(currdays));
                buf.AddRange(BitConverter.GetBytes(currrows));
                if (currpath != null)
                {
                    byte[] bs = Misc.Converter.StringToBytes(currpath);
                    int len = bs.Length;
                    buf.AddRange(BitConverter.GetBytes(len));
                    buf.AddRange(bs);
                }
                else 
                {
                    int len = 0;
                    buf.AddRange(BitConverter.GetBytes(len));
                }
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int cnt = 12;
                if (value.Length < cnt)
                    return false;
                int idx = 0;
                currdays = BitConverter.ToInt32(value, idx);
                idx += 4;
                currrows = BitConverter.ToInt32(value, idx);
                idx += 4;
                int len = BitConverter.ToInt32(value, idx);
                idx += 4;
                cnt += len;
                if (value.Length < cnt)
                    return false;
                if (len > 0)
                {
                    currpath = Misc.Converter.BytesToString(value, idx, len);
                }
                else 
                {
                    currpath = null;
                }
                return true;
            }
        }
        public class Pack_GetPathsResponse : IPack
        {
            public List<string> paths;
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                byte[] bs;

                buf.AddRange(BitConverter.GetBytes(paths.Count()));
                for (int i = 0; i < paths.Count(); i++)
                {
                    bs = Misc.Converter.StringToBytes(paths[i]);
                    buf.AddRange(BitConverter.GetBytes(bs.Count()));
                    buf.AddRange(bs);
                }
                return buf.ToArray();
            }
            /// <summary>
            /// 返回由字节数组中指定位置的9个字节转换来的数据。
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public bool TryParse(byte[] value)
            {
                int cnt = 4;
                if (value.Length < cnt)
                    return false;

                int idx = 0;
                int len = BitConverter.ToInt32(value, idx);
                idx += 4;
                cnt += len * 4;
                if (value.Length < cnt)
                    return false;
                if (paths == null)
                    paths = new List<string>();
                paths.Clear();
                for (int i = 0; i < len; i++)
                {
                    int l = BitConverter.ToInt32(value, idx);
                    idx += 4;
                    cnt += l;
                    if (value.Length < cnt)
                        return false;

                    string s = Misc.Converter.BytesToString(value, idx, l);
                    idx += l;
                    paths.Add(s);
                }
                return true;
            }


            #endregion
        }
        public class Pack_GetPathsRequest : IPack 
        {
            public DateTime dt_begin;
            public DateTime dt_end;
            public string profilename;


            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(dt_begin.Ticks));
                buf.AddRange(BitConverter.GetBytes(dt_end.Ticks));
                if (!string.IsNullOrEmpty(profilename))
                {
                    byte[] bs = Misc.Converter.StringToBytes(profilename);
                    int len = bs.Count();
                    buf.AddRange(BitConverter.GetBytes(len));
                    buf.AddRange(bs);
                }
                else
                {
                    int len = 0;
                    buf.AddRange(BitConverter.GetBytes(len));
                }
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int cnt = 8+8+4;
                if (value.Length < cnt)
                    return false;

                int idx = 0;
                dt_begin = new DateTime(BitConverter.ToInt64(value, idx));
                idx += 8;
                dt_end = new DateTime(BitConverter.ToInt64(value, idx));
                idx += 8;

                int len = BitConverter.ToInt32(value, idx);
                idx += 4;

                cnt += len;
                if (value.Length < cnt)
                    return false;
                if (len > 0)
                {
                    profilename = Misc.Converter.BytesToString(value, idx, len);
                    idx += len;
                }
                return true;
            }
        }
        public class Pack_GetRootPathResponse : IPack
        {
            public string rootpath;
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                byte[] bs= Misc.Converter.StringToBytes(rootpath);
                buf.AddRange(BitConverter.GetBytes(bs.Count()));
                buf.AddRange(bs);
                return buf.ToArray();
            }
            /// <summary>
            /// 返回由字节数组中指定位置的9个字节转换来的数据。
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public bool TryParse(byte[] value)
            {
                int cnt = 4;
                if (value.Length < cnt)
                    return false;

                int idx = 0;
                int l = BitConverter.ToInt32(value, idx);
                idx += 4;

                cnt += l;
                if (value.Length < cnt)
                    return false;

                rootpath = Misc.Converter.BytesToString(value, idx, l);
                idx += l;
                 
                return true;
            }


            #endregion
        }
        #endregion

        #region GetValue
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 GET_PARAMS = 0;
        /// <summary>
        /// Pack_State
        /// </summary>
        public const UInt16 GET_STATE = 1;
        #endregion
        #region SetValue
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 SET_PARAMS = 0;
        #endregion
        #region PushMsg
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 PUSH_PARAMS = 0;
        /// <summary>
        /// Pack_State
        /// </summary>
        public const UInt16 PUSH_STATE = 1;
        #endregion
        #region CALL
        /// <summary>
        /// null
        /// </summary>
        public const UInt16 CALL_FLUSH = 1;
        /// <summary>
        /// Pack_GetPathsRequest
        /// Pack_GetPathsResponse
        /// </summary>
        public const UInt16 CALL_GETPATHS = 2;

        /// <summary>
        /// null
        /// Pack_GetRootPathResponse
        /// </summary>
        public const UInt16 CALL_GETROOTPATH = 3;
        #endregion
    }
}