Ejemplo n.º 1
0
        /// <summary> 
        /// Constructor for UDP channels.
        /// </summary>
        /// <param name="localInfo"> The local endpoint to which you bind the UDP connection.
        /// <param name="remoteInfo"> A struct with the host name  and the port number 
        /// of the remote host
        /// </param>
        /// <exception cref="System.IO.IOException">  if an error occurs
        /// </exception>
        public MulticastMessageChannel(ConnectionInfo multicastInfo)
        {
            try
            {
                // Create the Socket
                udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                // Set the reuse address option
                udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);

                // Create an IPEndPoint and bind to it
                IPEndPoint bindaddrs = new IPEndPoint(IPAddress.Any, multicastInfo.Port);
                udpSocket.Bind(bindaddrs);

                // Define a MulticastOption object specifying the multicast group
                // address and the local IPAddress.
                // The multicast group address is the same as the address used by the server.
                System.Net.IPAddress ipAddress = IPAddress.Parse(multicastInfo.Addr);
                MulticastOption mcastOption = new MulticastOption(ipAddress, IPAddress.Any);

                // IP multicast loopback.
                udpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, 1);

                // Add membership in the multicast group
                //BIG NOTE: If you have an exception at this point and your computer is disconnected
                // using Windows, the problem is due to a lack of a loopback interface.
                // You need to install a loopback interface Adapter.
                // The Microsoft Loopback adapter is a tool for testing in a
                // virtual network environment where access to a network is not feasible.
                // Click Start, point to Settings, click Control Panel, and then double-click Add/Remove Hardware.
                // You will find it in the Manufacturers section, Microsoft.
                udpSocket.SetSocketOption(SocketOptionLevel.IP,
                                        SocketOptionName.AddMembership,
                                        new MulticastOption(ipAddress, IPAddress.Any));

                // Set the Time to Live
                udpSocket.SetSocketOption(SocketOptionLevel.IP,
                                            SocketOptionName.MulticastTimeToLive, 2);

                udpMulticastAddress = new IPEndPoint(ipAddress, multicastInfo.Port);
                udpSocket.Connect(udpMulticastAddress);
            }
            catch (SocketException e)
            {
                if (log.IsErrorEnabled)
                    log.Error("SocketException caught!. Message : " + e.Message);
                throw e;
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled)
                    log.Error("Exception caught!. Message : " + e.Message);
                throw e;
            }

            if (log.IsDebugEnabled)
                log.Debug("Connected Multicast, own channel local point: " + udpSocket.LocalEndPoint
                        + " Multicast Address " + udpSocket.RemoteEndPoint);
        }
Ejemplo n.º 2
0
        /// <summary> 
        /// Constructor.
        /// </summary>
        /// <param name="port">the port on which to accept incoming connections
        /// </param>
        /// <exception cref="System.IO.IOException"> if an IO error occurs
        /// </exception>
        public TCPMessageChannelAcceptor(ConnectionInfo info)
        {
            System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(info.Addr);
            System.Net.IPAddress ipAddress = System.Net.Dns.GetHostEntry(hostEntry.HostName).AddressList[0];

            serverSocket = new System.Net.Sockets.TcpListener(ipAddress, info.Port);
            serverSocket.Start();

            closed = false;
        }
Ejemplo n.º 3
0
        public void Test_1()
        {
            if (log.IsDebugEnabled)
                log.Debug("In Test: " + System.Reflection.MethodBase.GetCurrentMethod());

            ConnectionInfo connection = new ConnectionInfo();
            connection.Addr = "localhost";
            connection.Port = 7777;

            TCPMessageChannelAcceptor channelAcceptor = channelMngr.StartNewListener(connection);

            Assert.AreEqual(channelAcceptor.IsClosed, false);

            channelAcceptor.Close();

            Assert.AreEqual(channelAcceptor.IsClosed, true);
        }
Ejemplo n.º 4
0
        /// <summary> 
        /// Constructor for UDP channels.
        /// </summary>
        /// <param name="localInfo"> The local endpoint to which you bind the UDP connection.
        /// <param name="remoteInfo"> A struct with the host name  and the port number 
        /// of the remote host
        /// </param>
        /// <exception cref="System.IO.IOException">  if an error occurs
        /// </exception>
        public UDPMessageChannel(ConnectionInfo localInfo, ConnectionInfo remoteInfo)
        {
            try
            {
                udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                if (localInfo != null)
                {
                    System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(localInfo.Addr);
                    System.Net.IPAddress ipAddress = System.Net.Dns.GetHostEntry(hostEntry.HostName).AddressList[0];
                    udpLocalAddress = new IPEndPoint(ipAddress, localInfo.Port);
                }
                else
                {
                    udpLocalAddress = new IPEndPoint(IPAddress.Any, 0);
                }
                udpSocket.Bind(udpLocalAddress);

                if (remoteInfo != null)
                {
                    System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(remoteInfo.Addr);
                    System.Net.IPAddress ipAddress = System.Net.Dns.GetHostEntry(hostEntry.HostName).AddressList[0];
                    udpRemoteAddress = new IPEndPoint(ipAddress, remoteInfo.Port);
                    udpSocket.Connect(udpRemoteAddress);
                }
            }
            catch (SocketException e)
            {
                if (log.IsErrorEnabled)
                    log.Error("SocketException caught!. Message : " + e.Message);
                throw e;
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled)
                    log.Error("Exception caught!. Message : " + e.Message);
                throw e;
            }

            if (log.IsDebugEnabled)
                log.Debug("Connected UDP, own channel local point: " + udpSocket.LocalEndPoint.ToString());
        }
Ejemplo n.º 5
0
        /// <summary> 
        /// Constructor for TCP channels.
        /// </summary>
        /// <param name="info"> a struct with the host name  and the port number 
        /// of the remote host
        /// </param>
        /// <exception cref="System.IO.IOException">  if an error occurs
        /// </exception>
        public TCPMessageChannel(ConnectionInfo info)
        {
            System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(info.Addr);
            System.Net.IPAddress ipAddress = System.Net.Dns.GetHostEntry(hostEntry.HostName).AddressList[0];
            tcpPacketAddress = new IPEndPoint(ipAddress, info.Port);

            try
            {
                Socket tmpS =
                    new Socket(tcpPacketAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                tmpS.Connect(tcpPacketAddress);

                if (tmpS.Connected)
                {
                    tcpSocket = tmpS;
                }
                else
                    tcpSocket = null;
            }
            catch (SocketException e)
            {
                if (log.IsErrorEnabled)
                    log.Error("SocketException caught!. Message : " + e.Message);
                throw e;
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled)
                    log.Error("Exception caught!. Message : " + e.Message);
                throw e;
            }

            if (log.IsDebugEnabled)
                log.Debug("Connected, own channel local point: " + tcpSocket.LocalEndPoint);

            tcpStream = new NetworkStream(tcpSocket);
        }
Ejemplo n.º 6
0
        public void Test_2()
        {
            if (log.IsDebugEnabled)
                log.Debug("In Test: " + System.Reflection.MethodBase.GetCurrentMethod());

            ConnectionInfo connection = new ConnectionInfo();
            connection.Addr = "localhost";
            connection.Port = 7777;

            TCPMessageChannelAcceptor channelAcceptor = channelMngr.StartNewListener(connection);

            System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(connection.Addr);
            System.Net.IPAddress ipAddress = System.Net.Dns.GetHostEntry(hostEntry.HostName).AddressList[0];

            Assert.AreEqual(channelAcceptor.LocalEndPoint.ToString(), ipAddress + ":7777");
        }
Ejemplo n.º 7
0
        private void StartNewUDPConnectedChannel(Uri connection)
        {
            ConnectionInfo info = new ConnectionInfo();
            info.Addr = connection.DnsSafeHost;
            info.Port = connection.Port;

            UDPMessageChannel channel = new UDPMessageChannel(null, info);
            AddChannel(channel);
            if (log.IsDebugEnabled)
                log.Debug("Created UDP Channel running in port " + channel.udpSocket.LocalEndPoint +
                          " remote addrs: " + channel.udpSocket.RemoteEndPoint);
        }
Ejemplo n.º 8
0
        private void StartNewTCPConnection(Uri connection)
        {
            ConnectionInfo info = new ConnectionInfo();
            info.Addr = connection.DnsSafeHost;
            info.Port = connection.Port;

            System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(info.Addr);
            System.Net.IPAddress ipAddress = System.Net.Dns.GetHostEntry(hostEntry.HostName).AddressList[0];
            IPEndPoint endpoint = new IPEndPoint(ipAddress, info.Port);
            foreach (MessageChannelAcceptor acceptor in channelAcceptorList)
            {
                if (!acceptor.IsClosed && acceptor.LocalEndPoint != null && acceptor.LocalEndPoint.Equals(endpoint))
                {
                    //We are trying to connect to our own acceptor!
                    return;
                }
            }

            TCPMessageChannel channel = new TCPMessageChannel(info);

            if (log.IsDebugEnabled)
            {
                log.Debug("Connected from " + channel.InternalSocket.LocalEndPoint +
                          " to " + channel.InternalSocket.RemoteEndPoint);
                log.Debug("Timeouts in mls: Receive " + channel.InternalSocket.ReceiveTimeout +
                          ", Send " + channel.InternalSocket.SendTimeout);
            }

            AddChannel(channel);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Create a new UDP Channel using the connection info supplied by parameter.
        /// Start a new thread to process new connections
        /// </summary>
        /// <param name="info">information for the connection, including address and port</param>
        /// <returns> the created channel </returns>
        public UDPMessageChannel StartNewUDPLocalChannel(ConnectionInfo info)
        {
            UDPMessageChannel channel = null;
            lock (this)
            {
                try
                {
                    channel = new UDPMessageChannel(info, null);
                    if (channel == null)
                    {
                        return null;
                    }

                    AddChannel(channel);
                    if (log.IsDebugEnabled)
                        log.Debug("Created UDP Channel running in port " + channel.udpSocket.LocalEndPoint);
                }
                catch (SocketException e)
                {
                    if (log.IsWarnEnabled)
                        log.Warn("Can't open socket running in port :" + e.Message);
                    //maybe it is in use. Just Return.
                    return null;
                }
                catch (Exception e)
                {
                    throw e;
                }
                return channel;
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Create a new acceptor using the connection info supplied by parameter.
        /// Start a new thread to process new connections
        /// </summary>
        /// <param name="listenerInfo">information for the connection, including address and port</param>
        /// <returns> the created acceptor </returns>
        public TCPMessageChannelAcceptor StartNewListener(ConnectionInfo listenerInfo)
        {
            TCPMessageChannelAcceptor channelAcceptor = null;

            lock (this)
            {
                try
                {
                    channelAcceptor = new TCPMessageChannelAcceptor(listenerInfo);

                    if (channelAcceptor == null)
                    {
                        return null;
                    }

                    channelAcceptorList.Add(channelAcceptor);
                    DoBeginAcceptSocket(channelAcceptor);

                    if (log.IsDebugEnabled)
                        log.Debug("Created acceptor running in port " + channelAcceptor.LocalEndPoint);
                }
                catch (SocketException e)
                {
                    if (log.IsWarnEnabled)
                        log.Warn("Can't open socket running in port :" + e.Message);
                    //maybe it is in use. Just Return.
                    return null;
                }
                catch (Exception e)
                {
                    throw e;
                }
                return channelAcceptor;
            }
        }
Ejemplo n.º 11
0
 public IList<ConnectionInfo> GetTCPListenerInfoList()
 {
     IList<ConnectionInfo> list = new List<ConnectionInfo>();
     ConnectionInfo item = new ConnectionInfo();
     item.Addr = source.Configs["Channels"].Get("DefaultAddr");
     item.Port = source.Configs["Channels"].GetInt("TcpPort");
     list.Add(item);
     return list;
 }