|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | + * which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | + */ |
| 11 | +package io.vertx.core.net; |
| 12 | + |
| 13 | +import io.vertx.codegen.annotations.Fluent; |
| 14 | +import io.vertx.codegen.annotations.Nullable; |
| 15 | +import io.vertx.codegen.annotations.VertxGen; |
| 16 | +import io.vertx.core.Future; |
| 17 | +import io.vertx.core.Handler; |
| 18 | +import io.vertx.core.buffer.Buffer; |
| 19 | +import io.vertx.core.streams.ReadStream; |
| 20 | +import io.vertx.core.streams.WriteStream; |
| 21 | + |
| 22 | +/** |
| 23 | + * Represents a socket-like interface on either the client or the server side. |
| 24 | + * <p> |
| 25 | + * It implements both {@link ReadStream} and {@link WriteStream} so it can be used with |
| 26 | + * {@link io.vertx.core.streams.Pipe} to pipe data with flow control. |
| 27 | + * |
| 28 | + * @author <a href="mailto:[email protected]">Julien Viet</a> |
| 29 | + */ |
| 30 | +@VertxGen(concrete = false) |
| 31 | +public interface Socket extends ReadStream<Buffer>, WriteStream<Buffer> { |
| 32 | + |
| 33 | + @Override |
| 34 | + Socket exceptionHandler(Handler<Throwable> handler); |
| 35 | + |
| 36 | + @Override |
| 37 | + Socket handler(Handler<Buffer> handler); |
| 38 | + |
| 39 | + @Override |
| 40 | + Socket pause(); |
| 41 | + |
| 42 | + @Override |
| 43 | + Socket resume(); |
| 44 | + |
| 45 | + @Override |
| 46 | + Socket fetch(long amount); |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritDoc} |
| 50 | + * <p> |
| 51 | + * This handler might be called after the close handler when the socket is paused and there are still |
| 52 | + * buffers to deliver. |
| 53 | + */ |
| 54 | + @Override |
| 55 | + Socket endHandler(Handler<Void> endHandler); |
| 56 | + |
| 57 | + @Override |
| 58 | + Socket setWriteQueueMaxSize(int maxSize); |
| 59 | + |
| 60 | + @Override |
| 61 | + Socket drainHandler(Handler<Void> handler); |
| 62 | + |
| 63 | + /** |
| 64 | + * Write a {@link String} to the connection, encoded in UTF-8. |
| 65 | + * |
| 66 | + * @param str the string to write |
| 67 | + * @return a future result of the write |
| 68 | + */ |
| 69 | + Future<Void> write(String str); |
| 70 | + |
| 71 | + /** |
| 72 | + * Write a {@link String} to the connection, encoded using the encoding {@code enc}. |
| 73 | + * |
| 74 | + * @param str the string to write |
| 75 | + * @param enc the encoding to use |
| 76 | + * @return a future completed with the result |
| 77 | + */ |
| 78 | + Future<Void> write(String str, String enc); |
| 79 | + |
| 80 | + /** |
| 81 | + * Tell the operating system to stream a file as specified by {@code filename} directly from disk to the outgoing connection, |
| 82 | + * bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to stream files. |
| 83 | + * |
| 84 | + * @param filename file name of the file to send |
| 85 | + * @return a future result of the send operation |
| 86 | + */ |
| 87 | + default Future<Void> sendFile(String filename) { |
| 88 | + return sendFile(filename, 0, Long.MAX_VALUE); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Tell the operating system to stream a file as specified by {@code filename} directly from disk to the outgoing connection, |
| 93 | + * bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to stream files. |
| 94 | + * |
| 95 | + * @param filename file name of the file to send |
| 96 | + * @param offset offset |
| 97 | + * @return a future result of the send operation |
| 98 | + */ |
| 99 | + default Future<Void> sendFile(String filename, long offset) { |
| 100 | + return sendFile(filename, offset, Long.MAX_VALUE); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Tell the operating system to stream a file as specified by {@code filename} directly from disk to the outgoing connection, |
| 105 | + * bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to stream files. |
| 106 | + * |
| 107 | + * @param filename file name of the file to send |
| 108 | + * @param offset offset |
| 109 | + * @param length length |
| 110 | + * @return a future result of the send operation |
| 111 | + */ |
| 112 | + Future<Void> sendFile(String filename, long offset, long length); |
| 113 | + |
| 114 | + /** |
| 115 | + * Calls {@link #close()} |
| 116 | + * |
| 117 | + * @return a future completed with the result |
| 118 | + */ |
| 119 | + @Override |
| 120 | + Future<Void> end(); |
| 121 | + |
| 122 | + /** |
| 123 | + * Close the socket |
| 124 | + * |
| 125 | + * @return a future completed with the result |
| 126 | + */ |
| 127 | + Future<Void> close(); |
| 128 | + |
| 129 | + /** |
| 130 | + * Set a {@code handler} notified when the socket is closed |
| 131 | + * |
| 132 | + * @param handler the handler |
| 133 | + * @return a reference to this, so the API can be used fluently |
| 134 | + */ |
| 135 | + @Fluent |
| 136 | + Socket closeHandler(@Nullable Handler<Void> handler); |
| 137 | + |
| 138 | + /** |
| 139 | + * Set a {@code handler} notified when the socket is shutdown: the client or server will close the connection |
| 140 | + * within a certain amount of time. This gives the opportunity to the {@code handler} to close the socket gracefully before |
| 141 | + * the socket is closed. |
| 142 | + * |
| 143 | + * @param handler the handler notified |
| 144 | + * @return a reference to this, so the API can be used fluently |
| 145 | + */ |
| 146 | + @Fluent |
| 147 | + Socket shutdownHandler(@Nullable Handler<Void> handler); |
| 148 | + |
| 149 | +} |
0 commit comments