/// <summary>
        ///     Joins the multicast group at the specified endpoint.
        /// </summary>
        /// <param name="multicastAddress">The address for the multicast group.</param>
        /// <param name="port">The port for the multicast group.</param>
        /// <param name="multicastOn">The <code>CommsInterface</code> to multicast on. If unspecified, all interfaces will be bound.</param>
        /// <returns></returns>
        public async Task JoinMulticastGroupAsync(string multicastAddress, int port, ICommsInterface multicastOn = null)
        {
            if (multicastOn != null && !multicastOn.IsUsable)
                throw new InvalidOperationException("Cannot multicast on an unusable interface. Check the IsUsable property before attemping to connect.");

            var bindingIp = multicastOn != null ? ((CommsInterface)multicastOn).NativeIpAddress : IPAddress.Any;
            var bindingEp = new IPEndPoint(bindingIp, port);

            var multicastIp = IPAddress.Parse(multicastAddress);

            try
            {
                _backingUdpClient = new UdpClient(bindingEp)
                {
                    EnableBroadcast = true
                };
                ProtectAgainstICMPUnreachable(_backingUdpClient);
            }
            catch (PlatformSocketException ex)
            {
                throw new PclSocketException(ex);
            }

            _messageCanceller = new CancellationTokenSource();
            
            await Task
                .Run(() => this._backingUdpClient.JoinMulticastGroup(multicastIp, this.TTL))
                .WrapNativeSocketExceptions();

            _multicastAddress = multicastAddress;
            _multicastPort = port;

            RunMessageReceiver(_messageCanceller.Token);
        }
Esempio n. 2
0
        /// <summary>
        ///     Binds the <code>TcpSocketListener</code> to the specified port on all endpoints and listens for TCP connections.
        /// </summary>
        /// <param name="port">The port to listen on. If '0', selection is delegated to the operating system.</param>
        /// <param name="listenOn">The <code>CommsInterface</code> to listen on. If unspecified, all interfaces will be bound.</param>
        /// <returns></returns>
        public Task StartListeningAsync(int port, ICommsInterface listenOn = null)
        {
            if (listenOn != null && !listenOn.IsUsable)
                throw new InvalidOperationException("Cannot listen on an unusable interface. Check the IsUsable property before attemping to bind.");
            
            _listenCanceller = new CancellationTokenSource();
            _backingStreamSocketListener = new StreamSocketListener();

            _backingStreamSocketListener.ConnectionReceived += (sender, args) =>
            {
                var nativeSocket = args.Socket;
                var wrappedSocket = new TcpSocketClient(nativeSocket, _bufferSize);

                var eventArgs = new TcpSocketListenerConnectEventArgs(wrappedSocket);
                if (ConnectionReceived != null)
                    ConnectionReceived(this, eventArgs);
            };

            var sn = port == 0 ? "" : port.ToString();
#if !WP80    
            if (listenOn != null)
            {
                var adapter = ((CommsInterface)listenOn).NativeNetworkAdapter;

                return _backingStreamSocketListener
                            .BindServiceNameAsync(sn, SocketProtectionLevel.PlainSocket, adapter)
                            .AsTask();
            }
            else
#endif
                return _backingStreamSocketListener
                            .BindServiceNameAsync(sn)
                            .AsTask();
        }
        /// <summary>
        ///     Binds the <code>TcpSocketListener</code> to the specified port on all endpoints and listens for TCP connections.
        /// </summary>
        /// <param name="port">The port to listen on. If '0', selection is delegated to the operating system.</param>
        /// <param name="listenOn">The <code>CommsInterface</code> to listen on. If unspecified, all interfaces will be bound.</param>
        /// <returns></returns>
        public Task StartListeningAsync(int port, ICommsInterface listenOn = null)
        {
            return Task.Run(() =>
            {
                if (listenOn != null && !listenOn.IsUsable)
                    throw new InvalidOperationException("Cannot listen on an unusable interface. Check the IsUsable property before attemping to bind.");

                var ipAddress = listenOn != null ? ((CommsInterface)listenOn).NativeIpAddress : IPAddress.Any;

                _listenCanceller = new CancellationTokenSource();

                _backingTcpListener = new TcpListener(ipAddress, port);

                try
                {
                    _backingTcpListener.Start();
                }
                catch(PlatformSocketException ex)
                {
                    throw new PclSocketException(ex);
                }

                WaitForConnections(_listenCanceller.Token);
            });
        }
Esempio n. 4
0
        /// <summary>
        ///     Binds the <code>UdpSocketReceiver</code> to the specified port on all endpoints and listens for UDP traffic.
        /// </summary>
        /// <param name="port">The port to listen on. If '0', selection is delegated to the operating system.</param>        
        /// <param name="listenOn">The <code>CommsInterface</code> to listen on. If unspecified, all interfaces will be bound.</param>
        /// <returns></returns>
        public Task StartListeningAsync(int port = 0, ICommsInterface listenOn = null)
        {
            if (listenOn != null && !listenOn.IsUsable)
                throw new InvalidOperationException("Cannot listen on an unusable interface. Check the IsUsable property before attemping to bind.");
            
            var sn = port == 0 ? "" : port.ToString();
#if !WP80    
            if (listenOn != null)
            {
                var adapter = ((CommsInterface) listenOn).NativeNetworkAdapter;
                return _backingDatagramSocket.BindServiceNameAsync(sn, adapter).AsTask();
            }
            else
#endif
                return _backingDatagramSocket.BindServiceNameAsync(sn).AsTask();
        }
Esempio n. 5
0
        /// <summary>
        ///     Binds the <code>UdpSocketServer</code> to the specified port on all endpoints and listens for UDP traffic.
        /// </summary>
        /// <param name="port">The port to listen on. If '0', selection is delegated to the operating system.</param>        
        /// <param name="listenOn">The <code>CommsInterface</code> to listen on. If unspecified, all interfaces will be bound.</param>
        /// <returns></returns>
        public Task StartListeningAsync(int port = 0, ICommsInterface listenOn = null)
        {
            if (listenOn != null && !listenOn.IsUsable)
                throw new InvalidOperationException("Cannot listen on an unusable interface. Check the IsUsable property before attemping to bind.");

            return Task.Run(() =>
            {
                var ip = listenOn != null ? ((CommsInterface)listenOn).NativeIpAddress : IPAddress.Any;
                var ep = new IPEndPoint(ip, port);

                _messageCanceller = new CancellationTokenSource();
                _backingUdpClient = new UdpClient(ep)
                {
                    EnableBroadcast = true
                };

                RunMessageReceiver(_messageCanceller.Token);
            });
        }
        /// <summary>
        ///     Joins the multicast group at the specified endpoint.
        /// </summary>
        /// <param name="multicastAddress">The address for the multicast group.</param>
        /// <param name="port">The port for the multicast group.</param>
        /// <param name="multicastOn">The <code>CommsInterface</code> to listen on. If unspecified, all interfaces will be bound.</param>
        /// <returns></returns>
        public async Task JoinMulticastGroupAsync(string multicastAddress, int port, ICommsInterface multicastOn = null)
        {
            if (multicastOn != null && !multicastOn.IsUsable)
                throw new InvalidOperationException("Cannot multicast on an unusable interface. Check the IsUsable property before attemping to connect.");

            var hn = new HostName(multicastAddress);
            var sn = port.ToString();

#if !WP80    
            if (multicastOn != null)
            {
                var adapter = ((CommsInterface)multicastOn).NativeNetworkAdapter;
                await _backingDatagramSocket.BindServiceNameAsync(sn, adapter);
            }
            else
#endif
            await _backingDatagramSocket.BindServiceNameAsync(sn);

            _backingDatagramSocket.Control.OutboundUnicastHopLimit = (byte) TTL;
            _backingDatagramSocket.JoinMulticastGroup(hn);

            _multicastAddress = multicastAddress;
            _multicastPort = port;
        }
Esempio n. 7
0
 /// <summary>
 ///     Binds the <code>UdpSocketReceiver</code> to the specified port on all endpoints and listens for UDP traffic.
 /// </summary>
 /// <param name="port">The port to listen on. If '0', selection is delegated to the operating system.</param>        
 /// <param name="listenOn">The <code>CommsInterface</code> to listen on. If unspecified, all interfaces will be bound.</param>
 /// <returns></returns>
 public Task StartListeningAsync(int port = 0, ICommsInterface listenOn = null)
 {
     throw new NotImplementedException(PCL.BaitWithoutSwitchMessage);
 }
 /// <summary>
 ///     Joins the multicast group at the specified endpoint.
 /// </summary>
 /// <param name="multicastAddress">The address for the multicast group.</param>
 /// <param name="port">The port for the multicast group.</param>
 /// <param name="multicastOn">The <code>CommsInterface</code> to multicast on. If unspecified, all interfaces will be bound.</param>
 /// <returns></returns>
 public Task JoinMulticastGroupAsync(string multicastAddress, int port, ICommsInterface multicastOn = null)
 {
     throw new NotImplementedException(PCL.BaitWithoutSwitchMessage);
 }