26
26
import org .elasticsearch .transport .TcpTransport ;
27
27
import org .elasticsearch .transport .nio .AcceptingSelector ;
28
28
import org .elasticsearch .transport .nio .SocketSelector ;
29
- import org .elasticsearch .transport .nio .TcpReadHandler ;
30
29
31
30
import java .io .Closeable ;
32
31
import java .io .IOException ;
39
38
40
39
public class ChannelFactory {
41
40
42
- private final TcpReadHandler handler ;
41
+ private final Consumer < NioSocketChannel > contextSetter ;
43
42
private final RawChannelFactory rawChannelFactory ;
44
43
45
- public ChannelFactory (TcpTransport .ProfileSettings profileSettings , TcpReadHandler handler ) {
46
- this (new RawChannelFactory (profileSettings ), handler );
44
+ public ChannelFactory (TcpTransport .ProfileSettings profileSettings , Consumer <NioSocketChannel > contextSetter ) {
45
+ this (new RawChannelFactory (profileSettings .tcpNoDelay ,
46
+ profileSettings .tcpKeepAlive ,
47
+ profileSettings .reuseAddress ,
48
+ Math .toIntExact (profileSettings .sendBufferSize .getBytes ()),
49
+ Math .toIntExact (profileSettings .receiveBufferSize .getBytes ())), contextSetter );
47
50
}
48
51
49
- ChannelFactory (RawChannelFactory rawChannelFactory , TcpReadHandler handler ) {
50
- this .handler = handler ;
52
+ ChannelFactory (RawChannelFactory rawChannelFactory , Consumer < NioSocketChannel > contextSetter ) {
53
+ this .contextSetter = contextSetter ;
51
54
this .rawChannelFactory = rawChannelFactory ;
52
55
}
53
56
54
57
public NioSocketChannel openNioChannel (InetSocketAddress remoteAddress , SocketSelector selector ,
55
58
Consumer <NioChannel > closeListener ) throws IOException {
56
59
SocketChannel rawChannel = rawChannelFactory .openNioChannel (remoteAddress );
57
60
NioSocketChannel channel = new NioSocketChannel (NioChannel .CLIENT , rawChannel , selector );
58
- channel . setContexts ( new TcpReadContext ( channel , handler ), new TcpWriteContext ( channel ) );
61
+ contextSetter . accept ( channel );
59
62
channel .getCloseFuture ().addListener (ActionListener .wrap (closeListener ::accept , (e ) -> closeListener .accept (channel )));
60
63
scheduleChannel (channel , selector );
61
64
return channel ;
@@ -65,7 +68,7 @@ public NioSocketChannel acceptNioChannel(NioServerSocketChannel serverChannel, S
65
68
Consumer <NioChannel > closeListener ) throws IOException {
66
69
SocketChannel rawChannel = rawChannelFactory .acceptNioChannel (serverChannel );
67
70
NioSocketChannel channel = new NioSocketChannel (serverChannel .getProfile (), rawChannel , selector );
68
- channel . setContexts ( new TcpReadContext ( channel , handler ), new TcpWriteContext ( channel ) );
71
+ contextSetter . accept ( channel );
69
72
channel .getCloseFuture ().addListener (ActionListener .wrap (closeListener ::accept , (e ) -> closeListener .accept (channel )));
70
73
scheduleChannel (channel , selector );
71
74
return channel ;
@@ -105,12 +108,13 @@ static class RawChannelFactory {
105
108
private final int tcpSendBufferSize ;
106
109
private final int tcpReceiveBufferSize ;
107
110
108
- RawChannelFactory (TcpTransport .ProfileSettings profileSettings ) {
109
- tcpNoDelay = profileSettings .tcpNoDelay ;
110
- tcpKeepAlive = profileSettings .tcpKeepAlive ;
111
- tcpReusedAddress = profileSettings .reuseAddress ;
112
- tcpSendBufferSize = Math .toIntExact (profileSettings .sendBufferSize .getBytes ());
113
- tcpReceiveBufferSize = Math .toIntExact (profileSettings .receiveBufferSize .getBytes ());
111
+ RawChannelFactory (boolean tcpNoDelay , boolean tcpKeepAlive , boolean tcpReusedAddress , int tcpSendBufferSize ,
112
+ int tcpReceiveBufferSize ) {
113
+ this .tcpNoDelay = tcpNoDelay ;
114
+ this .tcpKeepAlive = tcpKeepAlive ;
115
+ this .tcpReusedAddress = tcpReusedAddress ;
116
+ this .tcpSendBufferSize = tcpSendBufferSize ;
117
+ this .tcpReceiveBufferSize = tcpReceiveBufferSize ;
114
118
}
115
119
116
120
SocketChannel openNioChannel (InetSocketAddress remoteAddress ) throws IOException {
0 commit comments