Example #1
0
        public EndpointI bind()
        {
#if !SILVERLIGHT
            if (Network.isMulticast((IPEndPoint)_addr))
            {
                Network.setReuseAddress(_fd, true);
                _mcastAddr = (IPEndPoint)_addr;
                if (AssemblyUtil.platform_ == AssemblyUtil.Platform.Windows)
                {
                    //
                    // Windows does not allow binding to the mcast address itself
                    // so we bind to INADDR_ANY (0.0.0.0) instead. As a result,
                    // bi-directional connection won't work because the source
                    // address won't the multicast address and the client will
                    // therefore reject the datagram.
                    //
                    if (_addr.AddressFamily == AddressFamily.InterNetwork)
                    {
                        _addr = new IPEndPoint(IPAddress.Any, _port);
                    }
                    else
                    {
                        _addr = new IPEndPoint(IPAddress.IPv6Any, _port);
                    }
                }
                _addr = Network.doBind(_fd, _addr);
                if (_port == 0)
                {
                    _mcastAddr.Port = ((IPEndPoint)_addr).Port;
                }
                Network.setMcastGroup(_fd, _mcastAddr.Address, _mcastInterface);
            }
            else
            {
                if (AssemblyUtil.platform_ != AssemblyUtil.Platform.Windows)
                {
                    //
                    // Enable SO_REUSEADDR on Unix platforms to allow re-using
                    // the socket even if it's in the TIME_WAIT state. On
                    // Windows, this doesn't appear to be necessary and
                    // enabling SO_REUSEADDR would actually not be a good
                    // thing since it allows a second process to bind to an
                    // address even it's already bound by another process.
                    //
                    // TODO: using SO_EXCLUSIVEADDRUSE on Windows would
                    // probably be better but it's only supported by recent
                    // Windows versions (XP SP2, Windows Server 2003).
                    //
                    Network.setReuseAddress(_fd, true);
                }
                _addr = Network.doBind(_fd, _addr);
            }
#endif
            _bound    = true;
            _endpoint = _endpoint.endpoint(this);
            return(_endpoint);
        }
Example #2
0
        public bool startWrite(Buffer buf, AsyncCallback callback, object state, out bool completed)
        {
            Debug.Assert(_fd != null && _writeEventArgs != null);
            if (_state == StateConnectPending)
            {
                completed      = false;
                _writeCallback = callback;
                try
                {
                    EndPoint addr = _proxy != null?_proxy.getAddress() : _addr;

                    if (_sourceAddr != null)
                    {
                        Network.doBind(_fd, _sourceAddr);
                    }
                    _writeEventArgs.RemoteEndPoint = addr;
                    _writeEventArgs.UserToken      = state;
                    return(!_fd.ConnectAsync(_writeEventArgs));
                }
                catch (Exception ex)
                {
                    throw new Ice.SocketException(ex);
                }
            }

            int packetSize = getSendPacketSize(buf.b.remaining());

            try
            {
                _writeCallback            = callback;
                _writeEventArgs.UserToken = state;
                _writeEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), packetSize);
                bool completedSynchronously = !_fd.SendAsync(_writeEventArgs);
                completed = packetSize == buf.b.remaining();
                return(completedSynchronously);
            }
            catch (SocketException ex)
            {
                if (Network.connectionLost(ex))
                {
                    throw new Ice.ConnectionLostException(ex);
                }
                throw new Ice.SocketException(ex);
            }
            catch (ObjectDisposedException ex)
            {
                throw new Ice.ConnectionLostException(ex);
            }
        }
Example #3
0
 public virtual EndpointI listen()
 {
     try
     {
         _addr = Network.doBind(_fd, _addr);
         Network.doListen(_fd, _backlog);
     }
     catch (SystemException)
     {
         _fd = null;
         throw;
     }
     _endpoint = _endpoint.endpoint(this);
     return(_endpoint);
 }
Example #4
0
        public EndpointI bind()
        {
            Debug.Assert(_fd != null);
            if (Network.isMulticast((IPEndPoint)_addr))
            {
                Network.setReuseAddress(_fd, true);
                _mcastAddr = (IPEndPoint)_addr;
                if (AssemblyUtil.isWindows)
                {
                    //
                    // Windows does not allow binding to the mcast address itself
                    // so we bind to INADDR_ANY (0.0.0.0) instead. As a result,
                    // bi-directional connection won't work because the source
                    // address won't the multicast address and the client will
                    // therefore reject the datagram.
                    //
                    if (_addr.AddressFamily == AddressFamily.InterNetwork)
                    {
                        _addr = new IPEndPoint(IPAddress.Any, _port);
                    }
                    else
                    {
                        _addr = new IPEndPoint(IPAddress.IPv6Any, _port);
                    }
                }

                _addr = Network.doBind(_fd, _addr);
                if (_port == 0)
                {
                    _mcastAddr.Port = ((IPEndPoint)_addr).Port;
                }
                Debug.Assert(_mcastInterface != null);
                Network.setMcastGroup(_fd, _mcastAddr.Address, _mcastInterface);
            }
            else
            {
                _addr = Network.doBind(_fd, _addr);
            }
            _bound = true;
            Debug.Assert(_endpoint != null);
            _endpoint = _endpoint.endpoint(this);
            return(_endpoint);
        }
Example #5
0
        //
        // Only for use by UdpEndpoint.
        //
        internal UdpTransceiver(Instance instance, string host, int port, string mcastInterface, bool connect)
        {
            _traceLevels = instance.traceLevels();
            _logger      = instance.initializationData().logger;
            _stats       = instance.initializationData().stats;
            _state       = connect ? StateNeedConnect : StateNotConnected;
            _incoming    = true;

            try
            {
                _addr = Network.getAddressForServer(host, port, instance.protocolSupport(), instance.preferIPv6());

#if ICE_SOCKET_ASYNC_API
                _readEventArgs = new SocketAsyncEventArgs();
                _readEventArgs.RemoteEndPoint = _addr;
                _readEventArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(ioCompleted);

                _writeEventArgs = new SocketAsyncEventArgs();
                _writeEventArgs.RemoteEndPoint = _addr;
                _writeEventArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(ioCompleted);
#endif

                _fd = Network.createServerSocket(true, _addr.AddressFamily, instance.protocolSupport());
                setBufSize(instance);
#if !SILVERLIGHT
                Network.setBlock(_fd, false);
#endif
                if (_traceLevels.network >= 2)
                {
                    string s = "attempting to bind to udp socket " + Network.addrToString(_addr);
                    _logger.trace(_traceLevels.networkCat, s);
                }

#if !SILVERLIGHT
                if (Network.isMulticast((IPEndPoint)_addr))
                {
                    Network.setReuseAddress(_fd, true);
                    _mcastAddr = (IPEndPoint)_addr;
                    if (AssemblyUtil.platform_ == AssemblyUtil.Platform.Windows)
                    {
                        //
                        // Windows does not allow binding to the mcast address itself
                        // so we bind to INADDR_ANY (0.0.0.0) instead. As a result,
                        // bi-directional connection won't work because the source
                        // address won't the multicast address and the client will
                        // therefore reject the datagram.
                        //
                        if (_addr.AddressFamily == AddressFamily.InterNetwork)
                        {
                            _addr = new IPEndPoint(IPAddress.Any, port);
                        }
                        else
                        {
                            _addr = new IPEndPoint(IPAddress.IPv6Any, port);
                        }
                    }
                    _addr = Network.doBind(_fd, _addr);
                    if (port == 0)
                    {
                        _mcastAddr.Port = ((IPEndPoint)_addr).Port;
                    }
                    Network.setMcastGroup(_fd, _mcastAddr.Address, mcastInterface);
                }
                else
                {
                    if (AssemblyUtil.platform_ != AssemblyUtil.Platform.Windows)
                    {
                        //
                        // Enable SO_REUSEADDR on Unix platforms to allow
                        // re-using the socket even if it's in the TIME_WAIT
                        // state. On Windows, this doesn't appear to be
                        // necessary and enabling SO_REUSEADDR would actually
                        // not be a good thing since it allows a second
                        // process to bind to an address even it's already
                        // bound by another process.
                        //
                        // TODO: using SO_EXCLUSIVEADDRUSE on Windows would
                        // probably be better but it's only supported by recent
                        // Windows versions (XP SP2, Windows Server 2003).
                        //
                        Network.setReuseAddress(_fd, true);
                    }
                    _addr = Network.doBind(_fd, _addr);
                }
#endif
                if (_traceLevels.network >= 1)
                {
                    StringBuilder s = new StringBuilder("starting to receive udp packets\n");
                    s.Append(ToString());
                    List <string> interfaces = Network.getHostsForEndpointExpand(
                        Network.endpointAddressToString(_addr), instance.protocolSupport(), true);
                    if (interfaces.Count != 0)
                    {
                        s.Append("\nlocal interfaces: ");
                        s.Append(String.Join(", ", interfaces.ToArray()));
                    }
                    _logger.trace(_traceLevels.networkCat, s.ToString());
                }
            }
            catch (Ice.LocalException)
            {
                _fd = null;
                throw;
            }
        }