55import java .net .Socket ;
66import java .util .LinkedList ;
77import java .util .List ;
8+ import java .util .Timer ;
9+ import java .util .TimerTask ;
810
911class 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