包名变更

This commit is contained in:
zhangzc@shuhua.com
2017-06-06 08:46:12 +08:00
parent 36934e8011
commit c8fe421825
15 changed files with 29 additions and 152 deletions

View File

@@ -16,13 +16,6 @@
<version>4.1.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC03</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>

View File

@@ -1,8 +1,8 @@
package com.shua.server.decoder;
package cn._94zichao.server.decoder;
import com.shua.server.entity.SocketModel;
import com.shua.server.util.ByteUtil;
import com.shua.server.util.Content;
import cn._94zichao.server.util.ByteUtil;
import cn._94zichao.server.entity.SocketModel;
import cn._94zichao.server.util.Content;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

View File

@@ -1,6 +1,6 @@
package com.shua.server.encoder;
package cn._94zichao.server.encoder;
import com.shua.server.entity.SocketModel;
import cn._94zichao.server.entity.SocketModel;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

View File

@@ -1,7 +1,7 @@
package com.shua.server.encoder;
package cn._94zichao.server.encoder;
import com.shua.server.entity.SocketModel;
import com.shua.server.util.ByteUtil;
import cn._94zichao.server.entity.SocketModel;
import cn._94zichao.server.util.ByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
@@ -18,7 +18,7 @@ public class ToModelEncoder extends MessageToByteEncoder<SocketModel> {
byteBuf.writeByte(socketModel.getType());
byte[] data = socketModel.getData();
for (int i = 0;i<data.length;i++){
ByteUtil.writeByte(byteBuf,data[i]);
ByteUtil.writeByte(byteBuf, data[i]);
}
byteBuf.writeByte(socketModel.getEnd());

View File

@@ -1,8 +1,8 @@
package com.shua.server.entity;
package cn._94zichao.server.entity;
import com.shua.server.util.ByteUtil;
import com.shua.server.util.Content;
import cn._94zichao.server.util.ByteUtil;
import cn._94zichao.server.util.Content;
/**
* Created by SHUA on 2017/5/15.

View File

@@ -1,4 +1,4 @@
package com.shua.server.factory;
package cn._94zichao.server.factory;
import io.netty.channel.ChannelHandlerContext;

View File

@@ -1,10 +1,9 @@
package com.shua.server.handler;
package cn._94zichao.server.handler;
import com.shua.server.BarrierServer;
import com.shua.server.entity.SocketModel;
import com.shua.server.util.ByteUtil;
import com.shua.server.util.Content;
import cn._94zichao.server.util.ByteUtil;
import cn._94zichao.server.entity.SocketModel;
import cn._94zichao.server.util.Content;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
@@ -18,7 +17,6 @@ public class BarrierServerHandler extends ChannelInboundHandlerAdapter { // (1)
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
BarrierServer.map.put(ctx.channel().id(), ctx);
}
@Override
@@ -27,7 +25,7 @@ public class BarrierServerHandler extends ChannelInboundHandlerAdapter { // (1)
//记录日志
ctx.writeAndFlush(sk);
//校验CRC
if (!ByteUtil.isCRC(sk.retAllData(),sk.getCrcData())){
if (!ByteUtil.isCRC(sk.retAllData(), sk.getCrcData())){
//发送错误标识的包
//返回
}
@@ -65,7 +63,7 @@ public class BarrierServerHandler extends ChannelInboundHandlerAdapter { // (1)
//channel失效处理,客户端下线或者强制退出等任何情况都触发这个方法
System.out.println("捕获异常");
//移除当前channel句柄
BarrierServer.map.remove(ctx.channel().id());
super.channelInactive(ctx);
}
}

View File

@@ -1,4 +1,4 @@
package com.shua.server.service;
package cn._94zichao.server.service;
/**
* Created by SHUA on 2017/5/18.

View File

@@ -1,9 +1,9 @@
package com.shua.server.tcpServer;
package cn._94zichao.server.tcpServer;
import com.shua.server.decoder.EndBasedDecoder;
import com.shua.server.encoder.ToModelEncoder;
import com.shua.server.handler.BarrierServerHandler;
import com.shua.server.util.Content;
import cn._94zichao.server.decoder.EndBasedDecoder;
import cn._94zichao.server.handler.BarrierServerHandler;
import cn._94zichao.server.encoder.ToModelEncoder;
import cn._94zichao.server.util.Content;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;

View File

@@ -1,4 +1,4 @@
package com.shua.server.util;
package cn._94zichao.server.util;
import io.netty.buffer.ByteBuf;

View File

@@ -1,4 +1,4 @@
package com.shua.server.util;
package cn._94zichao.server.util;
/**
* Created by SHUA on 2017/5/16.

View File

@@ -1,4 +1,4 @@
package com.shua.server.util;
package cn._94zichao.server.util;
/**
* Created by SHUA on 2017/5/25.

View File

@@ -1,15 +0,0 @@
package com.shua.main;
import com.shua.server.BarrierServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by SHUA on 2017/5/11.
*/
public class ServiceStart {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
((BarrierServer)ctx.getBean("barrierServer")).run();
}
}

View File

@@ -1,75 +0,0 @@
package com.shua.server;
import com.shua.server.decoder.EndBasedDecoder;
import com.shua.server.encoder.TestEncoder;
import com.shua.server.entity.SocketModel;
import com.shua.server.handler.BarrierServerHandler;
import com.shua.server.util.Content;
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;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Discards any incoming data.
*/
public class BarrierServer implements Runnable {
public static Map map = new ConcurrentHashMap();
static final EventExecutorGroup group = new DefaultEventExecutorGroup(16);
private int port;
public BarrierServer() {
this.port = 8888;
}
public BarrierServer(int port) {
this.port = port;
}
public void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
@Override
public void initChannel(SocketChannel ch) throws Exception {
//添加结束符解码器
ch.pipeline().addLast("decoder1",new EndBasedDecoder(new SocketModel(),Content.END,Content.ACK,Content.REQ,Content.ANS,Content.NAK));
//添加编码器
ch.pipeline().addLast("encoder",new TestEncoder());
//添加帧解码器和业务处理
ch.pipeline().addLast(group, "handler", new BarrierServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(port).sync(); // (7)
System.out.println(5);
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public void start(){
new Thread(this).start();
}
}

View File

@@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
default-autowire="byName" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Spring公共配置文件</description>
<!--END 资源文件配置 -->
<!--annotation配置 -->
<context:annotation-config />
<context:component-scan base-package="com.shua" />
<!--END annotation配置 -->
<bean id="barrierServer" class="com.shua.server.BarrierServer"
init-method="run">
</bean>
</beans>