static void stratum_GotResponse(object sender, StratumEventArgs e) { StratumResponse Response = (StratumResponse)e.MiningEventArg; Console.Write("Got Response to {0} - ", (string)sender); switch ((string)sender) { case "mining.authorize": if ((bool)Response.result) { Console.WriteLine("Worker authorized"); } else { Console.WriteLine("Worker rejected"); Console.ReadLine(); } break; case "mining.subscribe": stratum.ExtraNonce1 = (string)((object[])Response.result)[1]; Console.WriteLine("Subscribed. ExtraNonce1 set to " + stratum.ExtraNonce1); break; case "mining.submit": if (Response.result != null && (bool)Response.result) { SharesAccepted++; Console.WriteLine("Share accepted ({0} of {1})", SharesAccepted, SharesSubmitted); } else { Console.WriteLine("Share rejected. {0}", Response.error[1]); } break; } }
// Callback for Read operation private void ReadCallback(IAsyncResult result) { NetworkStream networkStream; int bytesread; byte[] buffer = result.AsyncState as byte[]; try { networkStream = tcpClient.GetStream(); bytesread = networkStream.EndRead(result); } catch (Exception ex) { Console.WriteLine("Socket error:" + ex.Message); return; } if (bytesread == 0) { Console.WriteLine(DateTime.Now + " Disconnected. Reconnecting..."); Debug.WriteLine(DateTime.Now + " Disconnected. Reconnecting..."); tcpClient.Close(); tcpClient = null; PendingACKs.Clear(); ConnectToServer(Server, Port, Username, Password); return; } // Get the data string data = ASCIIEncoding.ASCII.GetString(buffer, 0, bytesread); Debug.WriteLine(data); page = page + data; int FoundClose = page.IndexOf('}'); while (FoundClose > 0) { string CurrentString = page.Substring(0, FoundClose + 1); // We can get either a command or response from the server. Try to deserialise both StratumCommand Command = Utilities.JsonDeserialize <StratumCommand>(CurrentString); StratumResponse Response = Utilities.JsonDeserialize <StratumResponse>(CurrentString); StratumEventArgs e = new StratumEventArgs(); if (Command.method != null) // We got a command { Debug.WriteLine(DateTime.Now + " Got Command: " + CurrentString); 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) // We got a response { Debug.WriteLine(DateTime.Now + " Got Response: " + CurrentString); e.MiningEventArg = Response; // Find the command that this is the response to and remove it from the list of commands that we're waiting on a response to string Cmd = (string)PendingACKs[Response.id]; PendingACKs.Remove(Response.id); if (Cmd == null) { Console.WriteLine("Unexpected Response"); } else if (GotResponse != null) { GotResponse(Cmd, e); } } page = page.Remove(0, FoundClose + 2); FoundClose = page.IndexOf('}'); } // Then start reading from the network again. networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer); }