예제 #1
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("DDE Console");
            String _myapp = "TOS";
            String _topic = "LAST";
            String _item = "SPY";

            try
            {
                // Create a client that connects to 'myapp|topic'.
                using (DdeClient client = new DdeClient(_myapp, _topic))
                {
                    // Subscribe to the Disconnected event.  This event will notify the application when a conversation has been terminated.
                    client.Disconnected += OnDisconnected;

                    // Connect to the server.  It must be running or an exception will be thrown.
                    client.Connect();

                    // Advise Loop
                    client.StartAdvise(_item, 1, true, 60000);
                    client.Advise += OnAdvise;

                    // Wait for the user to press ENTER before proceding.
                    Console.WriteLine("Press ENTER to quit...");
                    Console.ReadLine();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("Press ENTER to quit...");
                Console.ReadLine();
            }
        }
예제 #2
0
파일: Form1.cs 프로젝트: dpaluy/DDEConsole
        private void CollectPrice()
        {
            string _topic = "LAST";
            string _myapp = Properties.Settings.Default.AppDDE;
            string expiration = "110416";
            string item = "'.SPY" + expiration;  //'.SPY110416C132'
            DdeClient client;

            try
            {
                // Create a client that connects to 'myapp|topic'.
                using (client = new DdeClient(_myapp, _topic))
                {
                    // Subscribe to the Disconnected event.  This event will notify the application when a conversation has been terminated.
                    client.Disconnected += OnDisconnectedPrice;

                    // Connect to the server.  It must be running or an exception will be thrown.
                    client.Connect();

                    client.StartAdvise("SPY", 1, true, 60000);
                    // Advise Loop
                    for (int j = 0; j < 2; j++)
                    {
                        string cp = (j == 0) ? "C" : "P";
                        int strike = 127;
                        for (int i = 0; i < 10; i++)
                        {
                            ++strike;
                            string _item = item + cp + strike.ToString();
                            client.StartAdvise(_item, 1, true, 60000);
                        }
                    }
                    client.Advise += OnAdvisePrice;
                }
            }
            catch (Exception e)
            {
                //MessageBox.Show(e.ToString());
                log.WriteLine(getTimeStamp() + " Price " + e.ToString());
            }
        }
예제 #3
0
        public static void Main(string[] args)
        {
            // Wait for the user to press ENTER before proceding.
            Console.WriteLine("The Server sample must be running before the client can connect.");
            Console.WriteLine("Press ENTER to continue...");
            Console.ReadLine();
            try
            {
                // Create a client that connects to 'myapp|mytopic'.
                using (DdeClient client = new DdeClient("myapp", "mytopic"))
                {
                    // Subscribe to the Disconnected event.  This event will notify the application when a conversation has been terminated.
                    client.Disconnected += OnDisconnected;

                    // Connect to the server.  It must be running or an exception will be thrown.
                    client.Connect();

                    // Synchronous Execute Operation
                    client.Execute("mycommand", 60000);

                    // Synchronous Poke Operation
                    client.Poke("myitem", DateTime.Now.ToString(), 60000);

                    // Syncronous Request Operation
                    Console.WriteLine("Request: " + client.Request("myitem", 60000));

                    // Asynchronous Execute Operation
                    client.BeginExecute("mycommand", OnExecuteComplete, client);

                    // Asynchronous Poke Operation
                    client.BeginPoke("myitem", Encoding.ASCII.GetBytes(DateTime.Now.ToString() + "\0"), 1, OnPokeComplete, client);

                    // Asynchronous Request Operation
                    client.BeginRequest("myitem", 1, OnRequestComplete, client);

                    // Advise Loop
                    client.StartAdvise("myitem", 1, true, 60000);
                    client.Advise += OnAdvise;

                    // Wait for the user to press ENTER before proceding.
                    Console.WriteLine("Press ENTER to quit...");
                    Console.ReadLine();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("Press ENTER to quit...");
                Console.ReadLine();
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            var client = new DdeClient("MT4", "QUOTE");
            beat = new QuoteBeat();

            writer = new LogDataWriter("EURUSD");

            client.Advise += client_Advise;
            client.Connect();
            client.StartAdvise("EURUSD", 1, true, 60000);

            Console.ReadLine();
            writer.Close();
        }
        /** 新增項目(Item), 並取得該項目的值(Value)
         *   1. 呼叫 DdeClient 的 "Request()" & "StartAdvise" 取得該 Item 的值
         *   2. 利用 service|topic!item 字串為 key
         *   2.1 ht_gdv(HashTable) -> (key, DataGridViewRow)
         *   3. 在 dgItemInfo(DataGridView)新增一列, 顯示項目相關資訊
         *
         *   <param name="dc">     "新增項目" 按鈕事件所在列對應的 DdeClient
         *   <param name="item"> "新增項目" 名稱(Name)
         */
        private void AddItem(DdeClient dc, string item)
        {
            try
            {
                string key = dc.Service + "|" + dc.Topic + "!" + item;

                Item it = new Item();
                it.item = item; it.value = "";

                try
                {
                    //Synchronous Request Operation, 用以取得第一次呼叫的值
                    //eLeader, 康和 並未支援同步呼叫; yeswin/hts 則有支援
                    byte[] data = dc.Request(item, 1, 60000);
                    int codepage = Encoding.Default.CodePage;   //取得編碼的 codpage. 中文 Default: 950
                    string value = Encoding.GetEncoding(codepage).GetString(data, 0, data.Length);
                    //it.value = Encoding.ASCII.GetString(data);
                    it.value = value;
                }
                catch (Exception )
                {
                    ;  //忽略該錯誤
                }

                // Advise Loop
                dc.StartAdvise(item, 1, true, 60000);

                //Add a Row
                int idx = dgItemInfo.Rows.Add(dc.Service, dc.Topic, item, it.value);
                //保存 key 與 所在新增 Row
                ht_gdv.Add(key, dgItemInfo.Rows[idx]);
            }
            catch (Exception thrown)
            {
                MessageBox.Show("無法新增項目: \n" + thrown.Message);
            }
        }
예제 #6
0
        static void Main(string[] args)
        {
            XmlDocument xml = new XmlDocument();
            try
            {
                xml.Load(@"tase.xml");
            }
            catch (Exception )
            {
                Console.WriteLine("File dde.xml was not found in current folder!");
                Console.WriteLine("Press ENTER to quit...");
                Console.ReadLine();
                return;
            }

            Dictionary<string, string> myDict = new Dictionary<string, string>();
            XmlNodeList nodeList;
            XmlElement root = xml.DocumentElement;
            nodeList = root.SelectNodes("/tase/option");
            foreach (XmlNode option in nodeList)
            {
                XmlNodeList optionsData = option.ChildNodes;
                string opname = "";
                string id = "";
                string exp = "";
                foreach (XmlNode op in optionsData)
                {
                    if (op.Name == "name")
                    {
                        opname = op.InnerText.Substring(0, 6);
                    }
                    else if (op.Name == "id")
                    {
                        id = op.InnerText;
                    }
                    else if (op.Name == "expiration")
                    {
                        DateTime dt = DateTime.ParseExact(op.InnerText, "dd/MM/yyyy",
                                   CultureInfo.InvariantCulture);
                        exp = dt.Month.ToString();
                    }
                }
                //Console.Write("{0} {1}\n", opname+"_"+exp, id);
                myDict.Add(opname + "_" + exp, id);
            }

            // create a writer and open the file
            string filename = "data" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + ".txt";
            tw = new StreamWriter(filename);

            tw.WriteLine("DDE BIZPORTAL {0:MM/dd/yy}", DateTime.Now);
            string _myapp = "Star32";
            string _topic = "DDE";

            try
            {
                // Create a client that connects to 'myapp|topic'.
                using (DdeClient client = new DdeClient(_myapp, _topic))
                {
                    // Subscribe to the Disconnected event.  This event will notify the application when a conversation has been terminated.
                    client.Disconnected += OnDisconnected;

                    // Connect to the server.  It must be running or an exception will be thrown.
                    client.Connect();

                    // Advise Loop
                    foreach (KeyValuePair<string, string> pair in myDict)
                    {
                        foreach(string name in Enum.GetNames(typeof(DDE_VALUES)))
                        {
                            string advice = name + pair.Value;
                            client.StartAdvise(advice, 1, true, 60000);
                        }
                    }

                    client.Advise += OnAdvise;

                    // Wait for the user to press ENTER before proceding.
                    Console.WriteLine("Press Ctrl X to quit...");
                    ConsoleKeyInfo info = Console.ReadKey(true);
                    while (info.Key != ConsoleKey.X && info.Modifiers != ConsoleModifiers.Control)
                    {
                        info = Console.ReadKey(true);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("Press ENTER to quit...");
                Console.ReadLine();
            }

            // close the stream
            tw.Close();
        }
예제 #7
0
 public void Test_StopAdvise()
 {
     using (TestServer server = new TestServer(ServiceName))
     {
         server.Register();
         server.SetData(TopicName, ItemName, 1, Encoding.ASCII.GetBytes(TestData));
         using (DdeClient client = new DdeClient(ServiceName, TopicName))
         {
             client.Connect();
             client.StartAdvise(ItemName, 1, true, Timeout);
             client.StopAdvise(ItemName, Timeout);
         }
     }
 }
예제 #8
0
 public void Test_StartAdvise_Variation_3()
 {
     using (TestServer server = new TestServer(ServiceName))
     {
         server.Register();
         server.SetData(TopicName, ItemName, 1, Encoding.ASCII.GetBytes(TestData));
         using (DdeClient client = new DdeClient(ServiceName, TopicName))
         {
             EventListener listener = new EventListener();
             client.Advise += listener.OnEvent;
             client.Connect();
             client.StartAdvise(ItemName, 1, true, true, Timeout, "MyStateObject");
             server.Advise(TopicName, ItemName);
             Assert.IsTrue(listener.Received.WaitOne(Timeout, false));
             DdeAdviseEventArgs args = (DdeAdviseEventArgs)listener.Events[0];
             Assert.AreEqual(ItemName, args.Item);
             Assert.AreEqual(1, args.Format);
             Assert.AreEqual("MyStateObject", args.State);
             Assert.AreEqual(TestData, Encoding.ASCII.GetString(args.Data));
             Assert.AreEqual(TestData, args.Text);
         }
     }
 }
예제 #9
0
파일: client.cs 프로젝트: catataw/node-dde
        public async Task<object> GetInvoker(IDictionary<string, object> input)
        {
            var services = (IDictionary<string, object>)input["services"];
            var callbacks = (IDictionary<string, object>)input["callbacks"];
            var encoding = input.ContainsKey("encoding")
                ? Encoding.GetEncoding((string)input["encoding"]) : null;
            var decode = encoding == null
                ? (Func<object, string>)((s) => (string)s) : (s) => HttpUtility.UrlDecode((string)s, encoding);

            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(decode(service), decode(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"] = args.Text;
                    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 = decode((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 >= 1)
                {
                    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(decode(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]).Select(decode);
                                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);
                                    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]).Select(decode);
                                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]).Select(decode);;
                                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;
            });
        }
예제 #10
0
파일: Form1.cs 프로젝트: dpaluy/DDEConsole
        private void StartAssetDDE()
        {
            String _topic = "LAST";

            try
            {
                // Create a client that connects to 'myapp|topic'.
                using (DdeClient client = new DdeClient(Properties.Settings.Default.APP, _topic))
                {
                    // Subscribe to the Disconnected event.  This event will notify the application when a conversation has been terminated.
                    client.Disconnected += OnDisconnected;

                    // Connect to the server.  It must be running or an exception will be thrown.
                    client.Connect();

                    // Advise Loop
                    client.StartAdvise(Properties.Settings.Default.ITEM, 1, true, 60000);
                    client.Advise += OnAdvise;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Please run " + Properties.Settings.Default.APP + " and restart this application!");
            }
        }