-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerListener.java
101 lines (86 loc) · 2.95 KB
/
ServerListener.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
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
public class ServerListener extends Thread
{
private DataInputStream in;
private volatile boolean stopRun;
private Player player;
/**
* initializes
*
* @param socket
* socket to use
* @param player
* player to send msgs to
*/
public ServerListener( Socket socket, Player player )
{
try
{
in = new DataInputStream( socket.getInputStream() );
}
catch ( IOException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
this.player = player;
}
/**
* constantly checks for messages and sends them to player
*/
public void run()
{
while ( !stopRun )
{
try
{
String serverInput = in.readUTF();
int serverAuth = Integer.parseInt( serverInput.substring( 0, 1 ) );
if ( serverAuth == 4 )
{
if ( player.isInteger( serverInput.substring( 0, 1 ) )
&& serverInput.substring( 1, 2 ).equals( "t" ) )// when
// is
// your
// turn.
// Allows
// player
// to
// take
// a
// turn
// while
// server
// is
// updating
// info
{
player.checkInput = true;
player.input = serverInput;
}
else
{
player.takeInput( serverInput.substring( 1 ) );
}
}
else
{
}
}
catch ( IOException e )
{
System.out.println( "Server connection lost" );
System.exit( 0 );
}
}
}
/**
* ends thread
*/
public void end()
{
stopRun = true;
}
}