Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.client.network;
  2.  
  3. import java.net.InetSocketAddress;
  4. import java.util.concurrent.Executors;
  5.  
  6. import org.client.utility.Config;
  7. import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
  8. import org.jboss.netty.bootstrap.ClientBootstrap;
  9. import org.jboss.netty.channel.Channel;
  10. import org.jboss.netty.channel.ChannelFuture;
  11. import org.jboss.netty.channel.ChannelPipeline;
  12. import org.jboss.netty.channel.ChannelPipelineFactory;
  13. import org.jboss.netty.channel.Channels;
  14.  
  15. public class Connection {
  16.  
  17.     private static Channel channel;
  18.  
  19.     public static boolean connect() {
  20.         try {
  21.             ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
  22.             bootstrap.setPipelineFactory(new PipelineFactory());
  23.             bootstrap.setOption("child.tcpNoDelay", true);
  24.             ChannelFuture future = bootstrap.connect(new InetSocketAddress(
  25.                     Config.SERVER_IP, Config.PORT));
  26.             Channel channel = future.awaitUninterruptibly().getChannel();
  27.             if (!future.isSuccess()) {
  28.                 bootstrap.releaseExternalResources();
  29.                 return false;
  30.             }
  31.             setChannel(channel);
  32.         } catch (Exception ex) {
  33.             ex.printStackTrace();
  34.             return false;
  35.         }
  36.         return true;
  37.     }
  38.  
  39.     public static Channel getChannel() {
  40.         return channel;
  41.     }
  42.  
  43.     public static void setChannel(Channel channel) {
  44.         Connection.channel = channel;
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement