Пример #1
0
        /// <summary>
        /// 获取Adb内部版本,可能出现的错误,无法连接
        /// </summary>
        /// <param name="error">错误回调</param>
        /// <returns></returns>
        public static bool VerifyVersion(Action <ErrorResult> error)
        {
            // 连接到ADB服务
            var socket = SocketHelper.Connect();

            if (socket == null)
            {
                // 无法链接到ADB服务
                ErrorResult.InvokeError(error, ConnectError.AdbServerUnable, "Socket 无法链接到ADB服务端(5037)端口!");
                return(false);
            }
            // 执行查询内部版本命令
            const string cmd = "host:version";

            socket.Send(Encoding.ASCII.GetBytes($"{cmd.Length:X4}{cmd}"));
            // 读取结果,包含状态和版本
            var res  = SocketHelper.GetResult(socket);
            var flag = res.Okay && string.Equals(res.Data, AdbVersion);

            try
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            catch (Exception)
            {
                // ignored
            }
            return(flag);
        }
Пример #2
0
        private void Connect_Click(object sender, RoutedEventArgs e)
        {
            saveCursor           = this.Cursor;
            Mouse.OverrideCursor = Cursors.Wait;

            Connect_Button.IsEnabled = false;

            try
            {
                senderSock = SocketHelper.Connect(textBox.Text);
                IPEndPoint remoteIpEndPoint = senderSock.RemoteEndPoint as IPEndPoint;
                pageforNewConnection.reportActivity("\n Client connected to : " + remoteIpEndPoint, Brushes.Blue, FontWeights.Regular);
                //faccio prima anche se potrei fare dopo, cosi mi porto avanti prima di fare la recv, ma forse non va bene
                pageforNewConnection.addServer(senderSock);

                JsonObject jsObj = SocketHelper.ReceiveImmediatly <JsonObject>(senderSock);
                pageforNewConnection.firstInfo(jsObj, senderSock);

                Switcher.Switch(pageforNewConnection);      //forse meglio spostare, decido quando cambiare pagina
            }
            catch (Exception ex)
            {
                writeError(ex.Message);
            }
            finally
            {
                Connect_Button.IsEnabled = true;
                Mouse.OverrideCursor     = saveCursor;
            }
        }
Пример #3
0
        private void Connect()
        {
            var localEP = this.localEndPoint;

            if (this.logWriter != null && this.logWriter.Enable)
            {
                this.logWriter.WriteTimeLine("Connect {0}:{1}", this.host, this.port);
            }

            try
            {
                this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                if (localEP != null)
                {
                    this.socket.Bind(localEP);
                    //
                    if (this.logWriter != null && this.logWriter.Enable)
                    {
                        this.logWriter.WriteTimeLine("Bind local interface " + localEP.ToString());
                    }
                }

                //this.socket.Connect(this.host, this.port);
                SocketHelper.Connect(this.socket, this.host, this.port, this.timeout);

                //this.socket.ReceiveTimeout = this.timeout;
                //this.socket.SendTimeout = this.timeout;
                //
                var networkStream = new NetworkStream(socket, false);
                //
                if (this.enableSSL == true)
                {
                    this.stream = new SslStream(networkStream);

                    if (this.logWriter != null && this.logWriter.Enable)
                    {
                        this.logWriter.WriteTimeLine("SSL handshake");
                    }
                }
                else
                {
                    this.stream = networkStream;
                }
                this.stream.ReadTimeout  = this.timeout;
                this.stream.WriteTimeout = this.timeout;
            }
            catch (Exception exception)
            {
                throw new IOException(exception.Message, exception);
            }
        }
Пример #4
0
        public SocketResponse Connect()
        {
            string totalResponse = "";

            switch (EquipmentConnectionSetting.ConnectionType)
            {
            case ConnectionType.TcpIp:
                _socketClient.Connect();

                //Add login sequence here
                if (EquipmentConnectionSetting.LoginSequences != null && EquipmentConnectionSetting.LoginSequences.Any())
                {
                    foreach (var sequence in EquipmentConnectionSetting.LoginSequences.OrderBy(p => p.SequenceNumber))
                    {
                        if (!string.IsNullOrEmpty(sequence.ExpectedResponse))
                        {
                            var response = SendCommandAndWaitForResponse(sequence.Command.Unescape(), new List <string> {
                                sequence.ExpectedResponse
                            }, new TimeSpan(0, 0, 0, sequence.Timeout));
                            totalResponse += response.Data + Environment.NewLine;
                            if (response.TimeoutOccurred)
                            {
                                throw new Exception("Unable to login." + totalResponse);
                            }
                        }
                        else
                        {
                            SendCommand(sequence.Command);
                        }
                    }
                }

                break;

            case ConnectionType.Ssh:
                _sshClient.Connect();
                _shellStream = _sshClient.CreateShellStream("TestTerm", 80, 24, 800, 600, 1024);
                _shellStream.ErrorOccurred += _shellStream_ErrorOccurred;

                //Add login sequence here
                if (EquipmentConnectionSetting.LoginSequences != null && EquipmentConnectionSetting.LoginSequences.Any())
                {
                    foreach (var sequence in EquipmentConnectionSetting.LoginSequences.OrderBy(p => p.SequenceNumber))
                    {
                        if (!string.IsNullOrEmpty(sequence.ExpectedResponse))
                        {
                            var response = SendCommandAndWaitForResponse(sequence.Command.Unescape(), new List <string> {
                                sequence.ExpectedResponse
                            }, new TimeSpan(0, 0, 0, sequence.Timeout));
                            totalResponse += response.Data + Environment.NewLine;
                            if (response.TimeoutOccurred)
                            {
                                throw new Exception("Unable to login." + totalResponse);
                            }
                        }
                        else
                        {
                            SendCommand(sequence.Command);
                        }
                    }
                }

                break;

            case ConnectionType.Telnet:
                _socketClient.Connect();

                //Add login sequence here
                if (EquipmentConnectionSetting.LoginSequences != null && EquipmentConnectionSetting.LoginSequences.Any())
                {
                    foreach (var sequence in EquipmentConnectionSetting.LoginSequences.OrderBy(p => p.SequenceNumber))
                    {
                        if (!string.IsNullOrEmpty(sequence.ExpectedResponse))
                        {
                            var response = SendCommandAndWaitForResponse(sequence.Command.Unescape(), new List <string> {
                                sequence.ExpectedResponse
                            }, new TimeSpan(0, 0, 0, sequence.Timeout));
                            totalResponse += response.Data + Environment.NewLine;
                            if (response.TimeoutOccurred)
                            {
                                throw new Exception("Unable to login." + totalResponse);
                            }
                        }
                        else
                        {
                            SendCommand(sequence.Command);
                        }
                    }
                }

                break;

            default:
                throw new NotImplementedException();
            }

            return(new SocketResponse
            {
                Data = totalResponse,
                TimeoutOccurred = false
            });
        }
Пример #5
0
 private ClientService(IPEndPoint ipe)
 {
     _socket   = SocketHelper.GetSocket();
     Connected = SocketHelper.Connect(_socket, ipe);
 }