Beispiel #1
0
        public override string ToString()
        {
            if (_fd == null)
            {
                return("<closed>");
            }

            string s;

            if (_incoming && !_bound)
            {
                s = "local address = " + Network.AddrToString(_addr);
            }
            else if (_state == StateNotConnected)
            {
                s = "local address = " + Network.LocalAddrToString(Network.GetLocalAddress(_fd));
                if (_peerAddr != null)
                {
                    s += "\nremote address = " + Network.AddrToString(_peerAddr);
                }
            }
            else
            {
                s = Network.FdToString(_fd);
            }

            if (_mcastAddr != null)
            {
                s += "\nmulticast address = " + Network.AddrToString(_mcastAddr);
            }
            return(s);
        }
Beispiel #2
0
        public StreamSocket(TransportInstance instance, Socket fd)
        {
            _instance = instance;
            _fd       = fd;
            _state    = StateConnected;

            try
            {
                _desc = Network.FdToString(_fd);
            }
            catch (Exception)
            {
                Network.CloseSocketNoThrow(_fd);
                throw;
            }

            Network.SetBlock(_fd, false);
            Network.SetTcpBufSize(_fd, _instance);

            _readEventArgs            = new SocketAsyncEventArgs();
            _readEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            _writeEventArgs            = new SocketAsyncEventArgs();
            _writeEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            //
            // For timeouts to work properly, we need to receive/send
            // the data in several chunks. Otherwise, we would only be
            // notified when all the data is received/written. The
            // connection timeout could easily be triggered when
            // receiving/sending large messages.
            //
            _maxSendPacketSize = Math.Max(512, Network.GetSendBufferSize(_fd));
            _maxRecvPacketSize = Math.Max(512, Network.GetRecvBufferSize(_fd));
        }
Beispiel #3
0
        public int Connect(ref ArraySegment <byte> readBuffer, IList <ArraySegment <byte> > writeBuffer)
        {
            if (_state == StateNeedConnect)
            {
                _state = StateConnectPending;
                return(SocketOperation.Connect);
            }
            else if (_state <= StateConnectPending)
            {
                Debug.Assert(_writeEventArgs != null);
                if (_writeEventArgs.SocketError != SocketError.Success)
                {
                    var ex = new SocketException((int)_writeEventArgs.SocketError);
                    if (Network.ConnectionRefused(ex))
                    {
                        throw new ConnectionRefusedException(ex);
                    }
                    else
                    {
                        throw new ConnectFailedException(ex);
                    }
                }
                Debug.Assert(_fd != null);
                _desc  = Network.FdToString(_fd, _proxy, _addr);
                _state = _proxy != null ? StateProxyWrite : StateConnected;
            }

            if (_state == StateProxyWrite)
            {
                Debug.Assert(_proxy != null);
                Debug.Assert(_addr != null);
                _proxy.BeginWrite(_addr, writeBuffer);
                return(SocketOperation.Write);
            }
            else if (_state == StateProxyRead)
            {
                Debug.Assert(_proxy != null);
                readBuffer = _proxy.BeginRead();
                return(SocketOperation.Read);
            }
            else if (_state == StateProxyConnected)
            {
                Debug.Assert(_proxy != null);
                _proxy.Finish(readBuffer);

                // TODO Return buffers to the pool
                readBuffer = ArraySegment <byte> .Empty;
                writeBuffer.Clear();

                _state = StateConnected;
            }

            Debug.Assert(_state == StateConnected);
            return(SocketOperation.None);
        }
Beispiel #4
0
        public int connect(Buffer readBuffer, Buffer writeBuffer, ref bool moreData)
        {
            if (_state == StateNeedConnect)
            {
                _state = StateConnectPending;
                return(SocketOperation.Connect);
            }
            else if (_state <= StateConnectPending)
            {
                Debug.Assert(_writeEventArgs != null);
                if (_writeEventArgs.SocketError != SocketError.Success)
                {
                    SocketException ex = new SocketException((int)_writeEventArgs.SocketError);
                    if (Network.ConnectionRefused(ex))
                    {
                        throw new Ice.ConnectionRefusedException(ex);
                    }
                    else
                    {
                        throw new Ice.ConnectFailedException(ex);
                    }
                }
                Debug.Assert(_fd != null);
                _desc  = Network.FdToString(_fd, _proxy, _addr);
                _state = _proxy != null ? StateProxyWrite : StateConnected;
            }

            if (_state == StateProxyWrite)
            {
                Debug.Assert(_proxy != null);
                Debug.Assert(_addr != null);
                _proxy.BeginWrite(_addr, writeBuffer);
                return(SocketOperation.Write);
            }
            else if (_state == StateProxyRead)
            {
                Debug.Assert(_proxy != null);
                _proxy.BeginRead(readBuffer);
                return(SocketOperation.Read);
            }
            else if (_state == StateProxyConnected)
            {
                Debug.Assert(_proxy != null);
                _proxy.Finish(readBuffer, writeBuffer);

                readBuffer.Clear();
                writeBuffer.Clear();

                _state = StateConnected;
            }

            Debug.Assert(_state == StateConnected);
            return(SocketOperation.None);
        }