Skip to content

Commit dce213c

Browse files
committed
Refactored disconnect handling, updated engine
1 parent 8de830b commit dce213c

19 files changed

Lines changed: 154 additions & 127 deletions

ngengine_public.jar

10.8 KB
Binary file not shown.

src/main/NGECore.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.concurrent.TimeUnit;
3636

3737
import org.apache.mina.core.service.IoHandler;
38+
import org.apache.mina.core.session.IoSession;
3839

3940
import protocol.swg.ChatSystemMessage;
4041
import net.engio.mbassy.bus.config.BusConfiguration;
@@ -112,7 +113,7 @@ public class NGECore {
112113

113114
private volatile boolean isShuttingDown = false;
114115

115-
private ConcurrentHashMap<Integer, Client> clients = new ConcurrentHashMap<Integer, Client>();
116+
private ConcurrentHashMap<IoSession, Client> clients = new ConcurrentHashMap<IoSession, Client>();
116117

117118
// Database
118119

@@ -451,7 +452,7 @@ public ObjectDatabase getBuildingODB() {
451452

452453
public int getActiveClients() {
453454
int connections = 0;
454-
for (Map.Entry<Integer, Client> c : clients.entrySet()) {
455+
for (Map.Entry<IoSession, Client> c : clients.entrySet()) {
455456
if(c.getValue().getSession() != null) {
456457
if (c.getValue().getSession().isConnected()) {
457458
connections++;
@@ -463,7 +464,7 @@ public int getActiveClients() {
463464

464465
public int getActiveZoneClients() {
465466
int connections = 0;
466-
for (Map.Entry<Integer, Client> c : clients.entrySet()) {
467+
for (Map.Entry<IoSession, Client> c : clients.entrySet()) {
467468
if(c.getValue().getSession() != null) {
468469
if (c.getValue().getSession().isConnected() && c.getValue().getParent() != null) {
469470
connections++;
@@ -474,23 +475,23 @@ public int getActiveZoneClients() {
474475
}
475476

476477

477-
public Client getClient(int connectionID) {
478-
return clients.get(connectionID);
478+
public Client getClient(IoSession session) {
479+
return clients.get(session);
479480
}
480481

481-
public ConcurrentHashMap<Integer, Client> getActiveConnectionsMap() {
482+
public ConcurrentHashMap<IoSession, Client> getActiveConnectionsMap() {
482483
return clients;
483484
}
484485

485486
/*
486487
* --------------- Other methods for services ---------------
487488
*/
488-
public void addClient(Integer connectionID, Client client) {
489-
clients.put(connectionID, client);
489+
public void addClient(IoSession session, Client client) {
490+
clients.put(session, client);
490491
}
491492

492-
public void removeClient(Integer connectionID) {
493-
clients.remove(connectionID);
493+
public void removeClient(IoSession session) {
494+
clients.remove(session);
494495
}
495496

496497
// for python scripts

src/services/CharacterService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void handlePacket(IoSession session, IoBuffer data) throws Exception {
132132
@Override
133133
public void handlePacket(IoSession session, IoBuffer data) throws Exception {
134134

135-
Client client = core.getClient((Integer) session.getAttribute("connectionId"));
135+
Client client = core.getClient(session);
136136
data = data.order(ByteOrder.LITTLE_ENDIAN);
137137
data.position(0);
138138
ClientVerifyAndLockNameRequest message = new ClientVerifyAndLockNameRequest();
@@ -209,7 +209,7 @@ public void handlePacket(IoSession session, IoBuffer data) throws Exception {
209209
}
210210

211211
int galaxyId = config.getInt("GALAXY_ID");
212-
Client client = core.getClient((Integer) session.getAttribute("connectionId"));
212+
Client client = core.getClient(session);
213213

214214
try {
215215
if (client == null || checkForDuplicateName(getfirstName(clientCreateCharacter.getName(), clientCreateCharacter.getRaceTemplate()), client.getAccountId())) {

src/services/ConnectionService.java

Lines changed: 77 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -78,81 +78,16 @@ public void run() {
7878
synchronized(core.getActiveConnectionsMap()) {
7979
for(Client c : core.getActiveConnectionsMap().values()) {
8080
if(c.getParent() != null) {
81-
if ((System.currentTimeMillis() - c.getSession().getLastReadTime()) > 300000) {
81+
if ((System.currentTimeMillis() - c.getSession().getLastReadTime()) > 300000 && c.getSession().getAttribute("disconnectTask") == null) {
8282
disconnect(c.getSession());
8383
}
8484
}
8585
}
8686
}
8787
}
8888

89-
public void disconnect(IoSession session) {
90-
Client client = core.getClient((Integer) session.getAttribute("connectionId"));
91-
92-
if(client == null)
93-
return;
94-
95-
if(client.getParent() == null)
96-
return;
97-
98-
CreatureObject object = (CreatureObject) client.getParent();
99-
object.setInviteCounter(0);
100-
object.setInviteSenderId(0);
101-
object.setInviteSenderName("");
102-
object.setClient(null);
103-
PlayerObject ghost = (PlayerObject) object.getSlottedObject("ghost");
104-
105-
if(object.getGroupId() != 0)
106-
core.groupService.handleGroupDisband(object);
107-
108-
Point3D objectPos = object.getWorldPosition();
109-
110-
List<AbstractCollidable> collidables = core.simulationService.getCollidables(object.getPlanet(), objectPos.x, objectPos.z, 512);
111-
112-
for(AbstractCollidable collidable : collidables) {
113-
collidables.remove(object);
114-
}
115-
116-
117-
if (ghost != null) {
118-
String objectShortName = object.getCustomName();
119-
120-
if (object.getCustomName().contains(" ")) {
121-
String[] splitName = object.getCustomName().toLowerCase().split(" ");
122-
objectShortName = splitName[0];
123-
}
124-
125-
core.chatService.playerStatusChange(objectShortName, (byte) 0);
126-
}
127-
128-
session.suspendWrite();
129-
130-
boolean remove = core.simulationService.remove(object, object.getPosition().x, object.getPosition().z);
131-
if(remove)
132-
System.out.println("Successful quadtree remove");
133-
134-
//if(object.getContainer() == null) {
135-
HashSet<Client> oldObservers = new HashSet<Client>(object.getObservers());
136-
for(Iterator<Client> it = oldObservers.iterator(); it.hasNext();) {
137-
Client observerClient = it.next();
138-
if(observerClient.getParent() != null && !(observerClient.getSession() == session)) {
139-
observerClient.getParent().makeUnaware(object);
140-
}
141-
}
142-
//} else {
143-
// object.getContainer().remove(object);
144-
//}
145-
146-
147-
object.createTransaction(core.getCreatureODB().getEnvironment());
148-
core.getCreatureODB().put(object, Long.class, CreatureObject.class, object.getTransaction());
149-
object.getTransaction().commitSync();
150-
core.objectService.destroyObject(object);
151-
152-
core.getActiveConnectionsMap().remove((Integer) session.getAttribute("connectionId"));
153-
}
15489

155-
}, 15, 15, TimeUnit.MINUTES);
90+
}, 10, 10, TimeUnit.MINUTES);
15691

15792
}
15893

@@ -169,7 +104,7 @@ public void handlePacket(IoSession session, IoBuffer data) throws Exception {
169104
data.position(0);
170105
clientIdMsg.deserialize(data);
171106

172-
Client client = core.getClient((Integer) session.getAttribute("connectionId"));
107+
Client client = core.getClient(session);
173108
if(client == null) {
174109
System.out.println("NULL Client");
175110
return;
@@ -239,6 +174,80 @@ public void handlePacket(IoSession session, IoBuffer data) throws Exception {
239174

240175

241176
}
177+
178+
public void disconnect(IoSession session) {
179+
Client client = core.getClient(session);
180+
181+
if(client == null)
182+
return;
183+
184+
if(client.getParent() == null)
185+
return;
186+
187+
CreatureObject object = (CreatureObject) client.getParent();
188+
object.setInviteCounter(0);
189+
object.setInviteSenderId(0);
190+
object.setInviteSenderName("");
191+
object.setClient(null);
192+
PlayerObject ghost = (PlayerObject) object.getSlottedObject("ghost");
193+
194+
if(ghost == null)
195+
return;
196+
197+
if(object.getGroupId() != 0)
198+
core.groupService.handleGroupDisband(object);
199+
200+
Point3D objectPos = object.getWorldPosition();
201+
202+
List<AbstractCollidable> collidables = core.simulationService.getCollidables(object.getPlanet(), objectPos.x, objectPos.z, 512);
203+
204+
for(AbstractCollidable collidable : collidables) {
205+
collidables.remove(object);
206+
}
207+
208+
209+
if (ghost != null) {
210+
String objectShortName = object.getCustomName();
211+
212+
if (object.getCustomName().contains(" ")) {
213+
String[] splitName = object.getCustomName().toLowerCase().split(" ");
214+
objectShortName = splitName[0];
215+
}
216+
217+
core.chatService.playerStatusChange(objectShortName, (byte) 0);
218+
}
219+
220+
session.suspendWrite();
221+
222+
long parentId = object.getParentId();
223+
224+
if(object.getContainer() == null) {
225+
boolean remove = core.simulationService.remove(object, object.getPosition().x, object.getPosition().z);
226+
if(remove)
227+
System.out.println("Successful quadtree remove");
228+
} else {
229+
object.getContainer()._remove(object);
230+
object.setParentId(parentId);
231+
}
232+
233+
234+
HashSet<Client> oldObservers = new HashSet<Client>(object.getObservers());
235+
for(Iterator<Client> it = oldObservers.iterator(); it.hasNext();) {
236+
Client observerClient = it.next();
237+
if(observerClient.getParent() != null && !(observerClient.getSession() == session)) {
238+
observerClient.getParent().makeUnaware(object);
239+
}
240+
}
241+
ghost.toggleFlag(PlayerFlags.LD);
242+
243+
object.setAttachment("disconnectTask", null);
244+
object.createTransaction(core.getCreatureODB().getEnvironment());
245+
core.getCreatureODB().put(object, Long.class, CreatureObject.class, object.getTransaction());
246+
object.getTransaction().commitSync();
247+
core.objectService.destroyObject(object);
248+
249+
}
250+
242251

243252
@Override
244253
public void shutdown() {

src/services/EntertainmentService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void handlePacket(IoSession session, IoBuffer data) throws Exception {
6262
Console.println("BUFF_BUILDER_CHANGE RECIEVED");
6363
data.order(ByteOrder.LITTLE_ENDIAN);
6464

65-
Client client = core.getClient((Integer) session.getAttribute("connectionId"));
65+
Client client = core.getClient(session);
6666

6767
if(client == null)
6868
return;

src/services/LoginService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void handlePacket(IoSession session, IoBuffer data) throws Exception {
135135
client.setSession(session);
136136
client.setGM(checkForGmPermission(id));
137137

138-
core.addClient((Integer)session.getAttribute("connectionId"), client);
138+
core.addClient(session, client);
139139

140140
/*if(!checkIfAccountExistInGameDB(id)) {
141141
createAccountForGameDB(id, user, email, encryptPass);
@@ -179,7 +179,7 @@ public void handlePacket(IoSession session, IoBuffer data) throws Exception {
179179

180180
DeleteCharacterMessage packet = new DeleteCharacterMessage();
181181
packet.deserialize(data);
182-
Client client = core.getClient((Integer) session.getAttribute("connectionId"));
182+
Client client = core.getClient(session);
183183

184184
PreparedStatement preparedStatement;
185185

src/services/MissionService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void handlePacket(IoSession session, IoBuffer data) throws Exception {
6767
MissionListRequest request = new MissionListRequest();
6868
request.deserialize(data);
6969

70-
Client client = core.getClient((Integer) session.getAttribute("connectionId"));
70+
Client client = core.getClient(session);
7171

7272
if(client == null || client.getSession() == null)
7373
return;

src/services/PlayerService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public void insertOpcodes(Map<Integer, INetworkRemoteEvent> swgOpcodes, Map<Inte
158158

159159
@Override
160160
public void handlePacket(IoSession session, IoBuffer data) throws Exception {
161-
Client c = core.getClient((Integer) session.getAttribute("connectionId"));
161+
Client c = core.getClient(session);
162162
ChangeRoleIconChoice packet = new ChangeRoleIconChoice();
163163
PlayerObject player;
164164
SWGObject o;
@@ -185,7 +185,7 @@ public void handlePacket(IoSession session, IoBuffer data) throws Exception {
185185
public void handlePacket(IoSession session, IoBuffer data) throws Exception {
186186
data.order(ByteOrder.LITTLE_ENDIAN);
187187

188-
Client client = core.getClient((Integer) session.getAttribute("connectionId"));
188+
Client client = core.getClient(session);
189189

190190
if (client == null)
191191
return;

0 commit comments

Comments
 (0)