From 81bd0388494d45463de42cd91bd8c33f10f0030a Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期四, 17 六月 2021 10:27:21 +0800
Subject: [PATCH] 1. 新增用电量数据的接收协议 2. 调整socket接收模块的代码结构

---
 src/main/kotlin/com/flightfeather/uav/socket/UnderwaySocketServer.kt |   59 +++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 41 insertions(+), 18 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/socket/UnderwaySocketServer.kt b/src/main/kotlin/com/flightfeather/uav/socket/UnderwaySocketServer.kt
index 699f09e..c9d98ea 100644
--- a/src/main/kotlin/com/flightfeather/uav/socket/UnderwaySocketServer.kt
+++ b/src/main/kotlin/com/flightfeather/uav/socket/UnderwaySocketServer.kt
@@ -1,11 +1,14 @@
 package com.flightfeather.uav.socket
 
+import com.flightfeather.uav.socket.processor.BaseProcessor
 import io.netty.bootstrap.ServerBootstrap
+import io.netty.channel.ChannelHandler
 import io.netty.channel.ChannelInitializer
 import io.netty.channel.ChannelOption
 import io.netty.channel.nio.NioEventLoopGroup
 import io.netty.channel.socket.nio.NioServerSocketChannel
 import io.netty.channel.socket.nio.NioSocketChannel
+import io.netty.handler.codec.LineBasedFrameDecoder
 import io.netty.handler.codec.string.StringDecoder
 import io.netty.handler.codec.string.StringEncoder
 import java.nio.charset.Charset
@@ -19,8 +22,12 @@
     private val bossGroup = NioEventLoopGroup()
     private val workerGroup = NioEventLoopGroup()
 
-    fun startServer(port: Int) {
-        initialize()?.bind(port)?.sync()
+    fun startUnderwayServer(port: Int, processor: BaseProcessor) {
+        underwayServer(processor)?.bind(port)?.sync()
+    }
+
+    fun startElectricServer(port: Int, processor: BaseProcessor) {
+        electricServer(processor)?.bind(port)?.sync()
     }
 
     fun stopServer() {
@@ -28,28 +35,44 @@
         workerGroup.shutdownGracefully()
     }
 
-    private fun initialize(): ServerBootstrap? {
-
+    private fun newServer(childHandler: ChannelHandler): ServerBootstrap? {
         try {
             return ServerBootstrap()
-                    .group(bossGroup, workerGroup)
-                    .channel(NioServerSocketChannel::class.java)
-                    .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(UAVByteDataDecoder())
-                                    ?.addLast("encoder", StringEncoder(Charset.forName("UTF-8")))
-                                    ?.addLast(ServerHandler())
-                        }
-                    })
+                .group(bossGroup, workerGroup)
+                .channel(NioServerSocketChannel::class.java)
+                .option(ChannelOption.SO_BACKLOG, 128)
+                .childOption(ChannelOption.SO_KEEPALIVE, true)
+                .childOption(ChannelOption.TCP_NODELAY, true)
+                .childHandler(childHandler)
         } catch (e: Throwable) {
             e.printStackTrace()
         }
 
         return null
     }
+
+    /**
+     * 鐢ㄧ數閲忔湇鍔$
+     */
+    private fun electricServer(processor: BaseProcessor): ServerBootstrap? = newServer(object : ChannelInitializer<NioSocketChannel>() {
+        override fun initChannel(p0: NioSocketChannel?) {
+            p0?.pipeline()
+                ?.addLast("lineDecoder", LineBasedFrameDecoder(1024))
+                ?.addLast("stringDecoder", StringDecoder())
+                ?.addLast("encoder", StringEncoder(Charset.forName("UTF-8")))
+                ?.addLast(ServerHandler(processor))
+        }
+    })
+
+    /**
+     * 澶氬弬鏁拌蛋鑸湇鍔$
+     */
+    private fun underwayServer(processor: BaseProcessor):ServerBootstrap? = newServer(object : ChannelInitializer<NioSocketChannel>() {
+        override fun initChannel(p0: NioSocketChannel?) {
+            p0?.pipeline()
+                ?.addLast(UAVByteDataDecoder())
+                ?.addLast("encoder", StringEncoder(Charset.forName("UTF-8")))
+                ?.addLast(ServerHandler(processor))
+        }
+    })
 }
\ No newline at end of file

--
Gitblit v1.9.3