一、概述
EndPoint作为jetty-io的一个重要组成部分,是基于javaNIO的封装,用于底层网络的读写,一旦网络读写准备好,会调用相应的connection的handle方法。
二、类分析
EndPoint源码如下:
/** * * 一个传输端点 * * <h3>异步方法</h3> */ public interface EndPoint extends Closeable { /* ------------------------------------------------------------ */ /** * @return <code>EndPoint</code>绑定的本地Inet地址,如果<code>EndPoint</code>没有网络连接则为<code>null</code> */ InetSocketAddress getLocalAddress(); /* ------------------------------------------------------------ */ /** * @return 上面方法的远程封装 */ InetSocketAddress getRemoteAddress(); /* ------------------------------------------------------------ */ boolean isOpen(); /* ------------------------------------------------------------ */ long getCreatedTimeStamp(); /* ------------------------------------------------------------ */ /** Shutdown the output. * <p>在该endpoint远程结束需要读取一次EOF时调用,意味着数据传输完毕了。可能在TCP/IP级别 * 或是协议交换时关闭。 * <p> * If the endpoint has {@link #isInputShutdown()} true, then this call has the same effect * as {@link #close()}. */ void shutdownOutput(); /* ------------------------------------------------------------ */ /** Test if output is shutdown. * The output is shutdown by a call to {@link #shutdownOutput()} * or {@link #close()}. * @return true if the output is shutdown or the endpoint is closed. */ boolean isOutputShutdown(); /* ------------------------------------------------------------ */ /** Test if the input is shutdown. * The input is shutdown if an EOF has been read while doing * a {@link #fill(ByteBuffer)}. Once the input is shutdown, all calls to * {@link #fill(ByteBuffer)} will return -1, until such time as the * end point is close, when they will return {@link EofException}. * @return True if the input is shutdown or the endpoint is closed. */ boolean isInputShutdown(); /** * Close any backing stream associated with the endpoint */ @Override void close(); /** * Fill the passed buffer with data from this endpoint. The bytes are appended to any * data already in the buffer by writing from the buffers limit up to it‘s capacity. * The limit is updated to include the filled bytes. * * @param buffer The buffer to fill. The position and limit are modified during the fill. After the * operation, the position is unchanged and the limit is increased to reflect the new data filled. * @return an <code>int</code> value indicating the number of bytes * filled or -1 if EOF is read or the input is shutdown. * @throws IOException if the endpoint is closed. */ int fill(ByteBuffer buffer) throws IOException; /** * Flush data from the passed header/buffer to this endpoint. As many bytes as can be consumed * are taken from the header/buffer position up until the buffer limit. The header/buffers position * is updated to indicate how many bytes have been consumed. * @param buffer the buffers to flush * @return True IFF all the buffers have been consumed and the endpoint has flushed the data to its * destination (ie is not buffering any data). * @throws IOException If the endpoint is closed or output is shutdown. */ boolean flush(ByteBuffer... buffer) throws IOException; /* ------------------------------------------------------------ */ /** * @return The underlying transport object (socket, channel, etc.) */ Object getTransport(); /* ------------------------------------------------------------ */ /** Get the max idle time in ms. * <p>The max idle time is the time the endpoint can be idle before * extraordinary handling takes place. * @return the max idle time in ms or if ms <= 0 implies an infinite timeout */ long getIdleTimeout(); /* ------------------------------------------------------------ */ /** Set the idle timeout. * @param idleTimeout the idle timeout in MS. Timeout <= 0 implies an infinite timeout */ void setIdleTimeout(long idleTimeout); /** * <p>Requests callback methods to be invoked when a call to {@link #fill(ByteBuffer)} would return data or EOF.</p> * * @param callback the callback to call when an error occurs or we are readable. * @throws ReadPendingException if another read operation is concurrent. */ void fillInterested(Callback callback) throws ReadPendingException; /** * @return whether {@link #fillInterested(Callback)} has been called, but {@link #fill(ByteBuffer)} has not yet * been called */ boolean isFillInterested(); /** * <p>Writes the given buffers via {@link #flush(ByteBuffer...)} and invokes callback methods when either * all the data has been flushed or an error occurs.</p> * * @param callback the callback to call when an error occurs or the write completed. * @param buffers one or more {@link ByteBuffer}s that will be flushed. * @throws WritePendingException if another write operation is concurrent. */ void write(Callback callback, ByteBuffer... buffers) throws WritePendingException; /** * @return the {@link Connection} associated with this {@link EndPoint} * @see #setConnection(Connection) */ Connection getConnection(); /** * @param connection the {@link Connection} associated with this {@link EndPoint} * @see #getConnection() * @see #upgrade(Connection) */ void setConnection(Connection connection); /** * <p>Callback method invoked when this {@link EndPoint} is opened.</p> * @see #onClose() */ void onOpen(); /** * <p>Callback method invoked when this {@link EndPoint} is close.</p> * @see #onOpen() */ void onClose(); /** Is the endpoint optimized for DirectBuffer usage * @return True if direct buffers can be used optimally. */ boolean isOptimizedForDirectBuffers(); /** Upgrade connections. * Close the old connection, update the endpoint and open the new connection. * If the oldConnection is an instance of {@link Connection.UpgradeFrom} then * a prefilled buffer is requested and passed to the newConnection if it is an instance * of {@link Connection.UpgradeTo} * @param newConnection The connection to upgrade to */ public void upgrade(Connection newConnection); }
EndPoint使用fill(Buffer buffer)方法进行数据写入,作为Connection的一个端点,用于存放内容信息。
时间: 2024-10-13 06:54:27