/*
 * Copyright (c) 2016 MCPhoton <http://mcphoton.org> and contributors.
 *
 * This file is part of the Photon Server Implementation <https://github.com/mcphoton/Photon-Server>.
 *
 * The Photon Server Implementation is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Photon Server Implementation is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.mcphoton.impl.network;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.util.Optional;
import org.mcphoton.entity.living.Player;
import org.mcphoton.network.Client;
import org.mcphoton.network.ConnectionState;

/**
 * Implementation of the Client interface.
 *
 * @author TheElectronWill
 *
 */
public final class PhotonClient implements Client {

	final InetSocketAddress address;
	final Codec[] codecs;//the codecs, in the output order (server -> client).
	final SocketChannel channel;
	final MessageReader messageReader;
	final MessageWriter messageWriter;
	volatile Optional<Player> player = Optional.empty();
	volatile int authVerifyToken = -1;
	volatile ConnectionState state = ConnectionState.INIT;

	public PhotonClient(SocketChannel channel, Codec[] codecs) throws IOException {
		this.channel = channel;
		this.codecs = codecs;
		this.messageReader = new MessageReader(channel, 256, 512);
		this.messageWriter = new MessageWriter(channel);
		this.address = (InetSocketAddress) channel.getRemoteAddress();
	}

	@Override
	public InetSocketAddress getAddress() {
		return address;
	}

	@Override
	public Optional<Player> getPlayer() {
		return player;
	}

	@Override
	public ConnectionState getConnectionState() {
		return state;
	}

	@Override
	public void setConnectionState(ConnectionState state) {
		this.state = state;
	}

	@Override
	public void closeConnection() throws IOException {
		channel.close();
	}

}

@@ -1356,384 +1356,510 @@
 nal 
-Codec%5B%5D codecs;//the codecs, in the output order (server -%3E client).%0A%09final SocketChannel channel;%0A%09final MessageReader messageReader;%0A%09final MessageWriter messageWriter;%0A%09volatile Optional%3CPlayer%3E player = Optional.empty();%0A%09volatile int authVerifyToken = -1;%0A%09volatile ConnectionState state = ConnectionState.INIT;%0A%0A%09public PhotonClient(SocketChannel channel, Codec%5B%5D codecs
+SocketChannel channel;%0A%09final PacketReader packetReader;%0A%09final PacketWriter packetWriter;%0A%09volatile Optional%3CPlayer%3E player = Optional.empty();%0A%09volatile int authVerifyToken = -1;%0A%09volatile ConnectionState state = ConnectionState.INIT;%0A%09volatile Codec cipherCodec;%0A%09volatile Codec compressionCodec;%0A%0A%09public PhotonClient(SocketChannel channel) throws IOException %7B%0A%09%09this(channel, new NoCodec(), new NoCodec());%0A%09%7D%0A%0A%09public PhotonClient(SocketChannel channel, Codec cipherCodec, Codec compressionCodec
 ) th
@@ -1915,22 +1915,32 @@
 is.c
+ipherC
 odec
-s
  = c
+ipherC
 odec
-s
 ;%0A%09%09
@@ -1944,23 +1944,66 @@
 %0A%09%09this.
-message
+compressionCodec = compressionCodec;%0A%09%09this.packet
 Reader =
@@ -2007,23 +2007,22 @@
 r = new 
-Message
+Packet
 Reader(c
@@ -2033,11 +2033,11 @@
 el, 
-256
+128
 , 51
@@ -2051,15 +2051,14 @@
 his.
-message
+packet
 Writ
@@ -2070,15 +2070,14 @@
 new 
-Message
+Packet
 Writ

Improve the PhotonClient