-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
KryoClient.java
177 lines (148 loc) · 4.96 KB
/
KryoClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package io.anuke.kryonet;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectSet;
import com.esotericsoftware.kryonet.*;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.net.Address;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.Net.ClientProvider;
import io.anuke.mindustry.net.Net.SendMode;
import io.anuke.mindustry.net.Packets.Connect;
import io.anuke.mindustry.net.Packets.Disconnect;
import io.anuke.mindustry.net.Registrator;
import io.anuke.ucore.UCore;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;
public class KryoClient implements ClientProvider{
Client client;
public KryoClient(){
client = new Client();
client.setDiscoveryHandler(new ClientDiscoveryHandler() {
@Override
public DatagramPacket onRequestNewDatagramPacket() {
return new DatagramPacket(new byte[4], 4);
}
@Override
public void onDiscoveredHost(DatagramPacket datagramPacket) {
//TODO doesn't send data
UCore.log("DATA HOST FOUND: " + Arrays.toString(datagramPacket.getData()));
}
@Override
public void onFinally() {
}
});
client.addListener(new Listener(){
@Override
public void connected (Connection connection) {
Connect c = new Connect();
c.id = connection.getID();
c.addressTCP = connection.getRemoteAddressTCP().toString();
try{
Net.handleClientReceived(c);
}catch (Exception e){
Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
}
}
@Override
public void disconnected (Connection connection) {
Disconnect c = new Disconnect();
try{
Net.handleClientReceived(c);
}catch (Exception e){
Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
}
}
@Override
public void received (Connection connection, Object object) {
if(object instanceof FrameworkMessage) return;
try{
Net.handleClientReceived(object);
}catch (Exception e){
if(e instanceof KryoNetException && e.getMessage() != null && e.getMessage().toLowerCase().contains("incorrect")) {
UCore.log("Mismatch!");
}else{
Gdx.app.postRunnable(() -> {
throw new RuntimeException(e);
});
}
}
}
});
register(Registrator.getClasses());
}
@Override
public void connect(String ip, int port) throws IOException {
//just in case
client.stop();
Thread updateThread = new Thread(() -> {
try{
client.run();
}catch (Exception e){
handleException(e);
}
}, "Kryonet Client");
updateThread.setDaemon(true);
updateThread.start();
client.connect(5000, ip, port, port);
}
@Override
public void disconnect() {
client.close();
}
@Override
public void send(Object object, SendMode mode) {
if(mode == SendMode.tcp){
client.sendTCP(object);
}else{
client.sendUDP(object);
}
}
@Override
public void updatePing() {
client.updateReturnTripTime();
}
@Override
public int getPing() {
return client.getReturnTripTime();
}
@Override
public Array<Address> discover(){
List<InetAddress> list = client.discoverHosts(Vars.port, 3000);
ObjectSet<String> hostnames = new ObjectSet<>();
Array<Address> result = new Array<>();
for(InetAddress a : list){
if(!hostnames.contains(a.getHostName()))
result.add(new Address(a.getCanonicalHostName(), a.getHostAddress()));
hostnames.add(a.getHostName());
}
return result;
}
@Override
public void register(Class<?>... types) {
for(Class<?> c : types){
client.getKryo().register(c);
}
KryoRegistrator.register(client.getKryo());
}
@Override
public void dispose(){
try {
client.dispose();
}catch (IOException e){
throw new RuntimeException(e);
}
}
private void handleException(Exception e){
e.printStackTrace();
if(e instanceof KryoNetException){
Gdx.app.postRunnable(() -> Vars.ui.showError("$text.server.mismatch"));
}else{
//TODO better exception handling.
disconnect();
}
}
}