All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.roubsite.RoubSiteFS.server.RSFSServer Maven / Gradle / Ivy

The newest version!
package com.roubsite.RoubSiteFS.server;

import org.apache.log4j.BasicConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.roubsite.RoubSiteFS.block.FSConfig;
import com.roubsite.RoubSiteFS.msg.request.RSFSRequest;
import com.roubsite.RoubSiteFS.msg.response.RSFSResponse;
import com.roubsite.RoubSiteFS.server.common.RpcDecoder;
import com.roubsite.RoubSiteFS.server.common.RpcEncoder;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class RSFSServer {
	private static final Logger logger = LoggerFactory.getLogger(RSFSServer.class);

	private static final String ip = FSConfig.getListen();
	private static final int port = FSConfig.getPort();

	public void start() throws Exception {
		logger.info("RoubSiteFS服务初始化");
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
			final ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
					.option(ChannelOption.SO_BACKLOG, 1024).option(ChannelOption.SO_SNDBUF, 32 * 1024)
					.option(ChannelOption.SO_RCVBUF, 32 * 1024).option(ChannelOption.SO_KEEPALIVE, true)
					.childHandler(new ChannelInitializer() {
						protected void initChannel(SocketChannel socketChannel) throws Exception {
							socketChannel.pipeline().addLast(new RpcDecoder(RSFSRequest.class))
									.addLast(new RpcEncoder(RSFSResponse.class)).addLast(new RSFSServerHandler());
						}
					});
			serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); // 开启长连接
			ChannelFuture future = serverBootstrap.bind(ip, port).sync();
			future.channel().closeFuture().sync();
			logger.info("RoubSiteFS服务启动成功");
		} catch (Exception e) {
			logger.error("RoubSiteFS服务发生错误:", e);
		} finally {
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}

	public static void main(String[] args) throws Exception {
		BasicConfigurator.configure();
		new RSFSServer().start();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy