Example #1
0
        /// <summary>
        /// Return a
        /// <see cref="SocketInputWrapper"/>
        /// for the socket and set the given
        /// timeout. If the socket does not have an associated channel, then its socket
        /// timeout will be set to the specified value. Otherwise, a
        /// <see cref="SocketInputStream"/>
        /// will be created which reads with the configured
        /// timeout.
        /// Any socket created using socket factories returned by
        /// <see cref="NetUtils()"/>
        /// ,
        /// must use this interface instead of
        /// <see cref="System.Net.Sockets.Socket.GetInputStream()"/>
        /// .
        /// In general, this should be called only once on each socket: see the note
        /// in
        /// <see cref="SocketInputWrapper.SetTimeout(long)"/>
        /// for more information.
        /// </summary>
        /// <seealso cref="System.Net.Sockets.Socket.GetChannel()"/>
        /// <param name="socket"/>
        /// <param name="timeout">
        /// timeout in milliseconds. zero for waiting as
        /// long as necessary.
        /// </param>
        /// <returns>SocketInputWrapper for reading from the socket.</returns>
        /// <exception cref="System.IO.IOException"/>
        public static SocketInputWrapper GetInputStream(Socket socket, long timeout)
        {
            InputStream stm = (socket.GetChannel() == null) ? socket.GetInputStream() : new SocketInputStream
                                  (socket);
            SocketInputWrapper w = new SocketInputWrapper(socket, stm);

            w.SetTimeout(timeout);
            return(w);
        }
Example #2
0
        /// <exception cref="System.IO.IOException"/>
        private void AssertReadTimeout(SocketInputWrapper stm, int timeoutMillis)
        {
            long st = Runtime.NanoTime();

            try
            {
                stm.Read();
                NUnit.Framework.Assert.Fail("Didn't time out");
            }
            catch (SocketTimeoutException)
            {
                AssertTimeSince(st, timeoutMillis);
            }
        }
Example #3
0
        /// <exception cref="System.IO.IOException"/>
        private void DoSocketReadTimeoutTest(bool withChannel)
        {
            // Binding a ServerSocket is enough to accept connections.
            // Rely on the backlog to accept for us.
            Socket ss = Extensions.CreateServerSocket(0);
            Socket s;

            if (withChannel)
            {
                s = NetUtils.GetDefaultSocketFactory(new Configuration()).CreateSocket();
                Assume.AssumeNotNull(s.GetChannel());
            }
            else
            {
                s = new Socket();
                NUnit.Framework.Assert.IsNull(s.GetChannel());
            }
            SocketInputWrapper stm = null;

            try
            {
                NetUtils.Connect(s, ss.LocalEndPoint, 1000);
                stm = NetUtils.GetInputStream(s, 1000);
                AssertReadTimeout(stm, 1000);
                // Change timeout, make sure it applies.
                stm.SetTimeout(1);
                AssertReadTimeout(stm, 1);
                // If there is a channel, then setting the socket timeout
                // should not matter. If there is not a channel, it will
                // take effect.
                s.ReceiveTimeout = 1000;
                if (withChannel)
                {
                    AssertReadTimeout(stm, 1);
                }
                else
                {
                    AssertReadTimeout(stm, 1000);
                }
            }
            finally
            {
                IOUtils.CloseStream(stm);
                IOUtils.CloseSocket(s);
                ss.Close();
            }
        }