Пример #1
0
        private void Listen()
        {
            try
            {
                while (client.Connected)
                {
                    if ((DateTime.Now - lastComm) > TimeSpan.FromMinutes(30) || BadPacketCnt > 10)
                    {
                        break;
                    }

                    string message = reader.ReadLine();

                    if (string.IsNullOrEmpty(message) || string.IsNullOrWhiteSpace(message))
                    {
                        Console.WriteLine("Bad TCP packet!");
                        BadPacketCnt++;
                        continue;
                    }

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine();
                    Console.WriteLine("TCP IN: " + message + Environment.NewLine);
                    Console.ResetColor();



                    try
                    {
                        JObject msg = JsonConvert.DeserializeObject(message, new JsonSerializerSettings()
                        {
                            NullValueHandling = NullValueHandling.Ignore
                        }) as JObject;

                        var    method = ((JObject)msg)["method"].ToString();
                        string para   = "";
                        if (msg.ContainsKey("params"))
                        {
                            para = msg["params"].ToString().Replace("\\", "");
                        }

                        BadPacketCnt = 0;

                        switch (method)
                        {
                        case "job":
                            CurrentJob = JsonConvert.DeserializeObject <JobTemplate>(para, new JsonSerializerSettings()
                            {
                                NullValueHandling = NullValueHandling.Ignore
                            });
                            lastComm = DateTime.Now;
                            break;

                        case "submit":
                            if (msg.ContainsKey("result") && msg["result"].ToString() == "ok")
                            {
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine(
                                    @"
                                    _._
                               _.-='_-         _
                          _.-='   _-          | ||'''''''---._______     __..
              ___.===''''-.______-,,,,,,,,,,,,`-''----' '''''       '''''  __'
       __.--''     __        ,'                   o \           __        [__|
  __-''=======.--''  ''--.=================================.--''  ''--.=======:
 ]       [w] : /        \ : |========================|    : /        \ :  [w] :
 V___________:|          |: |========================|    :|          |:   _-'
  V__________: \        / :_|=======================/_____: \        / :__-'
  -----------'  ''____''  `-------------------------------'  ''____''

"
                                    );
                                Console.ResetColor();
                            }
                            break;

                        default:
                            Console.WriteLine(para);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                IsConnected = false;
                Console.WriteLine("Connection dropped.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Пример #2
0
        private void Listen()
        {
            try
            {
                while (client.Connected)
                {
                    if ((DateTime.Now - lastComm) > TimeSpan.FromMinutes(30) || BadPacketCnt > 10)
                    {
                        break;
                    }

                    string message = reader.ReadLine();

                    if (string.IsNullOrEmpty(message) || string.IsNullOrWhiteSpace(message))
                    {
                        Console.WriteLine("Bad TCP packet!");
                        BadPacketCnt++;
                        continue;
                    }

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine();
                    Console.WriteLine("TCP IN: " + message + Environment.NewLine);
                    Console.ResetColor();



                    try
                    {
                        JObject msg = JsonConvert.DeserializeObject(message, new JsonSerializerSettings()
                        {
                            NullValueHandling = NullValueHandling.Ignore
                        }) as JObject;

                        var    method = ((JObject)msg)["method"].ToString();
                        string para   = "";
                        if (msg.ContainsKey("params"))
                        {
                            para = msg["params"].ToString() /*.Replace("\\", "")*/;
                        }

                        BadPacketCnt = 0;

                        switch (method)
                        {
                        case "getjobtemplate":
                            if (msg.ContainsKey("result"))
                            {
                                CurrentJob = JsonConvert.DeserializeObject <JobTemplate>(msg["result"].ToString(), new JsonSerializerSettings()
                                {
                                    NullValueHandling = NullValueHandling.Ignore
                                });
                                if (CurrentJob != null && CurrentJob.pre_pow != null && CurrentJob.pre_pow != "")
                                {
                                    WaitForJob = false;
                                    lastComm   = DateTime.Now;
                                }
                            }
                            break;

                        case "job":
                            CurrentJob = JsonConvert.DeserializeObject <JobTemplate>(para, new JsonSerializerSettings()
                            {
                                NullValueHandling = NullValueHandling.Ignore
                            });
                            WaitForJob = false;
                            lastComm   = DateTime.Now;
                            break;

                        case "submit":
                            if (msg.ContainsKey("result") && msg["result"].ToString() == "ok")
                            {
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine("Share accepted");
                                Console.ResetColor();
                            }
                            else if (msg.ContainsKey("result") && msg["result"].ToString().StartsWith("blockfound"))
                            {
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine("###################################");
                                Console.WriteLine("######  Block mined!  #" + (++mined).ToString("D4") + "  ######");     // 8 chars
                                Console.WriteLine("###################################");
                                Console.ResetColor();
                                statistics.mined++;
                            }
                            break;

                        default:
                            Console.WriteLine(para);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                IsConnected = false;
                // TODO REMOVE when recconect is added
                WaitForJob = false;
                Console.WriteLine("Connection dropped.");
                // reconnect
                if (!terminated)
                {
                    Console.WriteLine("Reconnecting");
                    Connect();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }