Example #1
0
        void connect()
        {
            client = new TcpClient();

            //Notify we are connecting
            var eoc = this.OnConnecting;
            if (eoc != null)
                eoc(this.appleSettings.Host, this.appleSettings.Port);

            try
            {
                var connectDone = new AutoResetEvent(false);

                //Connect async so we can utilize a connection timeout
                connectAsyncResult = client.BeginConnect(
                    appleSettings.Host, appleSettings.Port,
                    new AsyncCallback(
                        delegate(IAsyncResult ar)
                        {
                            if (connectAsyncResult != ar)
                                return;

                            try
                            {
                                client.EndConnect(ar);

                                //Set keep alive on the socket may help maintain our APNS connection
                                //client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

                                //Really not sure if this will work on MONO....
                                try { client.SetSocketKeepAliveValues(20 * 60 * 1000, 30 * 1000); }
                                catch { }

                                Interlocked.Increment(ref reconnects);

                                //Trigger the reset event so we can continue execution below
                                connectDone.Set();
                            }
                            catch (Exception ex)
                            {
                                Log.Error("APNS Connect Callback Failed: " + ex);
                            }
                        }
                    ), client
                );

                if (!connectDone.WaitOne(appleSettings.ConnectionTimeout))
                {
                    throw new TimeoutException("Connection to Host Timed Out!");
                }
            }
            catch (Exception ex)
            {
                throw new ConnectionFailureException("Connection to Host Failed", ex);
            }

            if (appleSettings.SkipSsl)
            {
                networkStream = client.GetStream();
            }
            else
            {
                RemoteCertificateValidationCallback userCertificateValidation;

                if (appleSettings != null && appleSettings.ValidateServerCertificate)
                {
                    userCertificateValidation = ValidateRemoteCertificate;
                }
                else
                {
                    userCertificateValidation = (sender, cert, chain, sslPolicyErrors) => true; //Don't validate remote cert
                }

                stream = new SslStream(client.GetStream(), false,
                    userCertificateValidation,
                    (sender, targetHost, localCerts, remoteCert, acceptableIssuers) => certificate);

                try
                {
                    stream.AuthenticateAsClient(this.appleSettings.Host, this.certificates, System.Security.Authentication.SslProtocols.Ssl3, false);
                    //stream.AuthenticateAsClient(this.appleSettings.Host);
                }
                catch (System.Security.Authentication.AuthenticationException ex)
                {
                    throw new ConnectionFailureException("SSL Stream Failed to Authenticate as Client", ex);
                }

                if (!stream.IsMutuallyAuthenticated)
                    throw new ConnectionFailureException("SSL Stream Failed to Authenticate", null);

                if (!stream.CanWrite)
                    throw new ConnectionFailureException("SSL Stream is not Writable", null);

                networkStream = stream;
            }

            //Start reading from the stream asynchronously
            Reader();
        }
Example #2
0
		private void Connect()
		{
			if (_client != null)
			{
				Disconnect();
			}

			Log.Debug("Starting Connect with instance {0}", _channelInstanceId);
			_client = new TcpClient();

			var onConnecting = OnConnecting;
			if (onConnecting != null)
			{
				onConnecting(_appleSettings.Host, _appleSettings.Port);
			}

			try
			{
				var connectResult = _client.BeginConnect(_appleSettings.Host, _appleSettings.Port, null, null);
				if (!connectResult.AsyncWaitHandle.WaitOne(_appleSettings.ConnectionTimeout))
				{
					Disconnect();
					throw new TimeoutException("Timeout reached while attempting to connect.");
				}

				_client.EndConnect(connectResult);
				_client.SetSocketKeepAliveValues(
					(int)_appleSettings.KeepAlivePeriod.TotalMilliseconds,
					(int)_appleSettings.KeepAliveRetryPeriod.TotalMilliseconds);

				Interlocked.Increment(ref _reconnects);
			}
			catch (Exception exception)
			{
				throw new ConnectionFailureException("Connection to APNS host failed", exception);
			}

			SecureConnection();

			StartReader();
		}