public bool Send(byte[] bytes, int len)
        {
            //if either PTT is true, a microphone is available && socket connected etc
            if (_ready &&
                _listener != null &&
                _listener.Connected &&
                (_ptt || _clientStateSingleton.DcsPlayerRadioInfo.ptt) &&
                _clientStateSingleton.DcsPlayerRadioInfo.IsCurrent() &&
                _clientStateSingleton.MicrophoneAvailable &&
                (bytes != null))
            //can only send if DCS is connected
            {
                try
                {
                    var currentSelected = _clientStateSingleton.DcsPlayerRadioInfo.selected;
                    //removes race condition by assigning here with the current selected changing
                    if ((currentSelected >= 0) &&
                        (currentSelected < _clientStateSingleton.DcsPlayerRadioInfo.radios.Length))
                    {
                        var radio = _clientStateSingleton.DcsPlayerRadioInfo.radios[currentSelected];

                        if (((radio != null) && (radio.freq > 100) &&
                             (radio.modulation != RadioInformation.Modulation.DISABLED)) ||
                            (radio.modulation == RadioInformation.Modulation.INTERCOM))
                        {
                            //generate packet
                            var udpVoicePacket = new UDPVoicePacket
                            {
                                GuidBytes        = _guidAsciiBytes,
                                AudioPart1Bytes  = bytes,
                                AudioPart1Length = (ushort)bytes.Length,
                                Frequency        = radio.freq,
                                UnitId           = _clientStateSingleton.DcsPlayerRadioInfo.unitId,
                                Encryption       = radio.enc ? radio.encKey : (byte)0,
                                Modulation       = (byte)radio.modulation,
                                PacketNumber     = _packetNumber++
                            }.EncodePacket();

                            //send audio
                            _listener.Client.Send(udpVoicePacket);

                            //not sending or really quickly switched sending
                            if (!RadioSendingState.IsSending || (RadioSendingState.SendingOn != currentSelected))
                            {
                                _audioManager.PlaySoundEffectStartTransmit(currentSelected,
                                                                           radio.enc && (radio.encKey > 0), radio.volume);
                            }

                            //set radio overlay state
                            RadioSendingState = new RadioSendingState
                            {
                                IsSending  = true,
                                LastSentAt = DateTime.Now.Ticks,
                                SendingOn  = currentSelected
                            };
                            return(true);
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception Sending Audio Message " + e.Message);
                }
            }
            else
            {
                if (RadioSendingState.IsSending)
                {
                    RadioSendingState.IsSending = false;

                    if (RadioSendingState.SendingOn >= 0)
                    {
                        var radio = _clientStateSingleton.DcsPlayerRadioInfo.radios[RadioSendingState.SendingOn];

                        _audioManager.PlaySoundEffectEndTransmit(RadioSendingState.SendingOn, radio.volume);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        public bool Send(byte[] bytes, int len)
        {
            // List of radios the transmission is sent to (can me multiple if simultaneous transmission is enabled)
            List <RadioInformation> transmittingRadios;
            //if either PTT is true, a microphone is available && socket connected etc
            var sendingOn = -1;

            if (_ready &&
                _listener != null &&
                _clientStateSingleton.DcsPlayerRadioInfo.IsCurrent() &&
                _clientStateSingleton.MicrophoneAvailable &&
                (bytes != null) &&
                (transmittingRadios = PTTPressed(out sendingOn)).Count > 0)
            //can only send if DCS is connected
            {
                try
                {
                    if (transmittingRadios.Count > 0)
                    {
                        List <double> frequencies = new List <double>(transmittingRadios.Count);
                        List <byte>   encryptions = new List <byte>(transmittingRadios.Count);
                        List <byte>   modulations = new List <byte>(transmittingRadios.Count);

                        for (int i = 0; i < transmittingRadios.Count; i++)
                        {
                            var radio = transmittingRadios[i];

                            // Further deduplicate transmitted frequencies if they have the same freq./modulation/encryption (caused by differently named radios)
                            bool alreadyIncluded = false;
                            for (int j = 0; j < frequencies.Count; j++)
                            {
                                if (frequencies[j] == radio.freq &&
                                    modulations[j] == (byte)radio.modulation &&
                                    encryptions[j] == (radio.enc ? radio.encKey : (byte)0))
                                {
                                    alreadyIncluded = true;
                                    break;
                                }
                            }

                            if (alreadyIncluded)
                            {
                                continue;
                            }

                            frequencies.Add(radio.freq);
                            encryptions.Add(radio.enc ? radio.encKey : (byte)0);
                            modulations.Add((byte)radio.modulation);
                        }

                        //generate packet
                        var udpVoicePacket = new UDPVoicePacket
                        {
                            GuidBytes        = _guidAsciiBytes,
                            AudioPart1Bytes  = bytes,
                            AudioPart1Length = (ushort)bytes.Length,
                            Frequencies      = frequencies.ToArray(),
                            UnitId           = _clientStateSingleton.DcsPlayerRadioInfo.unitId,
                            Encryptions      = encryptions.ToArray(),
                            Modulations      = modulations.ToArray(),
                            PacketNumber     = _packetNumber++
                        };

                        var encodedUdpVoicePacket = udpVoicePacket.EncodePacket();

                        _listener.Send(encodedUdpVoicePacket, encodedUdpVoicePacket.Length, new IPEndPoint(_address, _port));

                        var currentlySelectedRadio = _clientStateSingleton.DcsPlayerRadioInfo.radios[sendingOn];

                        //not sending or really quickly switched sending
                        if (currentlySelectedRadio != null &&
                            (!RadioSendingState.IsSending || RadioSendingState.SendingOn != sendingOn))
                        {
                            _audioManager.PlaySoundEffectStartTransmit(sendingOn,
                                                                       currentlySelectedRadio.enc && (currentlySelectedRadio.encKey > 0),
                                                                       currentlySelectedRadio.volume);
                        }

                        //set radio overlay state
                        RadioSendingState = new RadioSendingState
                        {
                            IsSending  = true,
                            LastSentAt = DateTime.Now.Ticks,
                            SendingOn  = sendingOn
                        };
                        return(true);
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception Sending Audio Message " + e.Message);
                }
            }
            else
            {
                if (RadioSendingState.IsSending)
                {
                    RadioSendingState.IsSending = false;

                    if (RadioSendingState.SendingOn >= 0)
                    {
                        var radio = _clientStateSingleton.DcsPlayerRadioInfo.radios[RadioSendingState.SendingOn];

                        _audioManager.PlaySoundEffectEndTransmit(RadioSendingState.SendingOn, radio.volume);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        public void Send(byte[] bytes, int len)
        {
            if (part1 == null)
            {
                part1 = new byte[len];
                Buffer.BlockCopy(bytes, 0, part1, 0, len);
            }
            else if (part2 == null)
            {
                part2 = new byte[len];
                Buffer.BlockCopy(bytes, 0, part2, 0, len);
            }
            else
            {
                part2 = part1;

                part1 = new byte[len];
                Buffer.BlockCopy(bytes, 0, part1, 0, len);
            }

            //if either PTT is true
            if ((_ptt || RadioDCSSyncServer.DcsPlayerRadioInfo.ptt) &&
                RadioDCSSyncServer.DcsPlayerRadioInfo.IsCurrent() && part1 != null && part2 != null)
            //can only send if DCS is connected
            {
                try
                {
                    var currentSelected = RadioDCSSyncServer.DcsPlayerRadioInfo.selected;
                    //removes race condition by assigning here with the current selected changing
                    if (currentSelected >= 0 &&
                        currentSelected < RadioDCSSyncServer.DcsPlayerRadioInfo.radios.Length)
                    {
                        var radio = RadioDCSSyncServer.DcsPlayerRadioInfo.radios[currentSelected];

                        if (radio != null && radio.frequency > 100 && radio.modulation != 3 ||
                            radio.modulation == 2)
                        {
                            //generate packet
                            var udpVoicePacket = new UDPVoicePacket
                            {
                                GuidBytes        = _guidAsciiBytes,
                                AudioPart1Bytes  = part1,
                                AudioPart1Length = (ushort)part1.Length,
                                AudioPart2Bytes  = part2,
                                AudioPart2Length = (ushort)part2.Length,
                                Frequency        = radio.frequency,
                                UnitId           = RadioDCSSyncServer.DcsPlayerRadioInfo.unitId,
                                Encryption       = radio.enc ? radio.encKey : (byte)0,
                                Modulation       = radio.modulation
                            }.EncodePacket();

                            //clear audio
                            part1 = null;
                            part2 = null;

                            //no need to auto send packet anymore
                            hasSentVoicePacket = true;

                            var ip = new IPEndPoint(_address, _port);
                            _listener.Send(udpVoicePacket, udpVoicePacket.Length, ip);

                            //not sending or really quickly switched sending
                            if (!RadioSendingState.IsSending || RadioSendingState.SendingOn != currentSelected)
                            {
                                _audioManager.PlaySoundEffectStartTransmit(currentSelected,
                                                                           radio.enc && radio.encKey > 0, radio.volume);
                            }

                            //set radio overlay state
                            RadioSendingState = new RadioSendingState
                            {
                                IsSending  = true,
                                LastSentAt = Environment.TickCount,
                                SendingOn  = currentSelected
                            };
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception Sending Audio Message " + e.Message);
                }
            }
            else if (part1 != null && part2 != null)
            {
                if (RadioSendingState.IsSending)
                {
                    RadioSendingState.IsSending = false;

                    if (RadioSendingState.SendingOn >= 0)
                    {
                        var radio = RadioDCSSyncServer.DcsPlayerRadioInfo.radios[RadioSendingState.SendingOn];

                        _audioManager.PlaySoundEffectEndTransmit(RadioSendingState.SendingOn, radio.volume);
                    }
                }

                if (!hasSentVoicePacket)
                {
                    try
                    {
                        var udpVoicePacket = new UDPVoicePacket
                        {
                            GuidBytes        = _guidAsciiBytes,
                            AudioPart1Bytes  = part1,
                            AudioPart1Length = (ushort)part1.Length,
                            AudioPart2Bytes  = part2,
                            AudioPart2Length = (ushort)part2.Length,
                            Frequency        = 100,
                            UnitId           = 1,
                            Encryption       = 0,
                            Modulation       = 4
                        }.EncodePacket();

                        hasSentVoicePacket = true;

                        var ip = new IPEndPoint(_address, _port);
                        _listener.Send(udpVoicePacket, udpVoicePacket.Length, ip);

                        Logger.Info("Sent First Voice Packet");
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception Sending First Audio Message " + e.Message);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void Send(byte[] bytes, int len)
        {
            //if either PTT is true
            if ((_ptt || _clientStateSingleton.DcsPlayerRadioInfo.ptt) &&
                _clientStateSingleton.DcsPlayerRadioInfo.IsCurrent() && (bytes != null))
            //can only send if DCS is connected
            {
                try
                {
                    var currentSelected = _clientStateSingleton.DcsPlayerRadioInfo.selected;
                    //removes race condition by assigning here with the current selected changing
                    if ((currentSelected >= 0) &&
                        (currentSelected < _clientStateSingleton.DcsPlayerRadioInfo.radios.Length))
                    {
                        var radio = _clientStateSingleton.DcsPlayerRadioInfo.radios[currentSelected];

                        if (((radio != null) && (radio.freq > 100) &&
                             (radio.modulation != RadioInformation.Modulation.DISABLED)) ||
                            (radio.modulation == RadioInformation.Modulation.INTERCOM))
                        {
                            //generate packet
                            var udpVoicePacket = new UDPVoicePacket
                            {
                                GuidBytes        = _guidAsciiBytes,
                                AudioPart1Bytes  = bytes,
                                AudioPart1Length = (ushort)bytes.Length,
                                Frequency        = radio.freq,
                                UnitId           = _clientStateSingleton.DcsPlayerRadioInfo.unitId,
                                Encryption       = radio.enc ? radio.encKey : (byte)0,
                                Modulation       = (byte)radio.modulation,
                                PacketNumber     = _packetNumber++
                            }.EncodePacket();


                            //no need to auto send packet anymore
                            hasSentVoicePacket = true;

                            var ip = new IPEndPoint(_address, _port);
                            _listener.Send(udpVoicePacket, udpVoicePacket.Length, ip);

                            //not sending or really quickly switched sending
                            if (!RadioSendingState.IsSending || (RadioSendingState.SendingOn != currentSelected))
                            {
                                _audioManager.PlaySoundEffectStartTransmit(currentSelected,
                                                                           radio.enc && (radio.encKey > 0), radio.volume);
                            }

                            //set radio overlay state
                            RadioSendingState = new RadioSendingState
                            {
                                IsSending  = true,
                                LastSentAt = Environment.TickCount,
                                SendingOn  = currentSelected
                            };
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception Sending Audio Message " + e.Message);
                }
            }
            else
            {
                if (RadioSendingState.IsSending)
                {
                    RadioSendingState.IsSending = false;

                    if (RadioSendingState.SendingOn >= 0)
                    {
                        var radio = _clientStateSingleton.DcsPlayerRadioInfo.radios[RadioSendingState.SendingOn];

                        _audioManager.PlaySoundEffectEndTransmit(RadioSendingState.SendingOn, radio.volume);
                    }
                }

                if (!hasSentVoicePacket)
                {
                    try
                    {
                        var udpVoicePacket = new UDPVoicePacket
                        {
                            GuidBytes        = _guidAsciiBytes,
                            AudioPart1Bytes  = bytes,
                            AudioPart1Length = (ushort)bytes.Length,
                            Frequency        = 100,
                            UnitId           = 1,
                            Encryption       = 0,
                            Modulation       = 4
                        }.EncodePacket();

                        hasSentVoicePacket = true;

                        var ip = new IPEndPoint(_address, _port);
                        _listener.Send(udpVoicePacket, udpVoicePacket.Length, ip);

                        Logger.Info("Sent First Voice Packet");
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception Sending First Audio Message " + e.Message);
                    }
                }
            }
        }
        public bool Send(byte[] bytes, int len)
        {
            //if either PTT is true, a microphone is available && socket connected etc
            if (_ready &&
                _listener != null &&
                _listener.Connected &&
                (_ptt || _clientStateSingleton.DcsPlayerRadioInfo.ptt) &&
                _clientStateSingleton.DcsPlayerRadioInfo.IsCurrent() &&
                _clientStateSingleton.MicrophoneAvailable &&
                (bytes != null))
            //can only send if DCS is connected
            {
                try
                {
                    // List of radios the transmission is sent to (can me multiple if simultaneous transmission is enabled)
                    List <RadioInformation> transmittingRadios = new List <RadioInformation>();

                    // Always add currently selected radio (if valid)
                    var currentSelected = _clientStateSingleton.DcsPlayerRadioInfo.selected;
                    RadioInformation currentlySelectedRadio = null;
                    if (currentSelected >= 0 &&
                        currentSelected < _clientStateSingleton.DcsPlayerRadioInfo.radios.Length)
                    {
                        currentlySelectedRadio = _clientStateSingleton.DcsPlayerRadioInfo.radios[currentSelected];

                        if (currentlySelectedRadio != null && currentlySelectedRadio.modulation != RadioInformation.Modulation.DISABLED &&
                            (currentlySelectedRadio.freq > 100 || currentlySelectedRadio.modulation == RadioInformation.Modulation.INTERCOM))
                        {
                            transmittingRadios.Add(currentlySelectedRadio);
                        }
                    }

                    // Add all radios toggled for simultaneous transmission if the global flag has been set
                    if (_clientStateSingleton.DcsPlayerRadioInfo.simultaneousTransmission)
                    {
                        foreach (var radio in _clientStateSingleton.DcsPlayerRadioInfo.radios)
                        {
                            if (radio != null && radio.simul && radio.modulation != RadioInformation.Modulation.DISABLED &&
                                (radio.freq > 100 || radio.modulation == RadioInformation.Modulation.INTERCOM) &&
                                !transmittingRadios.Contains(radio))    // Make sure we don't add the selected radio twice
                            {
                                transmittingRadios.Add(radio);
                            }
                        }
                    }

                    if (transmittingRadios.Count > 0)
                    {
                        List <double> frequencies = new List <double>(transmittingRadios.Count);
                        List <byte>   encryptions = new List <byte>(transmittingRadios.Count);
                        List <byte>   modulations = new List <byte>(transmittingRadios.Count);

                        for (int i = 0; i < transmittingRadios.Count; i++)
                        {
                            var radio = transmittingRadios[i];

                            // Further deduplicate transmitted frequencies if they have the same freq./modulation/encryption (caused by differently named radios)
                            bool alreadyIncluded = false;
                            for (int j = 0; j < frequencies.Count; j++)
                            {
                                if (frequencies[j] == radio.freq &&
                                    modulations[j] == (byte)radio.modulation &&
                                    encryptions[j] == (radio.enc ? radio.encKey : (byte)0))
                                {
                                    alreadyIncluded = true;
                                    break;
                                }
                            }

                            if (alreadyIncluded)
                            {
                                continue;
                            }

                            frequencies.Add(radio.freq);
                            encryptions.Add(radio.enc ? radio.encKey : (byte)0);
                            modulations.Add((byte)radio.modulation);
                        }

                        //generate packet
                        var udpVoicePacket = new UDPVoicePacket
                        {
                            GuidBytes        = _guidAsciiBytes,
                            AudioPart1Bytes  = bytes,
                            AudioPart1Length = (ushort)bytes.Length,
                            Frequencies      = frequencies.ToArray(),
                            UnitId           = _clientStateSingleton.DcsPlayerRadioInfo.unitId,
                            Encryptions      = encryptions.ToArray(),
                            Modulations      = modulations.ToArray(),
                            PacketNumber     = _packetNumber++
                        };

                        var encodedUdpVoicePacket = udpVoicePacket.EncodePacket();

                        //send audio
                        _listener.Client.Send(encodedUdpVoicePacket);

                        //not sending or really quickly switched sending
                        if (currentlySelectedRadio != null &&
                            (!RadioSendingState.IsSending || RadioSendingState.SendingOn != currentSelected))
                        {
                            _audioManager.PlaySoundEffectStartTransmit(currentSelected,
                                                                       currentlySelectedRadio.enc && (currentlySelectedRadio.encKey > 0), currentlySelectedRadio.volume);
                        }

                        //set radio overlay state
                        RadioSendingState = new RadioSendingState
                        {
                            IsSending  = true,
                            LastSentAt = DateTime.Now.Ticks,
                            SendingOn  = currentSelected
                        };
                        return(true);
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception Sending Audio Message " + e.Message);
                }
            }
            else
            {
                if (RadioSendingState.IsSending)
                {
                    RadioSendingState.IsSending = false;

                    if (RadioSendingState.SendingOn >= 0)
                    {
                        var radio = _clientStateSingleton.DcsPlayerRadioInfo.radios[RadioSendingState.SendingOn];

                        _audioManager.PlaySoundEffectEndTransmit(RadioSendingState.SendingOn, radio.volume);
                    }
                }
            }
            return(false);
        }