public void SendAUTHORIZE()
        {
            Byte[]         bytesSent;
            ZtratumCommand Command = new ZtratumCommand();

            Command.id         = ID++;
            Command.method     = "mining.authorize";
            Command.parameters = new ArrayList();
            Command.parameters.Add(Username);
            Command.parameters.Add(Password);

            string request = Utilities.JsonSerialize(Command) + "\n";

            bytesSent = Encoding.ASCII.GetBytes(request);

            try
            {
                tcpClient.GetStream().Write(bytesSent, 0, bytesSent.Length);
                HT ht = new HT(Command.id, Command.method);
                PendingACKs.Add(ht);
            }
            catch (Exception ex)
            {
                if (!GV.StopMining)
                {
                    main.WriteLog("Socket error:" + ex.Message);
                    ConnectToServer(Server, Port, Username, Password);
                }

                return; //
            }

            main.WriteLog("Sent mining.authorize");
        }
        private void ztratum_GotSetDifficulty(object sender, ZtratumEventArgs e)
        {
            ZtratumCommand Command = (ZtratumCommand)e.MiningEventArg;

            GV.CurrentDifficulty = Convert.ToDouble(Command.parameters[0]);
            double diff = Convert.ToDouble(GV.CurrentDifficulty);

            diff             = 65536d / diff;
            GV.CurrentTarget = Convert.ToUInt32(diff);

            WriteLog("Got Set_Difficulty " + GV.CurrentDifficulty);
        }
        private void ztratum_GotNotify(object sender, ZtratumEventArgs e)
        {
            Job            ThisJob = new Job();
            ZtratumCommand Command = (ZtratumCommand)e.MiningEventArg;

            ThisJob.JobID = (string)Command.parameters[0];

            lastJob = ThisJob.JobID;


            ThisJob.PreviousHash = (string)Command.parameters[1];
            ThisJob.Coinb1       = (string)Command.parameters[2];
            ThisJob.Coinb2       = (string)Command.parameters[3];
            Array a = (Array)Command.parameters[4];

            ThisJob.Version           = (string)Command.parameters[5];
            ThisJob.NetworkDifficulty = (string)Command.parameters[6];
            ThisJob.NetworkTime       = (string)Command.parameters[7];
            ThisJob.CleanJobs         = (bool)Command.parameters[8];

            ThisJob.MerkleNumbers = new string[a.Length];

            int i = 0;

            foreach (string s in a)
            {
                ThisJob.MerkleNumbers[i++] = s;
            }


            if (ThisJob.CleanJobs)
            {
                WriteLog("Detected a new block. Stopping old threads.");

                IncomingJobs.Clear();
                CDigger.done = true;

                submitList = new List <uint>();
            }
            else
            {
                WriteLog("Detected a new job. Stopping old threads.");
                WriteLog("Job: " + ThisJob.JobID);

                IncomingJobs.Clear();
                CDigger.done = true;
            }

            IncomingJobs.Enqueue(ThisJob);
        }
        public void SendSUBMIT(string JobID, string nTime, uint Nonce, double Difficulty)
        {
            ZtratumCommand Command = new ZtratumCommand();

            Command.id         = ID++;
            Command.method     = "mining.submit";
            Command.parameters = new ArrayList();
            Command.parameters.Add(Username);
            Command.parameters.Add(JobID.ToLower());
            Command.parameters.Add(LittleEndian(ExtraNonce2).ToLower());
            Command.parameters.Add(nTime.ToLower());
            Command.parameters.Add(LittleEndian(Nonce).ToLower());

            string SubmitString = Utilities.JsonSerialize(Command) + "\n";

            Byte[] bytesSent = Encoding.ASCII.GetBytes(SubmitString);

            try
            {
                tcpClient.GetStream().Write(bytesSent, 0, bytesSent.Length);

                main.SharesSubmitted++;
                main.totalShareSubmited++;

                HT ht = new HT(Command.id, Command.method);
                PendingACKs.Add(ht);
            }
            catch (Exception ex)
            {
                if (!GV.StopMining)
                {
                    main.WriteLog("Socket error:" + ex.Message);
                    ConnectToServer(Server, Port, Username, Password);
                }

                return; //
            }

            main.WriteLog("Submit (Difficulty " + Difficulty + ")");
        }
        private void ReadCallback(IAsyncResult result)
        {
            try
            {
                NetworkStream networkStream;
                int           bytesread;

                byte[] buffer = result.AsyncState as byte[];

                try
                {
                    networkStream = tcpClient.GetStream();
                    bytesread     = networkStream.EndRead(result);
                }
                catch (Exception ex)
                {
                    if (!GV.StopMining)
                    {
                        main.WriteLog("Socket error:" + ex.Message);
                    }
                    return;
                }

                if (bytesread == 0)
                {
                    main.WriteLog("Disconnected. Reconnecting...");

                    tcpClient.Close();
                    tcpClient = null;
                    PendingACKs.Clear();

                    if (!GV.StopMining)
                    {
                        ConnectToServer(Server, Port, Username, Password);
                    }
                    return;
                }


                string data = ASCIIEncoding.ASCII.GetString(buffer, 0, bytesread);


                page = page + data;

                int FoundClose = page.IndexOf('}');

                while (FoundClose > 0)
                {
                    string CurrentString = page.Substring(0, FoundClose + 1);


                    ZtratumCommand  Command  = Utilities.JsonDeserialize <ZtratumCommand>(CurrentString);
                    ZtratumResponse Response = Utilities.JsonDeserialize <ZtratumResponse>(CurrentString);

                    ZtratumEventArgs e = new ZtratumEventArgs();

                    if (Command.method != null)
                    {
                        e.MiningEventArg = Command;

                        switch (Command.method)
                        {
                        case "mining.notify":
                            if (GotNotify != null)
                            {
                                GotNotify(this, e);
                            }
                            break;

                        case "mining.set_difficulty":
                            if (GotSetDifficulty != null)
                            {
                                GotSetDifficulty(this, e);
                            }
                            break;
                        }
                    }
                    else if (Response.error != null || Response.result != null)
                    {
                        e.MiningEventArg = Response;



                        string Cmd = "";
                        for (int i = 0; i < PendingACKs.Count; i++)
                        {
                            if (PendingACKs[i].id == Response.id)
                            {
                                Cmd = PendingACKs[i].method;
                                PendingACKs.RemoveAt(i);
                                break;
                            }
                        }

                        if (Cmd == null)
                        {
                            main.WriteLog("Unexpected Response");
                        }
                        else if (GotResponse != null)
                        {
                            GotResponse(Cmd, e);
                        }
                    }

                    page       = page.Remove(0, FoundClose + 2);
                    FoundClose = page.IndexOf('}');
                }


                networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
            }
            catch { }
        }