-
Notifications
You must be signed in to change notification settings - Fork 0
/
WallDeadMsg.java
63 lines (55 loc) · 1.37 KB
/
WallDeadMsg.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
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class WallDeadMsg implements Msg{
int msgType = Msg.WALL_DEAD_MSG;
TankClient tc;
Wall w;
public WallDeadMsg(TankClient tc){
this.tc=tc;
}
public WallDeadMsg(Wall w){
this.w=w;
}
@Override
public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeInt(msgType);
dos.writeInt(w.id);
} catch (IOException e) {
e.printStackTrace();
}
byte[] buf = baos.toByteArray();
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length,
new InetSocketAddress(IP, udpPort));
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void parse(DataInputStream dis) {
try {
int id = dis.readInt();
for (int i = 0; i < tc.walls.size(); i++) {
Wall w = tc.walls.get(i);
if (w.id == id) {
w.live=false;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}