Пример #1
0
        /// <summary>
        /// TODO: Documentation StunClient_OnReceivedSuccessResponse
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="receivedMsg"></param>
        /// <param name="sentMsg"></param>
        /// <param name="transactionObject"></param>
        private void StunClient_OnReceivedSuccessResponse(object sender, StunMessage receivedMsg, StunMessage sentMsg, object transactionObject)
        {
            switch (receivedMsg.MethodType)
            {
                case StunMethodType.Allocate:
                    TurnAllocation allocation = new TurnAllocation()
                    {
                        Username = sentMsg.Stun.Username.ValueString,
                        Password = transactionObject as String,
                        Realm = sentMsg.Stun.Realm.ValueString,
                        Nonce = sentMsg.Stun.Nonce.ValueString,
                        RelayedMappedAddress = receivedMsg.Turn.XorRelayedAddress,
                        MappedAddress = receivedMsg.Stun.XorMappedAddress,
                        StartTime = DateTime.Now,
                        LifeTime = StunUtilities.ReverseBytes(BitConverter.ToUInt32(receivedMsg.Turn.LifeTime.Value, 0))
                    };

                    if (this.Allocations.ContainsKey(receivedMsg.Turn.XorRelayedAddress))
                        this.Allocations[receivedMsg.Turn.XorRelayedAddress] = allocation;
                    else
                    {
                        this.Allocations.Add(receivedMsg.Turn.XorRelayedAddress, allocation);

                        if (this.OnAllocateSucceed != null)
                            this.OnAllocateSucceed(this, allocation, sentMsg, receivedMsg);
                    }
                    break;

                case StunMethodType.CreatePermission:
                    TurnPermission permission = new TurnPermission()
                    {
                        PeerAddress = sentMsg.Turn.XorPeerAddress,
                        StartTime = DateTime.Now,
                        LifeTime = 300
                    };
                    TurnAllocation permAllocation = transactionObject as TurnAllocation;

                    if (permAllocation.Permissions.ContainsKey(sentMsg.Turn.XorPeerAddress))
                        permAllocation.Permissions[sentMsg.Turn.XorPeerAddress] = permission;
                    else
                    {
                        permAllocation.Permissions.Add(sentMsg.Turn.XorPeerAddress, permission);

                        if (this.OnCreatePermissionSucceed != null)
                            this.OnCreatePermissionSucceed(this, permAllocation, permission, sentMsg, receivedMsg);
                    }

                    break;

                case StunMethodType.ChannelBind:
                    TurnChannel channel = new TurnChannel()
                    {
                        Channel = sentMsg.Turn.ChannelNumber,
                        PeerAddress = sentMsg.Turn.XorPeerAddress,
                        StartTime = DateTime.Now,
                        LifeTime = 600
                    };
                    TurnAllocation channelAllocation = transactionObject as TurnAllocation;

                    if (channelAllocation.Channels.ContainsKey(sentMsg.Turn.ChannelNumber))
                        channelAllocation.Channels[sentMsg.Turn.ChannelNumber] = channel;
                    else
                    {
                        channelAllocation.Channels.Add(sentMsg.Turn.ChannelNumber, channel);

                        if (this.OnChannelBindSucceed != null)
                            this.OnChannelBindSucceed(this, transactionObject as TurnAllocation, channel, sentMsg, receivedMsg);
                    }
                    break;

                case StunMethodType.ConnectionBind:
                    if (this.OnConnectionBindSucceed != null)
                        this.OnConnectionBindSucceed(this, transactionObject as Socket, receivedMsg);
                    break;
            }
        }
Пример #2
0
        /// <summary>
        /// TODO: Documentation StunClient_OnReceivedSuccessResponse
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="receivedMsg"></param>
        /// <param name="sentMsg"></param>
        /// <param name="transactionObject"></param>
        private void StunClient_OnReceivedSuccessResponse(object sender, StunMessage receivedMsg, StunMessage sentMsg, object transactionObject)
        {
            switch (receivedMsg.MethodType)
            {
            case StunMethodType.Allocate:
                TurnAllocation allocation = new TurnAllocation()
                {
                    Username             = sentMsg.Stun.Username.ValueString,
                    Password             = transactionObject as String,
                    Realm                = sentMsg.Stun.Realm.ValueString,
                    Nonce                = sentMsg.Stun.Nonce.ValueString,
                    RelayedMappedAddress = receivedMsg.Turn.XorRelayedAddress,
                    MappedAddress        = receivedMsg.Stun.XorMappedAddress,
                    StartTime            = DateTime.Now,
                    LifeTime             = StunUtilities.ReverseBytes(BitConverter.ToUInt32(receivedMsg.Turn.LifeTime.Value, 0))
                };

                if (this.Allocations.ContainsKey(receivedMsg.Turn.XorRelayedAddress))
                {
                    this.Allocations[receivedMsg.Turn.XorRelayedAddress] = allocation;
                }
                else
                {
                    this.Allocations.Add(receivedMsg.Turn.XorRelayedAddress, allocation);

                    if (this.OnAllocateSucceed != null)
                    {
                        this.OnAllocateSucceed(this, allocation, sentMsg, receivedMsg);
                    }
                }
                break;

            case StunMethodType.CreatePermission:
                TurnPermission permission = new TurnPermission()
                {
                    PeerAddress = sentMsg.Turn.XorPeerAddress,
                    StartTime   = DateTime.Now,
                    LifeTime    = 300
                };
                TurnAllocation permAllocation = transactionObject as TurnAllocation;

                if (permAllocation.Permissions.ContainsKey(sentMsg.Turn.XorPeerAddress))
                {
                    permAllocation.Permissions[sentMsg.Turn.XorPeerAddress] = permission;
                }
                else
                {
                    permAllocation.Permissions.Add(sentMsg.Turn.XorPeerAddress, permission);

                    if (this.OnCreatePermissionSucceed != null)
                    {
                        this.OnCreatePermissionSucceed(this, permAllocation, permission, sentMsg, receivedMsg);
                    }
                }

                break;

            case StunMethodType.ChannelBind:
                TurnChannel channel = new TurnChannel()
                {
                    Channel     = sentMsg.Turn.ChannelNumber,
                    PeerAddress = sentMsg.Turn.XorPeerAddress,
                    StartTime   = DateTime.Now,
                    LifeTime    = 600
                };
                TurnAllocation channelAllocation = transactionObject as TurnAllocation;

                if (channelAllocation.Channels.ContainsKey(sentMsg.Turn.ChannelNumber))
                {
                    channelAllocation.Channels[sentMsg.Turn.ChannelNumber] = channel;
                }
                else
                {
                    channelAllocation.Channels.Add(sentMsg.Turn.ChannelNumber, channel);

                    if (this.OnChannelBindSucceed != null)
                    {
                        this.OnChannelBindSucceed(this, transactionObject as TurnAllocation, channel, sentMsg, receivedMsg);
                    }
                }
                break;

            case StunMethodType.ConnectionBind:
                if (this.OnConnectionBindSucceed != null)
                {
                    this.OnConnectionBindSucceed(this, transactionObject as Socket, receivedMsg);
                }
                break;
            }
        }