Exemplo n.º 1
0
 public void StockInfo(string item, out string hata)
 {
     try
     {
         hata = "";
         if (client.IsConnected == true)
         {
             try
             {
                 client.StopAdvise(item, 1000);
             }
             catch
             { }
             client.StartAdvise(item, 1, true, 60000);
         }
         else
         {
             client.Connect();
             client.StartAdvise(item, 1, true, 60000);
         }
     }
     catch (Exception ex)
     {
         hata = ex.Message;
     }
 }
Exemplo n.º 2
0
        void ddeClient_Advise(object sender, DdeAdviseEventArgs e)
        {
            if (isPaused)
            {
                return;
            }

            //item is instrument name
            //text is  2010/07/15 21:27 1.29012 1.29016
            Tick t = new Tick();

            t.Instrument = instrument;
            string[] parts = e.Text.Split(' ');
            t.Time = DateTime.Parse(parts[0] + " " + parts[1]);
            t.Bid  = double.Parse(parts[2]);
            t.Ask  = double.Parse(parts[3]);

            if (ProcessTick != null)
            {
                ProcessTick(t);
            }
            tickLimit--;

            if (tickLimit == 0)
            {
                ddeClient.StopAdvise(instrument.ToString(), 1000);
                status = DataProviderStatus.Stopped;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Keep alive connection to ProfitChart DDE
        /// </summary>
        private void KeepAliveNPC()
        {
            while (_keealivenpc)
            {
                var timeout = 10000;
                System.Threading.Thread.Sleep(timeout);

                var span = (BvspTime - _lasttime["NPC"]).TotalMilliseconds;
                if (timeout > span || !IsDDEAvailable(_npcClient))
                {
                    continue;
                }

                NotifyDDE("OFF NPC: " + (int)span / 1e3 + " segundos sem novos dados.");

                foreach (var item in _npcSymbols)
                {
                    try { _npcClient.StopAdvise(item.Key, 1); }
                    catch (Exception x) { NotifyDDE("OFF NPC: " + x.Message); }
                }

                foreach (var item in _npcSymbols)
                {
                    try { _npcClient.StartAdvise(item.Key, 1, true, 60000); NotifyDDE("ProfitChart RT"); }
                    catch (Exception x) { NotifyDDE("OFF NPC: " + x.Message); }
                }
            }
        }
Exemplo n.º 4
0
 public void Test_StopAdvise_Before_StartAdvise()
 {
     using var server = new TestServer(ServiceName);
     server.Register();
     server.SetData(TopicName, ItemName, 1, Encoding.ASCII.GetBytes(TestData));
     using var client = new DdeClient(ServiceName, TopicName);
     client.Connect();
     Assert.Throws<InvalidOperationException>(() => client.StopAdvise(ItemName, Timeout));
 }
Exemplo n.º 5
0
 public void Test_StopAdvise()
 {
     using var server = new TestServer(ServiceName);
     server.Register();
     server.SetData(TopicName, ItemName, 1, Encoding.ASCII.GetBytes(TestData));
     using var client = new DdeClient(ServiceName, TopicName);
     client.Connect();
     client.StartAdvise(ItemName, 1, true, Timeout);
     client.StopAdvise(ItemName, Timeout);
 }
Exemplo n.º 6
0
 //Stop Advise
 private void Stop_advise(DdeClient dc, string item)
 {
     try
     {
         dc.StopAdvise(item, 60000);
     }
     catch (Exception thrown)
     {
         MessageBox.Show("Can not Stop Advise: \n" + thrown.Message);
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Keep alive connection to DDE Server
        /// </summary>
        private void KeepAliveDDE()
        {
            while (true)
            {
                var timeout = 10000;
                Thread.Sleep(timeout);

                if (ChooseServers())
                {
                    continue;
                }

                ReSubscribe();

                var now = DateTime.Now;

                foreach (var item in _timeofday)
                {
                    if (now < item.Value.AddMilliseconds(timeout))
                    {
                        continue;
                    }
                    try
                    {
                        if (_ddeclient.IsConnected)
                        {
                            _ddeclient.StopAdvise(item.Key, 1);
                        }
                        else
                        {
                            _ddeclient.Connect();
                        }
                    }
                    catch (Exception x) { NotifyDDE("OFF " + x.Message); }
                    finally
                    {
                        if (_ddeclient.IsConnected)
                        {
                            try { _ddeclient.StartAdvise(item.Key, 1, true, 60000); }
                            catch (Exception x) { NotifyDDE("OFF " + x.Message); }
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        public async Task <object> GetInvoker(IDictionary <string, object> input)
        {
            var services  = (IDictionary <string, object>)input["services"];
            var callbacks = (IDictionary <string, object>)input["callbacks"];

            var clients = new List <DdeClient>();

            foreach (string service in services.Keys)
            {
                var topics = (IDictionary <string, object>)services[service];
                foreach (string topic in topics.Keys)
                {
                    DdeClient client = new DdeClient(service, topic);
                    clients.Add(client);
                }
            }

            var onDisconnected = (Func <object, Task <object> >)callbacks["OnDisconnected"];

            foreach (DdeClient client in clients)
            {
                client.Disconnected += async(object sender, DdeDisconnectedEventArgs args) =>
                {
                    var obj = new Dictionary <string, object>();
                    obj["service"]           = client.Service;
                    obj["topic"]             = client.Topic;
                    obj["isDisposed"]        = args.IsServerInitiated;
                    obj["isServerInitiated"] = args.IsServerInitiated;
                    await Task.Run(async() => await onDisconnected(obj));
                };
            }

            var onAdvise = (Func <object, Task <object> >)callbacks["OnAdvise"];

            foreach (DdeClient client in clients)
            {
                client.Advise += async(object sender, DdeAdviseEventArgs args) =>
                {
                    var obj = new Dictionary <string, object>();
                    obj["service"] = client.Service;
                    obj["topic"]   = client.Topic;
                    obj["item"]    = args.Item;
                    obj["text"]    = Encoding.Default.GetString(args.Data).Trim('\0', ' ').ToString();
                    await Task.Run(async() => await onAdvise(obj));
                };
            }

            return((Func <object, Task <object> >)(async(i) =>
            {
                var opts = (IDictionary <string, object>)i;
                var method = (string)opts["method"];
                var command = opts.ContainsKey("command") ? (string)opts["command"] : null;
                var data = opts.ContainsKey("data") ? (string)opts["data"] : null;
                var format = opts.ContainsKey("format") ? (int)opts["format"] : 1;
                var timeout = opts.ContainsKey("timeout") ? (int)opts["timeout"] : 10000;
                var hot = opts.ContainsKey("hot") ? (bool)opts["hot"] : true;
                var callback = opts.ContainsKey("callback")
                    ? (Func <object, Task <object> >)opts["callback"] : (o) => null;

                var results = new List <IDictionary <string, object> >();

/*
 *                              string item = null;
 *                              if (opts.ContainsKey("item"))
 *                              {
 *                                      item = (string)opts["item"];
 *                              }
 *
 *              if (clients.Count == 1 && item != null) // a single client and only one 'item' is supplied.
 *              {
 *                  var client = clients.First();
 *                                      ((IDictionary<string, object>)services[client.Service])[client.Topic] = new[] { item };
 *
 *                  byte[] result = null;
 *
 *                  switch (method)
 *                  {
 *                      case "Request":
 *                          result = client.Request(item, format, timeout);
 *                          return Encoding.Default.GetString(result);
 *                      case "BeginExecute":
 *                          await Task.Run(() =>
 *                          {
 *                              var tcs = new TaskCompletionSource<bool>();
 *                              AsyncCallback cb = (ar) => { tcs.SetResult(true); };
 *                              client.BeginExecute(command, cb, client);
 *                              var r = tcs.Task.Result;
 *                          });
 *                          break;
 *                      case "BeginPoke":
 *                          await Task.Run(() =>
 *                          {
 *                              var tcs = new TaskCompletionSource<bool>();
 *                              AsyncCallback cb = (ar) => { tcs.SetResult(true); };
 *                              var bytes = Encoding.Default.GetBytes((string)opts["data"] + "\0");
 *                              client.BeginPoke(item, bytes, format, cb, client);
 *                              var r = tcs.Task.Result;
 *                          });
 *                          break;
 *                      case "BeginRequest":
 *                          await Task.Run(() =>
 *                          {
 *                              var tcs = new TaskCompletionSource<byte[]>();
 *                              AsyncCallback cb = (ar) =>
 *                              {
 *                                  tcs.SetResult(client.EndRequest(ar));
 *                              };
 *                              client.BeginRequest(item, format, cb, client);
 *                              result = tcs.Task.Result;
 *                          });
 *                          return Encoding.Default.GetString(result);
 *                      case "BeginStartAdvise":
 *                          await Task.Run(() =>
 *                          {
 *                              var tcs = new TaskCompletionSource<bool>();
 *                              AsyncCallback cb = (ar) => { tcs.SetResult(true); };
 *                              client.BeginStartAdvise(item, format, hot, cb, client);
 *                              var r = tcs.Task.Result;
 *                          });
 *                          break;
 *                      case "BeginStopAdvise":
 *                          await Task.Run(() =>
 *                          {
 *                              var tcs = new TaskCompletionSource<bool>();
 *                              AsyncCallback cb = (ar) => { tcs.SetResult(true); };
 *                              client.BeginStopAdvise(item, cb, client);
 *                              var r = tcs.Task.Result;
 *                          });
 *                          break;
 *                      case "Service":
 *                          return client.Service;
 *                      case "Topic":
 *                          return client.Topic;
 *                      case "Handle":
 *                          return client.Handle;
 *                      case "IsConnected":
 *                          return client.IsConnected;
 *                      case "IsPaused":
 *                          return client.IsPaused;
 *                  }
 *              } */

                if (clients.Count != 0)
                {
                    switch (method)
                    {
                    case "Connect":
                        clients.ForEach((c) => c.Connect());
                        break;

                    case "Disconnect":
                        clients.ForEach((c) => c.Disconnect());
                        break;

                    case "Pause":
                        clients.ForEach((c) => c.Pause());
                        break;

                    case "Resume":
                        clients.ForEach((c) => c.Resume());
                        break;

                    case "Execute":
                        clients.ForEach((c) => c.Execute(command, timeout));
                        break;

                    case "Poke":
                        foreach (DdeClient client in clients)
                        {
                            var topics = (IDictionary <string, object>)services[client.Service];
                            var items = (object[])topics[client.Topic];
                            foreach (string item_ in items)
                            {
                                client.Poke(item_, data, timeout);
                            }
                        }
                        break;

                    case "Request":
                        foreach (DdeClient client in clients)
                        {
                            var topics = (IDictionary <string, object>)services[client.Service];
                            // var items = (object[])topics[client.Topic];
                            var items = (object[])opts["item"];
                            foreach (string item_ in items)
                            {
                                var result = client.Request(item_, format, timeout);
                                var obj = new Dictionary <string, object>();
                                obj["service"] = client.Service;
                                obj["topic"] = client.Topic;
                                obj["item"] = item_;
                                obj["result"] = Encoding.Default.GetString(result).Trim('\0', ' ').ToString();
                                results.Add(obj);
                            }
                        }
                        return results.ToArray();

                    case "StartAdvise":
                        foreach (DdeClient client in clients)
                        {
                            var topics = (IDictionary <string, object>)services[client.Service];
                            var items = (object[])topics[client.Topic];
                            foreach (string item_ in items)
                            {
                                client.StartAdvise(item_, format, hot, timeout);
                            }
                        }
                        break;

                    case "StopAdvise":
                        foreach (DdeClient client in clients)
                        {
                            var topics = (IDictionary <string, object>)services[client.Service];
                            var items = (object[])topics[client.Topic];
                            foreach (string item_ in items)
                            {
                                client.StopAdvise(item_, timeout);
                            }
                        }
                        break;

                    case "Dispose":
                        clients.ForEach((c) => c.Dispose());
                        break;

                    case "Service":
                        return clients.Select((c) => c.Service).ToArray();

                    case "Topic":
                        return clients.Select((c) => c.Topic).Distinct().ToArray();

                    case "Handle":
                        return clients.Select((c) => c.Handle).ToArray();

                    case "IsConnected":
                        return clients.All((c) => c.IsConnected);

                    case "IsPaused":
                        return clients.All((c) => c.IsPaused);
                    }
                }

                return null;
            }));
        }