| | |
| | | package com.flightfeather.uav.socket |
| | | |
| | | import com.flightfeather.uav.domain.repository.SceneInfoRep |
| | | import com.flightfeather.uav.domain.repository.SourceTraceRep |
| | | import com.flightfeather.uav.socket.handler.ServerHandler |
| | | import com.flightfeather.uav.socket.handler.UnderwayWebSocketServerHandler |
| | | 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.http.HttpObjectAggregator |
| | | import io.netty.handler.codec.http.HttpServerCodec |
| | | import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler |
| | | import io.netty.handler.codec.string.StringDecoder |
| | | import io.netty.handler.codec.string.StringEncoder |
| | | import org.springframework.stereotype.Component |
| | | import java.nio.charset.Charset |
| | | |
| | | /********************************************************************************* |
| | | * 走航监测数据socket长连接服务端 |
| | | * 用于接收解析走航监测数据,前端监测设备目前包括车载走航、无人机走航以及无人船走航三种类型 |
| | | * *******************************************************************************/ |
| | | class UnderwaySocketServer { |
| | | @Component |
| | | class UnderwaySocketServer( |
| | | private val sceneInfoRep: SceneInfoRep, |
| | | private val sourceTraceRep: SourceTraceRep |
| | | ) { |
| | | |
| | | 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 startWebSocketServer(port: Int) { |
| | | webSocketServer()?.bind(port)?.sync() |
| | | } |
| | | |
| | | fun stopServer() { |
| | |
| | | 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)) |
| | | } |
| | | }) |
| | | |
| | | /** |
| | | * 多参数走航服务端 |
| | | */ |
| | | private fun webSocketServer():ServerBootstrap? = newServer(object : ChannelInitializer<NioSocketChannel>() { |
| | | override fun initChannel(p0: NioSocketChannel?) { |
| | | p0?.pipeline() |
| | | ?.addLast(HttpServerCodec()) |
| | | ?.addLast(HttpObjectAggregator(65535)) |
| | | ?.addLast(WebSocketServerProtocolHandler("/ws")) |
| | | ?.addLast(UnderwayWebSocketServerHandler(sceneInfoRep, sourceTraceRep)) |
| | | } |
| | | }) |
| | | } |