Exemplo n.º 1
0
        public void Tests()
        {
            var pool = new ByteArrayPool(arraySize: 16, poolCapacity: 2);

            var idx1 = pool.New();

            Assert.Equal(1, pool.Count);

            var buffer1 = pool.GetBuffer(idx1);

            Assert.Equal(16, buffer1.Length);

            var idx2 = pool.New();

            Assert.Equal(2, pool.Count);

            var buffer2 = pool.GetBuffer(idx2);

            Assert.Equal(16, buffer2.Length);

            var idx3 = pool.New(); // out of capacity

            Assert.Equal(2, pool.Count);

            buffer1[0] = 1;
            buffer1[1] = 2;

            buffer2[0] = 3;
            buffer2[1] = 4;

            Assert.Equal(1, buffer1[0]);
            Assert.Equal(2, buffer1[1]);
            Assert.Equal(3, buffer2[0]);
            Assert.Equal(4, buffer2[1]);

            pool.Free(idx1);

            Assert.Equal(1, pool.Count);

            var idx4 = pool.New();

            Assert.Equal(2, pool.Count);

            var buffer4 = pool.GetBuffer(idx4);

            buffer4[0] = 5;
            buffer4[1] = 6;

            Assert.Equal(3, buffer2[0]);
            Assert.Equal(4, buffer2[1]);
            Assert.Equal(5, buffer4[0]);
            Assert.Equal(6, buffer4[1]);

            pool.Free(idx2);
            pool.Free(idx4);

            Assert.Equal(0, pool.Count);
        }
Exemplo n.º 2
0
        private void PubRoutine()
        {
            if (_pubSocket != null)
            {
                while (true)
                {
                    try
                    {
                        byte[] tmp;
                        if (_marketDataTmp.Count > 0 && _marketDataTmp.TryDequeue(out tmp))
                        {
                            using (MemoryStream stream = new MemoryStream(tmp))
                            {
                                MarketData data = _fmt.Deserialize(stream) as MarketData;
                                if (data != null)
                                {
                                    //更新当前市场数据
                                    if (_currentMarketDatas.ContainsKey(data.Code))
                                    {
                                        _currentMarketDatas[data.Code] = data;
                                    }
                                    else
                                    {
                                        _currentMarketDatas.TryAdd(data.Code, data);
                                    }
                                    //发布市场数据
                                    if (_subCodes.ContainsKey(data.Code))
                                    {
                                        using (MemoryStream stream1 = new MemoryStream())
                                        {
                                            _fmt.Serialize(stream1, data);
                                            byte[] td = stream1.ToArray();
                                            using (ZMessage msg = new ZMessage())
                                            {
                                                msg.Add(new ZFrame(data.Code + ":" + "MarketData"));
                                                msg.Add(new ZFrame(td));
                                                lock (_pubSocket)
                                                {
                                                    _pubSocket.Send(msg);
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            _bytePool.Free(tmp);
                        }
                        else if (_marketTransactionTmp.Count > 0 && _marketTransactionTmp.TryDequeue(out tmp))
                        {
                            using (MemoryStream stream = new MemoryStream(tmp))
                            {
                                MarketTransaction trans = _fmt.Deserialize(stream) as MarketTransaction;
                                if (trans != null)
                                {
                                    //更新最近成交
                                    if (_lastTransactions.ContainsKey(trans.Code))
                                    {
                                        _lastTransactions[trans.Code] = trans;
                                    }
                                    else
                                    {
                                        _lastTransactions.TryAdd(trans.Code, trans);
                                    }
                                    //发布最新成交
                                    if (_subCodes.ContainsKey(trans.Code))
                                    {
                                        using (MemoryStream stream1 = new MemoryStream())
                                        {
                                            _fmt.Serialize(stream1, trans);
                                            byte[] td = stream1.ToArray();
                                            using (ZMessage msg = new ZMessage())
                                            {
                                                msg.Add(new ZFrame(trans.Code + ":" + "MarketTransaction"));
                                                msg.Add(new ZFrame(td));
                                                lock (_pubSocket)
                                                {
                                                    _pubSocket.Send(msg);
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            _bytePool.Free(tmp);
                        }
                        else
                        {
                            Thread.Sleep(50);
                        }
                    }
                    catch (Exception ex)
                    {
                        AASClient.Program.logger.LogRunning("DataServerClient:数据发送错误!{0}", ex.Message);
                    }
                }
            }
        }