Skip to content

Commit 6af95d3

Browse files
committed
updated InputStreamThread.java and OutputStreamThread.java
1 parent b3af35e commit 6af95d3

File tree

2 files changed

+125
-85
lines changed

2 files changed

+125
-85
lines changed

src/de/javasocketapi/core/InputStreamThread.java

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
import java.io.InputStream;
55
import java.lang.reflect.InvocationTargetException;
66
import java.net.Socket;
7+
import java.util.Timer;
8+
import java.util.TimerTask;
79
import java.util.UUID;
810

911
class InputStreamThread extends Thread {
1012

1113
private Client client;
1214
private Socket socket;
15+
private Timer timer;
16+
17+
{
18+
this.timer = new Timer();
19+
}
1320

1421
public InputStreamThread(final Client client) {
1522
this.client = client;
@@ -19,46 +26,63 @@ public InputStreamThread(final Client client) {
1926
@Override
2027
public void run() {
2128
super.run();
29+
//initialise inputStream
30+
InputStream inputStream = null;
2231
try {
23-
//initialise inputStream
24-
InputStream inputStream = this.socket.getInputStream();
25-
//read byte arrays
26-
byte[] bytes;
27-
while (true) {
28-
if (this.socket.isClosed()) {
29-
//interrupt thread
30-
interrupt();
31-
break;
32-
}
33-
int b = inputStream.read();
34-
if (b == -1) {
35-
//close socket
36-
this.socket.close();
37-
continue;
38-
}
39-
bytes = new byte[b];
40-
//receive bytes
41-
inputStream.read(bytes, 0, b);
42-
ReadingByteBuffer readingByteBuffer = new ReadingByteBuffer(bytes);
43-
//read packetId
44-
int packetId = readingByteBuffer.readInt();
45-
//check if packet is UpdateUUIDPacket
46-
if (packetId == -2) {
47-
//read connectionUUID
48-
UUID connectionUUID = readingByteBuffer.readUUID();
49-
//set updated connectionUUID
50-
this.client.getConnectionUUID().set(connectionUUID);
51-
} else {
52-
//get packet
53-
Class<? extends Packet> packet = PacketRegistry.get(packetId);
54-
//read connectionUUID
55-
UUID connectionUUID = readingByteBuffer.readUUID();
56-
//initialise packet
57-
packet.getConstructor(UUID.class).newInstance(connectionUUID).recieve(readingByteBuffer);
58-
}
59-
}
60-
} catch (IOException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
32+
inputStream = this.socket.getInputStream();
33+
} catch (IOException e) {
6134
e.printStackTrace();
6235
}
36+
final byte[][] bytes = new byte[1][1];
37+
//start reading byte arrays
38+
InputStream finalInputStream = inputStream;
39+
this.timer.scheduleAtFixedRate(new TimerTask() {
40+
@Override
41+
public void run() {
42+
try {
43+
if (socket.isClosed()) {
44+
//interrupt thread
45+
interrupt();
46+
return;
47+
}
48+
//check if finalInputStream is null
49+
assert finalInputStream != null;
50+
int b = finalInputStream.read();
51+
if (b != -1) {
52+
bytes[0] = new byte[b];
53+
//receive bytes
54+
finalInputStream.read(bytes[0], 0, b);
55+
ReadingByteBuffer readingByteBuffer = new ReadingByteBuffer(bytes[0]);
56+
//read packetId
57+
int packetId = readingByteBuffer.readInt();
58+
//check if packet is UpdateUUIDPacket
59+
if (packetId == -2) {
60+
//read connectionUUID
61+
UUID connectionUUID = readingByteBuffer.readUUID();
62+
//set updated connectionUUID
63+
client.getConnectionUUID().set(connectionUUID);
64+
} else {
65+
//get packet
66+
Class<? extends Packet> packet = PacketRegistry.get(packetId);
67+
//read connectionUUID
68+
UUID connectionUUID = readingByteBuffer.readUUID();
69+
//initialise packet
70+
packet.getConstructor(UUID.class).newInstance(connectionUUID).recieve(readingByteBuffer);
71+
}
72+
} else {
73+
//close socket
74+
socket.close();
75+
}
76+
} catch (IOException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
77+
e.printStackTrace();
78+
}
79+
}
80+
}, 0, 1);
81+
}
82+
83+
@Override
84+
public void interrupt() {
85+
super.interrupt();
86+
this.timer.cancel();
6387
}
6488
}

src/de/javasocketapi/core/OutputStreamThread.java

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
import java.net.Socket;
66
import java.util.LinkedList;
77
import java.util.List;
8+
import java.util.Timer;
9+
import java.util.TimerTask;
810

911
class OutputStreamThread extends Thread {
1012

1113
private Client client;
1214
private Socket socket;
1315
private List<Packet> packets;
16+
private Timer timer;
1417

1518
{
1619
this.packets = new LinkedList<>();
20+
this.timer = new Timer();
1721
}
1822

1923
public OutputStreamThread(final Client client) {
@@ -24,58 +28,70 @@ public OutputStreamThread(final Client client) {
2428
@Override
2529
public void run() {
2630
super.run();
31+
//initialise outputStream
32+
OutputStream outputStream = null;
2733
try {
28-
//initialise outputStream
29-
OutputStream outputStream = this.socket.getOutputStream();
30-
//send byte arrays
31-
while (true) {
32-
if (this.socket.isClosed()) {
33-
//interrupt thread
34-
interrupt();
35-
break;
36-
}
37-
if (this.packets.isEmpty()) {
38-
//skip when no packets available to send
39-
continue;
40-
}
41-
//get next packet available to send
42-
Packet packet = null;
34+
outputStream = this.socket.getOutputStream();
35+
} catch (IOException e) {
36+
e.printStackTrace();
37+
}
38+
OutputStream finalOutputStream = outputStream;
39+
//start sending send byte arrays
40+
this.timer.scheduleAtFixedRate(new TimerTask() {
41+
@Override
42+
public void run() {
4343
try {
44-
packet = this.packets.get(0);
45-
} catch (NullPointerException ignored) {
46-
}
47-
//check if packet is valid
48-
if (packet != null) {
49-
//remove packet
50-
this.packets.remove(0);
51-
WritingByteBuffer writingByteBuffer = new WritingByteBuffer();
52-
//check if packet is UpdateUUIDPacket
53-
if (packet.getClass().equals(UpdateUUIDPacket.class)) {
54-
writingByteBuffer.writeInt(-2);
55-
writingByteBuffer.writeUUID(packet.getConnectionUUID());
56-
} else {
57-
//get packetId
58-
int packetId = PacketRegistry.indexOf(packet.getClass());
59-
//write packetId
60-
writingByteBuffer.writeInt(packetId);
61-
//write connectionUuid
62-
writingByteBuffer.writeUUID(this.client.getConnectionUUID().get());
63-
//initialise packet
64-
packet.send(writingByteBuffer);
44+
if (socket.isClosed()) {
45+
//interrupt thread
46+
interrupt();
47+
return;
6548
}
66-
//receive bytes
67-
byte[] bytes = writingByteBuffer.toBytes();
68-
//write bytes length
69-
outputStream.write(bytes.length);
70-
//write bytes
71-
outputStream.write(bytes);
72-
//flush outputStream
73-
outputStream.flush();
49+
//skip when no packets available to send
50+
if (!packets.isEmpty()) {
51+
//get next packet available to send
52+
Packet packet = packets.get(0);
53+
//check if packet is valid
54+
if (packet != null) {
55+
//remove packet
56+
packets.remove(0);
57+
WritingByteBuffer writingByteBuffer = new WritingByteBuffer();
58+
//check if packet is UpdateUUIDPacket
59+
if (packet.getClass().equals(UpdateUUIDPacket.class)) {
60+
writingByteBuffer.writeInt(-2);
61+
writingByteBuffer.writeUUID(packet.getConnectionUUID());
62+
} else {
63+
//get packetId
64+
int packetId = PacketRegistry.indexOf(packet.getClass());
65+
//write packetId
66+
writingByteBuffer.writeInt(packetId);
67+
//write connectionUuid
68+
writingByteBuffer.writeUUID(client.getConnectionUUID().get());
69+
//initialise packet
70+
packet.send(writingByteBuffer);
71+
}
72+
//receive bytes
73+
byte[] bytes = writingByteBuffer.toBytes();
74+
//check if outputstream is null
75+
assert finalOutputStream != null;
76+
//write bytes length
77+
finalOutputStream.write(bytes.length);
78+
//write bytes
79+
finalOutputStream.write(bytes);
80+
//flush outputStream
81+
finalOutputStream.flush();
82+
}
83+
}
84+
} catch (IOException e) {
85+
e.printStackTrace();
7486
}
7587
}
76-
} catch (IOException e) {
77-
e.printStackTrace();
78-
}
88+
}, 0, 1);
89+
}
90+
91+
@Override
92+
public void interrupt() {
93+
super.interrupt();
94+
this.timer.cancel();
7995
}
8096

8197
public void send(Packet packet) {

0 commit comments

Comments
 (0)