private SocketAsyncEventArgs CreateIoEventArgs()
        {
            SocketAsyncEventArgs receiveArgs = CreateAcceptEventArgs();
            SocketAsyncEventArgs sendArgs    = CreateAcceptEventArgs();

            IBuffer receiveBuffer = null;
            IBuffer sendBuffer    = null;

            if (!this._bufferAllocator.Alloc(ChannelBufferSize, out receiveBuffer) || !this._bufferAllocator.Alloc(ChannelBufferSize, out sendBuffer))
            {
                // Failed to allocate buffers!
                throw new InvalidOperationException("Failed to allocate send & receive buffers");
            }

            receiveArgs.SetBuffer(receiveBuffer.Get(), 0, ChannelBufferSize);
            sendArgs.SetBuffer(sendBuffer.Get(), 0, ChannelBufferSize);

            Channel      channel      = new DefaultChannel(this._channelIdIndex++, this, sendArgs);
            ChannelToken channelToken = new ChannelToken(channel, new DataWriter());

            receiveArgs.UserToken = channelToken;
            sendArgs.UserToken    = channelToken;

            this._channelInitialiser.InitialiseChannel(channel);

            return(receiveArgs);
        }
Example #2
0
        /// <summary>
        /// Processes an accept operation
        /// </summary>
        /// <param name="acceptEventArgs"></param>
        private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs)
        {
            this.StartAccept();

            if (acceptEventArgs.SocketError != SocketError.Success)
            {
                this.CancelAccept(acceptEventArgs);
                return;
            }

            SocketAsyncEventArgs ioArgs = this._ioArgsPool.Take();

            if (ioArgs != null)
            {
                ChannelToken   channelToken = ioArgs.UserToken as ChannelToken;
                DefaultChannel channel      = channelToken.Channel as DefaultChannel;

                channel.Socket = acceptEventArgs.AcceptSocket;
                channel.SendArgs.AcceptSocket = acceptEventArgs.AcceptSocket;

                acceptEventArgs.AcceptSocket = null;
                this._acceptArgsPool.Return(acceptEventArgs);

                StartReceive(ioArgs);

                channel.Pipeline.OnChannelConnected(new ChannelHandlerContext(channel));
            }
        }
        public override async Task <int> ExecuteAsync()
        {
            try
            {
                IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger);

                DefaultChannel resolvedChannel = await ResolveSingleChannel();

                if (resolvedChannel == null)
                {
                    return(Constants.ErrorCode);
                }

                await remote.DeleteDefaultChannelAsync(resolvedChannel.Id);

                return(Constants.SuccessCode);
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine(e.Message);
                return(Constants.ErrorCode);
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error: Failed remove the default channel association.");
                return(Constants.ErrorCode);
            }
        }
Example #4
0
 protected void PlayerJoinServer(PlayerConnectionEventArgs args)
 {
     if (AutoJoin && DefaultChannel != null)
     {
         args.Player.SendInfoMessage(String.Format("Channels: You're entering {0} channel", DefaultChannel.Name));
         DefaultChannel.Join(args.Player);
     }
 }
Example #5
0
        /// <summary>
        /// Implements the default channel enable/disable operation
        /// </summary>
        /// <param name="options"></param>
        public override async Task <int> ExecuteAsync()
        {
            if ((_options.Enable && _options.Disable) ||
                (!_options.Enable && !_options.Disable))
            {
                Console.WriteLine("Please specify either --enable or --disable");
                return(Constants.ErrorCode);
            }

            IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger);

            try
            {
                DefaultChannel resolvedChannel = await ResolveSingleChannel();

                if (resolvedChannel == null)
                {
                    return(Constants.ErrorCode);
                }

                bool enabled;
                if (_options.Enable)
                {
                    if (resolvedChannel.Enabled)
                    {
                        Console.WriteLine($"Default channel association is already enabled");
                        return(Constants.ErrorCode);
                    }
                    enabled = true;
                }
                else
                {
                    if (!resolvedChannel.Enabled)
                    {
                        Console.WriteLine($"Default channel association is already disabled");
                        return(Constants.ErrorCode);
                    }
                    enabled = false;
                }

                await remote.UpdateDefaultChannelAsync(resolvedChannel.Id, enabled : enabled);

                Console.WriteLine($"Default channel association has been {(enabled ? "enabled" : "disabled")}.");

                return(Constants.SuccessCode);
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine(e.Message);
                return(Constants.ErrorCode);
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error: Failed enable/disable default channel association.");
                return(Constants.ErrorCode);
            }
        }
Example #6
0
        /// <summary>
        ///     Get a string for describing a default channel.
        /// </summary>
        /// <param name="defaultChannel">Default channel to get a string for</param>
        /// <returns>String describing the default channel.</returns>
        public static string GetDefaultChannelDescriptionString(DefaultChannel defaultChannel)
        {
            string disabled = !defaultChannel.Enabled ? " (Disabled)" : "";
            // Pad so that id's up to 9999 will result in consistent
            // listing
            string idPrefix = $"({defaultChannel.Id})".PadRight(7);

            return($"{idPrefix}{defaultChannel.Repository} @ {defaultChannel.Branch} -> {defaultChannel.Channel.Name}{disabled}");
        }
        /// <summary>
        /// Starts a receive operation with the provided event args
        /// </summary>
        /// <param name="receiveArgs">The data which specifies who's receiving what data</param>
        public void StartReceive(SocketAsyncEventArgs receiveArgs)
        {
            DefaultChannel channel = (receiveArgs.UserToken as ChannelToken).Channel as DefaultChannel;

            bool eventPending = channel.Socket.ReceiveAsync(receiveArgs);

            if (!eventPending)
            {
                this.ProcessReceive(receiveArgs);
            }
        }
Example #8
0
 internal void GivenADefaultChannel(bool enabled)
 {
     DefaultChannel = new DefaultChannel
     {
         Branch     = SourceBranch,
         Channel    = Channel,
         ChannelId  = Channel.Id,
         Enabled    = enabled,
         Repository = SourceRepo
     };
     ContextUpdates.Add(context => context.DefaultChannels.Add(DefaultChannel));
 }
        /// <summary>
        ///     Resolve channel based on the input options. If no channel could be resolved
        ///     based on the input options, returns null.
        /// </summary>
        /// <returns>Default channel or null</returns>
        protected async Task <DefaultChannel> ResolveSingleChannel()
        {
            IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger);

            IEnumerable <DefaultChannel> potentialDefaultChannels = await remote.GetDefaultChannelsAsync();

            // User should have supplied id or a combo of the channel name, repo, and branch.
            if (_options.Id != -1)
            {
                DefaultChannel defaultChannel = potentialDefaultChannels.SingleOrDefault(d => d.Id == _options.Id);
                if (defaultChannel == null)
                {
                    Console.WriteLine($"Could not find a default channel with id {_options.Id}");
                }
                return(defaultChannel);
            }
            else if (string.IsNullOrEmpty(_options.Repository) ||
                     string.IsNullOrEmpty(_options.Channel) ||
                     string.IsNullOrEmpty(_options.Branch))
            {
                Console.WriteLine("Please specify either the default channel id with --id or a combination of --channel, --branch and --repo");
                return(null);
            }

            // Otherwise, filter based on the other inputs. If more than one resolves, then print the possible
            // matches and return null
            var matchingChannels = potentialDefaultChannels.Where(d =>
            {
                return((string.IsNullOrEmpty(_options.Repository) || d.Repository.Contains(_options.Repository, StringComparison.OrdinalIgnoreCase)) &&
                       (string.IsNullOrEmpty(_options.Channel) || d.Channel.Name.Contains(_options.Channel, StringComparison.OrdinalIgnoreCase)) &&
                       (string.IsNullOrEmpty(_options.Branch) || d.Branch.Contains(GitHelpers.NormalizeBranchName(_options.Branch), StringComparison.OrdinalIgnoreCase)));
            });

            if (!matchingChannels.Any())
            {
                Console.WriteLine($"No channels found matching the specified criteria.");
                return(null);
            }
            else if (matchingChannels.Count() != 1)
            {
                Console.WriteLine($"More than one channel matching the specified criteria. Please change your options to be more specific.");
                foreach (DefaultChannel defaultChannel in matchingChannels)
                {
                    Console.WriteLine($"    {UxHelpers.GetDefaultChannelDescriptionString(defaultChannel)}");
                }
                return(null);
            }
            else
            {
                return(matchingChannels.Single());
            }
        }
Example #10
0
        /// <summary>
        ///     Removes a default channel based on the specified criteria
        /// </summary>
        /// <param name="repository">Repository having a default association</param>
        /// <param name="branch">Branch having a default association</param>
        /// <param name="channel">Name of channel that builds of 'repository' on 'branch' are being applied to.</param>
        /// <returns>Async task</returns>
        public async Task DeleteDefaultChannelAsync(string repository, string branch, string channel)
        {
            CheckForValidBarClient();

            DefaultChannel existingDefaultChannel =
                (await GetDefaultChannelsAsync(repository, branch, channel)).SingleOrDefault();

            if (existingDefaultChannel != null)
            {
                // Find the existing default channel.  If none found then nothing to do.
                await _barClient.DefaultChannels.DeleteAsync(existingDefaultChannel.Id.Value);
            }
        }
Example #11
0
        public void Send(byte[] message, Inkton.Nest.Model.Nest nest,
                         Type type = null, string correlationId = null, int cushion = -1)
        {
            string routingKey = "#";

            if (nest != null)
            {
                routingKey = GetQueue(nest, cushion);
            }

            _lastCorrelationId = DefaultChannel
                                 .Publish(routingKey, message, type, correlationId);
        }
Example #12
0
        public NesterQueueServer(NesterService service,
                                 Enviorenment enviorenment, bool durable = false, bool autoDelete = true)
            : base(service, enviorenment, durable, autoDelete)
        {
            DefaultChannel.QueueDeclare(QueueName);

            // The generic nest endpoint
            DefaultChannel.QueueBind(QueueName + ".*");

            // The specific nest cushion endpoint
            DefaultChannel.QueueBind(QueueName + "." +
                                     Environment.GetEnvironmentVariable("NEST_CUSHION_INDEX"));
        }
Example #13
0
        internal static ChannelBase GetChannelObject(ChannelTransport channelTransport, Connection connection)
        {
            if (channelTransport == null)
            {
                if (defaultChannel == null)
                {
                    defaultChannel = new DefaultChannel(connection);
                }

                return(defaultChannel);
            }


            ChannelBase channel;

            switch (channelTransport.ChannelType)
            {
            case ChannelType.Default:
                if (defaultChannel == null)
                {
                    defaultChannel = new DefaultChannel(connection);
                }
                channel = defaultChannel;
                break;

            case ChannelType.System:
                channel = new SystemChannel(channelTransport as SystemChannelTransport, connection);
                break;

            default:
                channel = null;
                break;
            }

            return(channel);
        }
Example #14
0
        public void Dispose()
        {
            try { DefaultChannel.ShutdownAsync().Wait(1000); } catch { }

            try { server.ShutdownAsync().Wait(1000); } catch { }
        }
Example #15
0
 /// <summary> Creates a new invite to the default channel of this server. </summary>
 /// <param name="maxAge"> Time (in seconds) until the invite expires. Set to null to never expire. </param>
 /// <param name="maxUses"> The max amount  of times this invite may be used. Set to null to have unlimited uses. </param>
 /// <param name="tempMembership"> If true, a user accepting this invite will be kicked from the server after closing their client. </param>
 /// <param name="withXkcd"> If true, creates a human-readable link. Not supported if maxAge is set to null. </param>
 public Task <Invite> CreateInvite(int?maxAge = 1800, int?maxUses = null, bool tempMembership = false, bool withXkcd = false)
 => DefaultChannel.CreateInvite(maxAge, maxUses, tempMembership, withXkcd);
Example #16
0
 public void Dispose()
 {
     DefaultChannel.ShutdownAsync().Wait();
     server.ShutdownAsync().Wait();
 }