From 58a16c3340f92a1ec0362565020f31de56faaf3e Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期四, 19 九月 2019 16:35:48 +0800
Subject: [PATCH] 1. 增加远程查看版本指令逻辑

---
 src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt |   38 +++++++++++++++++++++++++-------------
 1 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt b/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt
index bf89577..5ee34b1 100644
--- a/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt
+++ b/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt
@@ -1,12 +1,19 @@
 package com.flightfeather.obd.socket
 
 import io.netty.bootstrap.ServerBootstrap
+import io.netty.buffer.ByteBuf
+import io.netty.buffer.Unpooled
 import io.netty.channel.ChannelInitializer
 import io.netty.channel.ChannelOption
 import io.netty.channel.nio.NioEventLoopGroup
-import io.netty.channel.socket.SocketChannel
 import io.netty.channel.socket.nio.NioServerSocketChannel
 import io.netty.channel.socket.nio.NioSocketChannel
+import io.netty.handler.codec.DelimiterBasedFrameDecoder
+import io.netty.handler.codec.bytes.ByteArrayDecoder
+import io.netty.handler.codec.bytes.ByteArrayEncoder
+import io.netty.handler.codec.string.StringDecoder
+import io.netty.handler.codec.string.StringEncoder
+import java.nio.charset.Charset
 
 /**
  * socket闀胯繛鎺ユ湇鍔$
@@ -15,33 +22,38 @@
  */
 class SocketServerClient {
 
-//    val sessionMap = HashMap<String, IoSession>
+    private val bossGroup = NioEventLoopGroup()
+    private val workerGroup = NioEventLoopGroup()
 
     fun startServer(port: Int) {
         initialize()?.bind(port)?.sync()
-                ?.channel()?.closeFuture()?.sync()
+    }
+
+    fun stopServer() {
+        bossGroup.shutdownGracefully()
+        workerGroup.shutdownGracefully()
     }
 
     private fun initialize(): ServerBootstrap? {
-        val bossGroup = NioEventLoopGroup()
-        val workerGroup = NioEventLoopGroup()
 
         try {
             return ServerBootstrap()
                     .group(bossGroup, workerGroup)
                     .channel(NioServerSocketChannel::class.java)
-                    .childHandler(object : ChannelInitializer<NioSocketChannel>() {
-                        override fun initChannel(p0: NioSocketChannel?) {
-                            p0?.pipeline()?.addLast(ServerHandler())
-                        }
-                    })
                     .option(ChannelOption.SO_BACKLOG, 128)
                     .childOption(ChannelOption.SO_KEEPALIVE, true)
+                    .childOption(ChannelOption.TCP_NODELAY, true)
+                    .childHandler(object : ChannelInitializer<NioSocketChannel>() {
+                        override fun initChannel(p0: NioSocketChannel?) {
+                            p0?.pipeline()
+//                                    ?.addLast("decoder", StringDecoder())
+                                    ?.addLast(ObdByteDataDecoder())
+                                    ?.addLast("encoder", StringEncoder(Charset.forName("UTF-8")))
+                                    ?.addLast(ServerHandler())
+                        }
+                    })
         } catch (e: Throwable) {
             e.printStackTrace()
-        } finally {
-            bossGroup.shutdownGracefully()
-            workerGroup.shutdownGracefully()
         }
 
         return null

--
Gitblit v1.9.3