Beispiel #1
0
        private void Run()
        {
            try
            {
                var bytes      = new byte[1024];
                var ipAddr     = _host.AddressList[0];
                var ipEndPoint = new IPEndPoint(_ip, _port);


                using (var sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
                {
                    sender.Connect(ipEndPoint);
                    RiseClientConnectedEvent();

                    // ReSharper disable TooWideLocalVariableScope
                    Message msg;
                    // ReSharper restore TooWideLocalVariableScope

                    var order = new OrderItem(Id);

                    while (!_stop)
                    {
                        Thread.Sleep(_timeout);

                        switch (order.CommandType)
                        {
                        case CommandTypeEnum.Exit:
                            order.CommandType = CommandTypeEnum.Exit;
                            RiseClientDisConnectedEvent();
                            _stop = true;
                            continue;

                        case CommandTypeEnum.None:
                            order.CommandType  = CommandTypeEnum.RequestPrice;
                            order.CurrencyType = _clist[_r.Next(0, _clist.Count)];;
                            break;

                        case CommandTypeEnum.ResponsePrice:
                            order.Value       = _r.Next(1, 100000);
                            order.CommandType = CommandTypeEnum.RequestOrder;
                            break;

                        case CommandTypeEnum.ResponseOrderSaved:
                            order.Save();
                            order.Clear();
                            break;
                        }

                        msg = order.Serialize();
                        sender.Send(msg.Data);
                        RiseMessageSubmittedHandler(order);

                        sender.Receive(bytes);
                        msg.Data = bytes;
                        order    = msg.Deserialize().ToOrder();
                        RiseMessageReceivedEvent(order);
                    }

                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                }
            }
            catch (SocketException se)
            {
                if (se.ErrorCode == 10061 || se.ErrorCode == 10054)
                {
                    RiseReconnectWithErrorHandler(se.ErrorCode);
                    Thread.Sleep(5000);
                    Run();
                }
                else
                {
                    throw;
                }
            }
        }