Example #1
0
        // ****
        // **** CLIENT-ONLY CALLED METHODS
        // ****

        /// <summary>
        /// Will attempt to register a client with a server.
        /// </summary>
        /// <param name="serviceProxy">The server's service proxy. (Provides callback methods to the server.)</param>
        /// <param name="client">The client to register.</param>
        /// <param name="transportControllerAdvanced">Provides advanced access to the transport controller.</param>
        /// <returns><b>True</b> if registration succeeded; otherwise <b>false</b>.</returns>
        public bool Register(IService serviceProxy,
                             IClient client,
                             ITransportControllerAdvanced transportControllerAdvanced)
        {
            if ((serviceProxy != null) && (client != null) && (transportControllerAdvanced != null))
            {
                // RequestServerSetup
                var request  = (new dodSON.Core.Converters.TypeSerializer <string>()).ToByteArray("RequestServerSetup");
                var response = serviceProxy.RegistrationChannel(RegistrationTypeEnum.Register, request);
                try
                {
                    var transportConfiguration = (new dodSON.Core.Converters.TypeSerializer <ITransportConfiguration>()).FromByteArray(response);
                    transportControllerAdvanced.SetTransportConfiguration(transportConfiguration);
                    // Register
                    request  = (new dodSON.Core.Converters.TypeSerializer <IClientConfiguration>()).ToByteArray(client.ClientConfiguration);
                    response = serviceProxy.RegistrationChannel(RegistrationTypeEnum.Register, request);
                    return((new dodSON.Core.Converters.TypeSerializer <bool>()).FromByteArray(response));
                }
                catch { }
            }
            return(false);
        }
        // ****
        // **** CLIENT-ONLY CALLED METHODS
        // ****

        /// <summary>
        /// Will attempt to register a client with a server.
        /// </summary>
        /// <param name="serviceProxy">The server's service proxy. (Provides callback methods to the server.)</param>
        /// <param name="client">The client to register.</param>
        /// <param name="transportControllerAdvanced">Provides advanced access to the transport controller.</param>
        /// <returns><b>True</b> if registration succeeded; otherwise <b>false</b>.</returns>
        public bool Register(IService serviceProxy, IClient client, ITransportControllerAdvanced transportControllerAdvanced)
        {
            if ((serviceProxy != null) && (client != null) && (transportControllerAdvanced != null))
            {
                var transportConfigurationConvertor = new dodSON.Core.Converters.TypeSerializer <ITransportConfiguration>();
                var transportDataConverter          = new dodSON.Core.Converters.TypeSerializer <TransportData>();
                var listConverter = new dodSON.Core.Converters.TypeSerializer <List <byte[]> >();
                var boolConverter = new dodSON.Core.Converters.TypeSerializer <bool>();
                var clientConfigurationConverter = new dodSON.Core.Converters.TypeSerializer <IClientConfiguration>();
                // ######## HELLO (create tunnel)
                // create public/private keys
                System.Security.Cryptography.RSACryptoServiceProvider serverCryptoProvider = null;
                var clientCryptoProvider = AsymmetricCryptoProvider;
                var clientPublicKey      = clientCryptoProvider.ToXmlString(false);
                var clientPrivateKey     = clientCryptoProvider.ToXmlString(true);
                var request = transportDataConverter.ToByteArray(
                    new TransportData()
                {
                    Alpha   = ContextFor("Hello"),
                    Beta    = client.Id,
                    Gamma   = System.Text.Encoding.Unicode.GetBytes(clientPublicKey),
                    Delta   = null,
                    Epsilon = null
                });
                var response = transportDataConverter.FromByteArray(serviceProxy.RegistrationChannel(RegistrationTypeEnum.Register, request));
                // ######## test for HELLO
                if ((response.Alpha == ContextFor("Hello")) && (response.Beta == client.Id))
                {
                    // ######## get server public key
                    var serverPublicKey = System.Text.Encoding.Unicode.GetString(response.Gamma);
                    serverCryptoProvider = AsymmetricCryptoProvider;
                    serverCryptoProvider.FromXmlString(serverPublicKey);
                    // ######## meet challenge
                    request = transportDataConverter.ToByteArray(
                        new TransportData()
                    {
                        Alpha   = ContextFor("Challenge"),
                        Beta    = client.Id,
                        Gamma   = null,
                        Delta   = null,
                        Epsilon = PrepareForTransport(ChallengeEvidence, serverCryptoProvider, TransportPartsLengthInBytes)
                    });
                    if (boolConverter.FromByteArray(serviceProxy.RegistrationChannel(RegistrationTypeEnum.Register, request)))
                    {
                        // ######## get transport setup
                        request = transportDataConverter.ToByteArray(
                            new TransportData()
                        {
                            Alpha   = ContextFor("RequestServerSetup"),
                            Beta    = client.Id,
                            Gamma   = null,
                            Delta   = null,
                            Epsilon = PrepareForTransport(ChallengeEvidence, serverCryptoProvider, TransportPartsLengthInBytes)
                        });
                        var byteResponse = serviceProxy.RegistrationChannel(RegistrationTypeEnum.Register, request);
                        if (byteResponse != null)
                        {
                            var list     = listConverter.FromByteArray(byteResponse);
                            var restored = RestoreFromTransport(list, clientCryptoProvider);
                            var config   = transportConfigurationConvertor.FromByteArray(restored);
                            if (config != null)
                            {
                                transportControllerAdvanced.SetTransportConfiguration(config);
                                // ######## send registration
                                request = transportDataConverter.ToByteArray(
                                    new TransportData()
                                {
                                    Alpha = ContextFor("Register"),
                                    Beta  = client.Id,
                                    Gamma = null,
                                    Delta = PrepareForTransport(clientConfigurationConverter.ToByteArray(client.ClientConfiguration),
                                                                serverCryptoProvider,
                                                                TransportPartsLengthInBytes),
                                    Epsilon = PrepareForTransport(ChallengeEvidence, serverCryptoProvider, TransportPartsLengthInBytes)
                                });
                                return(boolConverter.FromByteArray(serviceProxy.RegistrationChannel(RegistrationTypeEnum.Register, request)));
                            }
                        }
                    }
                }
            }
            return(false);
        }