示例#1
0
        public string ExecuteCommand(MinerCommand command)
        {
            string response = string.Empty;

            byte[] byteBuffer = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(command));

            try
            {
                using (var client = new TcpClient())
                {
                    IAsyncResult ar = client.BeginConnect("localhost", PORT, null, null);

                    WaitHandle wh = ar.AsyncWaitHandle;
                    try
                    {
                        if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5), false))
                        {
                            client.Close();
                            throw new TimeoutException();
                        }

                        client.EndConnect(ar);
                    }
                    finally
                    {
                        wh.Close();
                    }

                    var stream = client.GetStream();
                    stream.Write(byteBuffer, 0, byteBuffer.Length);

                    var responseBytes = ReadFully(stream);
                    if (responseBytes != null && responseBytes.Length > 0)
                    {
                        response = Encoding.ASCII.GetString(responseBytes, 0, responseBytes.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                this.knownConnected = false;

                if (!(ex is SocketException))
                {
                    this.Message(this, new MessageEventArgs()
                    {
                        Message = "Unexpected error in ExecuteCommand: " + ex.ToString()
                    });
                }

                return(response);
            }

            if (command.Command != "quit")
            {
                this.consecutiveLaunches = 0; // reset consecutive launches

                if (!this.knownConnected)
                {
                    this.Connected(this, EventArgs.Empty);
                }

                this.knownConnected = true;
            }

            return(response);
        }
        public string ExecuteCommand(MinerCommand command)
        {
            string response = string.Empty;
            byte[] byteBuffer = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(command));

            try
            {
                using (var client = new TcpClient())
                {
                    IAsyncResult ar = client.BeginConnect("localhost", PORT, null, null);

                    WaitHandle wh = ar.AsyncWaitHandle;
                    try
                    {
                        if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5), false))
                        {
                            client.Close();
                            throw new TimeoutException();
                        }

                        client.EndConnect(ar);
                    }
                    finally
                    {
                        wh.Close();
                    }

                    var stream = client.GetStream();
                    stream.Write(byteBuffer, 0, byteBuffer.Length);

                    var responseBytes = ReadFully(stream);
                    if (responseBytes != null && responseBytes.Length > 0)
                    {
                        response = Encoding.ASCII.GetString(responseBytes, 0, responseBytes.Length);
                    }
                }
            }
            catch (Exception ex)
            {

                this.knownConnected = false;

                if (!(ex is SocketException))
                {
                    this.Message(this, new MessageEventArgs() { Message = "Unexpected error in ExecuteCommand: " + ex.ToString() });
                }

                return response;
            }

            if (command.Command != "quit")
            {
                this.consecutiveLaunches = 0; // reset consecutive launches

                if (!this.knownConnected)
                {
                    this.Connected(this, EventArgs.Empty);
                }

                this.knownConnected = true;
            }

            return response;
        }