/// <summary>
        /// 发送数据Datagram
        /// </summary>
        /// <param name="datagram"></param>
        public virtual bool Send(string datagram)
        {
            Console.WriteLine(" Send ================= " + datagram + "  " + (cc++));
            if (datagram.Length == 0)
            {
                return(true);
            }

            try
            {
                if (!_isConnected)
                {
                    _isConnected = false;
                    key          = null;
                    throw (new ApplicationException("没有连接Server,不能发送数据"));
                }

                datagram += Environment.NewLine;
                //获得Datagram的编码字节
                byte[] data = _coder.GetEncodingBytes(datagram);

                _session.ClientSocket.SendTimeout = 5000;

                _session.ClientSocket.BeginSend(data, 0, data.Length, SocketFlags.None,
                                                new AsyncCallback(SendDataEnd), _session.ClientSocket);
                Console.WriteLine(" Send ================= " + datagram + "  ");
            }
            catch (Exception e)
            {
                DateTime now = DateTime.Now;
                LogUtil.Error(" Send === Complete  " + e.StackTrace + "  " + now.ToLongTimeString() + " m  " + now.Minute + " s " + now.Second + "  ms " + now.Millisecond);
                _isConnected = false;
                key          = null;
                Console.WriteLine(e.StackTrace);
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// Establish Tcp连接后处理过程
        /// </summary>
        /// <param name="iar">Async Socket</param>
        protected virtual void Connected(IAsyncResult iar)
        {
            try
            {
                Socket socket = null;

                socket = (Socket)iar.AsyncState;

                socket.EndConnect(iar);


                //创建新的会话
                _session = new Session(socket);

                _isConnected = true;

                //触发连接Establish 事件
                if (ConnectedServer != null)
                {
                    ConnectedServer(this, new NetEventArgs(_session));
                }

                //Establish 连接后应该立即接收数据
                _session.ClientSocket.BeginReceive(_recvDataBuffer, 0,
                                                   DefaultBufferSize, SocketFlags.None,
                                                   new AsyncCallback(RecvData), socket);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                DateTime now = DateTime.Now;
                LogUtil.Error(" Send === Exception  " + "  " + now.ToLongTimeString() + " H " + now.Hour + " m  " + now.Minute + " s " + now.Second + "  ms " + now.Millisecond);
                LogUtil.Error("  " + e.StackTrace);
                timeOut = true;
            }
        }
        public override void Receive(object msg)
        {
            string[] msgs = (string[])msg;

            Protocol protocol    = AppUtil.ParseProtocol(msgs[1]);
            int      paramAmount = AppUtil.StringToInt(msgs[2]);

            if (protocol != Protocol.UNKNOWN)
            {
                if (msgs.Length >= 4)
                {
                    LogUtil.Info(" Signal content " + msgs[3]);
                    string[] info = msgs[3].Split(',');
                    if (info.Length >= 2)
                    {
                        Interval interval = AppUtil.StringToInterval(info[0]);
                        Symbol   symbol   = AppUtil.ParseSymbol(info[1]);
                        try
                        {
                            switch (protocol)
                            {
                            case Protocol.K0004_1:     // Arrow Signal
                                if (!AppContext.IsReconnecting && AppContext.IsPriceListInitialized &&
                                    AppContext.IsSignalListInitialized)
                                {
                                    if (!msgs.Contains(NULL))
                                    {
                                        Signal signal = new Signal();
                                        signal.Interval      = interval;
                                        signal.Symbol        = symbol;
                                        signal.ActPrice      = double.Parse(info[2]);
                                        signal.Arrow         = int.Parse(info[3]);
                                        signal.ActTime       = DateTime.Parse(info[4]);
                                        signal.StopLossPrice = double.Parse(info[5]);
                                        Console.WriteLine(Protocol.K0004_1 + "  " + signal);
                                        LogUtil.Info(Protocol.K0004_1 + "  " + signal);
                                        if (AppSetting.SYMBOLS.Contains(signal.Symbol) && AppSetting.INTEVALS.Contains(signal.Interval))
                                        {
                                            SignalProcess(signal, protocol);
                                        }
                                    }
                                }
                                break;

                            case Protocol.K0004_2:      // Stop Loss Signal
                                if (!AppContext.IsReconnecting && AppContext.IsPriceListInitialized &&
                                    AppContext.IsSignalListInitialized)
                                {
                                    if (!msgs.Contains(NULL))
                                    {
                                        Signal signal = new Signal();
                                        signal.Interval         = interval;
                                        signal.Symbol           = symbol;
                                        signal.StopLossBidPrice = double.Parse(info[2]);
                                        signal.StopLoss         = int.Parse(info[3]);
                                        signal.Arrow            = int.Parse(info[3]);
                                        signal.StopLossTime     = DateTime.Parse(info[4]);
                                        signal.ActTime          = DateTime.Parse(info[5]);
                                        Console.WriteLine(Protocol.K0004_2 + "  " + signal);
                                        LogUtil.Info(Protocol.K0004_2 + "  " + signal);
                                        if (signal.StopLossBidPrice > 0 && signal.StopLoss != 0)
                                        {
                                            SignalProcess(signal, protocol);
                                        }
                                    }
                                }
                                break;

                            case Protocol.K0004_3:      // Gain Tip Signal
                                if (!AppContext.IsReconnecting && AppContext.IsPriceListInitialized &&
                                    AppContext.IsSignalListInitialized)
                                {
                                    if (!msgs.Contains(NULL))
                                    {
                                        Signal signal = new Signal();
                                        signal.Interval     = interval;
                                        signal.Symbol       = symbol;
                                        signal.GainTip      = int.Parse(info[2]);
                                        signal.Arrow        = int.Parse(info[2]);
                                        signal.GainTipTime  = DateTime.Parse(info[3]);
                                        signal.ActTime      = DateTime.Parse(info[4]);
                                        signal.GainTipPrice = double.Parse(info[5]);
                                        Console.WriteLine(Protocol.K0004_3 + "  " + signal);
                                        LogUtil.Info(Protocol.K0004_3 + "  " + signal);
                                        if (signal.GainTipPrice > 0)
                                        {
                                            SignalProcess(signal, protocol);
                                        }
                                    }
                                }
                                break;

                            case Protocol.K0004_4:      // Stop Gain Signal
                                if (!AppContext.IsReconnecting && AppContext.IsPriceListInitialized &&
                                    AppContext.IsSignalListInitialized)
                                {
                                    if (!msgs.Contains(NULL))
                                    {
                                        Signal signal = new Signal();
                                        signal.Interval      = interval;
                                        signal.Symbol        = symbol;
                                        signal.StopGainPrice = double.Parse(info[2]);
                                        signal.StopGain      = int.Parse(info[3]);
                                        signal.Arrow         = int.Parse(info[3]);
                                        signal.StopGainTime  = DateTime.Parse(info[4]);
                                        signal.ActTime       = DateTime.Parse(info[5]);
                                        Console.WriteLine(Protocol.K0004_4 + "  " + signal);
                                        LogUtil.Info(Protocol.K0004_4 + "  " + signal);
                                        if (signal.StopGainPrice > 0)
                                        {
                                            SignalProcess(signal, protocol);
                                        }
                                    }
                                }
                                break;

                            case Protocol.K0004_5:      // Profit Signal
                                if (!AppContext.IsReconnecting && AppContext.IsPriceListInitialized &&
                                    AppContext.IsSignalListInitialized)
                                {
                                    if (!msgs.Contains(NULL))
                                    {
                                        Signal signal = new Signal();
                                        signal.Interval    = interval;
                                        signal.Symbol      = symbol;
                                        signal.Arrow       = int.Parse(info[2]);
                                        signal.ActTime     = DateTime.Parse(info[3]);
                                        signal.Profit      = int.Parse(info[4]);
                                        signal.ProfitTime  = DateTime.Parse(info[5]);
                                        signal.ProfitPrice = double.Parse(info[6]);
                                        Console.WriteLine(Protocol.K0004_5 + "  " + signal);
                                        LogUtil.Info(Protocol.K0004_5 + "  " + signal);
                                        if (signal.ProfitPrice > 0)
                                        {
                                            SignalProcess(signal, protocol);
                                        }
                                    }
                                }
                                break;
                            }
                        }
                        catch (Exception e)
                        {
                            LogUtil.Error(" Protocol " + protocol.ToString() + e.StackTrace);
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public override void Receive(object msg)
        {
            Console.WriteLine(" SignalListHandler  Receive ~~~~~~~~~~~ " + msg);
            string[] msgs     = (string[])msg;
            Protocol protocol = AppUtil.ParseProtocol(msgs[1]);

            int paramAmount = int.Parse(msgs[2]);

            if (paramAmount > 0)
            {
                AppContext.SignalDatas.Clear();
                try
                {
                    for (int i = 3; i < msgs.Length; i++)
                    {
                        string signalInfo = msgs[i];
                        if (!string.IsNullOrEmpty(signalInfo))
                        {
                            string[] info   = signalInfo.Split(',');
                            Signal   signal = new Signal();
                            signal.Interval      = AppUtil.StringToInterval(info[0]);
                            signal.Symbol        = AppUtil.ParseSymbol(info[1]);
                            signal.ActPrice      = double.Parse(info[2]);
                            signal.Arrow         = AppUtil.StringToInt(info[3]);
                            signal.ActTime       = DateTime.Parse(info[4]);
                            signal.StopLossPrice = double.Parse(info[5]);
                            if (NULL != info[6])
                            {
                                signal.StopLossTime     = DateTime.Parse(info[6]);
                                signal.StopLoss         = signal.Arrow;
                                signal.StopLossBidPrice = double.Parse(info[13]);
                            }
                            signal.GainTipPrice = double.Parse(info[7]);
                            if (NULL != info[8])
                            {
                                signal.GainTipTime = DateTime.Parse(info[8]);
                                signal.GainTip     = signal.Arrow;
                            }
                            signal.StopGainPrice = double.Parse(info[9]);
                            if (NULL != info[10])
                            {
                                signal.StopGainTime = DateTime.Parse(info[10]);
                                signal.StopGain     = signal.Arrow;
                            }
                            signal.Profit      = int.Parse(info[11]);
                            signal.ProfitPrice = double.Parse(info[13]);
                            if (signal.ProfitPrice != 0 && NULL != info[12])
                            {
                                signal.ProfitTime = DateTime.Parse(info[12]);
                            }
                            AppContext.SignalDatas.Add(signal);
                        }
                    }
                }
                catch (Exception e)
                {
                    LogUtil.Error("  M0004  " + e.StackTrace);
                }
            }
            signals = new Signal[AppContext.SignalDatas.Count];
            AppContext.SignalDatas.CopyTo(signals);
            MethodInvoker mi = new MethodInvoker(InitPriceList);

            priceListView.Invoke(mi);
            mi = new MethodInvoker(InitSignalList);
            signalListView.Invoke(mi);
            mi = new MethodInvoker(InitStatList);
            statListView.Invoke(mi);
            mi = new MethodInvoker(RefreshStatBtn);
            AppContext.TradeExpertControl.Invoke(mi);
        }