Example #1
0
 public void FreeConnectionID(int connectionID)
 {
     if (ActiveConnections.ContainsKey(connectionID))
     {
         this.ActiveConnections.Remove(connectionID);
     }
 }
Example #2
0
        private Enums.Mode SetConnection(CallEventArgs call)
        {
            Port portOut = Ports.FirstOrDefault(x => x.PhoneNumber == call.OutPhoneNumber);

            if (portOut == null)
            {
                return(Enums.Mode.NotExist);
            }
            Port portIn = Ports.First(x => x.PhoneNumber == call.InPhoneNumber);

            Enums.Mode modePort = portOut.Mode;
            if (modePort == Enums.Mode.Free)
            {
                if (!ActiveConnections.ContainsKey(call.InPhoneNumber))
                {
                    GetStation().TerminalAnswered   += portIn.OutCallAnswered;
                    GetStation().TerminalNoAnswered += portIn.OutCallNoAnswered;
                    GetStation().TerminalRejected   += portIn.OutCallRejected;
                    ActiveConnections.Add(call.InPhoneNumber, call.OutPhoneNumber);
                    portOut.Mode           = Enums.Mode.Ringing;
                    portIn.Mode            = Enums.Mode.Ringing;
                    portOut.AnswerEvent   += AnsweredCall;
                    portOut.RejectEvent   += RejectedCall;
                    portOut.NoAnswerEvent += NoAnsweredCall;
                    portOut.IncomingCall(call.InPhoneNumber);
                }
            }
            if (modePort == Enums.Mode.Ringing)
            {
                return(Enums.Mode.Busy);
            }
            return(modePort);
        }
        /// <summary>
        /// Gets a VoiceNext connection for specified guild.
        /// </summary>
        /// <param name="guild">Guild to get VoiceNext connection for.</param>
        /// <returns>VoiceNext connection for the specified guild.</returns>
        public VoiceNextConnection GetConnection(DiscordGuild guild)
        {
            if (ActiveConnections.ContainsKey(guild.Id))
            {
                return(ActiveConnections[guild.Id]);
            }

            return(null);
        }
        private void Vnc_VoiceDisconnected(DiscordGuild guild)
        {
            VoiceNextConnection vnc = null;

            if (ActiveConnections.ContainsKey(guild.Id))
            {
                ActiveConnections.TryRemove(guild.Id, out vnc);
            }

            var vsd = new VoiceDispatch
            {
                OpCode  = 4,
                Payload = new VoiceStateUpdatePayload
                {
                    GuildId   = guild.Id,
                    ChannelId = null
                }
            };
            var vsj = JsonConvert.SerializeObject(vsd, Formatting.None);

            (guild.Discord as DiscordClient)._webSocketClient.SendMessage(vsj);
        }
        /// <summary>
        /// Create a VoiceNext connection for the specified channel.
        /// </summary>
        /// <param name="channel">Channel to connect to.</param>
        /// <returns>VoiceNext connection for this channel.</returns>
        public async Task <VoiceNextConnection> ConnectAsync(DiscordChannel channel)
        {
            if (channel.Type != ChannelType.Voice)
            {
                throw new ArgumentException(nameof(channel), "Invalid channel specified; needs to be voice channel");
            }

            if (channel.Guild == null)
            {
                throw new ArgumentException(nameof(channel), "Invalid channel specified; needs to be guild channel");
            }

            if (!channel.PermissionsFor(channel.Guild.CurrentMember).HasPermission(Permissions.UseVoice))
            {
                throw new InvalidOperationException("You need UseVoice permission to connect to this voice channel");
            }

            var gld = channel.Guild;

            if (ActiveConnections.ContainsKey(gld.Id))
            {
                throw new InvalidOperationException("This guild already has a voice connection");
            }

            var vstut = new TaskCompletionSource <VoiceStateUpdateEventArgs>();
            var vsrut = new TaskCompletionSource <VoiceServerUpdateEventArgs>();

            this.VoiceStateUpdates[gld.Id]  = vstut;
            this.VoiceServerUpdates[gld.Id] = vsrut;

            var vsd = new VoiceDispatch
            {
                OpCode  = 4,
                Payload = new VoiceStateUpdatePayload
                {
                    GuildId   = gld.Id,
                    ChannelId = channel.Id,
                    Deafened  = false,
                    Muted     = false
                }
            };
            var vsj = JsonConvert.SerializeObject(vsd, Formatting.None);

            await(channel.Discord as DiscordClient)._webSocketClient.SendMessageAsync(vsj).ConfigureAwait(false);

            var vstu = await vstut.Task.ConfigureAwait(false);

            var vstup = new VoiceStateUpdatePayload
            {
                SessionId = vstu.SessionId,
                UserId    = vstu.User.Id
            };
            var vsru = await vsrut.Task.ConfigureAwait(false);

            var vsrup = new VoiceServerUpdatePayload
            {
                Endpoint = vsru.Endpoint,
                GuildId  = vsru.Guild.Id,
                Token    = vsru.VoiceToken
            };

            var vnc = new VoiceNextConnection(this.Client, gld, channel, this.Configuration, vsrup, vstup);

            vnc.VoiceDisconnected += this.Vnc_VoiceDisconnected;
            await vnc.ConnectAsync().ConfigureAwait(false);

            await vnc.WaitForReadyAsync().ConfigureAwait(false);

            this.ActiveConnections[gld.Id] = vnc;
            return(vnc);
        }
Example #6
0
        /// <summary>
        /// Create a VoiceNext connection for the specified channel.
        /// </summary>
        /// <param name="channel">Channel to connect to.</param>
        /// <returns>VoiceNext connection for this channel.</returns>
        public async Task <VoiceNextConnection> ConnectAsync(DiscordChannel channel)
        {
            if (channel.Type != ChannelType.Voice)
            {
                throw new ArgumentException(nameof(channel), "Invalid channel specified; needs to be voice channel");
            }

            if (channel.Parent == null)
            {
                throw new ArgumentException(nameof(channel), "Invalid channel specified; needs to be guild channel");
            }

            var gld = channel.Parent;

            if (ActiveConnections.ContainsKey(gld.ID))
            {
                throw new InvalidOperationException("This guild already has a voice connection");
            }

            var vstut = new TaskCompletionSource <VoiceStateUpdateEventArgs>();
            var vsrut = new TaskCompletionSource <VoiceServerUpdateEventArgs>();

            this.VoiceStateUpdates[gld.ID]  = vstut;
            this.VoiceServerUpdates[gld.ID] = vsrut;

            var vsd = new VoiceDispatch
            {
                OpCode  = 4,
                Payload = new VoiceStateUpdatePayload
                {
                    GuildId   = gld.ID,
                    ChannelId = channel.ID,
                    Deafened  = false,
                    Muted     = false
                }
            };
            var vsj = JsonConvert.SerializeObject(vsd, Formatting.None);

            DiscordClient._websocketClient.SendMessage(vsj);

            var vstu  = await vstut.Task;
            var vstup = new VoiceStateUpdatePayload
            {
                SessionId = vstu.SessionID,
                UserId    = vstu.UserID
            };
            var vsru  = await vsrut.Task;
            var vsrup = new VoiceServerUpdatePayload
            {
                Endpoint = vsru.Endpoint,
                GuildId  = vsru.GuildID,
                Token    = vsru.VoiceToken
            };

            var d1 = (TaskCompletionSource <VoiceStateUpdateEventArgs>)null;
            var d2 = (TaskCompletionSource <VoiceServerUpdateEventArgs>)null;

            this.VoiceStateUpdates.TryRemove(gld.ID, out d1);
            this.VoiceServerUpdates.TryRemove(gld.ID, out d2);

            var vnc = new VoiceNextConnection(this.Client, gld, channel, this.Configuration, vsrup, vstup);

            vnc.VoiceDisconnected += this.Vnc_VoiceDisconnected;
            await vnc.ConnectAsync();

            await vnc.WaitForReady();

            this.ActiveConnections[gld.ID] = vnc;
            return(vnc);
        }