示例#1
0
        static void WebImplementation()
        {
            OrderMatchList = new OrderMatchList(CurrentPrice);
            TradeRecord.SetTradeRecordStartId(TradeRecordStartId);
            TradeRecordList = new List <TradeRecord>();

            IPAddress   hostIp   = Dns.GetHostAddresses(WebHostMatchEngineIp)[0];
            TcpListener listener = new TcpListener(hostIp, WebHostMatchEnginePort);

            listener.Start();
            Console.WriteLine("Waiting for connection...");
            while (true)
            {
                TcpClient     client = listener.AcceptTcpClient();
                NetworkStream ns     = client.GetStream();

                byte[] byteNewOrder           = new byte[1024];
                int    byteNewOrderRealLength = ns.Read(byteNewOrder, 0, byteNewOrder.Length);
                Order  newOrder = Xml.Deserialize(typeof(Order), Encoding.UTF8.GetString(byteNewOrder, 0, byteNewOrderRealLength)) as Order;
                Console.WriteLine("Order is received and latest price is sent.");
                TradeRecordList.AddRange(PrintTradeRecord(OrderMatchList.AddOrderGetTradeRecord(newOrder)));

                CurrentPrice = OrderMatchList.CurrentPrice;
                byte[] bytePrice = Encoding.ASCII.GetBytes(CurrentPrice.ToString());
                ns.Write(bytePrice, 0, bytePrice.Length);
                client.Close();
            }
        }
示例#2
0
        static void FileImplementation()
        {
            Order[] OrderQueue = new Order[FileOrderAmount];
            ReadOrderFromXml(ref OrderQueue);

            OrderMatchList = new OrderMatchList(CurrentPrice);
            TradeRecord.SetTradeRecordStartId(TradeRecordStartId);
            TradeRecordList = new List <TradeRecord>();

            for (int i = 0; i < FileOrderAmount; i++)
            {
                TradeRecordList.AddRange(OrderMatchList.AddOrderGetTradeRecord(OrderQueue[i]));
            }
            WriteTradeRecordToXml(ref TradeRecordList);
        }
示例#3
0
        static void MessageQueueImplementation()
        {
            OrderMatchList = new OrderMatchList(CurrentPrice);
            TradeRecord.SetTradeRecordStartId(TradeRecordStartId);
            TradeRecordList = new List <TradeRecord>();

            if (MessageQueue.Exists(OrderMessageQueuePath))
            {
                OrderQueue = new MessageQueue(OrderMessageQueuePath);
            }
            else
            {
                OrderQueue                    = MessageQueue.Create(OrderMessageQueuePath);
                OrderQueue.Label              = OrderMessageQueueName;
                OrderQueue.UseJournalQueue    = true;
                OrderQueue.MaximumJournalSize = OrderMessageQueueJournalSize;
            }
            if (MessageQueue.Exists(PriceMessageQueuePath))
            {
                PriceQueue = new MessageQueue(PriceMessageQueuePath);
            }
            else
            {
                PriceQueue                    = MessageQueue.Create(PriceMessageQueuePath);
                PriceQueue.Label              = PriceMessageQueueName;
                PriceQueue.UseJournalQueue    = true;
                PriceQueue.MaximumJournalSize = PriceMessageQueueJournalSize;
            }

            OrderQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(Order) });
            PriceQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(int) });
            Timer timer = new Timer(MessageQueueOrderReceiveInterval);

            timer.Elapsed  += new ElapsedEventHandler(ReceiveOrderFromMessageQueue);
            timer.AutoReset = true;
            timer.Enabled   = true;
        }