Skip to content

Commit 711e1eb

Browse files
committed
Introduce io.vertx.core.net.Socket interface that abstracts the socket part of NetSocket which is a TCP socket
1 parent 570f26a commit 711e1eb

File tree

6 files changed

+573
-348
lines changed

6 files changed

+573
-348
lines changed

vertx-core/src/main/java/io/vertx/core/internal/net/NetSocketInternal.java

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -33,72 +33,16 @@
3333
*
3434
* @author <a href="mailto:[email protected]">Julien Viet</a>
3535
*/
36-
public interface NetSocketInternal extends NetSocket {
36+
public interface NetSocketInternal extends NetSocket, SocketInternal {
3737

3838
/**
3939
* Common reusable connection closed exception.
4040
*/
4141
VertxException CLOSED_EXCEPTION = new VertxException("Connection was closed", true);
4242

43-
/**
44-
* Returns the {@link ChannelHandlerContext} of the last handler (named {@code handler}) of the pipeline that
45-
* delivers message to the application with the {@link #handler(Handler)} and {@link #messageHandler(Handler)}.
46-
* <p/>
47-
* Handlers should be inserted in the pipeline using {@link io.netty.channel.ChannelPipeline#addBefore(String, String, ChannelHandler)}:
48-
* <p/>
49-
* <code><pre>
50-
* ChannelPipeline pipeline = so.channelHandlerContext().pipeline();
51-
* pipeline.addBefore("handler", "myhandler", new MyHandler());
52-
* </pre></code>
53-
* @return the channel handler context
54-
*/
55-
ChannelHandlerContext channelHandlerContext();
56-
57-
/**
58-
* Write a message in the channel pipeline.
59-
* <p/>
60-
* When a read operation is in progress, the flush operation is delayed until the read operation completes.
61-
*
62-
* Note: this handler does not take in account the eventually pending buffers
63-
*
64-
* @param message the message to write, it should be handled by one of the channel pipeline handlers
65-
* @return a future completed with the result
66-
*/
67-
Future<Void> writeMessage(Object message);
68-
69-
/**
70-
* Set a {@code handler} on this socket to process the messages produced by this socket. The message can be
71-
* {@link io.netty.buffer.ByteBuf} or other messages produced by channel pipeline handlers.
72-
* <p/>
73-
* The {@code} handler should take care of releasing pooled / direct messages.
74-
* <p/>
75-
* The handler replaces any {@link #handler(Handler)} previously set.
76-
*
77-
* @param handler the handler to set
78-
* @return a reference to this, so the API can be used fluently
79-
*/
80-
NetSocketInternal messageHandler(Handler<Object> handler);
81-
82-
/**
83-
* Set a {@code handler} on this socket to process the read complete event produced by this socket. This handler
84-
* is called when the socket has finished delivering message to the message handler. It should not be used
85-
* when it comes to buffers, since buffer delivery might be further buffered.
86-
* <p/>
87-
* The handler replaces any {@link #handler(Handler)} previously set.
88-
*
89-
* @param handler the handler to set
90-
* @return a reference to this, so the API can be used fluently
91-
*/
92-
NetSocketInternal readCompletionHandler(Handler<Void> handler);
93-
94-
/**
95-
* Set a handler to process pipeline user events.
96-
*
97-
* The handler should take care of releasing event, e.g calling {@code ReferenceCountUtil.release(evt)}.
98-
*
99-
* @param handler the handler to set
100-
* @return a reference to this, so the API can be used fluently
101-
*/
102-
NetSocketInternal eventHandler(Handler<Object> handler);
43+
@Override
44+
Future<Void> write(String str);
10345

46+
@Override
47+
Future<Void> write(String str, String enc);
10448
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2011-2019 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+
12+
package io.vertx.core.internal.net;
13+
14+
import io.netty.channel.ChannelHandler;
15+
import io.netty.channel.ChannelHandlerContext;
16+
import io.vertx.core.Future;
17+
import io.vertx.core.Handler;
18+
import io.vertx.core.VertxException;
19+
import io.vertx.core.net.NetSocket;
20+
import io.vertx.core.net.Socket;
21+
22+
/**
23+
* Extends to expose Netty interactions for reusing existing Netty codecs:
24+
*
25+
* @author <a href="mailto:[email protected]">Julien Viet</a>
26+
*/
27+
public interface SocketInternal extends Socket {
28+
29+
/**
30+
* Returns the {@link ChannelHandlerContext} of the last handler (named {@code handler}) of the pipeline that
31+
* delivers message to the application with the {@link #handler(Handler)} and {@link #messageHandler(Handler)}.
32+
* <p/>
33+
* Handlers should be inserted in the pipeline using {@link io.netty.channel.ChannelPipeline#addBefore(String, String, ChannelHandler)}:
34+
* <p/>
35+
* <code><pre>
36+
* ChannelPipeline pipeline = so.channelHandlerContext().pipeline();
37+
* pipeline.addBefore("handler", "myhandler", new MyHandler());
38+
* </pre></code>
39+
* @return the channel handler context
40+
*/
41+
ChannelHandlerContext channelHandlerContext();
42+
43+
/**
44+
* Write a message in the channel pipeline.
45+
* <p/>
46+
* When a read operation is in progress, the flush operation is delayed until the read operation completes.
47+
*
48+
* Note: this handler does not take in account the eventually pending buffers
49+
*
50+
* @param message the message to write, it should be handled by one of the channel pipeline handlers
51+
* @return a future completed with the result
52+
*/
53+
Future<Void> writeMessage(Object message);
54+
55+
/**
56+
* Set a {@code handler} on this socket to process the messages produced by this socket. The message can be
57+
* {@link io.netty.buffer.ByteBuf} or other messages produced by channel pipeline handlers.
58+
* <p/>
59+
* The {@code} handler should take care of releasing pooled / direct messages.
60+
* <p/>
61+
* The handler replaces any {@link #handler(Handler)} previously set.
62+
*
63+
* @param handler the handler to set
64+
* @return a reference to this, so the API can be used fluently
65+
*/
66+
SocketInternal messageHandler(Handler<Object> handler);
67+
68+
/**
69+
* Set a {@code handler} on this socket to process the read complete event produced by this socket. This handler
70+
* is called when the socket has finished delivering message to the message handler. It should not be used
71+
* when it comes to buffers, since buffer delivery might be further buffered.
72+
* <p/>
73+
* The handler replaces any {@link #handler(Handler)} previously set.
74+
*
75+
* @param handler the handler to set
76+
* @return a reference to this, so the API can be used fluently
77+
*/
78+
SocketInternal readCompletionHandler(Handler<Void> handler);
79+
80+
/**
81+
* Set a handler to process pipeline user events.
82+
*
83+
* The handler should take care of releasing event, e.g calling {@code ReferenceCountUtil.release(evt)}.
84+
*
85+
* @param handler the handler to set
86+
* @return a reference to this, so the API can be used fluently
87+
*/
88+
SocketInternal eventHandler(Handler<Object> handler);
89+
90+
}

vertx-core/src/main/java/io/vertx/core/net/NetSocket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* @author <a href="http://tfox.org">Tim Fox</a>
4343
*/
4444
@VertxGen
45-
public interface NetSocket extends ReadStream<Buffer>, WriteStream<Buffer> {
45+
public interface NetSocket extends Socket {
4646

4747
@Override
4848
NetSocket exceptionHandler(Handler<Throwable> handler);
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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

Comments
 (0)