Guest User

EMDR Client in C#

a guest
Apr 17th, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Web.Script.Serialization; // Needs reference to 'System.Web.Extensions.dll'
  8. using ZMQ; // Needs reference to 'clrzmq.dll' and adding 'libzmq.dll' to project
  9.            // 'clrzmq' can be found at: https://github.com/zeromq/clrzmq/downloads
  10.  
  11. namespace EMDR_Client
  12. {
  13.     public class Program
  14.     {
  15.         private static void Main()
  16.         {
  17.             using (var context = new Context())
  18.             {
  19.                 using (var subscriber = context.Socket(SocketType.SUB))
  20.                 {
  21.                     //Connect to the first publicly available relay.
  22.                     subscriber.Connect("tcp://relay-linode-atl-1.eve-emdr.com:8050");
  23.  
  24.                     // Disable filtering.
  25.                     subscriber.SetSockOpt(SocketOpt.SUBSCRIBE, Encoding.UTF8.GetBytes(""));
  26.  
  27.                     // Alternatively 'Subscribe' can be used
  28.                     //subscriber.Subscribe("", Encoding.UTF8);
  29.  
  30.                     while (true)
  31.                     {
  32.                         try
  33.                         {
  34.                             // Receive compressed raw market data.
  35.                             var receivedData = subscriber.Recv();
  36.  
  37.                             // The following code lines remove the need of 'zlib' usage;
  38.                             // 'zlib' actually uses the same algorith as 'DeflateStream'.
  39.                             // To make the data compatible for 'DeflateStream', we only have to remove
  40.                             // the four last bytes which are the adler32 checksum and
  41.                             // the two first bytes which are the 'zlib' header.
  42.                             byte[] decompressed;
  43.                             byte[] choppedRawData = new byte[(receivedData.Length - 4)];
  44.                             Array.Copy(receivedData, choppedRawData, choppedRawData.Length);
  45.                             choppedRawData = choppedRawData.Skip(2).ToArray();
  46.  
  47.                             // Decompress the raw market data.
  48.                             using (MemoryStream inStream = new MemoryStream(choppedRawData))
  49.                             using (MemoryStream outStream = new MemoryStream())
  50.                             {
  51.                                 DeflateStream outZStream = new DeflateStream(inStream, CompressionMode.Decompress);
  52.                                 outZStream.CopyTo(outStream);
  53.                                 decompressed = outStream.ToArray();
  54.                             }
  55.  
  56.                             // Transform data into JSON strings.
  57.                             string marketJson = Encoding.UTF8.GetString(decompressed);
  58.  
  59.                             // Un-serialize the JSON data to a dictionary.
  60.                             var serializer = new JavaScriptSerializer();
  61.                             var dictionary = serializer.Deserialize<Dictionary<string, object>>(marketJson);
  62.  
  63.                             // Dump the market data to console or, you know, do more fun things here.
  64.                             foreach (KeyValuePair<string, object> pair in dictionary)
  65.                             {
  66.                                 Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
  67.                             }
  68.                             Console.WriteLine();
  69.                         }
  70.                         catch (ZMQ.Exception ex)
  71.                         {
  72.                             Console.WriteLine("ZMQ Exception occurred : {0}", ex.Message);
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment