use of client.MapleClient in project HeavenMS by ronancpl.
the class MaplePacketEncoder method encode.
@Override
public void encode(final IoSession session, final Object message, final ProtocolEncoderOutput out) throws Exception {
final MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
try {
client.lockClient();
try {
final MapleAESOFB send_crypto = client.getSendCrypto();
final byte[] input = (byte[]) message;
final byte[] unencrypted = new byte[input.length];
System.arraycopy(input, 0, unencrypted, 0, input.length);
final byte[] ret = new byte[unencrypted.length + 4];
final byte[] header = send_crypto.getPacketHeader(unencrypted.length);
MapleCustomEncryption.encryptData(unencrypted);
send_crypto.crypt(unencrypted);
System.arraycopy(header, 0, ret, 0, 4);
System.arraycopy(unencrypted, 0, ret, 4, unencrypted.length);
out.write(IoBuffer.wrap(ret));
} finally {
client.unlockClient();
}
// System.arraycopy(unencrypted, 0, ret, 4, unencrypted.length);
// out.write(ByteBuffer.wrap(ret));
} catch (NullPointerException npe) {
out.write(IoBuffer.wrap(((byte[]) message)));
}
}
use of client.MapleClient in project HeavenMS by ronancpl.
the class Server method disconnectIdlesOnLoginState.
private void disconnectIdlesOnLoginState() {
srvLock.lock();
try {
List<MapleClient> toDisconnect = new LinkedList<>();
long timeNow = System.currentTimeMillis();
for (Entry<MapleClient, Long> mc : inLoginState.entrySet()) {
if (timeNow > mc.getValue()) {
toDisconnect.add(mc.getKey());
}
}
for (MapleClient c : toDisconnect) {
if (c.isLoggedIn()) {
c.disconnect(false, false);
} else {
c.getSession().close(true);
}
inLoginState.remove(c);
}
} finally {
srvLock.unlock();
}
}
use of client.MapleClient in project HeavenMS by ronancpl.
the class MapleInventory method checkSpots.
public static boolean checkSpots(MapleCharacter chr, List<Pair<Item, MapleInventoryType>> items, List<Integer> typesSlotsUsed) {
// assumption: no "UNDEFINED" or "EQUIPPED" items shall be tested here, all counts are >= 0.
Map<Integer, Short> rcvItems = new LinkedHashMap<>();
Map<Integer, Byte> rcvTypes = new LinkedHashMap<>();
for (Pair<Item, MapleInventoryType> item : items) {
Integer itemId = item.left.getItemId();
Short qty = rcvItems.get(itemId);
if (qty == null) {
rcvItems.put(itemId, item.left.getQuantity());
rcvTypes.put(itemId, item.right.getType());
} else {
rcvItems.put(itemId, (short) (qty + item.left.getQuantity()));
}
}
MapleClient c = chr.getClient();
for (Entry<Integer, Short> it : rcvItems.entrySet()) {
int itemType = rcvTypes.get(it.getKey()) - 1;
int usedSlots = typesSlotsUsed.get(itemType);
int result = MapleInventoryManipulator.checkSpaceProgressively(c, it.getKey(), it.getValue(), "", usedSlots);
boolean hasSpace = ((result % 2) != 0);
if (!hasSpace)
return false;
typesSlotsUsed.set(itemType, (result >> 1));
}
return true;
}
use of client.MapleClient in project HeavenMS by ronancpl.
the class MapleServerHandler method exceptionCaught.
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
System.out.println("disconnect by exception");
cause.printStackTrace();
if (cause instanceof IOException || cause instanceof ClassCastException) {
return;
}
MapleClient mc = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
if (mc != null && mc.getPlayer() != null) {
FilePrinter.printError(FilePrinter.EXCEPTION_CAUGHT, cause, "Exception caught by: " + mc.getPlayer());
}
}
use of client.MapleClient in project HeavenMS by ronancpl.
the class MapleServerHandler method sessionOpened.
@Override
public void sessionOpened(IoSession session) {
if (!Server.getInstance().isOnline()) {
session.close(true);
return;
}
if (channel > -1 && world > -1) {
if (Server.getInstance().getChannel(world, channel) == null) {
session.close(true);
return;
}
} else {
FilePrinter.print(FilePrinter.SESSION, "IoSession with " + session.getRemoteAddress() + " opened on " + sdf.format(Calendar.getInstance().getTime()), false);
}
byte[] key = { 0x13, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, (byte) 0xB4, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00 };
byte[] ivRecv = { 70, 114, 122, 82 };
byte[] ivSend = { 82, 48, 120, 115 };
ivRecv[3] = (byte) (Math.random() * 255);
ivSend[3] = (byte) (Math.random() * 255);
MapleAESOFB sendCypher = new MapleAESOFB(key, ivSend, (short) (0xFFFF - ServerConstants.VERSION));
MapleAESOFB recvCypher = new MapleAESOFB(key, ivRecv, (short) ServerConstants.VERSION);
MapleClient client = new MapleClient(sendCypher, recvCypher, session);
client.setWorld(world);
client.setChannel(channel);
// Generates a reasonable session id.
client.setSessionId(sessionId.getAndIncrement());
session.write(MaplePacketCreator.getHello(ServerConstants.VERSION, ivSend, ivRecv));
session.setAttribute(MapleClient.CLIENT_KEY, client);
}
Aggregations