예제 #1
0
        public IEnumerable <CurrencyStore.Entity.CurrencyInfo> Read(bool onlyUnLoaded)
        {
            const int ROW_HEADER_LENGTH = 4;
            var       count             = FileHeader.SumRecords;
            var       buff  = new byte[350];
            var       index = 128;

            for (int i = 0; i < count; i++)
            {
                bool uploaded = false;
                var  len      = reader.Read(buff, 0, 4);
                index += 4;

                if (buff[0] != 0xAA)
                {
                    uploaded = true;
                }

                var contentLen = buff.ReadShort(2);

                len = reader.Read(buff, 4, contentLen - ROW_HEADER_LENGTH);

                if (len != (contentLen - ROW_HEADER_LENGTH))
                {
                    throw new ArgumentException("错误的文件内容在:" + index);
                }

                index += len;
                var item = Message.ConvertFrom(buff);

                if (uploaded && onlyUnLoaded)
                {
                    continue;
                }

                yield return(item.Currency);
            }
        }
예제 #2
0
        static void HandleDataReceived(ServerConnection sender, DataEventArgs e)
        {
            /*
             * Interlocked.Increment(ref currencies);
             *
             * byte[] temp = new byte[] { 0xAA, 0xBB, 0x00, 0x09, 0xA0, 0x0A, 0x00, 0x00, 0x00 };
             *
             * sender.SendData(temp, 0, temp.Length);
             *
             * return;
             */

            /**/

            byte[] datas = e.Data;
            Interlocked.Increment(ref messages);
            Interlocked.Add(ref bytes, datas.Length);

            var msg = Message.ConvertFrom(datas);

            //var line = e.Data.ToHexString();
            //File.AppendAllText("data.txt", line + Environment.NewLine);
            if (msg == null)
            {
                return;
            }

            if (msg.Datas == null)
            {
                msg.Datas = new byte[] { 0 };
            }

            byte flag = 0;

            if (msg.Type == (int)MessageType.Beep)
            {
                UpdateDeviceInfo(msg, e.Device);
            }

            else if (msg.Type == (int)MessageType.Login)
            {
                if (msg.Device == null)
                {
                    ServerInstrumentation.Current.LoginFailed();

                    if (!allowAllowAnonymous)
                    {
                        flag = 1;
                        sender.Disconnect();
                        return;
                    }
                }

                if (!allowAllowAnonymous || msg.Device != null)
                {
                    UpdateDeviceInfo(msg, e.Device);
                    sender.AuthorizationType = AuthorizationType.Authorized;
                }
            }

            else if (msg.Type == (int)MessageType.Detail)
            {
                Interlocked.Increment(ref currencies);

                if (!allowAllowAnonymous && sender.AuthorizationType == AuthorizationType.Anonymous)
                {
                    ServerInstrumentation.Current.Unregisted();

                    if (!allowAllowAnonymous)
                    {
                        sender.Disconnect();
                        return;
                    }
                }
                UpdateDeviceInfo(msg, e.Device);
                e.Device.Increate();
                if (msg.Currency != null)
                {
                    DataPool.Push(msg.Currency);
                }
            }

            else if (msg.Type == (int)MessageType.BlackTable)
            {
            }

            else if (msg.Type == (int)MessageType.DownLoadBlackTable)
            {
            }

            var data = msg.GetBytes(flag);

            sender.SendData(data, 0, data.Length);
        }