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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
package ru.olamedia.olacraft.network;
import java.io.IOException;
import java.net.BindException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import ru.olamedia.liveEntity.LiveEntity;
import ru.olamedia.olacraft.game.Game;
import ru.olamedia.olacraft.game.SpawnLocation;
import ru.olamedia.olacraft.network.discovery.DiscoveryThread;
import ru.olamedia.olacraft.network.packet.ConnectionPacket;
import ru.olamedia.olacraft.network.packet.ConnectionRequestPacket;
import ru.olamedia.olacraft.network.packet.GetRegionPacket;
import ru.olamedia.olacraft.network.packet.IPacket;
import ru.olamedia.olacraft.network.packet.LiveEntityLocationUpdatePacket;
import ru.olamedia.olacraft.network.packet.RegionDataPacket;
import ru.olamedia.olacraft.network.packet.SpawnPacket;
import ru.olamedia.olacraft.network.packet.SpawnRequestPacket;
import ru.olamedia.olacraft.network.packet.WorldInfoPacket;
import ru.olamedia.olacraft.scene.GameScene;
import ru.olamedia.olacraft.world.data.RegionData;
import ru.olamedia.olacraft.world.dataProvider.CachedChunkDataProvider;
import ru.olamedia.olacraft.world.dataProvider.LocalChunkDataProvider;
import ru.olamedia.olacraft.world.provider.WorldProvider;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
public class GameServer {
private WorldProvider worldProvider;
private ExecutorService threadPool = Executors.newFixedThreadPool(1);
public static Server server = new Server(30 * 1024 * 1024, 1024 * 1024) {
@Override
protected PlayerConnection newConnection() {
// By providing our own connection implementation, we can store per
// connection state without a connection ID to state look up.
return new PlayerConnection();
}
};
private boolean isServerRunning = false;
private GameScene scene;
private static boolean DEBUG = true;
private void debug(String s) {
if (DEBUG) {
System.out.println("[GameServer] " + s);
}
}
public GameServer() {
// INIT WORLD
worldProvider = new WorldProvider();
// worldProvider.setChunkDataProvider(new
// LocalChunkDataProvider(worldProvider.getInfo().name));
worldProvider.setChunkDataProvider(new CachedChunkDataProvider(new LocalChunkDataProvider(worldProvider
.getInfo().name)));
// CREATE SCENE
scene = new GameScene(worldProvider);
// worldProvider.getInfo().name = "world";
Kryo kryo = server.getKryo();
Network.registerPackets(kryo);
server.addListener(new Listener.ThreadedListener(new Listener() {
@Override
public void disconnected(Connection connection) {
}
@Override
public void received(Connection connection, Object object) {
debug("received " + object.getClass());
if (object instanceof ConnectionRequestPacket) {
ConnectionPacket p = new ConnectionPacket();
p.connectionId = connection.getID();
server.sendToTCP(connection.getID(), p);
}
if (object instanceof GetRegionPacket) {
GetRegionPacket p = (GetRegionPacket) object;
RegionData data = worldProvider.getRegion(p.location);
RegionDataPacket response = new RegionDataPacket(data);
server.sendToTCP(connection.getID(), response);
}
if (object instanceof SpawnRequestPacket) {
SpawnLocation loc = worldProvider.getSpawnLocation(connection.getID());
if (null != loc) {
server.sendToTCP(connection.getID(), new WorldInfoPacket(worldProvider));
LiveEntity entity = new LiveEntity();
entity.setX(loc.x);
entity.setY(loc.y);
entity.setZ(loc.z);
entity.setConnectionId(connection.getID());
scene.registerLiveEntity(entity);
// send all entity locations
for (LiveEntity nextEntity : scene.getLiveEntities().values()) {
SpawnPacket p = new SpawnPacket();
p.x = nextEntity.getX();
p.y = nextEntity.getY();
p.z = nextEntity.getZ();
p.connectionId = nextEntity.getConnectionId();
if (p.connectionId == connection.getID()) {
server.sendToAllTCP(p);
} else {
server.sendToTCP(connection.getID(), p);
}
}
}
}
if (object instanceof LiveEntityLocationUpdatePacket) {
LiveEntityLocationUpdatePacket p = ((LiveEntityLocationUpdatePacket) object);
p.connectionId = connection.getID();
LiveEntity entity = scene.getLiveEntity(connection.getID());
if (null != entity) {
entity.setLocation(p.x, p.y, p.z);
server.sendToAllTCP(object);
}
}
// super.received(connection, object);
}
}, threadPool));
}
private DiscoveryThread discovery;
public void start() {
try {
server.start();
server.bind(Game.port);
discovery = new DiscoveryThread("SERVER DISCOVERY");
discovery.start();
isServerRunning = true;
// server.addListener(new Listener());
} catch (BindException ex) {
server.stop();
if (null != discovery && discovery.isAlive()) {
discovery.interrupt();
}
isServerRunning = false;
} catch (IOException e1) {
server.stop();
if (null != discovery && discovery.isAlive()) {
discovery.interrupt();
}
e1.printStackTrace();
isServerRunning = false;
}
}
public void stop() {
server.close();
server.stop();
isServerRunning = false;
}
public boolean isRunning() {
return isServerRunning;
}
public void send(IPacket p) {
}
public void dispose() {
stop();
// threadPool.awaitTermination(1000, TimeUnit.MILLISECONDS);
threadPool.shutdownNow();
}
}
|