public boolean reconnect() { try { Logger.println("Attempting to connect to LS"); SocketConnector conn = new SocketConnector(); SocketConnectorConfig config = new SocketConnectorConfig(); ((SocketSessionConfig) config.getSessionConfig()).setKeepAlive(true); ((SocketSessionConfig) config.getSessionConfig()).setTcpNoDelay(true); ConnectFuture future = conn.connect( new InetSocketAddress(Config.LS_IP, Config.LS_PORT), connectionHandler, config); future.join(3000); if (future.isConnected()) { session = future.getSession(); Logger.println("Registering world (" + Config.SERVER_NUM + ") with LS"); actionSender.registerWorld(); connectionAttempts = 0; return true; } if (connectionAttempts++ >= 100) { Logger.println("Unable to connect to LS, giving up after " + connectionAttempts + " tries"); System.exit(1); return false; } return reconnect(); } catch (Exception e) { Logger.println("Error connecting to LS: " + e.getMessage()); return false; } }
public void setRegistered(boolean registered) { if (registered) { this.registered = true; Logger.print("World successfully registered with LS"); } else { Logger.error(new Exception("Error registering world")); } }
public void processIncomingPackets() { for (LSPacket p : packetQueue.getPackets()) { PacketHandler handler; if (((handler = uniqueHandlers.get(p.getUID())) != null) || ((handler = packetHandlers.get(p.getID())) != null)) { try { handler.handlePacket(p, session); uniqueHandlers.remove(p.getUID()); } catch (Exception e) { Logger.error("Exception with p[" + p.getID() + "] from LOGIN_SERVER: " + e.getMessage()); } } else { Logger.error("Unhandled packet from LS: " + p.getID()); } } }
public void handlePacket(Packet p1, IoSession session) throws Exception { Player player = (Player) session.getAttachment(); final String ip = ((InetSocketAddress) session.getRemoteAddress()) .getAddress() .toString() .replaceAll("/", ""); byte loginCode; try { byte[] data = RSA.decrypt(p1.getData()); Packet p = new Packet(session, data); boolean reconnecting = (p.readByte() == 1); int clientVersion = p.readInt(); if (Config.SERVER_VERSION != clientVersion) { Logger.println( "ip: " + ip + " | clientversion: " + clientVersion + " : " + Config.SERVER_VERSION); } int[] sessionKeys = new int[4]; for (int key = 0; key < sessionKeys.length; key++) { sessionKeys[key] = p.readInt(); } String username = ""; String password = ""; username = p.readString(20).trim(); password = p.readString(20).trim(); if (world.countPlayers() >= Config.MAX_PLAYERS) { loginCode = 10; } else if (clientVersion < Config.SERVER_VERSION) { loginCode = 4; } else if (!player.setSessionKeys(sessionKeys)) { loginCode = 5; } else { player.load(username, password, 0, reconnecting); if (clientVersion < 39) { player.clientWarn(true); } return; } } catch (Exception e) { System.err.println("Login exception with: " + ip); e.printStackTrace(); loginCode = 4; } RSCPacketBuilder pb = new RSCPacketBuilder(); pb.setBare(true); pb.addByte((byte) loginCode); session.write(pb.toPacket()); player.destroy(true); }
public synchronized void sendQueuedPackets() { try { List<LSPacket> packets = actionSender.getPackets(); for (LSPacket packet : packets) { session.write(packet); } actionSender.clearPackets(); } catch (Exception e) { Logger.println("Stack processInc: "); e.printStackTrace(); } }
private void loadPacketHandlers() { PacketHandlerDef[] handlerDefs = (PacketHandlerDef[]) PersistenceManager.load("LSPacketHandlers.xml"); for (PacketHandlerDef handlerDef : handlerDefs) { try { String className = handlerDef.getClassName(); Class c = Class.forName(className); if (c != null) { PacketHandler handler = (PacketHandler) c.newInstance(); for (int packetID : handlerDef.getAssociatedPackets()) { packetHandlers.put(packetID, handler); } } } catch (Exception e) { Logger.error(e); } } }
public void kill() { running = false; Logger.print("Unregistering world (" + Config.SERVER_NUM + ") with LS"); actionSender.unregisterWorld(); }
public void handlePacket(Packet p, IoSession session) throws Exception { Player player = (Player) session.getAttachment(); int pID = ((RSCPacket) p).getID(); Player affectedPlayer; if (busy(player)) { affectedPlayer = player.getWishToTrade(); unsetOptions(player); unsetOptions(affectedPlayer); return; } switch (pID) { case 166: // Sending trade request affectedPlayer = world.getPlayer(p.readShort()); if (affectedPlayer == null) return; if (affectedPlayer.isTrading()) { player.getActionSender().sendMessage("That person is already trading"); return; } if (affectedPlayer == null || affectedPlayer.isDueling() || !player.withinRange(affectedPlayer, 8) || player.isTrading() || player.tradeDuelThrottling()) { unsetOptions(player); return; } if (player == null) { Logger.println("Player is null affectedplayer is: " + affectedPlayer.getUsername()); } if (affectedPlayer == player) { player.setSuspiciousPlayer(true); Logger.println("Warning: " + player.getUsername() + " tried to trade to himself."); unsetOptions(player); return; } if (!new PathGenerator( player.getX(), player.getY(), affectedPlayer.getX(), affectedPlayer.getY()) .isValid()) { player.getActionSender().sendMessage("you can't reach this person"); unsetOptions(player); return; } if ((affectedPlayer.getPrivacySetting(2) && !affectedPlayer.isFriendsWith(player.getUsernameHash())) || affectedPlayer.isIgnoring(player.getUsernameHash())) { player.getActionSender().sendMessage("This player has trade requests blocked."); return; } player.setWishToTrade(affectedPlayer); player .getActionSender() .sendMessage( affectedPlayer.isTrading() ? affectedPlayer.getUsername() + " is already in a trade" : "Sending trade request"); affectedPlayer .getActionSender() .sendMessage(player.getUsername() + " wishes to trade with you"); if (!player.isTrading() && affectedPlayer.getWishToTrade() != null && affectedPlayer.getWishToTrade().equals(player) && !affectedPlayer.isTrading()) { player.setTrading(true); player.resetPath(); player.resetAllExceptTrading(); affectedPlayer.setTrading(true); affectedPlayer.resetPath(); affectedPlayer.resetAllExceptTrading(); player.getActionSender().sendTradeWindowOpen(); affectedPlayer.getActionSender().sendTradeWindowOpen(); world.addEntryToSnapshots( new Activity( player.getUsername(), player.getUsername() + " sent trade request " + affectedPlayer.getUsername() + " at: " + player.getX() + "/" + player.getY() + " | " + affectedPlayer.getX() + "/" + affectedPlayer.getY())); } break; case 211: // Trade accepted affectedPlayer = player.getWishToTrade(); if (affectedPlayer == null || busy(affectedPlayer) || !player.isTrading() || !affectedPlayer.isTrading()) { // This // shouldn't // happen player.setSuspiciousPlayer(true); unsetOptions(player); unsetOptions(affectedPlayer); return; } player.setTradeOfferAccepted(true); player.getActionSender().sendTradeAcceptUpdate(); affectedPlayer.getActionSender().sendTradeAcceptUpdate(); if (affectedPlayer.isTradeOfferAccepted()) { player.getActionSender().sendTradeAccept(); affectedPlayer.getActionSender().sendTradeAccept(); } world.addEntryToSnapshots( new Activity( player.getUsername(), player.getUsername() + " accepted trade " + affectedPlayer.getUsername() + " at: " + player.getX() + "/" + player.getY() + " | " + affectedPlayer.getX() + "/" + affectedPlayer.getY())); break; case 53: // Confirm accepted affectedPlayer = player.getWishToTrade(); if (affectedPlayer == null || busy(affectedPlayer) || !player.isTrading() || !affectedPlayer.isTrading() || !player.isTradeOfferAccepted() || !affectedPlayer.isTradeOfferAccepted()) { // This // shouldn't // happen player.setSuspiciousPlayer(true); unsetOptions(player); unsetOptions(affectedPlayer); return; } player.setTradeConfirmAccepted(true); if (affectedPlayer.isTradeConfirmAccepted()) { world.addEntryToSnapshots( new Activity( player.getUsername(), player.getUsername() + " finished trade " + affectedPlayer.getUsername() + " at: " + player.getX() + "/" + player.getY() + " | " + affectedPlayer.getX() + "/" + affectedPlayer.getY())); ArrayList<InvItem> myOffer = player.getTradeOffer(); ArrayList<InvItem> theirOffer = affectedPlayer.getTradeOffer(); int myRequiredSlots = player.getInventory().getRequiredSlots(theirOffer); int myAvailableSlots = (30 - player.getInventory().size()) + player.getInventory().getFreedSlots(myOffer); int theirRequiredSlots = affectedPlayer.getInventory().getRequiredSlots(myOffer); int theirAvailableSlots = (30 - affectedPlayer.getInventory().size()) + affectedPlayer.getInventory().getFreedSlots(theirOffer); if (theirRequiredSlots > theirAvailableSlots) { player .getActionSender() .sendMessage("The other player does not have room to accept your items."); affectedPlayer .getActionSender() .sendMessage("You do not have room in your inventory to hold those items."); unsetOptions(player); unsetOptions(affectedPlayer); return; } if (myRequiredSlots > myAvailableSlots) { player .getActionSender() .sendMessage("You do not have room in your inventory to hold those items."); affectedPlayer .getActionSender() .sendMessage("The other player does not have room to accept your items."); unsetOptions(player); unsetOptions(affectedPlayer); return; } for (InvItem item : myOffer) { InvItem affectedItem = player.getInventory().get(item); if (affectedItem == null) { player.setSuspiciousPlayer(true); unsetOptions(player); unsetOptions(affectedPlayer); return; } if (item.getDef().isMembers() && !World.isMembers()) { player .getActionSender() .sendMessage("This feature is only avaliable on a members server"); unsetOptions(player); unsetOptions(affectedPlayer); return; } if (affectedItem.isWielded()) { affectedItem.setWield(false); player.updateWornItems( affectedItem.getWieldableDef().getWieldPos(), player .getPlayerAppearance() .getSprite(affectedItem.getWieldableDef().getWieldPos())); } player.getInventory().remove(item); } for (InvItem item : theirOffer) { InvItem affectedItem = affectedPlayer.getInventory().get(item); if (affectedItem == null) { affectedPlayer.setSuspiciousPlayer(true); unsetOptions(player); unsetOptions(affectedPlayer); return; } if (item.getDef().isMembers() && !World.isMembers()) { player .getActionSender() .sendMessage("This feature is only avaliable on a members server"); unsetOptions(player); unsetOptions(affectedPlayer); return; } if (affectedItem.isWielded()) { affectedItem.setWield(false); affectedPlayer.updateWornItems( affectedItem.getWieldableDef().getWieldPos(), affectedPlayer .getPlayerAppearance() .getSprite(affectedItem.getWieldableDef().getWieldPos())); } affectedPlayer.getInventory().remove(item); } MiscPacketBuilder loginServer = Instance.getServer().getLoginConnector().getActionSender(); long playerhash = DataConversions.usernameToHash(player.getUsername()); long affectedPlayerhash = DataConversions.usernameToHash(affectedPlayer.getUsername()); for (InvItem item : myOffer) { affectedPlayer.getInventory().add(item); } for (InvItem item : theirOffer) { player.getInventory().add(item); } boolean senddata = false; for (InvItem item : myOffer) { loginServer.tradeLog( playerhash, affectedPlayerhash, item.getID(), item.getAmount(), player.getX(), player.getY(), 1); if (item.getAmount() > 10000000 || Formulae.isRareItem(item.getID())) senddata = true; } if (senddata) DBConnection.getReport() .submitDupeData(DataConversions.hashToUsername(playerhash), playerhash); senddata = false; for (InvItem item : theirOffer) { loginServer.tradeLog( affectedPlayerhash, playerhash, item.getID(), item.getAmount(), player.getX(), player.getY(), 1); if (item.getAmount() > 10000000 || Formulae.isRareItem(item.getID())) senddata = true; } if (senddata) DBConnection.getReport() .submitDupeData( DataConversions.hashToUsername(affectedPlayerhash), affectedPlayerhash); player.getActionSender().sendInventory(); player.getActionSender().sendEquipmentStats(); Long now = System.currentTimeMillis(); player.save(); player.setLastSaveTime(now); affectedPlayer.save(); affectedPlayer.setLastSaveTime(now); player.getActionSender().sendMessage("Trade completed."); affectedPlayer.getActionSender().sendInventory(); affectedPlayer.getActionSender().sendEquipmentStats(); affectedPlayer.getActionSender().sendMessage("Trade completed."); unsetOptions(player); unsetOptions(affectedPlayer); World.getWar().handleCapeWield(player); World.getWar().handleCapeWield(affectedPlayer); } break; case 216: // Trade declined affectedPlayer = player.getWishToTrade(); if (affectedPlayer == null || busy(affectedPlayer) || !player.isTrading() || !affectedPlayer.isTrading()) { // This // shouldn't // happen player.setSuspiciousPlayer(true); unsetOptions(player); unsetOptions(affectedPlayer); return; } affectedPlayer .getActionSender() .sendMessage(player.getUsername() + " has declined the trade."); unsetOptions(player); unsetOptions(affectedPlayer); break; case 70: // Receive offered item data affectedPlayer = player.getWishToTrade(); if (affectedPlayer == null || busy(affectedPlayer) || !player.isTrading() || !affectedPlayer.isTrading() || (player.isTradeOfferAccepted() && affectedPlayer.isTradeOfferAccepted()) || player.isTradeConfirmAccepted() || affectedPlayer.isTradeConfirmAccepted()) { // This // shouldn't // happen player.setSuspiciousPlayer(true); unsetOptions(player); unsetOptions(affectedPlayer); return; } player.setTradeOfferAccepted(false); player.setTradeConfirmAccepted(false); affectedPlayer.setTradeOfferAccepted(false); affectedPlayer.setTradeConfirmAccepted(false); // player.getActionSender().sendTradeAcceptUpdate(); // affectedPlayer.getActionSender().sendTradeAcceptUpdate(); Inventory tradeOffer = new Inventory(); player.resetTradeOffer(); int count = (int) p.readByte(); for (int slot = 0; slot < count; slot++) { InvItem tItem = new InvItem(p.readShort(), p.readInt()); if (tItem.getAmount() < 1) { player.setSuspiciousPlayer(true); player.setRequiresOfferUpdate(true); continue; } ItemDef def = tItem.getDef(); if (!def.canTrade() && !player.isMod()) { player .getActionSender() .sendMessage(def.getName() + " cannot be traded with other players"); player.setRequiresOfferUpdate(true); continue; } tradeOffer.add(tItem); } for (InvItem item : tradeOffer.getItems()) { if (tradeOffer.countId(item.getID()) > player.getInventory().countId(item.getID())) { player.setSuspiciousPlayer(true); unsetOptions(player); unsetOptions(affectedPlayer); return; } player.addToTradeOffer(item); } player.setRequiresOfferUpdate(true); break; } }