Exemplo n.º 1
0
        public virtual void OnAllocateResponse(MessageSTUN message)
        {
            lock (_lock)
            {
                ip2ChannelNumber.Clear();
                channelNumber2Peer.Clear();
            }
            GenerateChannelNumberForAllPeers();
            RequestRefreshAllBindingChannelLifeTime();
            StartRefreshTimer();
            StartCheckingConnectionTimer();

            STUNAddress sa = (STUNAddress)message.Get(STUNAttribute.XorRelayedAddress);

            //OnServerDataReceived?.BeginInvoke(this, new STUN_ServerDataReceivedEventArgs((int)responseChannelNumber, new ArraySegment<byte>(appData)), EndAsyncEvent, null);
            OnAllocateResponseEvent?.Invoke(this, new NATP_SignalingEventArgs(true, "", new IPEndPoint(new IPAddress(sa.address), sa.port)));
        }
Exemplo n.º 2
0
        public virtual void OnResponse(byte[] buffer, long offset, long size)
        {
            byte[]      appData;
            MessageSTUN message = new MessageSTUN(buffer, offset, size, out appData);

            if (!message.IsSTUNMessage) // Normal Data Received
            {
                string ip;
                uint   responseChannelNumber;
                if (message.isIndicateData) // sender's ip and channel bind's ip are same, but port are different, so stun server send indicate data
                {
                    if (!autoUpdatePort)
                    {
                        // deal with it
                        return;
                    }

                    if (ChannelNumberMode)
                    {
                        AddPeer(message.indicateAddress);
                        RequestChannelBind(message.indicateAddress);
                        return;
                    }
                    else
                    {
                        ip = message.indicateAddress.ToString();
                        if (!ip2ChannelNumber.ContainsKey(ip))
                        {
                            AddPeer(message.indicateAddress);
                            return;
                        }
                    }
                    responseChannelNumber = ip2ChannelNumber[ip];
                }
                else
                {
                    responseChannelNumber = message.responseChannelNumber;
                }

                if (IsClientHeartbeat(appData))
                {
                    SendHeartBeatToClient(responseChannelNumber);
                }
                else if (IsClientDisconnectHeartbeat(appData))
                {
                    lock (_lockHeartbeat)
                    {
                        peerHeartbeat.Remove(responseChannelNumber);
                    }
                    DisconnectClient(responseChannelNumber);
                }
                else
                {
                    OnServerDataReceived?.Invoke(this, new NATP_STUN_ServerDataReceivedEventArgs((int)responseChannelNumber, new ArraySegment <byte>(appData)));
                    //OnServerDataReceived?.BeginInvoke(this, new STUN_ServerDataReceivedEventArgs((int)responseChannelNumber, new ArraySegment<byte>(appData)), EndAsyncEvent, null);
                }
            }
            else // Is STUN Server response
            {
                var newNonce = (byte[])message.Get(STUNAttribute.Nonce);
                if (newNonce != null)
                {
                    nonce = newNonce;
                }
                switch (message.method)
                {
                case STUNMethod.AllocateResponse:
                    OnAllocateResponse(message);
                    break;

                case STUNMethod.BindingResponse:
                    OnBindingResponse(message);
                    break;

                case STUNMethod.ChannelBindError:
                    OnChannelBindError(message);
                    break;

                case STUNMethod.ChannelBindResponse:
                    OnChannelBindResponse(message);
                    break;

                case STUNMethod.AllocateError:
                    OnAllocateError(message);
                    break;

                case STUNMethod.RefreshResponse:
                    OnRefreshResponse(message);
                    break;

                case STUNMethod.RefereshError:
                    OnRefreshErrorResponse(message);
                    break;

                case STUNMethod.CreatePermissionResponse:
                    OnCreatePermissionResponse(message);
                    break;
                }
            }
        }