AbstractPacket.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
import java.nio.ByteBuffer;
/**
* @author KenM
*
*/
public abstract class AbstractPacket<T extends MMOClient>
{
protected ByteBuffer _buf;
protected T _client;
protected void setClient(T client)
{
_client = client;
}
public T getClient()
{
return _client;
}
protected void setByteBuffer(ByteBuffer buf)
{
_buf = buf;
}
protected ByteBuffer getByteBuffer()
{
return _buf;
}
}
IAcceptFilter.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
import java.nio.channels.SocketChannel;
/**
* @author KenM
*
*/
public interface IAcceptFilter
{
public boolean accept(SocketChannel sc);
}
IClientFactory.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
/**
* @author KenM
*
*/
public interface IClientFactory<T extends MMOClient>
{
public T create(MMOConnection<T> con);
}
IMMOExecutor.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
/**
* @author KenM
*
*/
public interface IMMOExecutor<T extends MMOClient>
{
public void execute(ReceivablePacket<T> packet);
}
IPacketHandler.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
import java.nio.ByteBuffer;
/**
* @author KenM
*
*/
public interface IPacketHandler<T extends MMOClient>
{
public ReceivablePacket<T> handlePacket(ByteBuffer buf, T client);
}
MMOClient.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
import java.nio.ByteBuffer;
/**
* @author KenM
*
*/
public abstract class MMOClient<T extends MMOConnection>
{
private T _connection;
@SuppressWarnings("unchecked")
public MMOClient(T con)
{
this.setConnection(con);
con.setClient(this);
}
protected void setConnection(T con)
{
_connection = con;
}
public T getConnection()
{
return _connection;
}
public void closeNow()
{
this.getConnection().closeNow();
}
public void closeLater()
{
this.getConnection().closeLater();
}
public abstract boolean decrypt(ByteBuffer buf, int size);
public abstract boolean encrypt(ByteBuffer buf, int size);
protected void onDisconection()
{
}
protected void onForcedDisconnection()
{
}
}MMOConnection.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
import java.nio.ByteBuffer;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import javolution.util.FastList;
/**
* @author KenM
*
*/
public class MMOConnection<T extends MMOClient>
{
private final SelectorThread<T> _selectorThread;
private T _client;
private SocketChannel _socketChannel;
private FastList<SendablePacket<T>> _sendQueue = new FastList<SendablePacket<T>>();
private SelectionKey _selectionKey;
private ByteBuffer _readBuffer;
private ByteBuffer _writeBuffer;
private boolean _pendingClose;
public MMOConnection(SelectorThread<T> selectorThread, SocketChannel sc, SelectionKey key)
{
_selectorThread = selectorThread;
this.setSocketChannel(sc);
this.setSelectionKey(key);
}
protected void setClient(T client)
{
_client = client;
}
protected T getClient()
{
return _client;
}
public void sendPacket(SendablePacket<T> sp)
{
sp.setClient(this.getClient());
synchronized (this.getSendQueue())
{
if (!_pendingClose)
{
try
{
this.getSelectionKey().interestOps(this.getSelectionKey().interestOps() | SelectionKey.OP_WRITE);
this.getSendQueue().addLast(sp);
}
catch (CancelledKeyException e)
{
// ignore
}
}
}
}
protected SelectorThread<T> getSelectorThread()
{
return _selectorThread;
}
protected void setSelectionKey(SelectionKey key)
{
_selectionKey = key;
}
protected SelectionKey getSelectionKey()
{
return _selectionKey;
}
protected void enableReadInterest()
{
try
{
this.getSelectionKey().interestOps(this.getSelectionKey().interestOps() | SelectionKey.OP_READ);
}
catch (CancelledKeyException e)
{
// ignore
}
}
protected void disableReadInterest()
{
try
{
this.getSelectionKey().interestOps(this.getSelectionKey().interestOps() & ~SelectionKey.OP_READ);
}
catch (CancelledKeyException e)
{
// ignore
}
}
protected void enableWriteInterest()
{
try
{
this.getSelectionKey().interestOps(this.getSelectionKey().interestOps() | SelectionKey.OP_WRITE);
}
catch (CancelledKeyException e)
{
// ignore
}
}
protected void disableWriteInterest()
{
try
{
this.getSelectionKey().interestOps(this.getSelectionKey().interestOps() & ~SelectionKey.OP_WRITE);
}
catch (CancelledKeyException e)
{
// ignore
}
}
protected void setSocketChannel(SocketChannel sc)
{
_socketChannel = sc;
}
public SocketChannel getSocketChannel()
{
return _socketChannel;
}
protected FastList<SendablePacket<T>> getSendQueue()
{
return _sendQueue;
}
protected void setWriteBuffer(ByteBuffer buf)
{
_writeBuffer = buf;
}
protected ByteBuffer getWriteBuffer()
{
return _writeBuffer;
}
protected void setReadBuffer(ByteBuffer buf)
{
_readBuffer = buf;
}
protected ByteBuffer getReadBuffer()
{
return _readBuffer;
}
public boolean isClosed()
{
return _pendingClose;
}
protected void closeNow()
{
synchronized (this.getSendQueue())
{
_pendingClose = true;
this.getSendQueue().clear();
this.disableWriteInterest();
}
this.getSelectorThread().closeConnection(this);
}
public void close(SendablePacket<T> sp)
{
synchronized (this.getSendQueue())
{
this.getSendQueue().clear();
this.sendPacket(sp);
_pendingClose = true;
}
this.getSelectorThread().closeConnection(this);
}
protected void closeLater()
{
synchronized (this.getSendQueue())
{
_pendingClose = true;
}
this.getSelectorThread().closeConnection(this);
}
protected void onDisconection()
{
this.getClient().onDisconection();
}
protected void onForcedDisconnection()
{
this.getClient().onForcedDisconnection();
}
}
ReceivablePacket.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
import javolution.text.TextBuilder;
/**
* @author KenM
*
*/
public abstract class ReceivablePacket<T extends MMOClient> extends AbstractPacket<T> implements Runnable
{
protected ReceivablePacket()
{
}
protected int getAvaliableBytes()
{
return this.getByteBuffer().remaining();
}
protected abstract boolean read();
public abstract void run();
protected void readB(byte[] dst)
{
this.getByteBuffer().get(dst);
}
protected void readB(byte[] dst, int offset, int len)
{
this.getByteBuffer().get(dst, offset, len);
}
protected int readC()
{
return this.getByteBuffer().get() & 0xFF;
}
protected int readH()
{
return this.getByteBuffer().getShort() & 0xFFFF;
}
protected int readD()
{
return this.getByteBuffer().getInt();
}
protected long readQ()
{
return this.getByteBuffer().getLong();
}
protected double readF()
{
return this.getByteBuffer().getDouble();
}
protected String readS()
{
TextBuilder tb = TextBuilder.newInstance();
char ch;
while((ch = getByteBuffer().getChar()) != 0)
tb.append(ch);
String str = tb.stringValue();
TextBuilder.recycle(tb);
return str;
}
}
SelectorConfig.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
import java.nio.ByteOrder;
/**
* @author KenM
*
*/
public class SelectorConfig
{
private int READ_BUFFER_SIZE = 64*1024;
private int WRITE_BUFFER_SIZE = 64*1024;
private HeaderSize HEADER_TYPE = HeaderSize.SHORT_HEADER;
private int HELPER_BUFFER_SIZE = 64*1024;
private int HELPER_BUFFER_COUNT = 20;
private ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
/**
* BYTE_HEADER: unsigned byte, max size: 255 <BR>
* SHORT_HEADER: unsigned short, max size: 65535<BR>
* INT_HEADER: signed integer, max size: Integer.MAX_SIZE<BR>
* @author KenM
*/
public static enum HeaderSize
{
BYTE_HEADER,
SHORT_HEADER,
INT_HEADER,
}
public SelectorConfig()
{
}
public int getReadBufferSize()
{
return READ_BUFFER_SIZE;
}
public int getWriteBufferSize()
{
return READ_BUFFER_SIZE;
}
public int getHelperBufferSize()
{
return HELPER_BUFFER_SIZE;
}
public int getHelperBufferCount()
{
return HELPER_BUFFER_COUNT;
}
public ByteOrder getByteOrder()
{
return BYTE_ORDER;
}
public HeaderSize getHeaderType()
{
return HEADER_TYPE;
}
}
SelectorServerConfig.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
import java.net.InetAddress;
/**
* @author KenM
*
*/
public class SelectorServerConfig extends SelectorConfig
{
private final int SERVER_PORT;
private final InetAddress SERVER_ADDRESS;
public SelectorServerConfig(int port)
{
this(null, port);
}
public SelectorServerConfig(InetAddress address, int port)
{
SERVER_PORT = port;
SERVER_ADDRESS = address;
}
public int getPort()
{
return SERVER_PORT;
}
public InetAddress getAddress()
{
return SERVER_ADDRESS;
}
}SelectorThread.java
Code:
http://www.4shared.com/file/183645656/a50d2d56/SelectorThread.html
SendablePacket.java
Code:
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.mmocore.network;
import com.l2jserver.mmocore.network.SelectorConfig.HeaderSize;
/**
* @author KenM
*
*/
public abstract class SendablePacket<T extends MMOClient> extends AbstractPacket<T>
{
protected void writeC(int data)
{
this.getByteBuffer().put((byte) data);
}
protected void writeF(double value)
{
this.getByteBuffer().putDouble(value);
}
protected void writeH(int value)
{
this.getByteBuffer().putShort((short) value);
}
protected void writeD(int value)
{
this.getByteBuffer().putInt(value);
}
protected void writeQ(long value)
{
this.getByteBuffer().putLong(value);
}
protected void writeB(byte[] data)
{
this.getByteBuffer().put(data);
}
protected void writeS(CharSequence charSequence)
{
if (charSequence == null)
{
charSequence = "";
}
int length = charSequence.length();
for (int i = 0; i < length; i++)
{
this.getByteBuffer().putChar(charSequence.charAt(i));
}
this.getByteBuffer().putChar('\000');
}
protected abstract void write();
protected void writeHeader(HeaderSize ht, int pos)
{
switch (ht)
{
case BYTE_HEADER:
this.getByteBuffer().put(pos, (byte) this.getByteBuffer().position());
break;
case SHORT_HEADER:
this.getByteBuffer().putShort(pos, (short) this.getByteBuffer().position());
break;
case INT_HEADER:
this.getByteBuffer().putInt(pos, this.getByteBuffer().position());
break;
}
}
}
Credits : L2j !!!
With Special Respect ÐøwnUиde®™