Click here to Skip to main content
11,647,170 members (68,846 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
Add your own
alternative version

Using OpenTK/OpenAL to Develop Cross Platform DIS VOIP Application

, 15 Mar 2010 BSD 19.1K 1.3K 22
Application allows voice communications (VOIP) utilizing the Distributed Interactive Simulation protocol (IEEE 1278.1)
MonoDISRadioTransmitter_SourceCode.zip
MonoDISRadioTransmitter
AudioIN
bin
Properties
StyleCop.Cache
AudioOUT
bin
Properties
DISNET
bin
DataStreams
DISPDU
Enumerations
Properties
Utilities
OpenDISRadioTransmitterExample
AudioCodecs
bin
OpenDISRadioTransmitter.csproj.user
OpenTK
OpenTK.Compatibility.dll
OpenTK.dll
OpenTK.GLControl.dll
Properties
Settings.settings
StyleCop.Cache
OpenDISRadioTransmitterSolution.suo
RadioFeatureSelection
bin
Properties
Sockets
bin
Properties
// Copyright (c) 2010, Peter Smith
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list 
// of conditions and the following disclaimer. 
//
// Redistributions in binary form must reproduce the above copyright notice, this 
// list of conditions and the following disclaimer in the documentation and/or 
// other materials provided with the distribution. 
//
// Neither the name of the  Author nor the names of its contributors may be used to 
// endorse or promote products derived from this software without specific prior 
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
// OF THE POSSIBILITY OF SUCH DAMAGE.

namespace SocketCommunications
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;

    public class BroadCastSend
    {
        #region Fields

        // Signals when the resolve has finished.
        public static ManualResetEvent GetHostEntryFinished = new ManualResetEvent(false);

        public IPAddress broadcastIPAddress;
        public Socket broadcastSocket;

        const int defaultPortNumber = 9737;

        private int broadcastPort;
        private List<IPAddress> hostIPAddresses = new List<IPAddress>();
        string hostName;
        IPHostEntry resolvedIPs;

        #endregion Fields

        #region Constructors

        public BroadCastSend()
            : this(Dns.GetHostName())
        {
        }

        public BroadCastSend(string host)
        {
            //hostName = host;
            //DoGetHostEntryAsync(hostName);
        }

        #endregion Constructors

        #region Properties

        public int BroadcastPort
        {
            get
            {
                return broadcastPort;
            }

            set
            {
                if (value >= 0 || value < 65336)
                    broadcastPort = value;
                else
                    broadcastPort = defaultPortNumber;
            }
        }

        //public List<string> getIPAddresses
        //{
        //    get
        //    {
        //        List<string> Sendipaddresses = new List<string>();

        //        foreach (IPAddress ipAddress in hostIPAddresses)
        //        {
        //           string convertIPToBroadcast = ipAddress.ToString();

        //           bool exists = false;
        //           foreach (string s in Sendipaddresses)
        //           {
        //               if (s == convertIPToBroadcast)
        //               {
        //                   exists = true;
        //                   break;
        //               }
        //           }

        //           if (exists == false)
        //               Sendipaddresses.Add(convertIPToBroadcast);

        //        }

        //        return Sendipaddresses;

        //    }
        //}

        public string host
        {
            get { return hostName; }
            set { hostName = value; }
        }

        //public List<IPAddress> HostIPAddresses
        //{
        //    get
        //    {
        //        if (this.hostIPAddresses.Count == 0)
        //            DoGetHostEntryAsync(this.hostName);
        //        return this.hostIPAddresses;
        //    }
        //}

        //public IPHostEntry IPs
        //{
        //    get { return resolvedIPs; }
        //    set { resolvedIPs = value; }
        //}

        //public List<string> receiveIPAddresses
        //{
        //    get
        //    {
        //        List<string> Receiveipaddresses = new List<string>();

        //        foreach (IPAddress ipAddress in hostIPAddresses)
        //        {
        //            string convertIPToBroadcast = ipAddress.ToString();

        //            int lastOctect = convertIPToBroadcast.LastIndexOf('.');
        //            if (lastOctect != -1)
        //            {
        //                convertIPToBroadcast = convertIPToBroadcast.Substring(0, lastOctect + 1) + "255";
        //                Receiveipaddresses.Add(ipAddress.ToString());
        //                Receiveipaddresses.Add(convertIPToBroadcast);
        //            }
        //        }

        //        return Receiveipaddresses;
        //    }
        //}

        #endregion Properties

        #region Methods

        public void BroadcastMessage(byte[] message)
        {
            IPEndPoint endPoint;

            try
            {
                endPoint = new IPEndPoint(broadcastIPAddress, broadcastPort);
                broadcastSocket.SendTo(message, endPoint);
            }
            catch (Exception e)
            {
                string error = e.Message;
                Trace.WriteLine("Broadcast Message Error : " + error);
            }
        }

        // Determine the Internet Protocol (IP) addresses for
        // this host asynchronously.
        //public void DoGetHostEntryAsync(string hostname)
        //{
        //    GetHostEntryFinished.Reset();
        //    //BroadCastSend ioContext = null;

        //    IPStateObject ioContext = null;

        //    try
        //    {

        //        ioContext = new IPStateObject();
        //        ioContext.host = hostname;

        //        Dns.BeginGetHostEntry(ioContext.host, new AsyncCallback(GetHostEntryCallback), ioContext);

        //        // Wait here until the resolve completes (the callback calls .Set())
        //        GetHostEntryFinished.WaitOne();

        //        resolvedIPs = ioContext.ipHostEntry;
        //    }
        //    catch (Exception ex)
        //    {
        //        string error = ex.ToString();
        //    }

        //    foreach (IPAddress ip in ioContext.ipHostEntry.AddressList)
        //    {
        //        hostIPAddresses.Add(ip);

        //    }
        //}

        // Record the IPs in the state object for later use.
        //public void GetHostEntryCallback(IAsyncResult ar)
        //{
        //    //BroadCastSend ioContext = (BroadCastSend)ar.AsyncState;

        //    IPStateObject ioContext = (IPStateObject)ar.AsyncState;

        //    //ioContext.IPs = Dns.EndGetHostEntry(ar);
        //    ioContext.ipHostEntry = Dns.EndGetHostEntry(ar);
        //    GetHostEntryFinished.Set();
        //}

        public void ShutdownSocket()
        {
            if (broadcastSocket != null)
            {
                if (broadcastSocket.Connected == true)
                    broadcastSocket.Disconnect(true);
                broadcastSocket.Shutdown(SocketShutdown.Both);
                broadcastSocket.Close();
            }
        }

        public void StartBroadCastSend(string ipAddress)
        {
            //Uses port number set by user or default assigned in constructor
            StartBroadCastSend(ipAddress, BroadcastPort, 0);
        }

        public void StartBroadCastSend(string ipAddress, int portNumber)
        {
            StartBroadCastSend(ipAddress, portNumber, 0);
        }

        public void StartBroadCastSend(string ipAddress, int portNumber, int interfaceCard)
        {
            broadcastIPAddress = IPAddress.Parse(ipAddress);

            // Join the listener multicast group.
            CreateSendingBroadCastSocket(broadcastIPAddress, portNumber, interfaceCard);
        }

        /// <summary>
        /// Broadcast socket interface setup
        /// </summary>
        /// <param name="ipAddress">Interface card IP address or blank to default to main card</param>
        /// <param name="interfaceCard"></param>
        private void CreateSendingBroadCastSocket(IPAddress ipAddress, int portNumber, int interfaceCard)
        {
            try
            {
                //Assign port number to field
                this.BroadcastPort = portNumber;

                // Create a broadcast socket.
                broadcastSocket = new Socket(AddressFamily.InterNetwork,
                                         SocketType.Dgram,
                                         ProtocolType.Udp);

                broadcastSocket.EnableBroadcast = true;
                broadcastSocket.ExclusiveAddressUse = false;
            }
            catch (Exception e)
            {
                string error = e.Message;
                Trace.WriteLine("Multicasting Broadcast Error : " + error);
            }
        }

        #endregion Methods
    }

    public class IPStateObject
    {
        #region Fields

        public string host;
        public IPHostEntry ipHostEntry;

        #endregion Fields
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

pesmith
Software Developer
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150804.2 | Last Updated 15 Mar 2010
Article Copyright 2010 by pesmith
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid