Ejemplo n.º 1
0
        public void Connect(string hostname, int port)
        {
            if (string.IsNullOrEmpty(hostname) || port < 1)
            {
                return;
            }

            if (!IPAddress.TryParse(hostname, out IPAddress ip))
            {
                ip = Dns.GetHostAddresses(hostname)[0];
            }
            if (ip == null)
            {
                throw new Exception("Could not resolve hostname \"" + hostname + "\"");
            }

            _socket.BeginConnect(ip.ToString(), port, ar => {
                try {
                    _socket.EndConnect(ar);

                    if ((( TcpSocket )ar.AsyncState)._socket.Connected)
                    {
                        ConnectionSuccessful?.Invoke(this);
                    }
                    else
                    {
                        ConnectionFailed?.Invoke(this, null);
                    }
                } catch (Exception ex) {
                    ConnectionFailed?.Invoke(this, ex);
                }
            }, this);
        }
Ejemplo n.º 2
0
        private void SendPing(int tries)
        {
            if (tries <= 0)
            {
                if (_ping.Received)
                {
                    Console.WriteLine("Pong received!");
                    Connected = true;
                    ConnectionSuccessful?.Invoke(this);
                }
                else
                {
                    Console.WriteLine("Ping did not come back on time.");
                    Connected = false;
                    ConnectionFailed?.Invoke(this, null);
                    return;
                }
            }

            Task.Run(() => {
                _ping         = new Ping();
                string pingID = _ping.ID;

                Send(_ping);
                Thread.Sleep(200);

                if (_ping.ID == pingID && !_ping.Received)
                {
                    SendPing(--tries);
                }
            });
        }
Ejemplo n.º 3
0
        protected virtual void OnConnectionSuccessful()
        {
            // DEBUG: Log successful connection.
            Log.Debug($"Connection to ('{_address}:{_port}') was successful after ({ConnectionAttempts}) attempt(s).");

            ConnectionState = ClientConnectionState.Connected;
            ConnectionSuccessful?.Invoke(this, new EventArgs());
        }
 public void Connect()
 {
     try
     {
         _connection.Open();
         ConnectionSuccessful?.Invoke(this, new EventArgs());
     }
     catch (MySqlException e)
     {
         throw new CouldNotConnectException("Could not connect to Database!", e);
     }
 }
        /// <summary>
        /// Connects to the Database.
        /// </summary>
        /// <exception cref="MissingConnectionStringException" />
        /// <exception cref="CouldNotConnectException" />
        public void Connect()
        {
            if (!_isConnectionStringSet)
            {
                throw new MissingConnectionStringException("ConnectionString not set. Please use MySqlDatabaseManager.SetConnectionString() before.");
            }

            try
            {
                _connection.Open();
                ConnectionSuccessful?.Invoke(this, new EventArgs());
            }
            catch (MySqlException e)
            {
                throw new CouldNotConnectException("Could not connect to Database!", e);
            }
        }
Ejemplo n.º 6
0
        public void Initiate()
        {
            listenThread = new Thread(Listener);
            listenThread.Start();

            socket = hc06.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));

            try
            {
                socket.Connect();
                Toast.MakeText(context, "Socketverbindung erfolgreich", ToastLength.Long).Show();
                ConnectionSuccessful?.Invoke(this, new EventArgs());
            }
            catch (SocketException e)
            {
                socket.Close();
                Toast.MakeText(context, "Socketverbindung fehlgeschlagen", ToastLength.Long).Show();
                ConnectionFailed?.Invoke(this, new EventArgs());
            }
        }