Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import io.netty.channel.Channel;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.SimpleChannelInboundHandler;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.LinkedBlockingQueue;
- public class T2Handler extends SimpleChannelInboundHandler<T2Packet> {
- private Channel channel;
- private final BlockingQueue<T2Packet> packetsQueue = new LinkedBlockingQueue<>();
- private boolean destabilised = false;
- public T2Handler(boolean client) {
- Thread thread = new Thread(() -> {
- try {
- T2Packet p;
- while (!destabilised) {
- p = packetsQueue.take();
- System.out.println(p);
- if (!client)send(new T2Packet(p.getId(), p.getValue()+1, "t2 answer "+p.getName()));
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- });
- thread.setDaemon(true);
- thread.start();
- }
- public void send(T2Packet packet){
- channel.writeAndFlush(packet);
- }
- @Override
- public void channelActive(ChannelHandlerContext ctx) throws Exception {
- channel = ctx.channel();
- super.channelActive(ctx);
- }
- @Override
- protected void channelRead0(ChannelHandlerContext ctx, T2Packet msg) throws Exception {
- packetsQueue.offer( msg);
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- destabilised = true;
- super.exceptionCaught(ctx, cause);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement