Newer
Older
Twilight / src / main / kotlin / net / taehui / twilight / etc / EtcBoot.kt
@taehui taehui on 19 Aug 1 KB 2024-08-19 오후 2:38
package net.taehui.twilight.etc

import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.Channel
import io.netty.channel.ChannelInitializer
import io.netty.channel.EventLoopGroup
import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.handler.codec.http.HttpClientCodec
import io.netty.handler.codec.http.HttpObjectAggregator
import io.netty.handler.codec.http.HttpServerCodec
import net.taehui.twilight.Logger

class EtcBoot(eventLoopGroup: EventLoopGroup) : Logger, AutoCloseable {
    private val mainBootstrap: ServerBootstrap = ServerBootstrap().group(eventLoopGroup).channel(NioServerSocketChannel::class.java)
        .childHandler(object : ChannelInitializer<SocketChannel>() {
            public override fun initChannel(ch: SocketChannel) {
                ch.pipeline()
                    .addLast(HttpServerCodec())
                    .addLast(HttpObjectAggregator(Int.MAX_VALUE))
                    .addLast(HttpClientCodec())
                    .addLast(EtcAvatar())
            }
        })
    private val mainChannel: Channel

    init {
        logInfo("Loading Etc")
        mainChannel = mainBootstrap.bind(8300).channel().closeFuture()
            .addListener {
                logInfo("Closed Etc")
            }.channel()
    }

    override fun close() {
        mainChannel.close()
    }
}