001/* 002 * Trident - A Multithreaded Server Alternative 003 * Copyright 2014 The TridentSDK Team 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package net.tridentsdk.server.netty; 019 020import io.netty.channel.ChannelHandlerContext; 021import io.netty.channel.ChannelInitializer; 022import io.netty.channel.ChannelOption; 023import io.netty.channel.ChannelPromise; 024import io.netty.channel.socket.SocketChannel; 025import net.tridentsdk.server.netty.packet.*; 026 027import javax.annotation.concurrent.ThreadSafe; 028 029/** 030 * The netty channel initializer which appends the handlers to the socket channel 031 * 032 * @author The TridentSDK Team 033 */ 034@ThreadSafe 035public class ClientChannelInitializer extends ChannelInitializer<SocketChannel> { 036 private ClientConnection connection; 037 038 @Override 039 protected void initChannel(SocketChannel channel) throws Exception { 040 //channel.config().setOption(ChannelOption.IP_TOS, 24); 041 channel.config().setOption(ChannelOption.TCP_NODELAY, true); 042 043 this.connection = ClientConnection.registerConnection(channel); 044 045 //Decode: 046 channel.pipeline().addLast(new PacketDecrypter()); 047 channel.pipeline().addLast(new PacketDecoder()); 048 channel.pipeline().addLast(new PacketHandler()); 049 050 //Encode: 051 channel.pipeline().addLast(new PacketEncrypter()); 052 channel.pipeline().addLast(new PacketEncoder()); 053 } 054 055 @Override 056 public void channelInactive(ChannelHandlerContext ctx) throws Exception { 057 super.channelInactive(ctx); 058 059 this.connection.logout(); 060 } 061 062 @Override 063 public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { 064 super.disconnect(ctx, promise); 065 066 this.connection.logout(); 067 } 068}