Пример #1
0
        public Frame Peek()
        {
            lock (m_LockObject)
            {
                T tRet;

                if (m_Q.TryPeek(out tRet))
                {
                    return(tRet);
                }
                else
                {
                    return(default);
Пример #2
0
 private void CheckStart()
 {
     if (System.Threading.Interlocked.CompareExchange(ref mRunStatus, 1, 0) == 0)
     {
         T item;
         if (mQueue.TryPeek(out item))
         {
             ThreadPool.UnsafeQueueUserWorkItem(OnStart, null);
         }
         else
         {
             System.Threading.Interlocked.Exchange(ref mRunStatus, 0);
         }
     }
 }
Пример #3
0
        private static bool CheckRepetition(System.Collections.Concurrent.ConcurrentQueue <ChatLogItem> Log, ChatLogItem item)
        {
            if (item == null)
            {
                return(false);
            }
            if (item.Bytes == null)
            {
                return(false);
            }

            bool repetitonFlag = true;

            if (item.Line.Length > 1)
            {
                ChatLogItem previusChatLogItem = null;

                if (Log.TryPeek(out previusChatLogItem))
                {
                    if (!CheckChatEquality(previusChatLogItem, item))
                    {
                        while (Log.TryDequeue(out previusChatLogItem))
                        {
                            ;
                        }

                        //result.ChatLogItems.Add(chatLogItem);
                        repetitonFlag = false;

                        Log.Enqueue(item);
                    }
                }
                else
                {
                    Log.Enqueue(item);

                    //result.ChatLogItems.Add(chatLogItem);
                    repetitonFlag = false;
                }
            }

            /*if (repetitonFlag)
             *  item = null;//*/
            return(repetitonFlag);
        }
Пример #4
0
 private void Timer_Tick(object sender, EventArgs e)
 {
     temp = Graphics.FromImage(bm);
     temp.Clear(Color.Transparent);
     temp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
     foreach (userstring u in info_queue)
     {
         temp.DrawString(u.info, new Font(new FontFamily("幼圆"), emSize), new SolidBrush(u.style), u.x, u.y);
         u.x -= 2;
     }
     if (info_queue.TryPeek(out tempus) == false)
     {
         tempus = new userstring("", 0, 0);
     }
     if (tempus.x < SystemInformation.VirtualScreen.Width * (-1))
     {
         info_queue.TryDequeue(out tempus);
     }
     pb.Image = bm;
 }
Пример #5
0
        private Task <int> _Read(byte[] buffer, int offset, int count, System.Collections.Concurrent.ConcurrentQueue <MemoryStream> read_queue)
        {
            MemoryStream stream1;
            MemoryStream stream2;

            if (read_queue.TryPeek(out stream1))
            {
                var readSize = stream1.Read(buffer, offset, count);
                if (stream1.Length == stream1.Position)
                {
                    read_queue.TryDequeue(out stream2);
                    if (stream1 != stream2)
                    {
                        throw new System.Exception("stream1 != stream2");
                    }
                }

                return(System.Threading.Tasks.Task <int> .FromResult(readSize));
            }
            return(System.Threading.Tasks.Task <int> .FromResult(0));
        }
Пример #6
0
        /// <summary>
        /// 板卡通用工作回调
        /// [注:读写通用变量值]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _bkgrdwkForGeneralBaseData_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            uint[]   uintData;
            ushort[] ushortData;
            short[]  shortData;
            int[]    intData;
            float[]  floatData;
            byte[]   byteData;
            bool     queueState;

            //板卡工作状态
            while (_netWorkState)
            {
                try
                {
                    //板卡连接状态
                    if (_netConnected)
                    {
                        //数据单元队列非空(并发队列)
                        if (_netQueue.Count > 0)
                        {
                            ProDriver.Auxiliary.HZZHComBaseData bsData;
                            queueState = _netQueue.TryPeek(out bsData);
                            //获取队列元素成功
                            if (queueState)
                            {
                                //数据单元读取状态
                                if (bsData.InitiativeRead)
                                {
                                    try
                                    {
                                        switch (bsData.DataType)
                                        {
                                        case ProDriver.Auxiliary.HZZHComDataType.Short:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out shortData);
                                            bsData.ShortValue = (short[])shortData.Clone();
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Uint:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out uintData);
                                            bsData.UintValue  = (uint[])uintData.Clone();
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Ushort:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out ushortData);
                                            bsData.UshortValue = (ushort[])ushortData.Clone();
                                            bsData.ComSucceed  = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Float:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out floatData);
                                            bsData.FloatValue = (float[])floatData.Clone();
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Byte:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out byteData);

                                            byte[] btArr = new byte[byteData.Length];
                                            for (int i = 0; i < byteData.Length; i++)
                                            {
                                                //高低位互换
                                                if (i % 2 != 0)
                                                {
                                                    btArr[i - 1] = byteData[i];
                                                    btArr[i]     = byteData[i - 1];
                                                }
                                            }

                                            bsData.ByteValue  = (byte[])btArr.Clone();
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Int:
                                        default:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out intData);
                                            bsData.IntValue   = (int[])intData.Clone();
                                            bsData.ComSucceed = true;
                                            break;
                                        }
                                    }
                                    catch (System.Exception ex)
                                    { throw new Exception("ReadError:" + ex.Message); }
                                }
                                else //数据单元写入状态
                                {
                                    try
                                    {
                                        switch (bsData.DataType)
                                        {
                                        case ProDriver.Auxiliary.HZZHComDataType.Int:
                                            _comNet.Write(1, bsData.Address, bsData.IntValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Short:
                                            _comNet.Write(1, bsData.Address, bsData.ShortValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Uint:
                                            _comNet.Write(1, bsData.Address, bsData.UintValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Ushort:
                                            _comNet.Write(1, bsData.Address, bsData.UshortValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Float:
                                            _comNet.Write(1, bsData.Address, bsData.FloatValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Byte:
                                            _comNet.Write(1, bsData.Address, bsData.ByteValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        default: break;
                                        }
                                    }
                                    catch (System.Exception ex)
                                    { throw new Exception("WriteError:" + ex.Message); }
                                }
                            }
                            else
                            {
                                throw new Exception("Obtain base data failed !");
                            }
                            queueState = _netQueue.TryDequeue(out bsData);
                        }
                    }
                }
                catch
                {
                    _netWorkState = false;
                    _netConnected = false;
                }

                System.Threading.Thread.Sleep(10);
            }
        }
Пример #7
0
        private void AcknowledgeMessage(byte[] messageBytes)
        {
            var stream = new System.IO.MemoryStream(messageBytes);
            //var message = TanksCommon.MessageDecoder.DecodeMessage<TanksCommon.SharedObjects.DataReceived>(stream);
            short messageType = TanksCommon.MessageDecoder.DecodeMessageType(stream);

            _log.Debug("Received Message Type: " + messageType);
            CallReceivedDataLog("Received Message Type: " + messageType);
            if (messageType == 500)
            {
                _log.Debug("Recieved RSA Public Key");
                CallReceivedDataLog("Recieved RSA Public Key");
                var message = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.DataReceived>(stream);
                SendObjectToTcpClient(new TanksCommon.SharedObjects.DataReceived()
                {
                    MessageId = message.MessageId, Id = 99
                });

                var rsaPublicKeys = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.Encryption.RsaPublicKey>(stream);
                encryptioinKeys.ImportPublicKey(rsaPublicKeys.Key);

                aesEncrypter = new Encrypt(encryptioinKeys);
                _log.Debug("Sending AES Public Keys");
                CallReceivedDataLog("Sending AES Public Keys");
                SendObjectToTcpClient(new TanksCommon.Encryption.AesPublicKey()
                {
                    Iv = encryptioinKeys.Iv, SessionKey = encryptioinKeys.SessionKey
                });
            }
            else if (messageType == 501)
            {
                _log.Debug("Recieved AES Public Keys");
                CallReceivedDataLog("Recieved AES Public Keys");
                var message = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.DataReceived>(stream);
                SendObjectToTcpClient(new TanksCommon.SharedObjects.DataReceived()
                {
                    MessageId = message.MessageId, Id = 99
                });

                var aesPublicKeys = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.Encryption.AesPublicKey>(stream);
                encryptioinKeys.SetIvAndSessionKey(aesPublicKeys.Iv, aesPublicKeys.SessionKey);
            }
            else if (messageType == 900)
            {
                //resend message
                HandleDebugRecievedMessage?.Invoke(messageBytes);
                var message = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.DataReceived>(stream);
                if (_messageHistory.ContainsKey(message.MessageId))
                {
                    SendObjectToTcpClient(_messageHistory[message.MessageId]);
                }
                else
                {
                    _log.Error("Peer is requesting Message that is not in message history");
                    CallReceivedDataLog("Peer is requesting Message that is not in message history");
                }
            }
            else if (messageType != 99)
            {
                var message = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.DataReceived>(stream);
                SendObjectToTcpClient(new TanksCommon.SharedObjects.DataReceived()
                {
                    MessageId = message.MessageId, Id = 99
                });
            }
            else
            {
                //remove message from queue, marking it complete
                var message = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.DataReceived>(stream);
                var top     = new MessageInfo <TanksCommon.SharedObjects.IMessage>();
                if (_messageQueue.TryPeek(out top))
                {
                    if (top.OriginalMessage.MessageId == message.MessageId)
                    {
                        //success remove from queue
                        _messageQueue.TryDequeue(out top);
                    }
                }
                else
                {
                    _log.Error("Message not found in queue");
                }
            }
        }