17
17
* Created by Rolando on 12/21/2016.
18
18
*/
19
19
public class NetUtil {
20
- private static HashMap < String , byte []> buffers = new HashMap < String , byte []>() ;
21
- private static HashMap < String , Long > timestamps = new HashMap < String , Long >() ;
22
- private static boolean pingSent = false ;
20
+ private byte [] buffer = new byte [0 ] ;
21
+ private long timestamp = 0 ;
22
+ private boolean pingSent = false ;
23
23
24
- public static byte [] recv (Socket socket , String bufferId ) throws DisconnectedException , ReadTimeoutException {
24
+ public byte [] recv (Socket socket ) throws DisconnectedException , ReadTimeoutException {
25
25
try {
26
26
BufferedInputStream inStream = new BufferedInputStream (socket .getInputStream ());
27
- if (!buffers .containsKey (bufferId ))
28
- buffers .put (bufferId , new byte [0 ]);
29
- if (!timestamps .containsKey (bufferId ))
30
- timestamps .put (bufferId , System .currentTimeMillis ());
31
- while (!new String (buffers .get (bufferId )).contains (Codec .endDelimiter )) {
27
+ if (timestamp == 0 )
28
+ timestamp = System .currentTimeMillis ();
29
+ while (!new String (buffer ).contains (Codec .endDelimiter )) {
32
30
// Disconnected
33
- long time = System .currentTimeMillis () - timestamps . get ( bufferId ) ;
34
- if (time >= 10000 && ! Client . connect ( sockets . getIp (), false ) ) {
31
+ long time = System .currentTimeMillis () - timestamp ;
32
+ if (time >= 10000 ) {
35
33
Logger .debug ("Disconnected from server" );
36
34
clear ();
37
35
throw new DisconnectedException ();
38
36
}
39
37
if (time >= 5000 && !pingSent ) {
40
38
Logger .debug ("Sending PING command to server" );
41
39
sockets .sendCommand (Constants .COMMAND_PING );
42
- Client .connect (sockets .getIp (), false );
43
40
pingSent = true ;
44
41
}
45
42
// Timeout
46
43
if (inStream .available () < 2 )
47
44
throw new ReadTimeoutException ();
48
- timestamps . put ( bufferId , System .currentTimeMillis () );
45
+ timestamp = System .currentTimeMillis ();
49
46
// Read
50
47
byte [] read = new byte [100000 ];
51
48
int numRead = inStream .read (read );
52
49
read = Arrays .copyOfRange (read , 0 , numRead );
53
50
// Combine saved and new bytes
54
- byte [] newBytes = new byte [buffers . get ( bufferId ) .length + read .length ];
55
- System .arraycopy (buffers . get ( bufferId ) , 0 , newBytes , 0 , buffers . get ( bufferId ) .length );
56
- System .arraycopy (read , 0 , newBytes , buffers . get ( bufferId ) .length , read .length );
51
+ byte [] newBytes = new byte [buffer .length + read .length ];
52
+ System .arraycopy (buffer , 0 , newBytes , 0 , buffer .length );
53
+ System .arraycopy (read , 0 , newBytes , buffer .length , read .length );
57
54
// Save
58
- buffers . put ( bufferId , newBytes ) ;
55
+ buffer = newBytes ;
59
56
}
60
- int index = Bytes .indexOf (buffers .get (bufferId ), Codec .endDelimiter .getBytes ());
61
- byte [] packet = Arrays .copyOfRange (buffers .get (bufferId ), 0 , index );
62
- buffers .put (bufferId , Arrays .copyOfRange (buffers .get (bufferId ), index + Codec .endDelimiter .length (),
63
- buffers .get (bufferId ).length ));
57
+ int index = Bytes .indexOf (buffer , Codec .endDelimiter .getBytes ());
58
+ byte [] packet = Arrays .copyOfRange (buffer , 0 , index );
59
+ buffer = Arrays .copyOfRange (buffer , index + Codec .endDelimiter .length (), buffer .length );
64
60
return Codec .decode (packet );
65
61
}
66
62
catch (IOException e ) {
@@ -70,14 +66,14 @@ public static byte[] recv(Socket socket, String bufferId) throws DisconnectedExc
70
66
}
71
67
}
72
68
73
- static void clear () {
74
- timestamps . clear () ;
75
- buffers . clear () ;
69
+ private void clear () {
70
+ timestamp = 0 ;
71
+ buffer = new byte [ 0 ] ;
76
72
pingSent = false ;
77
73
}
78
74
79
- public static void resetTimeout () {
80
- timestamps . clear () ;
75
+ public void resetTimeout () {
76
+ timestamp = 0 ;
81
77
pingSent = false ;
82
78
}
83
79
0 commit comments