Thread: Adding netty networking to Project Insanity

Page 1 of 7 123 ... LastLast
Results 1 to 10 of 69
  1. #1 Adding netty networking to Project Insanity 
    Registered Member

    Join Date
    May 2012
    Posts
    253
    Thanks given
    5
    Thanks received
    146
    Rep Power
    122
    Download netty deps.
    Download and replace your util, net and task.
    Download .classpath and .project (not required) - just to set up the library project and classpath.

    After you've added those, replace your Server.java with (additional, not required):

    Code:
    package server;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.text.DecimalFormat;
    import java.util.concurrent.Executors;
    import org.jboss.netty.bootstrap.ServerBootstrap;
    import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
    import org.jboss.netty.util.HashedWheelTimer;
    import server.event.CycleEventHandler;
    import server.event.Task;
    import server.event.TaskScheduler;
    import server.world.StillGraphicsManager;
    import server.world.PlayerManager;
    import server.net.PipelineFactory;
    import server.util.log.Logger;
    
    /**
     * Server.java
     *
     * @author Sanity
     * @author Graham
     * @author Blake
     * @author Ryan Lmctruck30
     *
     */
    
    public class Server {
    
    	
    	
    	public static PlayerManager playerManager = null;
    	private static StillGraphicsManager stillGraphicsManager = null;
    	
    	public static boolean sleeping;
    	public static final int cycleRate;
    	public static boolean UpdateServer = false;
    	public static long lastMassSave = System.currentTimeMillis();
    	private static long cycleTime, cycles, totalCycleTime, sleepTime;
    	private static DecimalFormat debugPercentFormat;
    	public static boolean shutdownServer = false;		
    	public static boolean shutdownClientHandler;			
    	public static int serverlistenerPort; 
    
    	/**
    	 * The task scheduler.
    	 */
    	private static final TaskScheduler scheduler = new TaskScheduler();
    
    	/**
    	 * Gets the task scheduler.
    	 * @return The task scheduler.
    	 */
    	public static TaskScheduler getTaskScheduler() {
    		return scheduler;
    	}
    	
    	static {
    		if(!Config.SERVER_DEBUG) {
    			serverlistenerPort = 43594;
    		} else {
    			serverlistenerPort = 43594;
    		}
    		cycleRate = 600;
    		shutdownServer = false;
    		sleepTime = 0;
    		debugPercentFormat = new DecimalFormat("0.0#%");
    	}
            
    	public static void main(java.lang.String args[]) throws NullPointerException, IOException {
    		/**
    		 * Starting Up Server
    		 */
    		long startTime = System.currentTimeMillis();
    		System.setOut(new Logger(System.out));
    		System.setErr(new Logger(System.err));
    	
    		bind();
                    
                    
    		playerManager = PlayerManager.getSingleton();
    		playerManager.setupRegionPlayers();
    		stillGraphicsManager = new StillGraphicsManager();
    
    		//all all your .initialize() methods here. didn't import connection,
    		  do it yourself. ;)
    
    		Connection.initialize();
    
    		
    		/**
    		 * Server Successfully Loaded 
    		 */
                    long endTime = System.currentTimeMillis();
                    long elapsed = endTime-startTime;
                    System.out.println("Server started up in "+elapsed+" ms");
    		System.out.println("Server listening on port 127.0.0.1:" + serverlistenerPort);
    		
                    
                    
                    /**
    		 * Main Server Tick
    		 */
    		scheduler.schedule(new Task() {
    			@Override
                        protected void execute() {
                            CycleEventHandler.getSingleton().process();
                            }
                    });
    		
    	}
    	
    	
    	
    	public static boolean playerExecuted = false;
    	
    	public static long getSleepTimer() {
    		return sleepTime;
    	}
    
    	
    	public static StillGraphicsManager getStillGraphicsManager() {
    		return stillGraphicsManager;
    	}
    	
    	public static PlayerManager getPlayerManager() {
    		return playerManager;
    	}
    	
    	public static ObjectManager getObjectManager() {
    		return objectManager;
    	}
    
            private static void bind() {
                ServerBootstrap serverBootstrap = new ServerBootstrap (new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
                serverBootstrap.setPipelineFactory (new PipelineFactory(new HashedWheelTimer()));
                serverBootstrap.bind (new InetSocketAddress(serverlistenerPort));	
            }
    	
    }
    You'll receive some errors, (obviously) because you need to do some importing and adding. You'll also have to re-dump all your minigame instances (lul).

    Go into Client.java and change methods to these:

    Code:
    	public Client(Channel s, int _playerId) {
    		super(_playerId);
    		this.session = s;
    		synchronized(this) {
    			outStream = new Stream(new byte[Config.BUFFER_SIZE]);
    			outStream.currentOffset = 0;
    		
    		inStream = new Stream(new byte[Config.BUFFER_SIZE]);
    		inStream.currentOffset = 0;
    		buffer = new byte[Config.BUFFER_SIZE];
                    }
    	}
    Code:
    	private Channel session;
    Code:
    	public void queueMessage(Packet arg1) {
    		synchronized (queuedPackets) {
    			queuedPackets.add(arg1);
    		}
    	}
    
    	public boolean processQueuedPackets() {
    		synchronized (queuedPackets) {
    			Packet p = null;
    			while ((p = queuedPackets.poll()) != null) {
    				inStream.currentOffset = 0;
    				packetType = p.getOpcode();
    				packetSize = p.getLength();
    				inStream.buffer = p.getPayload().array();
    				if (packetType > 0) {
    					PacketHandler.processPacket(this, packetType, packetSize);
    				}
    			}
    		}
    		return true;
    	}
    Code:
    	public Future<?> getCurrentTask() {
    		return currentTask;
    	}
    	
    	public synchronized Stream getInStream() {
    		return inStream;
    	}
    	
    	public synchronized int getPacketType() {
    		return packetType;
    	}
    	
    	public synchronized int getPacketSize() {
    		return packetSize;
    	}
    	
    	public synchronized Stream getOutStream() {
    		return outStream;
    	}
    Code:
    	public void destruct() {
    		if(session == null) 
    			return;
    		//PlayerSaving.getSingleton().requestSave(playerId);
    
    		if (inPits)
    			Server.fightPits.removePlayerFromPits(playerId);
    		if (clanId >= 0)
    			Server.clanChat.leaveClan(playerId, clanId);
    		Misc.println("[DEREGISTERED]: "+playerName+"");
    		CycleEventHandler.getSingleton().stopEvents(this);
    		disconnected = true;
    		session.close();
    		session = null;
    		inStream = null;
    		outStream = null;
    		isActive = false;
    		buffer = null;
    		super.destruct();
    	}
    Now add the imports:
    Code:
    import org.jboss.netty.buffer.ChannelBuffers;
    import org.jboss.netty.channel.Channel;
    import server.net.Packet.Type;
    Replace your PlayerHandler

    Replace these in Player.java with your old methods:
    Code:
    	private ISAACCipher inStreamDecryption = null, outStreamDecryption = null;
    	
    	public void setInStreamDecryption(ISAACCipher inStreamDecryption) {
    		this.inStreamDecryption = inStreamDecryption;
    	}
    
    	public void setOutStreamDecryption(ISAACCipher outStreamDecryption) {
    		this.outStreamDecryption = outStreamDecryption;
    	}
    Now import:
    Code:
    import server.util.ISAACCipher;

    NEXT BIT IS UP TO YOU, YOU DON'T HAVE TO DO THIS.
    IF YOU DON'T DO THIS, COMMENT OUT EVERYTHING TO DO WITH PlayerManager AND StillGraphics.

    Download these Graphics

    Add this in Player.java:

    Code:
    	public boolean withinDistance(int absX, int getY, int getHeightLevel) {
    		if (this.getHeightLevel() != getHeightLevel)
    			return false;
    		int deltaX = this.getX() - absX, deltaY = this.getY() - getY;
    		return deltaX <= 15 && deltaX >= -16 && deltaY <= 15 && deltaY >= -16;
    	}
    	
    	public int getHeightLevel;
    	public int getHeightLevel() {
    	return getHeightLevel;
    	}
    Now add this in PlayerAssistant.java:
    Code:
    	public void sendStillGraphics(int id, int heightS, int y, int x, int timeBCS) {
    		c.getOutStream().createFrame(85);
    		c.getOutStream().writeByteC(y - (c.mapRegionY * 8));
    		c.getOutStream().writeByteC(x - (c.mapRegionX * 8));
    		c.getOutStream().createFrame(4);
    		c.getOutStream().writeByte(0);// Tiles away (X >> 4 + Y & 7)
    											// //Tiles away from
    		// absX and absY.
    		c.getOutStream().writeWord(id); // Graphic ID.
    		c.getOutStream().writeByte(heightS); // Height of the graphic when
    													// cast.
    		c.getOutStream().writeWord(timeBCS); // Time before the graphic
    													// plays.
    		c.flushOutStream();
    	}

    Compiler:
    Code:
    @echo off
    "C:\Program Files\Java\jdk1.7.0_02\bin\javac.exe" -classpath deps/log4j-1.2.15.jar;deps/jython.jar;deps/xstream.jar;deps/netty.jar;deps/mysql.jar;deps/poi.jar;deps/slf4j.jar;deps/slf4j-nop.jar -d bin src\server\world\map\*.java src\server\event\*.java src\server\model\items\*.java src\server\model\minigames\*.java src\server\model\npcs\*.java src\server\model\objects\*.java src\server\model\players\*.java src\server\model\players\skills\*.java src\server\model\players\packets\*.java src\server\model\shops\*.java src\server\task\*.java src\server\util\*.java src\server\world\*.java src\server\util\log\*.java src\server\*.java src\server\net\login\*.java src\server\net\*.java 
    pause
    Run:
    Code:
    @echo off
    title Project Insanity
    java -Xmx800m -cp bin;deps/poi.jar;deps/netty.jar;deps/mysql.jar;deps/slf4j.jar;deps/slf4j-nop.jar;deps/jython.jar;log4j-1.2.15.jar; server.Server
    pause

    Credits:
    bob is bob/arrowzftw for the actual networking - Jboss for the library.
    Me (Int Bauk) - for forking out.
    5tuart - for some references.


    Extra information:

    Most Project Insanity's have some sort of a UID system (which is shit) - to stop multiple clients from logging in. You might want to consider adding something like this.

    Fixes:

    If you keep disconnecting and reconnecting, then go into Client.java and in the process() method delete:

    Code:
    	if(timeOutCounter > Config.TIMEOUT) {
    	    disconnected = true;
    	}
    
    	timeOutCounter++;
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Registered Member

    Join Date
    Aug 2011
    Posts
    2,760
    Thanks given
    297
    Thanks received
    534
    Rep Power
    1596
    Code:
                    /**
    		 * Main Server Tick
    		 */
    		scheduler.schedule(new Task() {
    			@Override
                        protected void execute() {
                            CycleEventHandler.getSingleton().process();
                            }
                    });
    		
    	}
    is not included in netty, the only neccesary thing to modify the server class should be the server boostrap.
    Quote Originally Posted by Aj View Post
    This is not even a tutorial. It's fail for rep. It's fail for life.
    Reply With Quote  
     

  4. #3  
    Registered Member

    Join Date
    May 2012
    Posts
    253
    Thanks given
    5
    Thanks received
    146
    Rep Power
    122
    Quote Originally Posted by 'Spike View Post
    Code:
                    /**
    		 * Main Server Tick
    		 */
    		scheduler.schedule(new Task() {
    			@Override
                        protected void execute() {
                            CycleEventHandler.getSingleton().process();
                            }
                    });
    		
    	}
    is not included in netty, the only neccesary thing to modify the server class should be the server boostrap.
    More of an additional feature. As I said, I only forked it out.
    Reply With Quote  
     

  5. #4  
    Registered Member
    Join Date
    Nov 2009
    Posts
    3,052
    Thanks given
    112
    Thanks received
    838
    Rep Power
    740
    nice o.O
    Reply With Quote  
     

  6. #5  
    Registered Member
    iHybrid's Avatar
    Join Date
    Sep 2010
    Posts
    2,095
    Thanks given
    636
    Thanks received
    352
    Rep Power
    81
    Someone should post the advantages of Netty vs w/e framework PI uses by default.
    Reply With Quote  
     

  7. #6  
    RuneFatality

    Join Date
    May 2009
    Age
    27
    Posts
    2,350
    Thanks given
    1,099
    Thanks received
    388
    Rep Power
    531
    Quote Originally Posted by iHybrid View Post
    Someone should post the advantages of Netty vs w/e framework PI uses by default.
    Google has plenty of this sort of information. Everytime it is mentioned here it turns into an argument not a debate. Btw i'd check your terminology

    FYI: PI uses MINA

    Edit: is that PlayerManager class regional updating? And what exactly does the StillGraphicsManager class do? Just update graphics of other players in sight?
    Reply With Quote  
     

  8. #7  
    Registered Member
    iHybrid's Avatar
    Join Date
    Sep 2010
    Posts
    2,095
    Thanks given
    636
    Thanks received
    352
    Rep Power
    81
    Quote Originally Posted by Flux View Post
    Google has plenty of this sort of information. Everytime it is mentioned here it turns into an argument not a debate. Btw i'd check your terminology

    FYI: PI uses MINA

    Edit: is that PlayerManager class regional updating? And what exactly does the StillGraphicsManager class do? Just update graphics of other players in sight?
    Yeah I'm guessing the term 'framework' was wrong Idk haven't been in the community for a long while.

    I could see how It'd end up being an argument. I'll do some research myself I guess I just wanted some opinions from people with past experience
    Reply With Quote  
     

  9. #8  
    we up

    Number 0's Avatar
    Join Date
    Apr 2012
    Posts
    533
    Thanks given
    107
    Thanks received
    161
    Rep Power
    224
    Not very much of an informative tutorial and I love how you rip code out and don't know what it does lol.
    june.
    Reply With Quote  
     

  10. #9  
    RuneFatality

    Join Date
    May 2009
    Age
    27
    Posts
    2,350
    Thanks given
    1,099
    Thanks received
    388
    Rep Power
    531
    Quote Originally Posted by iHybrid View Post
    Yeah I'm guessing the term 'framework' was wrong Idk haven't been in the community for a long while.

    I could see how It'd end up being an argument. I'll do some research myself I guess I just wanted some opinions from people with past experience
    At the end of that day it's up to you as an individual programmer, different programmers have different preferences. However, it has been proven that Netty is more or less a better, faster version of MINA. Iirc they were developed by the same person.
    Reply With Quote  
     

  11. #10  
    Registered Member

    Join Date
    Aug 2011
    Posts
    2,760
    Thanks given
    297
    Thanks received
    534
    Rep Power
    1596
    Quote Originally Posted by Flux View Post
    Google has plenty of this sort of information. Everytime it is mentioned here it turns into an argument not a debate. Btw i'd check your terminology

    FYI: PI uses MINA

    Edit: is that PlayerManager class regional updating? And what exactly does the StillGraphicsManager class do? Just update graphics of other players in sight?
    They're useless afaik.
    Quote Originally Posted by Aj View Post
    This is not even a tutorial. It's fail for rep. It's fail for life.
    Reply With Quote  
     

  12. Thankful user:


Page 1 of 7 123 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2012, 11:42 AM
  2. Adding drops on Project Insanity?
    By Throat in forum Help
    Replies: 3
    Last Post: 04-25-2012, 12:43 PM
  3. Adding dialogues to your Project Insanity
    By Santi in forum Tutorials
    Replies: 33
    Last Post: 03-31-2012, 10:29 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •