forked from JnCrMx/discord-game-sdk4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkExample.java
211 lines (177 loc) · 5.45 KB
/
NetworkExample.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import de.jcm.discordgamesdk.Core;
import de.jcm.discordgamesdk.CreateParams;
import de.jcm.discordgamesdk.DiscordEventAdapter;
import de.jcm.discordgamesdk.lobby.*;
import de.jcm.discordgamesdk.user.DiscordUser;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class NetworkExample extends DiscordEventAdapter
{
private CreateParams createParams;
private Core core;
private DiscordUser currentUser;
private Lobby lobby;
private long myPeerId;
private String myRoute;
private List<Long> openConnections = new ArrayList<>();
public NetworkExample() throws InterruptedException
{
createParams = new CreateParams();
createParams.setClientID(698611073133051974L);
createParams.registerEventHandler(this);
core = new Core(createParams);
int i = 0;
for(long l = 0; ; l = (l + 1) % 100)
{
core.runCallbacks();
Thread.sleep(16);
if(l == 0)
{
for(long peer : openConnections)
{
core.networkManager().sendMessage(peer, (byte) 1,
(i+":"+System.currentTimeMillis()).getBytes(StandardCharsets.UTF_8));
}
core.networkManager().flush();
i++;
}
}
}
@Override
public void onMessage(long peerId, byte channelId, byte[] data)
{
long receiveTime = System.currentTimeMillis();
System.out.println("Message from "+Long.toUnsignedString(peerId)+" on channel "+channelId);
String msg = new String(data, StandardCharsets.UTF_8);
String[] parts = msg.split(":");
String index = parts[0];
long sendTime = Long.parseLong(parts[1]);
long ping = receiveTime - sendTime;
System.out.println(index+": latency = "+ping);
}
@Override
public void onRouteUpdate(String routeData)
{
myRoute = routeData;
if(lobby != null)
{
LobbyMemberTransaction txn = core.lobbyManager().getMemberUpdateTransaction(lobby, currentUser.getUserId());
txn.setMetadata("route", routeData);
core.lobbyManager().updateMember(lobby, currentUser.getUserId(), txn, result -> {
System.out.println(core.lobbyManager().getMemberMetadata(lobby, currentUser.getUserId()));
});
}
}
@Override
public void onMemberUpdate(long lobbyId, long userId)
{
if(userId == currentUser.getUserId())
return;
Map<String, String> metadata = core.lobbyManager().getMemberMetadata(lobby, userId);
if(metadata.containsKey("peer_id") && metadata.containsKey("route"))
{
long peerId = Long.parseUnsignedLong(metadata.get("peer_id"));
String route = metadata.get("route");
if(openConnections.contains(userId))
{
System.out.println("Updating route for " + Long.toUnsignedString(peerId) + ": " + route);
core.networkManager().updatePeer(peerId, route);
}
else
{
System.out.println("Connecting to " + Long.toUnsignedString(peerId) + " over " + route);
core.networkManager().openPeer(peerId, route);
System.out.println("Opening reliable channel 1");
core.networkManager().openChannel(peerId, (byte) 1, true);
openConnections.add(peerId);
}
}
}
private void initNetwork()
{
myPeerId = core.networkManager().getPeerId();
LobbyMemberTransaction txn = core.lobbyManager().getMemberUpdateTransaction(lobby, currentUser.getUserId());
txn.setMetadata("peer_id", Long.toUnsignedString(myPeerId));
if(myRoute != null)
{
txn.setMetadata("route", myRoute);
}
core.lobbyManager().updateMember(lobby, currentUser.getUserId(), txn, result -> {
System.out.println(core.lobbyManager().getMemberMetadata(lobby, currentUser.getUserId()));
});
List<Long> users = core.lobbyManager().getMemberUserIds(lobby);
for(Long userId : users)
{
if(userId == currentUser.getUserId())
continue;
Map<String, String> metadata = core.lobbyManager().getMemberMetadata(lobby, userId);
if(metadata.containsKey("peer_id") && metadata.containsKey("route"))
{
long peerId = Long.parseUnsignedLong(metadata.get("peer_id"));
String route = metadata.get("route");
System.out.println("Connecting to "+Long.toUnsignedString(peerId)+" over "+route);
core.networkManager().openPeer(peerId, route);
System.out.println("Opening reliable channel 1");
core.networkManager().openChannel(peerId, (byte) 1, true);
openConnections.add(peerId);
}
}
}
private void letsGo()
{
LobbySearchQuery query = core.lobbyManager().getSearchQuery();
core.lobbyManager().search(query, r->{
if(core.lobbyManager().lobbyCount() == 0)
{
LobbyTransaction txn = core.lobbyManager().getLobbyCreateTransaction();
txn.setType(LobbyType.PUBLIC);
txn.setCapacity(10);
core.lobbyManager().createLobby(txn, lobby -> {
this.lobby = lobby;
initNetwork();
});
}
else
{
Lobby lobby = core.lobbyManager().getLobbies().get(0);
core.lobbyManager().connectLobby(lobby.getId(), lobby.getSecret(), (result, lobby1) -> {
this.lobby = lobby;
initNetwork();
});
}
});
}
@Override
public void onCurrentUserUpdate()
{
boolean ready = this.currentUser == null;
this.currentUser = core.userManager().getCurrentUser();
if(ready)
{
letsGo();
}
}
public static void main(String[] args) throws IOException
{
File discordLibrary = DownloadNativeLibrary.downloadDiscordLibrary();
if(discordLibrary == null)
{
System.err.println("Error downloading Discord SDK.");
System.exit(-1);
}
// Initialize the Core
Core.init(discordLibrary);
try
{
new NetworkExample();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}