Netty 之Oio程序编写

http://fokman.iteye.com/blog/1684330

今日将android里面使用的socket 方式改成Oio,因为之前使用的是Netty框架,所以直接将Nio替换成Oio,然后修改了一些其他的代码。

    为了维护的方便我定义了一个BaseClient,用来处理停止socket线程。

    

Java代码   收藏代码
  1. public abstract class BaseClient {  
  2.     protected Channel m_channel;  
  3.     protected String m_tag;  
  4.   
  5.     public BaseClient(String tag) {  
  6.         m_tag = tag;  
  7.     }  
  8.   
  9.     public Channel getChannel() {  
  10.         return m_channel;  
  11.     }  
  12.   
  13.     public void stop(boolean releaseResOnly) {  
  14.         this.stopInThread(releaseResOnly);  
  15.     }  
  16.   
  17.     protected void createBootstrap() {  
  18.         System.setProperty("java.net.preferIPv4Stack""true");  
  19.         System.setProperty("java.net.preferIPv6Addresses""false");  
  20.     }  
  21.   
  22.     protected void stopInThread(Bootstrap bootstrap, boolean releaseResOnly) {  
  23.         try {  
  24.             final Bootstrap bs = bootstrap;  
  25.             final Channel ch = m_channel;  
  26.             final boolean resOnly = releaseResOnly;  
  27.   
  28.             m_channel = null;  
  29.   
  30.             Thread t = new Thread(new Runnable() {  
  31.                 public void run() {  
  32.                     try {  
  33.                         if (!resOnly) {  
  34.                             if (ch != null) {  
  35.                                 ch.getCloseFuture().addListener(new ChannelFutureListener() {  
  36.                                     @Override  
  37.                                     public void operationComplete(ChannelFuture cf) throws Exception {  
  38.                                         if (bs != null) {  
  39.                                             final Bootstrap bs2 = bs;  
  40.                                             new Thread(new Runnable() {  
  41.                                                 public void run() {  
  42.                                                     try {  
  43.                                                         System.out.println(m_tag  
  44.                                                                 + "--- netty ch.close and releaseExtraRes-1");  
  45.                                                         bs2.releaseExternalResources();  
  46.                                                         System.out.println(m_tag  
  47.                                                                 + "--- netty ch.close and releaseExtraRes-1 done");  
  48.                                                     } catch (Throwable th) {  
  49.                                                     }  
  50.                                                 }  
  51.                                             }).start();  
  52.                                         }  
  53.                                     }  
  54.                                 });  
  55.                                 ch.close();  
  56.                             }  
  57.                         } else {  
  58.                             if (bs != null) {  
  59.                                 try {  
  60.                                     System.out.println(m_tag + "--- netty releaseExtraRes-2");  
  61.                                     bs.releaseExternalResources();  
  62.                                     System.out.println(m_tag + "--- netty releaseExtraRes-2 done");  
  63.                                 } catch (Throwable th) {  
  64.                                 }  
  65.                             }  
  66.                         }  
  67.   
  68.                     } catch (Exception ee) {  
  69.                         ee.printStackTrace();  
  70.                     }  
  71.                 }  
  72.             });  
  73.             t.start();  
  74.   
  75.         } catch (Exception e) {  
  76.             e.printStackTrace();  
  77.         }  
  78.   
  79.     }//stopInThread()  
  80.   
  81.     protected abstract void stopInThread(boolean releaseResOnly);  

 

   CxClient继承了BaseClient,增加了一些关于不同的错误编码,方便在日志中查看

 

Java代码   收藏代码
  1. public class CxClient extends BaseClient {  
  2.   
  3.     private ClientBootstrap m_bootstrap;  
  4.     private CxListener m_listener;  
  5.     private final Timer timer;  
  6.   
  7.     public CxClient(String tag) {  
  8.         super(tag);  
  9.         timer = new HashedWheelTimer();  
  10.     }  
  11.   
  12.     @Override  
  13.     protected void createBootstrap() {  
  14.         super.createBootstrap();  
  15.   
  16.         m_bootstrap = new ClientBootstrap(new OioClientSocketChannelFactory(Executors.newCachedThreadPool()));  
  17.         final CxClient client = this;  
  18.         m_bootstrap.setPipelineFactory(new ChannelPipelineFactory() {  
  19.             @Override  
  20.             public ChannelPipeline getPipeline() throws Exception {  
  21.                 ChannelPipeline pip = Channels.pipeline();  
  22.   
  23.                 pip.addLast("timeout"new ReadTimeoutHandler(timer, 300));  
  24.                 pip.addLast("decoder"new CxDecoder());  
  25.                 pip.addLast("handler"new CxHandler(m_listener, client));  
  26.   
  27.                 return pip;  
  28.             }  
  29.         });  
  30.         m_bootstrap.setOption("tcpNoDelay"true);  
  31.         m_bootstrap.setOption("keepAlive"true);  
  32.         m_bootstrap.setOption("reuseAddress"true);  
  33.         m_bootstrap.setOption("connectTimeoutMillis""7000");  
  34.     }  
  35.   
  36.     public void start(String host, int port, CxListener li) {  
  37.   
  38.         try {  
  39.             m_listener = li;  
  40.             createBootstrap();  
  41.             System.out.println("CxClient::start() Connecting... " + host + ":" + port);  
  42.             ChannelFuture f = m_bootstrap.connect(new InetSocketAddress(host, port));  
  43.   
  44.             f.addListener(new ChannelFutureListener() {  
  45.                 @Override  
  46.                 public void operationComplete(ChannelFuture cf) throws Exception {  
  47.                     if (m_listener != null) {  
  48.                         if (cf.isSuccess()) {  
  49.                             m_channel = cf.getChannel();  
  50.                             m_listener.connected();  
  51.                         } else {  
  52.                             Throwable th = cf.getCause();  
  53.                             System.out.println("CxClient::start() A 1");  
  54.                             int errorCode = CxnetConstants.UNKNOWN_EXCEPTION;  
  55.                             if (th != null) {  
  56.                                 Class<?> c = th.getClass();  
  57.                                 if (c == java.net.BindException.class)  
  58.                                     errorCode = CxnetConstants.BIND_EXCEPTION;  
  59.                                 else if (c == java.net.ConnectException.class)  
  60.                                     errorCode = CxnetConstants.CONNECT_EXCEPTION;  
  61.                                 else if (c == java.net.MalformedURLException.class)  
  62.                                     errorCode = CxnetConstants.MAILFORMEDURL_EXCEPTION;  
  63.                                 else if (c == java.net.NoRouteToHostException.class)  
  64.                                     errorCode = CxnetConstants.NOROUTETOHOST_EXCEPTION;  
  65.                                 else if (c == java.net.PortUnreachableException.class)  
  66.                                     errorCode = CxnetConstants.PORTUNREACHABLE_EXCEPTION;  
  67.                                 else if (c == java.net.ProtocolException.class)  
  68.                                     errorCode = CxnetConstants.PROTOCOL_EXCEPTION;  
  69.                                 else if (c == java.net.SocketException.class)  
  70.                                     errorCode = CxnetConstants.SOCKET_EXCEPTION;  
  71.                                 else if (c == java.net.SocketTimeoutException.class)  
  72.                                     errorCode = CxnetConstants.SOCKETTIMEOUT_EXCEPTION;  
  73.                                 else if (c == java.net.UnknownHostException.class)  
  74.                                     errorCode = CxnetConstants.UNKNOWNHOST_EXCEPTION;  
  75.                                 else if (c == java.net.UnknownServiceException.class)  
  76.                                     errorCode = CxnetConstants.UNKNOWNSERVICE_EXCEPTION;  
  77.                                 else if (c == java.net.URISyntaxException.class)  
  78.                                     errorCode = CxnetConstants.URISYNTAX_EXCEPTION;  
  79.                                 th.printStackTrace();  
  80.                                 System.out.println("CxClient::start() A 2 errCode=" + errorCode);  
  81.                             }  
  82.                             m_listener.disconnected(errorCode);  
  83.                         }  
  84.                     }  
  85.                 }  
  86.             });  
  87.         } catch (Exception e) {  
  88.             System.out.println("CxClient::start() excep B 1");  
  89.             e.printStackTrace();  
  90.             System.out.println("CxClient::start() excep B 2");  
  91.             m_listener.disconnected(CxnetConstants.UNKNOWN_EXCEPTION);  
  92.         }  
  93.   
  94.     }//start()  
  95.   
  96.     @Override  
  97.     protected void stopInThread(boolean releaseResOnly) {  
  98.         final Bootstrap bs = m_bootstrap;  
  99.         m_bootstrap = null;  
  100.         super.stopInThread(bs, releaseResOnly);  
  101.     }  
  102.   
  103. }//end cls - CxClient  

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值