<commit_before>/**
 * kcp服务器
 */
package org.beykery.jkcp;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import java.net.InetSocketAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author beykery
 */
public abstract class KcpServer implements Output, KcpListerner
{

  private static final Logger LOG = LoggerFactory.getLogger(KcpServer.class);
  private final NioDatagramChannel channel;
  private final InetSocketAddress addr;
  private int nodelay;
  private int interval = Kcp.IKCP_INTERVAL;
  private int resend;
  private int nc;
  private int sndwnd = Kcp.IKCP_WND_SND;
  private int rcvwnd = Kcp.IKCP_WND_RCV;
  private int mtu = Kcp.IKCP_MTU_DEF;
  private KcpThread[] workers;
  private boolean running;
  private long timeout;

  /**
   * server
   *
   * @param port
   * @param workerSize
   */
  public KcpServer(int port, int workerSize)
  {
    if (port <= 0 || workerSize <= 0)
    {
      throw new IllegalArgumentException("参数非法");
    }
    this.workers = new KcpThread[workerSize];
    final NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.channel(NioDatagramChannel.class);
    bootstrap.group(nioEventLoopGroup);
    bootstrap.handler(new ChannelInitializer<NioDatagramChannel>()
    {

      @Override
      protected void initChannel(NioDatagramChannel ch) throws Exception
      {
        ChannelPipeline cp = ch.pipeline();
        cp.addLast(new KcpServer.UdpHandler());
      }
    });
    ChannelFuture sync = bootstrap.bind(port).syncUninterruptibly();
    channel = (NioDatagramChannel) sync.channel();
    addr = channel.localAddress();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable()
    {
      @Override
      public void run()
      {
        nioEventLoopGroup.shutdownGracefully();
      }
    }));
  }

  /**
   * 开始
   */
  public void start()
  {
    if (!this.running)
    {
      this.running = true;
      for (int i = 0; i < this.workers.length; i++)
      {
        workers[i] = new KcpThread(this, this);
        workers[i].wndSize(sndwnd, rcvwnd);
        workers[i].noDelay(nodelay, interval, resend, nc);
        workers[i].setMtu(mtu);
        workers[i].setTimeout(timeout);
        workers[i].start();
      }
    }
  }

  /**
   * close
   *
   * @return
   */
  public ChannelFuture close()
  {
    if (this.running)
    {
      for (KcpThread kt : this.workers)
      {
        kt.close();
      }
      this.workers = null;<commit_msg>thread name<commit_after>/**
 * kcp服务器
 */
package org.beykery.jkcp;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import java.net.InetSocketAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author beykery
 */
public abstract class KcpServer implements Output, KcpListerner
{

  private static final Logger LOG = LoggerFactory.getLogger(KcpServer.class);
  private final NioDatagramChannel channel;
  private final InetSocketAddress addr;
  private int nodelay;
  private int interval = Kcp.IKCP_INTERVAL;
  private int resend;
  private int nc;
  private int sndwnd = Kcp.IKCP_WND_SND;
  private int rcvwnd = Kcp.IKCP_WND_RCV;
  private int mtu = Kcp.IKCP_MTU_DEF;
  private KcpThread[] workers;
  private boolean running;
  private long timeout;

  /**
   * server
   *
   * @param port
   * @param workerSize
   */
  public KcpServer(int port, int workerSize)
  {
    if (port <= 0 || workerSize <= 0)
    {
      throw new IllegalArgumentException("参数非法");
    }
    this.workers = new KcpThread[workerSize];
    final NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.channel(NioDatagramChannel.class);
    bootstrap.group(nioEventLoopGroup);
    bootstrap.handler(new ChannelInitializer<NioDatagramChannel>()
    {

      @Override
      protected void initChannel(NioDatagramChannel ch) throws Exception
      {
        ChannelPipeline cp = ch.pipeline();
        cp.addLast(new KcpServer.UdpHandler());
      }
    });
    ChannelFuture sync = bootstrap.bind(port).syncUninterruptibly();
    channel = (NioDatagramChannel) sync.channel();
    addr = channel.localAddress();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable()
    {
      @Override
      public void run()
      {
        nioEventLoopGroup.shutdownGracefully();
      }
    }));
  }

  /**
   * 开始
   */
  public void start()
  {
    if (!this.running)
    {
      this.running = true;
      for (int i = 0; i < this.workers.length; i++)
      {
        workers[i] = new KcpThread(this, this);
        workers[i].setName("kcp thread " + i);
        workers[i].wndSize(sndwnd, rcvwnd);
        workers[i].noDelay(nodelay, interval, resend, nc);
        workers[i].setMtu(mtu);
        workers[i].setTimeout(timeout);
        workers[i].start();
      }
    }
  }

  /**
   * close
   *
   * @return
   */
  public ChannelFuture close()
  {
    if (this.running)
    {
      for (KcpThread kt : this.workers)
      {
        kt.close();
      }
      this.workers = null;
{'fraction_non_alphanumeric': 0.1127409176189664, 'fraction_numerical': 0.0017056114617090227, 'mean_word_length': 3.1237693389592125, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4638759', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1902, 'n_words': 385}