-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathInputStreamSenderTest.java
More file actions
67 lines (55 loc) · 1.93 KB
/
Copy pathInputStreamSenderTest.java
File metadata and controls
67 lines (55 loc) · 1.93 KB
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
package com.esotericsoftware.kryonet;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import com.esotericsoftware.kryonet.util.InputStreamSender;
public class InputStreamSenderTest extends KryoNetTestCase {
boolean success;
public void testStream () throws IOException {
final int largeDataSize = 12345;
final Server server = new Server(16384, 8192);
server.getKryo().setRegistrationRequired(false);
startEndPoint(server);
server.bind(tcpPort, udpPort);
server.addListener(new Listener() {
public void connected (Connection connection) {
ByteArrayOutputStream output = new ByteArrayOutputStream(largeDataSize);
for (int i = 0; i < largeDataSize; i++)
output.write(i);
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
// Send data in 512 byte chunks.
connection.addListener(new InputStreamSender(input, 512) {
protected void start () {
// Normally would send an object so the receiving side knows how to handle the chunks we are about to send.
System.out.println("starting");
}
protected Object next (byte[] bytes) {
System.out.println("sending " + bytes.length);
return bytes; // Normally would wrap the byte[] with an object so the receiving side knows how to handle it.
}
});
}
});
// ----
final Client client = new Client(16384, 8192);
client.getKryo().setRegistrationRequired(false);
startEndPoint(client);
client.addListener(new Listener() {
int total;
public void received (Connection connection, Object object) {
if (object instanceof byte[]) {
int length = ((byte[])object).length;
System.out.println("received " + length);
total += length;
if (total == largeDataSize) {
success = true;
stopEndPoints();
}
}
}
});
client.connect(5000, host, tcpPort, udpPort);
waitForThreads(5000);
if (!success) fail();
}
}