private PandoraWalletWebService CreatePandoraWalletServer()
        {
            PandoraWalletWebService lResult;

            // Create connection binding
            var lBinding = new BasicHttpBinding();

            // Set maximum size of packages (maximum of 2147483647 = 2GB)
            lBinding.MaxReceivedMessageSize = 5242880; // => 5MB limit
            lBinding.MaxBufferPoolSize      = 5242880;
            lBinding.MaxBufferSize          = 5242880;
            if (EncryptedConnection)
            {
                lBinding.Security.Mode = BasicHttpSecurityMode.Transport;
                lBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                lBinding.Security.Transport.ProxyCredentialType  = HttpProxyCredentialType.None;
                lBinding.Security.Transport.Realm = string.Empty;
                lBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
                lBinding.Security.Message.AlgorithmSuite       = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
            }
            lBinding.Name = "PWService_SOAP_Binding";
            EndpointAddress lAddress = new EndpointAddress(GetConnectionURL(RemoteServer));

            lResult = new PandoraWalletWebService(lBinding, lAddress);
            lResult.GetServerId();
            if (FPandoraWalletWebService == null)
            {
                FPandoraWalletWebService = new PandoraWalletWebService(lBinding, lAddress);
            }
            return(lResult);
        }
 public PandoraWalletServiceAccess(string aRemoteserver, int aPort, bool aEncryptedConnection, string aConnectionId)
 {
     FSyncContext        = SynchronizationContext.Current;
     RemoteServer        = aRemoteserver;
     Port                = aPort;
     EncryptedConnection = aEncryptedConnection;
     ConnectionId        = aConnectionId;
     FServer             = CreatePandoraWalletServer();
     Run();
     this.OnErrorEvent += PandoraWalletServiceAccess_OnErrorEvent;
 }
        private void MethodMessage_OnAfterEvent(object sender, EventArgs e)
        {
            try
            {
                DelegateMessage lMethodMessage = sender as DelegateMessage;

                if (lMethodMessage.ExceptionObject != null) // do not try reconnect if the logon method is called
                {
                    /// if we cant connet to the server because its down
                    if (lMethodMessage.ExceptionObject is System.ServiceModel.EndpointNotFoundException)
                    {
                        Log.Write(LogLevel.Error, lMethodMessage.ExceptionObject.Message);
                        lMethodMessage.ExceptionObject = new PandoraServiceError("Internet connection error occured while connecting to the remote server.");
                        if (FServer != null)
                        {
                            FServer.Disconnected = true;
                        }
                    }
                    var lName = lMethodMessage.ToString();

                    // an exception occured
                    // Now test if the error is because of a communication issue or
                    // if the error is because the server sent the error to be thrown
                    if (!(lMethodMessage.ExceptionObject is PandoraServerException) && lName != "ThreadLogon")
                    {      // if there is a comunication faild we need to reconnect
                        int lCounter = 0;
                        PandoraWalletWebService lServer = null;
                        do
                        {
                            Log.Write(LogLevel.Debug, "MethodMessage_OnAfterEvent: try reconnect to server {0}", lCounter);
                            try
                            {
                                lServer = CreatePandoraWalletServer();
                                FServer = lServer;
                            }
                            catch
                            {
                                System.Threading.Thread.Sleep(1000);
                                lCounter++;
                            }
                        } while (lServer == null && lCounter < 2);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogLevel.Error, "Connection to Pandora Server failed with: " + ex.Message);
                throw;
            }
        }
 private void InternalDisconnect()
 {
     lock (this)
     {
         if (ConnectionId != null)
         {
             ConnectionId = null;
             FServer      = null;
             if (OnDiconnect != null)
             {
                 OnDiconnect(this, null);
             }
         }
     }
 }
 private bool ThreadLogon(string aEmail, string aUserName, string aPassword, string aVersion)
 {
     try
     {
         // only this thread an no other app has access to the
         // Only one method will be executed at a time
         //
         //  This Method will create an test the conneciton and find
         //  Other servers to connect to.
         FServer = CreatePandoraWalletServer();
         // Assuming this connection is now solid call the logon
         var lServerResult = FServer.Logon(aEmail, aUserName, aPassword, aVersion);
         LastConnected = lServerResult.LastConnected;
         if (!string.IsNullOrEmpty(lServerResult.ErrorMsg))
         {
             throw new PandoraServerException("Server Error: " + lServerResult.ErrorMsg);
         }
         ConnectionId = (string)lServerResult.result;
         UserName     = aUserName;
         Email        = aEmail;
         return(Connected);
     }
     catch (EndpointNotFoundException ex)
     {
         Log.Write(LogLevel.Error, "Connection to Pandora Server failed with: " + ex.Message);
         Connected = false;
         throw new PandoraServerException("Server not available. Please ensure you have an active internet conection.");
     }
     catch (Exception ex)
     {
         Log.Write(LogLevel.Error, "Connection to Pandora Server failed with: " + ex.Message);
         Connected = false;
         //Note: if the web server is using intergrated security it will fail with this result.
         if (ex.Message.Contains("The request failed with HTTP status 401: Unauthorized"))
         {
             throw new PandoraServerException("Access denied.  Invalid user name or password.");
         }
         throw;
     }
 }
 // This method is overrdden so that we can try reconnecting when ever the ThreadXXX method is called
 // so we can do what ever w can to recover from bad connections or if the one server
 // goes down for maintainane
 protected override bool InvokeMethodMessage(DelegateMessage aMethodMessage)
 {
     aMethodMessage.OnAfterEvent += MethodMessage_OnAfterEvent;
     if (FServer != null && FServer.Disconnected)
     {
         int lCounter = 0;
         PandoraWalletWebService lServer = null;
         do
         {
             Log.Write(LogLevel.Debug, "InvokeMethodMessage: try reconnect to server {0}", lCounter);
             try
             {
                 lServer = CreatePandoraWalletServer();
                 FServer = lServer;
             }
             catch
             {
                 lCounter++;
             }
         } while (lServer == null && lCounter < 2);
     }
     return(base.InvokeMethodMessage(aMethodMessage));
 }