view src/org/eclipse/jetty/io/nio/ChannelEndPoint.java @ 1017:d2c3ff33387c

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 24 Oct 2016 01:56:08 -0600
parents 0e96ce3db20a
children 921c25a05eaa
line wrap: on
line source

//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.SocketChannel;

import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Channel End Point.
 * <p>Holds the channel and socket for an NIO endpoint.
 *
 */
public class ChannelEndPoint implements EndPoint
{
	private static final Logger LOG = LoggerFactory.getLogger(ChannelEndPoint.class);

	private final SocketChannel _channel;
	private final ByteBuffer[] _gather2 = new ByteBuffer[2];
	protected final Socket _socket;
	private final InetSocketAddress _local;
	private final InetSocketAddress _remote;
	protected volatile int _maxIdleTime;
	private volatile boolean _ishut = false;
	private volatile boolean _oshut = false;

	protected ChannelEndPoint(SocketChannel channel, int maxIdleTime) throws IOException
	{
		this._channel = channel;
		_maxIdleTime = maxIdleTime;
		_socket = channel.socket();
		_local = (InetSocketAddress)_socket.getLocalSocketAddress();
		_remote = (InetSocketAddress)_socket.getRemoteSocketAddress();
		_socket.setSoTimeout(_maxIdleTime);
	}

	@Override
	public final boolean isBlocking()
	{
		return _channel.isBlocking();
	}

	@Override
	public boolean blockReadable(long millisecs) throws IOException
	{
		return true;
	}

	@Override
	public boolean blockWritable(long millisecs) throws IOException
	{
		return true;
	}

	@Override
	public final boolean isOpen()
	{
		return _channel.isOpen();
	}

	@Override
	public final void shutdownInput() throws IOException
	{
		LOG.debug("ishut {}", this);
		_ishut = true;
		if (_channel.isOpen())
		{
			try
			{
				if (!_socket.isInputShutdown())
				{
					_socket.shutdownInput();
				}
			}
			catch (SocketException e)
			{
				LOG.debug(e.toString());
				LOG.trace("",e);
			}
			finally
			{
				if (_oshut)
				{
					close();
				}
			}
		}
	}

	@Override
	public final void shutdownOutput() throws IOException
	{
		LOG.debug("oshut {}",this);
		_oshut = true;
		if (_channel.isOpen())
		{
			try
			{
				if (!_socket.isOutputShutdown())
				{
					_socket.shutdownOutput();
				}
			}
			catch (SocketException e)
			{
				LOG.debug(e.toString());
				LOG.trace("",e);
			}
			finally
			{
				if (_ishut)
				{
					close();
				}
			}
		}
	}

	@Override
	public final boolean isOutputShutdown()
	{
		return _oshut || !_channel.isOpen() || _socket.isOutputShutdown();
	}

	@Override
	public final boolean isInputShutdown()
	{
		return _ishut || !_channel.isOpen() || _socket.isInputShutdown();
	}

	@Override
	public void close() throws IOException
	{
//Thread.dumpStack();
		LOG.debug("close {}",this);
		_channel.close();
	}

	@Override
	public int fill(Buffer buffer) throws IOException
	{
		if (_ishut)
			return -1;
		Buffer buf = buffer.buffer();
		int len = 0;
		if (buf instanceof NIOBuffer)
		{
			final NIOBuffer nbuf = (NIOBuffer)buf;
			final ByteBuffer bbuf = nbuf.getByteBuffer();

			//noinspection SynchronizationOnLocalVariableOrMethodParameter
			try
			{
				synchronized(bbuf)
				{
					try
					{
						bbuf.position(buffer.putIndex());
						len=_channel.read(bbuf);
					}
					finally
					{
						buffer.setPutIndex(bbuf.position());
						bbuf.position(0);
					}
				}

				if (len<0 && isOpen())
				{
					if (!isInputShutdown())
						shutdownInput();
					if (isOutputShutdown())
						_channel.close();
				}
			}
			catch (IOException x)
			{
				LOG.debug("Exception while filling", x);
				try
				{
					if (_channel.isOpen())
						_channel.close();
				}
				catch (Exception xx)
				{
					LOG.trace("",xx);
				}

				if (len>0)
					throw x;
				len=-1;
			}
		}
		else
		{
			throw new IOException("Not Implemented");
		}

		return len;
	}

	@Override
	public int flush(Buffer buffer) throws IOException
	{
		Buffer buf = buffer.buffer();
		int len=0;
		if (buf instanceof NIOBuffer)
		{
			final NIOBuffer nbuf = (NIOBuffer)buf;
			final ByteBuffer bbuf=nbuf.getByteBuffer().asReadOnlyBuffer();
			try
			{
				bbuf.position(buffer.getIndex());
				bbuf.limit(buffer.putIndex());
				len=_channel.write(bbuf);
			}
			finally
			{
				if (len>0)
					buffer.skip(len);
			}
		}
		else if (buffer.array()!=null)
		{
			ByteBuffer b = ByteBuffer.wrap(buffer.array(), buffer.getIndex(), buffer.length());
			len=_channel.write(b);
			if (len>0)
				buffer.skip(len);
		}
		else
		{
			throw new IOException("Not Implemented");
		}
		return len;
	}

	@Override
	public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
	{
		int length=0;

		Buffer buf0 = header==null?null:header.buffer();
		Buffer buf1 = buffer==null?null:buffer.buffer();

		if (_channel instanceof GatheringByteChannel &&
			header!=null && header.length()!=0 && buf0 instanceof NIOBuffer &&
			buffer!=null && buffer.length()!=0 && buf1 instanceof NIOBuffer)
		{
			length = gatheringFlush(header,((NIOBuffer)buf0).getByteBuffer(),buffer,((NIOBuffer)buf1).getByteBuffer());
		}
		else
		{
			// flush header
			if (header!=null && header.length()>0)
				length=flush(header);

			// flush buffer
			if ((header==null || header.length()==0) &&
				 buffer!=null && buffer.length()>0)
				length+=flush(buffer);

			// flush trailer
			if ((header==null || header.length()==0) &&
				(buffer==null || buffer.length()==0) &&
				 trailer!=null && trailer.length()>0)
				length+=flush(trailer);
		}

		return length;
	}

	private int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException
	{
		int length;

		synchronized(this)
		{
			// Adjust position indexs of buf0 and buf1
			bbuf0=bbuf0.asReadOnlyBuffer();
			bbuf0.position(header.getIndex());
			bbuf0.limit(header.putIndex());
			bbuf1=bbuf1.asReadOnlyBuffer();
			bbuf1.position(buffer.getIndex());
			bbuf1.limit(buffer.putIndex());

			_gather2[0]=bbuf0;
			_gather2[1]=bbuf1;

			// do the gathering write.
			length=(int)((GatheringByteChannel)_channel).write(_gather2);

			int hl=header.length();
			if (length>hl)
			{
				header.clear();
				buffer.skip(length-hl);
			}
			else if (length>0)
			{
				header.skip(length);
			}
		}
		return length;
	}

	public final SocketChannel getChannel()
	{
		return _channel;
	}

	@Override
	public final String getLocalAddr()
	{
	   if (_local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
		   return StringUtil.ALL_INTERFACES;
		return _local.getAddress().getHostAddress();
	}

	@Override
	public final String getLocalHost()
	{
	   if (_local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
		   return StringUtil.ALL_INTERFACES;
		return _local.getAddress().getCanonicalHostName();
	}

	@Override
	public final int getLocalPort()
	{
		return _local.getPort();
	}

	@Override
	public final String getRemoteAddr()
	{
		return _remote.getAddress().getHostAddress();
	}

	@Override
	public final String getRemoteHost()
	{
		return _remote.getAddress().getCanonicalHostName();
	}

	@Override
	public final int getRemotePort()
	{
		return _remote.getPort();
	}

	@Override
	public void flush()
		throws IOException
	{
	}

	@Override
	public final int getMaxIdleTime()
	{
		return _maxIdleTime;
	}

	@Override
	public void setMaxIdleTime(int timeMs) throws IOException
	{
		if (timeMs!=_maxIdleTime)
			_socket.setSoTimeout(timeMs>0?timeMs:0);
		_maxIdleTime=timeMs;
	}
}