Example #1
0
        /// <summary>Starts an asynchronous write operation of the buffer data to the socket
        /// starting at the given offset, completed is set to true if the write operation
        /// account for the remaining of the buffer data or false otherwise, returns whenever
        /// the asynchronous operation completed synchronously or not.</summary>
        /// <param name="buffer">The data to write to the socket as a list of byte array segments.</param>
        /// <param name="offset">The zero based byte offset into the buffer at what start writing.</param>
        /// <param name="callback">The asynchronous completion callback.</param>
        /// <param name="state">A state object that is associated with the asynchronous operation.</param>
        /// <param name="completed">True if the write operation accounts for the buffer remaining data, from
        /// offset to the end of the buffer.</param>
        /// <returns>True if the asynchronous operation completed synchronously otherwise false.</returns>
        public bool StartWrite(IList <ArraySegment <byte> > buffer, int offset, AsyncCallback callback, object state,
                               out bool completed)
        {
            Debug.Assert(_fd != null && _writeEventArgs != null);
            if (_state == StateConnectPending)
            {
                completed      = false;
                _writeCallback = callback;
                try
                {
                    if (_sourceAddr != null)
                    {
                        Network.DoBind(_fd, new IPEndPoint(_sourceAddr, 0));
                    }

                    EndPoint?addr = _proxy != null?_proxy.GetAddress() : _addr;

                    Debug.Assert(addr != null);

                    _writeEventArgs.RemoteEndPoint = addr;
                    _writeEventArgs.UserToken      = state;
                    return(!_fd.ConnectAsync(_writeEventArgs));
                }
                catch (Exception ex)
                {
                    throw new TransportException(ex);
                }
            }

            try
            {
                int count     = buffer.GetByteCount();
                int remaining = count - offset;
                buffer.FillSegments(offset, _sendSegments, Math.Min(remaining, _maxSendPacketSize));
                _writeCallback             = callback;
                _writeEventArgs.UserToken  = state;
                _writeEventArgs.BufferList = _sendSegments;
                bool completedSynchronously = !_fd.SendAsync(_writeEventArgs);
                completed = _maxSendPacketSize >= remaining;
                return(completedSynchronously);
            }
            catch (SocketException ex)
            {
                if (Network.ConnectionLost(ex))
                {
                    throw new ConnectionLostException(ex);
                }
                throw new Ice.TransportException(ex);
            }
            catch (ObjectDisposedException ex)
            {
                throw new ConnectionLostException(ex);
            }
        }
Example #2
0
 public virtual Endpoint Listen()
 {
     try
     {
         Debug.Assert(_fd != null);
         _addr = Network.DoBind(_fd, _addr);
         Network.DoListen(_fd, _backlog);
     }
     catch (SystemException)
     {
         _fd = null;
         throw;
     }
     _endpoint = _endpoint.GetEndpoint(this);
     return(_endpoint);
 }
Example #3
0
        public Endpoint 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);
        }