예제 #1
0
        /// <summary>
        /// Creates a new instance of the RemotingClient class.
        /// </summary>
        /// <param name="config">Configuration settings</param>
        public RemotingClient(ClientConfig config) : this()
        {
            if (config == null)
            {
                throw new ArgumentException("No config provided and no default configuration found.");
            }

            Serializer        = config.Serializer ?? new BsonSerializerAdapter();
            MessageEncryption = config.MessageEncryption;

            _config = config;

            if (MessageEncryption)
            {
                _keyPair = new RsaKeyPair(config.KeySize);
            }

            _channel = config.Channel ?? new WebsocketClientChannel();

            _channel.Init(this);
            _rawMessageTransport = _channel.RawMessageTransport;
            _rawMessageTransport.ReceiveMessage += OnMessage;
            _rawMessageTransport.ErrorOccured   += (s, exception) =>
            {
                if (exception != null)
                {
                    throw exception;
                }

                throw new NetworkException(s);
            };

            _clientInstances.AddOrUpdate(
                key: config.UniqueClientInstanceName,
                addValueFactory: uniqueInstanceName => this,
                updateValueFactory: (uniqueInstanceName, oldClient) =>
            {
                oldClient?.Dispose();
                return(this);
            });

            if (!config.IsDefault)
            {
                return;
            }

            RemotingClient.DefaultRemotingClient ??= this;
        }