2
2
3
3
import com .google .common .base .Suppliers ;
4
4
import com .google .common .util .concurrent .ThreadFactoryBuilder ;
5
+ import com .mojang .logging .LogUtils ;
5
6
import dev .isxander .controlify .splitscreen .SocketConnectionMethod ;
6
7
import dev .isxander .controlify .splitscreen .protocol .ConnectionUtils ;
7
8
import dev .isxander .controlify .splitscreen .client .protocol .handshake .ControllerboundHandshakePacket ;
25
26
import net .minecraft .network .Connection ;
26
27
import net .minecraft .network .protocol .PacketFlow ;
27
28
import org .apache .commons .lang3 .Validate ;
29
+ import org .slf4j .Logger ;
28
30
29
31
import java .net .InetAddress ;
30
32
import java .util .function .Supplier ;
31
33
32
34
public class PawnConnectionListener {
35
+ private static final Logger LOGGER = LogUtils .getLogger ();
36
+
33
37
private static final Supplier <EpollEventLoopGroup > NETWORK_EPOLL_WORKER_GROUP = Suppliers .memoize (
34
38
() -> new EpollEventLoopGroup (2 , new ThreadFactoryBuilder ().setNameFormat ("Controlify Netty Epoll Client IO #%d" ).setDaemon (true ).build ())
35
39
);
@@ -42,21 +46,25 @@ public class PawnConnectionListener {
42
46
public PawnConnectionListener (Minecraft minecraft , SocketConnectionMethod connectionMethod ) {
43
47
this .controllerConnection = switch (connectionMethod ) {
44
48
case SocketConnectionMethod .TCP (int port ) -> connectToTcp (port , minecraft );
45
- case SocketConnectionMethod .Unix (String socketPath ) -> connectToSocket (socketPath , minecraft );
49
+ case SocketConnectionMethod .Unix (String socketPath ) -> connectToUnixSocket (socketPath , minecraft );
46
50
};
47
51
}
48
52
49
53
public Connection getControllerConnection () {
50
54
return controllerConnection ;
51
55
}
52
56
53
- private Connection connectToSocket (String socketPath , Minecraft minecraft ) {
57
+ private Connection connectToUnixSocket (String socketPath , Minecraft minecraft ) {
58
+ LOGGER .info ("Connecting to controller unix socket at {}" , socketPath );
59
+
54
60
return connect (minecraft , new Bootstrap ()
55
61
.channel (Epoll .isAvailable () ? EpollDomainSocketChannel .class : NioServerDomainSocketChannel .class )
56
62
.remoteAddress (new DomainSocketAddress (socketPath )));
57
63
}
58
64
59
65
private Connection connectToTcp (int port , Minecraft minecraft ) {
66
+ LOGGER .info ("Connecting to controller tcp port {}" , port );
67
+
60
68
return connect (minecraft , new Bootstrap ()
61
69
.channel (Epoll .isAvailable () ? EpollSocketChannel .class : NioSocketChannel .class )
62
70
.remoteAddress (InetAddress .getLoopbackAddress (), port ));
@@ -69,10 +77,20 @@ private Connection connect(Minecraft minecraft, Bootstrap bootstrap) {
69
77
70
78
bootstrap
71
79
.group (Epoll .isAvailable () ? NETWORK_EPOLL_WORKER_GROUP .get () : NETWORK_WORKER_GROUP .get ())
72
- .handler (new ChannelInitializer <DomainSocketChannel >() {
80
+ .handler (new ChannelInitializer <>() {
73
81
@ Override
74
- protected void initChannel (DomainSocketChannel ch ) {
75
- PawnConnectionListener .this .initChannel (ch , connection );
82
+ protected void initChannel (Channel ch ) {
83
+ try {
84
+ ch .config ().setOption (ChannelOption .TCP_NODELAY , true );
85
+ } catch (ChannelException ignored ) {
86
+ }
87
+
88
+ ChannelPipeline pipeline = ch .pipeline ()
89
+ .addLast ("timeout" , new ReadTimeoutHandler (5 ));
90
+ ConnectionUtils .configureSerialization (pipeline , PacketFlow .CLIENTBOUND , false , HandshakeProtocols .CONTROLLERBOUND );
91
+ connection .configurePacketHandler (pipeline );
92
+
93
+ LOGGER .info ("Established connection with controller" );
76
94
}
77
95
}).connect ().syncUninterruptibly ();
78
96
@@ -82,20 +100,9 @@ protected void initChannel(DomainSocketChannel ch) {
82
100
c .setupOutboundProtocol (PlayProtocols .CONTROLLERBOUND );
83
101
});
84
102
// will run after above since it is flushed above
85
- connection .send (new ControllerboundHelloPacket (Minecraft .getInstance ().getWindow ().getWindow ()));
103
+ // TODO: too early to access window handle, it hasn't been created yet, have to negotiate window later
104
+ connection .send (new ControllerboundHelloPacket (0L ));
86
105
87
106
return connection ;
88
107
}
89
-
90
- private void initChannel (Channel ch , Connection connection ) {
91
- try {
92
- ch .config ().setOption (ChannelOption .TCP_NODELAY , true );
93
- } catch (ChannelException ignored ) {
94
- }
95
-
96
- ChannelPipeline pipeline = ch .pipeline ()
97
- .addLast ("timeout" , new ReadTimeoutHandler (5 ));
98
- ConnectionUtils .configureSerialization (pipeline , PacketFlow .CLIENTBOUND , false , HandshakeProtocols .CONTROLLERBOUND );
99
- connection .configurePacketHandler (pipeline );
100
- }
101
108
}
0 commit comments