/// <summary> /// Creates new channel with custom event handler, authenticator and options /// </summary> /// <exception cref="OperationCanceledException">Thrown when channel limit is exceeded for the server</exception> /// <exception cref="DuplicateNameException">Thrown when there is already a channel with same name</exception> public Channel CreateChannel(string name, IChannelAuthenticator authenticator, IChannelEventHandler eventHandler, IMessageDeliveryHandler deliveryHandler, ChannelOptions options) { if (Options.ChannelLimit > 0 && _channels.Count >= Options.ChannelLimit) { throw new OperationCanceledException("Channel limit is exceeded for the server"); } Channel channel = _channels.Find(x => x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); if (channel != null) { throw new DuplicateNameException("There is already a channel with same name: " + name); } channel = new Channel(this, options, name, authenticator, eventHandler, deliveryHandler); _channels.Add(channel); if (eventHandler != null) { _ = eventHandler.OnChannelCreated(channel); } return(channel); }
/// <summary> /// Creates new channel with custom event handler, authenticator and default options /// </summary> /// <exception cref="OperationCanceledException">Thrown when channel limit is exceeded for the server</exception> /// <exception cref="DuplicateNameException">Thrown when there is already a channel with same name</exception> public Channel CreateChannel(string name, IChannelAuthenticator authenticator, IChannelEventHandler eventHandler, IMessageDeliveryHandler deliveryHandler) { ChannelOptions options = ChannelOptions.CloneFrom(Options); return(CreateChannel(name, authenticator, eventHandler, deliveryHandler, options)); }
/// <summary> /// Sets default channel event handler and authenticator /// </summary> /// <exception cref="ReadOnlyException">Thrown when default channel event handler or default channel authenticator already is set</exception> public void SetDefaultChannelHandler(IChannelEventHandler eventHandler, IChannelAuthenticator authenticator) { if (DefaultChannelEventHandler != null) { throw new ReadOnlyException("Default channel event handler can be set only once"); } DefaultChannelEventHandler = eventHandler; if (DefaultChannelAuthenticator != null) { throw new ReadOnlyException("Default channel authenticator can be set only once"); } DefaultChannelAuthenticator = authenticator; }
internal Channel(MqServer server, ChannelOptions options, string name, IChannelAuthenticator authenticator, IChannelEventHandler eventHandler, IMessageDeliveryHandler deliveryHandler) { Server = server; Options = options; Name = name; Authenticator = authenticator; EventHandler = eventHandler; DeliveryHandler = deliveryHandler; _queues = new SafeList <ChannelQueue>(8); _clients = new SafeList <ChannelClient>(256); }
/// <summary> /// Creates new channel /// </summary> private async Task <Channel> CreateChannel(MqClient client, TmqMessage message, bool createForQueue) { Channel channel = _server.FindChannel(message.Target); if (channel != null) { return(channel); } //check create channel access if (_server.Authorization != null) { bool grant = await _server.Authorization.CanCreateChannel(client, _server, message.Target); if (!grant) { if (message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized)); } return(null); } } if (!createForQueue && message.Length > 0 && message.Content != null && message.Content.Length > 0) { NetworkOptionsBuilder builder = new NetworkOptionsBuilder(); builder.Load(message.ToString()); ChannelOptions options = ChannelOptions.CloneFrom(_server.Options); builder.ApplyToChannel(options); IChannelEventHandler eventHandler = _server.DefaultChannelEventHandler; if (!string.IsNullOrEmpty(builder.ChannelEventHandler)) { IChannelEventHandler e = _server.Registry.GetChannelEvent(builder.ChannelEventHandler); if (e != null) { eventHandler = e; } } IChannelAuthenticator authenticator = _server.DefaultChannelAuthenticator; if (!string.IsNullOrEmpty(builder.ChannelAuthenticator)) { IChannelAuthenticator e = _server.Registry.GetChannelAuthenticator(builder.ChannelAuthenticator); if (e != null) { authenticator = e; } } IMessageDeliveryHandler deliveryHandler = _server.DefaultDeliveryHandler; if (!string.IsNullOrEmpty(builder.MessageDeliveryHandler)) { IMessageDeliveryHandler e = _server.Registry.GetMessageDelivery(builder.MessageDeliveryHandler); if (e != null) { deliveryHandler = e; } } Channel ch = _server.CreateChannel(message.Target, authenticator, eventHandler, deliveryHandler, options); if (ch != null && message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Ok)); } } Channel c = _server.CreateChannel(message.Target); if (!createForQueue && c != null && message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Ok)); } return(c); }
/// <summary> /// Adds default channel handler implementation to builder /// </summary> public void AddDefaultChannelHandler <T>(params object[] ctorArgs) where T : class, IChannelEventHandler, new() { IChannelEventHandler handler = Activator.CreateInstance(typeof(T), ctorArgs) as IChannelEventHandler; _channelEventHandler = handler; }
/// <summary> /// Adds default channel handler implementation to builder /// </summary> public void AddDefaultChannelHandler <T>() where T : class, IChannelEventHandler, new() { _channelEventHandler = new T(); }
/// <summary> /// Adds default channel handler implementation to builder /// </summary> public void AddDefaultChannelHandler(IChannelEventHandler handler) { _channelEventHandler = handler; }
/// <summary> /// Register a channel event handler to the container /// </summary> public void Register(string key, IChannelEventHandler value) { lock (_channelEventHandlers) _channelEventHandlers.Add(key, value); }