feiyu02
2024-01-26 01eae19a4870033e879a3faa6749eece92926cab
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))
        }
    })
}