Inheritance: java.nio.channels.spi.AbstractSelectableChannel, ByteChannel, ScatteringByteChannel, GatheringByteChannel
Exemplo n.º 1
0
        public void Listen_internal(int backlog, out int error)
        {
            error = 0;

            if (jSocket == null || !jSocket.isBound())
            {
                error = 10022;                 //WSAEINVAL (Invalid argument)
                return;
            }

            if (jSocket.isConnected() || jSocketChannel.isConnectionPending())
            {
                error = 10056;                 //WSAEISCONN (Socket is already connected)
                return;
            }

            bool blockMode = jSocketChannel.isBlocking();
            bool reuseAddr = jSocket.getReuseAddress();

            try
            {
                jSocket.close();
            }
            catch (Exception e)
            {
#if DEBUG
                Console.WriteLine("Caught exception during Listen_internal close old jSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
            }

            try
            {
                jSocketChannel.close();
            }
            catch (Exception e)
            {
#if DEBUG
                Console.WriteLine("Caught exception during Listen_internal close old jSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
            }

            jSocket        = null;
            jSocketChannel = null;

            try
            {
                jServerSocketChannel = java.nio.channels.ServerSocketChannel.open();
                jServerSocket        = jServerSocketChannel.socket();
                jServerSocket.bind(jTempLocalSocketAddress, backlog);
                jServerSocketChannel.configureBlocking(blockMode);
                jServerSocket.setReuseAddress(reuseAddr);
            }
            catch (Exception e)
            {
                error = 10048;                 //WSAEADDRINUSE (Address already in use)
#if DEBUG
                Console.WriteLine("Caught exception during Listen_internal create server socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
            }
        }
Exemplo n.º 2
0
        public void Close_internal(out int error)
        {
            error = 0;

            if (jServerSocket != null)
            {
                try
                {
                    jServerSocket.close();
                }
                catch (Exception e)
                {
                    error = 10022;                     //WSAEINVAL (Invalid argument)
#if DEBUG
                    Console.WriteLine("Caught exception during Close_internal jServerSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
                }
                try
                {
                    jServerSocketChannel.close();
                }
                catch (Exception e)
                {
                    error = 10022;                     //WSAEINVAL (Invalid argument)
#if DEBUG
                    Console.WriteLine("Caught exception during Close_internal jServerSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
                }
                jServerSocket        = null;
                jServerSocketChannel = null;
            }
            else if (jSocket != null)
            {
                try
                {
                    jSocket.close();
                }
                catch (Exception e)
                {
                    error = 10022;                     //WSAEINVAL (Invalid argument)
#if DEBUG
                    Console.WriteLine("Caught exception during Close_internal jSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
                }
                try
                {
                    jSocketChannel.close();
                }
                catch (Exception e)
                {
                    error = 10022;                     //WSAEINVAL (Invalid argument)
#if DEBUG
                    Console.WriteLine("Caught exception during Close_internal jSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
                }
                jSocket        = null;
                jSocketChannel = null;
            }
        }
Exemplo n.º 3
0
        public GHSocket Accept_internal(out int error)
        {
            error = 0;

            if (jServerSocket == null)
            {
                throw new InvalidOperationException("You must call Bind and Listen before calling Accept.");
            }

            try
            {
                /*
                 *      If this channel is in non-blocking mode then this method will immediately
                 *      return null if there are no pending connections.
                 *      Otherwise it will block indefinitely until a new connection is
                 *      available or an I/O error occurs.
                 */
                java.nio.channels.SocketChannel acceptedSocket = jServerSocketChannel.accept();
                if (acceptedSocket == null)
                {
                    error = 10035;                     //WSAEWOULDBLOCK (Resource temporarily unavailable)
#if DEBUG
                    Console.WriteLine("The Accept_internal is in non-blocking mode and no pending connections are available");
#endif
                    return(null);
                }

                return(new GHStreamSocket(acceptedSocket));
            }
            catch (AsynchronousCloseException) {
                error = 10004;
            }
            catch (Exception e)
            {
                error = 10061;                 //WSAECONNREFUSED (Connection refused)
#if DEBUG
                Console.WriteLine("Caught exception during Accept_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
            }

            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Opens a socket channel and connects it to a remote address.
        ///
        /// <para> This convenience method works as if by invoking the <seealso cref="#open()"/>
        /// method, invoking the <seealso cref="#connect(SocketAddress) connect"/> method upon
        /// the resulting socket channel, passing it <tt>remote</tt>, and then
        /// returning that channel.  </para>
        /// </summary>
        /// <param name="remote">
        ///         The remote address to which the new channel is to be connected
        /// </param>
        /// <returns>  A new, and connected, socket channel
        /// </returns>
        /// <exception cref="AsynchronousCloseException">
        ///          If another thread closes this channel
        ///          while the connect operation is in progress
        /// </exception>
        /// <exception cref="ClosedByInterruptException">
        ///          If another thread interrupts the current thread
        ///          while the connect operation is in progress, thereby
        ///          closing the channel and setting the current thread's
        ///          interrupt status
        /// </exception>
        /// <exception cref="UnresolvedAddressException">
        ///          If the given remote address is not fully resolved
        /// </exception>
        /// <exception cref="UnsupportedAddressTypeException">
        ///          If the type of the given remote address is not supported
        /// </exception>
        /// <exception cref="SecurityException">
        ///          If a security manager has been installed
        ///          and it does not permit access to the given remote endpoint
        /// </exception>
        /// <exception cref="IOException">
        ///          If some other I/O error occurs </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static SocketChannel open(java.net.SocketAddress remote) throws java.io.IOException
        public static SocketChannel Open(SocketAddress remote)
        {
            SocketChannel sc = Open();

            try
            {
                sc.Connect(remote);
            }
            catch (Throwable x)
            {
                try
                {
                    sc.Close();
                }
                catch (Throwable suppressed)
                {
                    x.AddSuppressed(suppressed);
                }
                throw x;
            }
            Debug.Assert(sc.Connected);
            return(sc);
        }
Exemplo n.º 5
0
		public void Listen_internal(int backlog, out int error)
		{
			error = 0;

			if (jSocket == null || !jSocket.isBound())
			{
				error = 10022; //WSAEINVAL (Invalid argument)
				return;
			}

			if (jSocket.isConnected() || jSocketChannel.isConnectionPending())
			{
				error = 10056; //WSAEISCONN (Socket is already connected)
				return;
			}

			bool blockMode = jSocketChannel.isBlocking();
			bool reuseAddr = jSocket.getReuseAddress();

			try
			{
				jSocket.close();
			}
			catch (Exception e)
			{
#if DEBUG
				Console.WriteLine("Caught exception during Listen_internal close old jSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
			}

			try
			{
				jSocketChannel.close();
			}
			catch (Exception e)
			{
#if DEBUG
				Console.WriteLine("Caught exception during Listen_internal close old jSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
			}

			jSocket = null;
			jSocketChannel = null;

			try
			{
				jServerSocketChannel = java.nio.channels.ServerSocketChannel.open();
				jServerSocket = jServerSocketChannel.socket();
				jServerSocket.bind(jTempLocalSocketAddress, backlog);
				jServerSocketChannel.configureBlocking(blockMode);
				jServerSocket.setReuseAddress(reuseAddr);
			}
			catch (Exception e)
			{
				error = 10048; //WSAEADDRINUSE (Address already in use)
#if DEBUG
				Console.WriteLine("Caught exception during Listen_internal create server socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
			}
		}
Exemplo n.º 6
0
		public GHStreamSocket(java.nio.channels.SocketChannel socketChannel)
		{
			jSocketChannel = socketChannel;
			jSocket = jSocketChannel.socket();
		}
Exemplo n.º 7
0
		public void Close_internal(out int error)
		{
			error = 0;

			if (jServerSocket != null)
			{
				try
				{
					jServerSocket.close();
				}
				catch (Exception e)
				{
					error = 10022; //WSAEINVAL (Invalid argument)
#if DEBUG
					Console.WriteLine("Caught exception during Close_internal jServerSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
				}
				try
				{
					jServerSocketChannel.close();
				}
				catch (Exception e)
				{
					error = 10022; //WSAEINVAL (Invalid argument)
#if DEBUG
					Console.WriteLine("Caught exception during Close_internal jServerSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
				}
				jServerSocket = null;
				jServerSocketChannel = null;
			}
			else if (jSocket != null)
			{
				try
				{
					jSocket.close();
				}
				catch (Exception e)
				{
					error = 10022; //WSAEINVAL (Invalid argument)
#if DEBUG
					Console.WriteLine("Caught exception during Close_internal jSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
				}
				try
				{
					jSocketChannel.close();
				}
				catch (Exception e)
				{
					error = 10022; //WSAEINVAL (Invalid argument)
#if DEBUG
					Console.WriteLine("Caught exception during Close_internal jSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
				}
				jSocket = null;
				jSocketChannel = null;
			}
		}
Exemplo n.º 8
0
		public GHStreamSocket()
		{
			jSocketChannel = java.nio.channels.SocketChannel.open();
			jSocket = jSocketChannel.socket();
		}
Exemplo n.º 9
0
 public GHStreamSocket(java.nio.channels.SocketChannel socketChannel)
 {
     jSocketChannel = socketChannel;
     jSocket        = jSocketChannel.socket();
 }
Exemplo n.º 10
0
 public GHStreamSocket()
 {
     jSocketChannel = java.nio.channels.SocketChannel.open();
     jSocket        = jSocketChannel.socket();
 }