/*
* Created by Andrey Cherkashin (acherkashin)
* http://acherkashin.me
*
* License
* Copyright (c) 2015 Andrey Cherkashin
* The project released under the MIT license: http://opensource.org/licenses/MIT
*/
package ragefist.core.network;
import com.juniform.JUniformObject;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author acherkashin
*/
public abstract class SocketClient
{
protected PacketBuffer _packetBuffer;
public abstract int getPacketBufferSize();
public abstract int read(final ByteBuffer buffer) throws IOException;
public abstract int write(final ByteBuffer buffer) throws IOException;
public abstract void sendPacket(JUniformObject result);
public abstract void sendPacket(byte[] bytes);
public abstract void close();
public abstract JUniformObject[] readPackets(ByteBuffer readBuffer) throws IOException;
public Object getPacketBuffer(Class packetBufferClass) {
if (_packetBuffer == null ||
false == _packetBuffer.getClass().equals(packetBufferClass)
) {
_packetBuffer = null;
try {
_packetBuffer = (PacketBuffer)packetBufferClass.getConstructor(Integer.class).newInstance(this.getPacketBufferSize());
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
}
}
return _packetBuffer;
}
}