Moved mmocore in loginserver.
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @author KenM
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class AbstractPacket<T extends MMOClient<?>>
|
||||
{
|
||||
protected ByteBuffer _buf;
|
||||
|
||||
T _client;
|
||||
|
||||
public final T getClient()
|
||||
{
|
||||
return _client;
|
||||
}
|
||||
}
|
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
import java.nio.channels.SocketChannel;
|
||||
|
||||
/**
|
||||
* @author KenM
|
||||
*/
|
||||
public interface IAcceptFilter
|
||||
{
|
||||
boolean accept(SocketChannel sc);
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
/**
|
||||
* @author KenM
|
||||
* @param <T>
|
||||
*/
|
||||
public interface IClientFactory<T extends MMOClient<?>>
|
||||
{
|
||||
T create(MMOConnection<T> con);
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
/**
|
||||
* @author KenM
|
||||
* @param <T>
|
||||
*/
|
||||
public interface IMMOExecutor<T extends MMOClient<?>>
|
||||
{
|
||||
void execute(ReceivablePacket<T> packet);
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @author KenM
|
||||
* @param <T>
|
||||
*/
|
||||
public interface IPacketHandler<T extends MMOClient<?>>
|
||||
{
|
||||
ReceivablePacket<T> handlePacket(ByteBuffer buf, T client);
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @author KenM
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class MMOClient<T extends MMOConnection<?>>
|
||||
{
|
||||
private final T _con;
|
||||
|
||||
public MMOClient(T con)
|
||||
{
|
||||
_con = con;
|
||||
}
|
||||
|
||||
public T getConnection()
|
||||
{
|
||||
return _con;
|
||||
}
|
||||
|
||||
public abstract boolean decrypt(ByteBuffer buf, int size);
|
||||
|
||||
public abstract boolean encrypt(ByteBuffer buf, int size);
|
||||
|
||||
protected abstract void onDisconnection();
|
||||
|
||||
protected abstract void onForcedDisconnection();
|
||||
}
|
@@ -1,283 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.CancelledKeyException;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
|
||||
/**
|
||||
* @author KenM
|
||||
* @param <T>
|
||||
*/
|
||||
public class MMOConnection<T extends MMOClient<?>>
|
||||
{
|
||||
private final SelectorThread<T> _selectorThread;
|
||||
|
||||
private final Socket _socket;
|
||||
|
||||
private final InetAddress _address;
|
||||
|
||||
private final ReadableByteChannel _readableByteChannel;
|
||||
|
||||
private final WritableByteChannel _writableByteChannel;
|
||||
|
||||
private final int _port;
|
||||
|
||||
private final NioNetStackList<SendablePacket<T>> _sendQueue;
|
||||
|
||||
private final SelectionKey _selectionKey;
|
||||
|
||||
private ByteBuffer _readBuffer;
|
||||
|
||||
private ByteBuffer _primaryWriteBuffer;
|
||||
|
||||
private ByteBuffer _secondaryWriteBuffer;
|
||||
|
||||
private volatile boolean _pendingClose;
|
||||
|
||||
private T _client;
|
||||
|
||||
public MMOConnection(SelectorThread<T> selectorThread, Socket socket, SelectionKey key, boolean tcpNoDelay)
|
||||
{
|
||||
_selectorThread = selectorThread;
|
||||
_socket = socket;
|
||||
_address = socket.getInetAddress();
|
||||
_readableByteChannel = socket.getChannel();
|
||||
_writableByteChannel = socket.getChannel();
|
||||
_port = socket.getPort();
|
||||
_selectionKey = key;
|
||||
|
||||
_sendQueue = new NioNetStackList<>();
|
||||
|
||||
try
|
||||
{
|
||||
_socket.setTcpNoDelay(tcpNoDelay);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
final void setClient(T client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public final T getClient()
|
||||
{
|
||||
return _client;
|
||||
}
|
||||
|
||||
public final void sendPacket(SendablePacket<T> sp)
|
||||
{
|
||||
sp._client = _client;
|
||||
|
||||
if (_pendingClose)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
synchronized (getSendQueue())
|
||||
{
|
||||
_sendQueue.addLast(sp);
|
||||
}
|
||||
|
||||
if (!_sendQueue.isEmpty())
|
||||
{
|
||||
try
|
||||
{
|
||||
_selectionKey.interestOps(_selectionKey.interestOps() | SelectionKey.OP_WRITE);
|
||||
}
|
||||
catch (CancelledKeyException e)
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final SelectionKey getSelectionKey()
|
||||
{
|
||||
return _selectionKey;
|
||||
}
|
||||
|
||||
public final InetAddress getInetAddress()
|
||||
{
|
||||
return _address;
|
||||
}
|
||||
|
||||
public final int getPort()
|
||||
{
|
||||
return _port;
|
||||
}
|
||||
|
||||
final void close() throws IOException
|
||||
{
|
||||
_socket.close();
|
||||
}
|
||||
|
||||
final int read(ByteBuffer buf) throws IOException
|
||||
{
|
||||
return _readableByteChannel.read(buf);
|
||||
}
|
||||
|
||||
final int write(ByteBuffer buf) throws IOException
|
||||
{
|
||||
return _writableByteChannel.write(buf);
|
||||
}
|
||||
|
||||
final void createWriteBuffer(ByteBuffer buf)
|
||||
{
|
||||
if (_primaryWriteBuffer == null)
|
||||
{
|
||||
_primaryWriteBuffer = _selectorThread.getPooledBuffer();
|
||||
_primaryWriteBuffer.put(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
final ByteBuffer temp = _selectorThread.getPooledBuffer();
|
||||
temp.put(buf);
|
||||
|
||||
final int remaining = temp.remaining();
|
||||
_primaryWriteBuffer.flip();
|
||||
final int limit = _primaryWriteBuffer.limit();
|
||||
|
||||
if (remaining >= _primaryWriteBuffer.remaining())
|
||||
{
|
||||
temp.put(_primaryWriteBuffer);
|
||||
_selectorThread.recycleBuffer(_primaryWriteBuffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
_primaryWriteBuffer.limit(remaining);
|
||||
temp.put(_primaryWriteBuffer);
|
||||
_primaryWriteBuffer.limit(limit);
|
||||
_primaryWriteBuffer.compact();
|
||||
_secondaryWriteBuffer = _primaryWriteBuffer;
|
||||
}
|
||||
_primaryWriteBuffer = temp;
|
||||
}
|
||||
}
|
||||
|
||||
final boolean hasPendingWriteBuffer()
|
||||
{
|
||||
return _primaryWriteBuffer != null;
|
||||
}
|
||||
|
||||
final void movePendingWriteBufferTo(ByteBuffer dest)
|
||||
{
|
||||
_primaryWriteBuffer.flip();
|
||||
dest.put(_primaryWriteBuffer);
|
||||
_selectorThread.recycleBuffer(_primaryWriteBuffer);
|
||||
_primaryWriteBuffer = _secondaryWriteBuffer;
|
||||
_secondaryWriteBuffer = null;
|
||||
}
|
||||
|
||||
final void setReadBuffer(ByteBuffer buf)
|
||||
{
|
||||
_readBuffer = buf;
|
||||
}
|
||||
|
||||
final ByteBuffer getReadBuffer()
|
||||
{
|
||||
return _readBuffer;
|
||||
}
|
||||
|
||||
public final boolean isClosed()
|
||||
{
|
||||
return _pendingClose;
|
||||
}
|
||||
|
||||
final NioNetStackList<SendablePacket<T>> getSendQueue()
|
||||
{
|
||||
return _sendQueue;
|
||||
}
|
||||
|
||||
/*
|
||||
* final SendablePacket<T> getClosePacket() { return _closePacket; }
|
||||
*/
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public final void close(SendablePacket<T> sp)
|
||||
{
|
||||
close(new SendablePacket[]
|
||||
{
|
||||
sp
|
||||
});
|
||||
}
|
||||
|
||||
public final void close(SendablePacket<T>[] closeList)
|
||||
{
|
||||
if (_pendingClose)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
synchronized (getSendQueue())
|
||||
{
|
||||
if (!_pendingClose)
|
||||
{
|
||||
_pendingClose = true;
|
||||
_sendQueue.clear();
|
||||
for (SendablePacket<T> sp : closeList)
|
||||
{
|
||||
_sendQueue.addLast(sp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_selectionKey.interestOps(_selectionKey.interestOps() & ~SelectionKey.OP_WRITE);
|
||||
}
|
||||
catch (CancelledKeyException e)
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
|
||||
// _closePacket = sp;
|
||||
_selectorThread.closeConnection(this);
|
||||
}
|
||||
|
||||
final void releaseBuffers()
|
||||
{
|
||||
if (_primaryWriteBuffer != null)
|
||||
{
|
||||
_selectorThread.recycleBuffer(_primaryWriteBuffer);
|
||||
_primaryWriteBuffer = null;
|
||||
|
||||
if (_secondaryWriteBuffer != null)
|
||||
{
|
||||
_selectorThread.recycleBuffer(_secondaryWriteBuffer);
|
||||
_secondaryWriteBuffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (_readBuffer != null)
|
||||
{
|
||||
_selectorThread.recycleBuffer(_readBuffer);
|
||||
_readBuffer = null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
/**
|
||||
* @author Forsaiken
|
||||
* @param <E>
|
||||
*/
|
||||
public final class NioNetStackList<E>
|
||||
{
|
||||
private final NioNetStackNode _start = new NioNetStackNode();
|
||||
|
||||
private final NioNetStackNodeBuf _buf = new NioNetStackNodeBuf();
|
||||
|
||||
private NioNetStackNode _end = new NioNetStackNode();
|
||||
|
||||
public NioNetStackList()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
public final void addLast(E elem)
|
||||
{
|
||||
final NioNetStackNode newEndNode = _buf.removeFirst();
|
||||
_end._value = elem;
|
||||
_end._next = newEndNode;
|
||||
_end = newEndNode;
|
||||
}
|
||||
|
||||
public final E removeFirst()
|
||||
{
|
||||
final NioNetStackNode old = _start._next;
|
||||
final E value = old._value;
|
||||
_start._next = old._next;
|
||||
_buf.addLast(old);
|
||||
return value;
|
||||
}
|
||||
|
||||
public final boolean isEmpty()
|
||||
{
|
||||
return _start._next == _end;
|
||||
}
|
||||
|
||||
public final void clear()
|
||||
{
|
||||
_start._next = _end;
|
||||
}
|
||||
|
||||
protected final class NioNetStackNode
|
||||
{
|
||||
protected NioNetStackNode _next;
|
||||
|
||||
protected E _value;
|
||||
}
|
||||
|
||||
private final class NioNetStackNodeBuf
|
||||
{
|
||||
private final NioNetStackNode _start = new NioNetStackNode();
|
||||
|
||||
private NioNetStackNode _end = new NioNetStackNode();
|
||||
|
||||
NioNetStackNodeBuf()
|
||||
{
|
||||
_start._next = _end;
|
||||
}
|
||||
|
||||
final void addLast(NioNetStackNode node)
|
||||
{
|
||||
node._next = null;
|
||||
node._value = null;
|
||||
_end._next = node;
|
||||
_end = node;
|
||||
}
|
||||
|
||||
final NioNetStackNode removeFirst()
|
||||
{
|
||||
if (_start._next == _end)
|
||||
{
|
||||
return new NioNetStackNode();
|
||||
}
|
||||
|
||||
final NioNetStackNode old = _start._next;
|
||||
_start._next = old._next;
|
||||
return old;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
import java.nio.BufferOverflowException;
|
||||
|
||||
/**
|
||||
* @author Forsaiken
|
||||
*/
|
||||
public final class NioNetStringBuffer
|
||||
{
|
||||
private final char[] _buf;
|
||||
|
||||
private final int _size;
|
||||
|
||||
private int _len;
|
||||
|
||||
public NioNetStringBuffer(int size)
|
||||
{
|
||||
_buf = new char[size];
|
||||
_size = size;
|
||||
_len = 0;
|
||||
}
|
||||
|
||||
public final void clear()
|
||||
{
|
||||
_len = 0;
|
||||
}
|
||||
|
||||
public final void append(char c)
|
||||
{
|
||||
if (_len < _size)
|
||||
{
|
||||
_buf[_len++] = c;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new BufferOverflowException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString()
|
||||
{
|
||||
return new String(_buf, 0, _len);
|
||||
}
|
||||
}
|
@@ -1,139 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @author KenM
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class ReceivablePacket<T extends MMOClient<?>>extends AbstractPacket<T> implements Runnable
|
||||
{
|
||||
NioNetStringBuffer _sbuf;
|
||||
|
||||
protected ReceivablePacket()
|
||||
{
|
||||
}
|
||||
|
||||
protected abstract boolean read();
|
||||
|
||||
@Override
|
||||
public abstract void run();
|
||||
|
||||
/**
|
||||
* Reads <B>byte[]</B> from the buffer. <BR>
|
||||
* Reads as many bytes as the length of the array.
|
||||
* @param dst : the byte array which will be filled with the data.
|
||||
*/
|
||||
protected final void readB(byte[] dst)
|
||||
{
|
||||
_buf.get(dst);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads <B>byte[]</B> from the buffer. <BR>
|
||||
* Reads as many bytes as the given length (len). Starts to fill the byte array from the given offset to <B>offset</B> + <B>len</B>.
|
||||
* @param dst : the byte array which will be filled with the data.
|
||||
* @param offset : starts to fill the byte array from the given offset.
|
||||
* @param len : the given length of bytes to be read.
|
||||
*/
|
||||
protected final void readB(byte[] dst, int offset, int len)
|
||||
{
|
||||
_buf.get(dst, offset, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads <B>byte</B> from the buffer. <BR>
|
||||
* 8bit integer (00)
|
||||
* @return
|
||||
*/
|
||||
protected final int readC()
|
||||
{
|
||||
return _buf.get() & 0xFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads <B>short</B> from the buffer. <BR>
|
||||
* 16bit integer (00 00)
|
||||
* @return
|
||||
*/
|
||||
protected final int readH()
|
||||
{
|
||||
return _buf.getShort() & 0xFFFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads <B>int</B> from the buffer. <BR>
|
||||
* 32bit integer (00 00 00 00)
|
||||
* @return
|
||||
*/
|
||||
protected final int readD()
|
||||
{
|
||||
return _buf.getInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads <B>long</B> from the buffer. <BR>
|
||||
* 64bit integer (00 00 00 00 00 00 00 00)
|
||||
* @return
|
||||
*/
|
||||
protected final long readQ()
|
||||
{
|
||||
return _buf.getLong();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads <B>double</B> from the buffer. <BR>
|
||||
* 64bit double precision float (00 00 00 00 00 00 00 00)
|
||||
* @return
|
||||
*/
|
||||
protected final double readF()
|
||||
{
|
||||
return _buf.getDouble();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads <B>String</B> from the buffer.
|
||||
* @return
|
||||
*/
|
||||
protected final String readS()
|
||||
{
|
||||
_sbuf.clear();
|
||||
|
||||
char ch;
|
||||
while ((ch = _buf.getChar()) != 0)
|
||||
{
|
||||
_sbuf.append(ch);
|
||||
}
|
||||
|
||||
return _sbuf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* packet forge purpose
|
||||
* @param data
|
||||
* @param client
|
||||
* @param sBuffer
|
||||
*/
|
||||
public void setBuffers(ByteBuffer data, T client, NioNetStringBuffer sBuffer)
|
||||
{
|
||||
_buf = data;
|
||||
_client = client;
|
||||
_sbuf = sBuffer;
|
||||
}
|
||||
}
|
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
/**
|
||||
* @author KenM
|
||||
*/
|
||||
public final class SelectorConfig
|
||||
{
|
||||
public int READ_BUFFER_SIZE = 64 * 1024;
|
||||
|
||||
public int WRITE_BUFFER_SIZE = 64 * 1024;
|
||||
|
||||
public int HELPER_BUFFER_COUNT = 20;
|
||||
|
||||
public int HELPER_BUFFER_SIZE = 64 * 1024;
|
||||
|
||||
/**
|
||||
* Server will try to send MAX_SEND_PER_PASS packets per socket write call<br>
|
||||
* however it may send less if the write buffer was filled before achieving this value.
|
||||
*/
|
||||
public int MAX_SEND_PER_PASS = 10;
|
||||
|
||||
/**
|
||||
* Server will try to read MAX_READ_PER_PASS packets per socket read call<br>
|
||||
* however it may read less if the read buffer was empty before achieving this value.
|
||||
*/
|
||||
public int MAX_READ_PER_PASS = 10;
|
||||
|
||||
/**
|
||||
* Defines how much time (in milis) should the selector sleep, an higher value increases throughput but also increases latency(to a max of the sleep value itself).<BR>
|
||||
* Also an extremely high value(usually > 100) will decrease throughput due to the server not doing enough sends per second (depends on max sends per pass).<BR>
|
||||
* <BR>
|
||||
* Recommended values:<BR>
|
||||
* 1 for minimal latency.<BR>
|
||||
* 10-30 for an latency/troughput trade-off based on your needs.<BR>
|
||||
*/
|
||||
public int SLEEP_TIME = 10;
|
||||
|
||||
/**
|
||||
* Used to enable/disable TCP_NODELAY which disable/enable Nagle's algorithm.<BR>
|
||||
* <BR>
|
||||
* Nagle's algorithm try to conserve bandwidth by minimizing the number of segments that are sent. When applications wish to decrease network latency and increase performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY). Data will be sent earlier, at the cost of an increase
|
||||
* in bandwidth consumption. The Nagle's algorithm is described in RFC 896.<BR>
|
||||
* <BR>
|
||||
* Summary, data will be sent earlier, thus lowering the ping, at the cost of a small increase in bandwidth consumption.
|
||||
*/
|
||||
public boolean TCP_NODELAY = true;
|
||||
}
|
@@ -1,714 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* Parts of design based on network core from WoodenGil
|
||||
* @param <T>
|
||||
* @author KenM
|
||||
*/
|
||||
public final class SelectorThread<T extends MMOClient<?>>extends Thread
|
||||
{
|
||||
// default BYTE_ORDER
|
||||
private static final ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
|
||||
// default HEADER_SIZE
|
||||
private static final int HEADER_SIZE = 2;
|
||||
// Selector
|
||||
private final Selector _selector;
|
||||
// Implementations
|
||||
private final IPacketHandler<T> _packetHandler;
|
||||
private final IMMOExecutor<T> _executor;
|
||||
private final IClientFactory<T> _clientFactory;
|
||||
private final IAcceptFilter _acceptFilter;
|
||||
// Configurations
|
||||
private final int HELPER_BUFFER_SIZE;
|
||||
private final int HELPER_BUFFER_COUNT;
|
||||
private final int MAX_SEND_PER_PASS;
|
||||
private final int MAX_READ_PER_PASS;
|
||||
private final long SLEEP_TIME;
|
||||
public boolean TCP_NODELAY;
|
||||
// Main Buffers
|
||||
private final ByteBuffer DIRECT_WRITE_BUFFER;
|
||||
private final ByteBuffer WRITE_BUFFER;
|
||||
private final ByteBuffer READ_BUFFER;
|
||||
// String Buffer
|
||||
private final NioNetStringBuffer STRING_BUFFER;
|
||||
// ByteBuffers General Purpose Pool
|
||||
private final LinkedList<ByteBuffer> _bufferPool;
|
||||
// Pending Close
|
||||
private final NioNetStackList<MMOConnection<T>> _pendingClose;
|
||||
|
||||
private boolean _shutdown;
|
||||
|
||||
public SelectorThread(SelectorConfig sc, IMMOExecutor<T> executor, IPacketHandler<T> packetHandler, IClientFactory<T> clientFactory, IAcceptFilter acceptFilter) throws IOException
|
||||
{
|
||||
super.setName("SelectorThread-" + super.getId());
|
||||
|
||||
HELPER_BUFFER_SIZE = sc.HELPER_BUFFER_SIZE;
|
||||
HELPER_BUFFER_COUNT = sc.HELPER_BUFFER_COUNT;
|
||||
MAX_SEND_PER_PASS = sc.MAX_SEND_PER_PASS;
|
||||
MAX_READ_PER_PASS = sc.MAX_READ_PER_PASS;
|
||||
SLEEP_TIME = sc.SLEEP_TIME;
|
||||
TCP_NODELAY = sc.TCP_NODELAY;
|
||||
|
||||
DIRECT_WRITE_BUFFER = ByteBuffer.allocateDirect(sc.WRITE_BUFFER_SIZE).order(BYTE_ORDER);
|
||||
WRITE_BUFFER = ByteBuffer.wrap(new byte[sc.WRITE_BUFFER_SIZE]).order(BYTE_ORDER);
|
||||
READ_BUFFER = ByteBuffer.wrap(new byte[sc.READ_BUFFER_SIZE]).order(BYTE_ORDER);
|
||||
|
||||
STRING_BUFFER = new NioNetStringBuffer(64 * 1024);
|
||||
|
||||
_pendingClose = new NioNetStackList<>();
|
||||
_bufferPool = new LinkedList<>();
|
||||
|
||||
for (int i = 0; i < HELPER_BUFFER_COUNT; i++)
|
||||
{
|
||||
_bufferPool.addLast(ByteBuffer.wrap(new byte[HELPER_BUFFER_SIZE]).order(BYTE_ORDER));
|
||||
}
|
||||
|
||||
_acceptFilter = acceptFilter;
|
||||
_packetHandler = packetHandler;
|
||||
_clientFactory = clientFactory;
|
||||
_executor = executor;
|
||||
_selector = Selector.open();
|
||||
}
|
||||
|
||||
public final void openServerSocket(InetAddress address, int tcpPort) throws IOException
|
||||
{
|
||||
final ServerSocketChannel selectable = ServerSocketChannel.open();
|
||||
selectable.configureBlocking(false);
|
||||
|
||||
final ServerSocket ss = selectable.socket();
|
||||
|
||||
if (address != null)
|
||||
{
|
||||
ss.bind(new InetSocketAddress(address, tcpPort));
|
||||
}
|
||||
else
|
||||
{
|
||||
ss.bind(new InetSocketAddress(tcpPort));
|
||||
}
|
||||
|
||||
selectable.register(_selector, SelectionKey.OP_ACCEPT);
|
||||
}
|
||||
|
||||
final ByteBuffer getPooledBuffer()
|
||||
{
|
||||
if (_bufferPool.isEmpty())
|
||||
{
|
||||
return ByteBuffer.wrap(new byte[HELPER_BUFFER_SIZE]).order(BYTE_ORDER);
|
||||
}
|
||||
|
||||
return _bufferPool.removeFirst();
|
||||
}
|
||||
|
||||
final void recycleBuffer(ByteBuffer buf)
|
||||
{
|
||||
if (_bufferPool.size() < HELPER_BUFFER_COUNT)
|
||||
{
|
||||
buf.clear();
|
||||
_bufferPool.addLast(buf);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public final void run()
|
||||
{
|
||||
int selectedKeysCount = 0;
|
||||
|
||||
SelectionKey key;
|
||||
MMOConnection<T> con;
|
||||
|
||||
Iterator<SelectionKey> selectedKeys;
|
||||
|
||||
while (!_shutdown)
|
||||
{
|
||||
try
|
||||
{
|
||||
selectedKeysCount = _selector.selectNow();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (selectedKeysCount > 0)
|
||||
{
|
||||
selectedKeys = _selector.selectedKeys().iterator();
|
||||
|
||||
while (selectedKeys.hasNext())
|
||||
{
|
||||
key = selectedKeys.next();
|
||||
selectedKeys.remove();
|
||||
|
||||
con = (MMOConnection<T>) key.attachment();
|
||||
|
||||
switch (key.readyOps())
|
||||
{
|
||||
case SelectionKey.OP_CONNECT:
|
||||
{
|
||||
finishConnection(key, con);
|
||||
break;
|
||||
}
|
||||
case SelectionKey.OP_ACCEPT:
|
||||
{
|
||||
acceptConnection(key, con);
|
||||
break;
|
||||
}
|
||||
case SelectionKey.OP_READ:
|
||||
{
|
||||
readPacket(key, con);
|
||||
break;
|
||||
}
|
||||
case SelectionKey.OP_WRITE:
|
||||
{
|
||||
writePacket(key, con);
|
||||
break;
|
||||
}
|
||||
case SelectionKey.OP_READ | SelectionKey.OP_WRITE:
|
||||
{
|
||||
writePacket(key, con);
|
||||
if (key.isValid())
|
||||
{
|
||||
readPacket(key, con);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
synchronized (_pendingClose)
|
||||
{
|
||||
while (!_pendingClose.isEmpty())
|
||||
{
|
||||
try
|
||||
{
|
||||
con = _pendingClose.removeFirst();
|
||||
writeClosePacket(con);
|
||||
closeConnectionImpl(con.getSelectionKey(), con);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Thread.sleep(SLEEP_TIME);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
closeSelectorThread();
|
||||
}
|
||||
|
||||
private final void finishConnection(SelectionKey key, MMOConnection<T> con)
|
||||
{
|
||||
try
|
||||
{
|
||||
((SocketChannel) key.channel()).finishConnect();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
con.getClient().onForcedDisconnection();
|
||||
closeConnectionImpl(key, con);
|
||||
}
|
||||
|
||||
// key might have been invalidated on finishConnect()
|
||||
if (key.isValid())
|
||||
{
|
||||
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
|
||||
key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT);
|
||||
}
|
||||
}
|
||||
|
||||
private final void acceptConnection(SelectionKey key, MMOConnection<T> con)
|
||||
{
|
||||
final ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
|
||||
SocketChannel sc;
|
||||
|
||||
try
|
||||
{
|
||||
while ((sc = ssc.accept()) != null)
|
||||
{
|
||||
if ((_acceptFilter == null) || _acceptFilter.accept(sc))
|
||||
{
|
||||
sc.configureBlocking(false);
|
||||
final SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
|
||||
con = new MMOConnection<>(this, sc.socket(), clientKey, TCP_NODELAY);
|
||||
con.setClient(_clientFactory.create(con));
|
||||
clientKey.attach(con);
|
||||
}
|
||||
else
|
||||
{
|
||||
sc.socket().close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private final void readPacket(SelectionKey key, MMOConnection<T> con)
|
||||
{
|
||||
if (con.isClosed())
|
||||
{
|
||||
return;
|
||||
}
|
||||
ByteBuffer buf = con.getReadBuffer();
|
||||
if (buf == null)
|
||||
{
|
||||
buf = READ_BUFFER;
|
||||
}
|
||||
|
||||
// if we try to to do a read with no space in the buffer it will read 0 bytes going into infinite loop
|
||||
if (buf.position() == buf.limit())
|
||||
{
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
int result = -2;
|
||||
|
||||
try
|
||||
{
|
||||
result = con.read(buf);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// error handling goes bellow
|
||||
}
|
||||
|
||||
if (result > 0)
|
||||
{
|
||||
buf.flip();
|
||||
|
||||
final T client = con.getClient();
|
||||
|
||||
for (int i = 0; i < MAX_READ_PER_PASS; i++)
|
||||
{
|
||||
if (!tryReadPacket(key, client, buf, con))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// only reachable if MAX_READ_PER_PASS has been reached
|
||||
// check if there are some more bytes in buffer
|
||||
// and allocate/compact to prevent content lose.
|
||||
if (buf.remaining() > 0)
|
||||
{
|
||||
// did we use the READ_BUFFER ?
|
||||
if (buf == READ_BUFFER)
|
||||
{
|
||||
// move the pending byte to the connections READ_BUFFER
|
||||
allocateReadBuffer(con);
|
||||
}
|
||||
else
|
||||
{
|
||||
// move the first byte to the beginning :)
|
||||
buf.compact();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (result)
|
||||
{
|
||||
case 0:
|
||||
case -1:
|
||||
{
|
||||
closeConnectionImpl(key, con);
|
||||
break;
|
||||
}
|
||||
case -2:
|
||||
{
|
||||
con.getClient().onForcedDisconnection();
|
||||
closeConnectionImpl(key, con);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final boolean tryReadPacket(SelectionKey key, T client, ByteBuffer buf, MMOConnection<T> con)
|
||||
{
|
||||
switch (buf.remaining())
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
// buffer is full nothing to read
|
||||
return false;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
// we don`t have enough data for header so we need to read
|
||||
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
|
||||
|
||||
// did we use the READ_BUFFER ?
|
||||
if (buf == READ_BUFFER)
|
||||
{
|
||||
// move the pending byte to the connections READ_BUFFER
|
||||
allocateReadBuffer(con);
|
||||
}
|
||||
else
|
||||
{
|
||||
// move the first byte to the beginning :)
|
||||
buf.compact();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
{
|
||||
// data size excluding header size :>
|
||||
final int dataPending = (buf.getShort() & 0xFFFF) - HEADER_SIZE;
|
||||
|
||||
// do we got enough bytes for the packet?
|
||||
if (dataPending <= buf.remaining())
|
||||
{
|
||||
// avoid parsing dummy packets (packets without body)
|
||||
if (dataPending > 0)
|
||||
{
|
||||
final int pos = buf.position();
|
||||
parseClientPacket(pos, buf, dataPending, client);
|
||||
buf.position(pos + dataPending);
|
||||
}
|
||||
|
||||
if (buf.hasRemaining())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (buf != READ_BUFFER)
|
||||
{
|
||||
con.setReadBuffer(null);
|
||||
recycleBuffer(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
READ_BUFFER.clear();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// we don`t have enough bytes for the dataPacket so we need to read
|
||||
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
|
||||
|
||||
// move it`s position
|
||||
buf.position(buf.position() - HEADER_SIZE);
|
||||
// did we use the READ_BUFFER ?
|
||||
if (buf == READ_BUFFER)
|
||||
{
|
||||
// move the pending byte to the connections READ_BUFFER
|
||||
allocateReadBuffer(con);
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.compact();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final void allocateReadBuffer(MMOConnection<T> con)
|
||||
{
|
||||
con.setReadBuffer(getPooledBuffer().put(READ_BUFFER));
|
||||
READ_BUFFER.clear();
|
||||
}
|
||||
|
||||
private final void parseClientPacket(int pos, ByteBuffer buf, int dataSize, T client)
|
||||
{
|
||||
if (!client.decrypt(buf, dataSize) || !buf.hasRemaining())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// apply limit
|
||||
final int limit = buf.limit();
|
||||
buf.limit(pos + dataSize);
|
||||
final ReceivablePacket<T> cp = _packetHandler.handlePacket(buf, client);
|
||||
|
||||
if (cp != null)
|
||||
{
|
||||
cp._buf = buf;
|
||||
cp._sbuf = STRING_BUFFER;
|
||||
cp._client = client;
|
||||
|
||||
if (cp.read())
|
||||
{
|
||||
_executor.execute(cp);
|
||||
}
|
||||
|
||||
cp._buf = null;
|
||||
cp._sbuf = null;
|
||||
}
|
||||
buf.limit(limit);
|
||||
}
|
||||
|
||||
private final void writeClosePacket(MMOConnection<T> con)
|
||||
{
|
||||
SendablePacket<T> sp;
|
||||
synchronized (con.getSendQueue())
|
||||
{
|
||||
if (con.getSendQueue().isEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
while ((sp = con.getSendQueue().removeFirst()) != null)
|
||||
{
|
||||
WRITE_BUFFER.clear();
|
||||
|
||||
putPacketIntoWriteBuffer(con.getClient(), sp);
|
||||
|
||||
WRITE_BUFFER.flip();
|
||||
|
||||
try
|
||||
{
|
||||
con.write(WRITE_BUFFER);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// error handling goes on the if bellow
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected final void writePacket(SelectionKey key, MMOConnection<T> con)
|
||||
{
|
||||
if (!prepareWriteBuffer(con))
|
||||
{
|
||||
key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
|
||||
return;
|
||||
}
|
||||
|
||||
DIRECT_WRITE_BUFFER.flip();
|
||||
|
||||
final int size = DIRECT_WRITE_BUFFER.remaining();
|
||||
|
||||
int result = -1;
|
||||
|
||||
try
|
||||
{
|
||||
result = con.write(DIRECT_WRITE_BUFFER);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// error handling goes on the if bellow
|
||||
}
|
||||
|
||||
// check if no error happened
|
||||
if (result >= 0)
|
||||
{
|
||||
// check if we written everything
|
||||
if (result == size)
|
||||
{
|
||||
// complete write
|
||||
synchronized (con.getSendQueue())
|
||||
{
|
||||
if (con.getSendQueue().isEmpty() && !con.hasPendingWriteBuffer())
|
||||
{
|
||||
key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// incomplete write
|
||||
con.createWriteBuffer(DIRECT_WRITE_BUFFER);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
con.getClient().onForcedDisconnection();
|
||||
closeConnectionImpl(key, con);
|
||||
}
|
||||
}
|
||||
|
||||
private final boolean prepareWriteBuffer(MMOConnection<T> con)
|
||||
{
|
||||
boolean hasPending = false;
|
||||
DIRECT_WRITE_BUFFER.clear();
|
||||
|
||||
// if there is pending content add it
|
||||
if (con.hasPendingWriteBuffer())
|
||||
{
|
||||
con.movePendingWriteBufferTo(DIRECT_WRITE_BUFFER);
|
||||
hasPending = true;
|
||||
}
|
||||
|
||||
if ((DIRECT_WRITE_BUFFER.remaining() > 1) && !con.hasPendingWriteBuffer())
|
||||
{
|
||||
final NioNetStackList<SendablePacket<T>> sendQueue = con.getSendQueue();
|
||||
final T client = con.getClient();
|
||||
SendablePacket<T> sp;
|
||||
|
||||
for (int i = 0; i < MAX_SEND_PER_PASS; i++)
|
||||
{
|
||||
synchronized (con.getSendQueue())
|
||||
{
|
||||
if (sendQueue.isEmpty())
|
||||
{
|
||||
sp = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
sp = sendQueue.removeFirst();
|
||||
}
|
||||
}
|
||||
|
||||
if (sp == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
hasPending = true;
|
||||
|
||||
// put into WriteBuffer
|
||||
putPacketIntoWriteBuffer(client, sp);
|
||||
|
||||
WRITE_BUFFER.flip();
|
||||
|
||||
if (DIRECT_WRITE_BUFFER.remaining() < WRITE_BUFFER.limit())
|
||||
{
|
||||
con.createWriteBuffer(WRITE_BUFFER);
|
||||
break;
|
||||
}
|
||||
DIRECT_WRITE_BUFFER.put(WRITE_BUFFER);
|
||||
}
|
||||
}
|
||||
return hasPending;
|
||||
}
|
||||
|
||||
private final void putPacketIntoWriteBuffer(T client, SendablePacket<T> sp)
|
||||
{
|
||||
WRITE_BUFFER.clear();
|
||||
|
||||
// reserve space for the size
|
||||
final int headerPos = WRITE_BUFFER.position();
|
||||
final int dataPos = headerPos + HEADER_SIZE;
|
||||
WRITE_BUFFER.position(dataPos);
|
||||
|
||||
// set the write buffer
|
||||
sp._buf = WRITE_BUFFER;
|
||||
// set the client.
|
||||
sp._client = client;
|
||||
// write content to buffer
|
||||
sp.write();
|
||||
// delete the write buffer
|
||||
sp._buf = null;
|
||||
|
||||
// size (inclusive header)
|
||||
int dataSize = WRITE_BUFFER.position() - dataPos;
|
||||
WRITE_BUFFER.position(dataPos);
|
||||
client.encrypt(WRITE_BUFFER, dataSize);
|
||||
|
||||
// recalculate size after encryption
|
||||
dataSize = WRITE_BUFFER.position() - dataPos;
|
||||
|
||||
WRITE_BUFFER.position(headerPos);
|
||||
// write header
|
||||
WRITE_BUFFER.putShort((short) (dataSize + HEADER_SIZE));
|
||||
WRITE_BUFFER.position(dataPos + dataSize);
|
||||
}
|
||||
|
||||
final void closeConnection(MMOConnection<T> con)
|
||||
{
|
||||
synchronized (_pendingClose)
|
||||
{
|
||||
_pendingClose.addLast(con);
|
||||
}
|
||||
}
|
||||
|
||||
private final void closeConnectionImpl(SelectionKey key, MMOConnection<T> con)
|
||||
{
|
||||
try
|
||||
{
|
||||
// notify connection
|
||||
con.getClient().onDisconnection();
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
// close socket and the SocketChannel
|
||||
con.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// ignore, we are closing anyway
|
||||
}
|
||||
finally
|
||||
{
|
||||
con.releaseBuffers();
|
||||
// clear attachment
|
||||
key.attach(null);
|
||||
// cancel key
|
||||
key.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final void shutdown()
|
||||
{
|
||||
_shutdown = true;
|
||||
}
|
||||
|
||||
protected void closeSelectorThread()
|
||||
{
|
||||
for (SelectionKey key : _selector.keys())
|
||||
{
|
||||
try
|
||||
{
|
||||
key.channel().close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_selector.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,139 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.mmocore;
|
||||
|
||||
/**
|
||||
* @author KenM
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class SendablePacket<T extends MMOClient<?>>extends AbstractPacket<T>
|
||||
{
|
||||
protected final void putInt(int value)
|
||||
{
|
||||
_buf.putInt(value);
|
||||
}
|
||||
|
||||
protected final void putDouble(double value)
|
||||
{
|
||||
_buf.putDouble(value);
|
||||
}
|
||||
|
||||
protected final void putFloat(float value)
|
||||
{
|
||||
_buf.putFloat(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write <B>byte</B> to the buffer. <BR>
|
||||
* 8bit integer (00)
|
||||
* @param data
|
||||
*/
|
||||
protected final void writeC(boolean data)
|
||||
{
|
||||
_buf.put((byte) (data ? 0x01 : 0x00));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write <B>byte</B> to the buffer. <BR>
|
||||
* 8bit integer (00)
|
||||
* @param data
|
||||
*/
|
||||
protected final void writeC(int data)
|
||||
{
|
||||
_buf.put((byte) data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write <B>double</B> to the buffer. <BR>
|
||||
* 64bit double precision float (00 00 00 00 00 00 00 00)
|
||||
* @param value
|
||||
*/
|
||||
protected final void writeF(double value)
|
||||
{
|
||||
_buf.putDouble(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write <B>short</B> to the buffer. <BR>
|
||||
* 16bit integer (00 00)
|
||||
* @param value
|
||||
*/
|
||||
protected final void writeH(int value)
|
||||
{
|
||||
_buf.putShort((short) value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write <B>int</B> to the buffer. <BR>
|
||||
* 32bit integer (00 00 00 00)
|
||||
* @param value
|
||||
*/
|
||||
protected final void writeD(int value)
|
||||
{
|
||||
_buf.putInt(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write <B>int</B> to the buffer. <BR>
|
||||
* 32bit integer (00 00 00 00)
|
||||
* @param value
|
||||
*/
|
||||
protected final void writeD(boolean value)
|
||||
{
|
||||
_buf.putInt(value ? 0x01 : 0x00);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write <B>long</B> to the buffer. <BR>
|
||||
* 64bit integer (00 00 00 00 00 00 00 00)
|
||||
* @param value
|
||||
*/
|
||||
protected final void writeQ(long value)
|
||||
{
|
||||
_buf.putLong(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write <B>byte[]</B> to the buffer. <BR>
|
||||
* 8bit integer array (00 ...)
|
||||
* @param data
|
||||
*/
|
||||
protected final void writeB(byte[] data)
|
||||
{
|
||||
_buf.put(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write <B>String</B> to the buffer.
|
||||
* @param text
|
||||
*/
|
||||
protected final void writeS(String text)
|
||||
{
|
||||
if (text != null)
|
||||
{
|
||||
final int len = text.length();
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
_buf.putChar(text.charAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
_buf.putChar('\000');
|
||||
}
|
||||
|
||||
protected abstract void write();
|
||||
}
|
@@ -1,152 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.commons.util;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.l2jmobius.commons.mmocore.IAcceptFilter;
|
||||
|
||||
/**
|
||||
* IPv4 filter.
|
||||
* @author Forsaiken
|
||||
*/
|
||||
public class IPv4Filter implements IAcceptFilter, Runnable
|
||||
{
|
||||
protected final Logger _log = Logger.getLogger(getClass().getName());
|
||||
|
||||
private final HashMap<Integer, Flood> _ipFloodMap;
|
||||
private static final long SLEEP_TIME = 5000;
|
||||
|
||||
public IPv4Filter()
|
||||
{
|
||||
_ipFloodMap = new HashMap<>();
|
||||
final Thread t = new Thread(this, getClass().getSimpleName());
|
||||
t.setDaemon(true);
|
||||
t.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ip
|
||||
* @return
|
||||
*/
|
||||
private static final int hash(byte[] ip)
|
||||
{
|
||||
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
|
||||
}
|
||||
|
||||
protected static final class Flood
|
||||
{
|
||||
long lastAccess;
|
||||
int trys;
|
||||
|
||||
Flood()
|
||||
{
|
||||
lastAccess = System.currentTimeMillis();
|
||||
trys = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(SocketChannel sc)
|
||||
{
|
||||
final InetAddress addr = sc.socket().getInetAddress();
|
||||
if (!(addr instanceof Inet4Address))
|
||||
{
|
||||
_log.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
|
||||
return false;
|
||||
}
|
||||
|
||||
final int h = hash(addr.getAddress());
|
||||
|
||||
final long current = System.currentTimeMillis();
|
||||
Flood f;
|
||||
synchronized (_ipFloodMap)
|
||||
{
|
||||
f = _ipFloodMap.get(h);
|
||||
}
|
||||
if (f != null)
|
||||
{
|
||||
if (f.trys == -1)
|
||||
{
|
||||
f.lastAccess = current;
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((f.lastAccess + 1000) > current)
|
||||
{
|
||||
f.lastAccess = current;
|
||||
|
||||
if (f.trys >= 3)
|
||||
{
|
||||
f.trys = -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
f.trys++;
|
||||
}
|
||||
else
|
||||
{
|
||||
f.lastAccess = current;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
synchronized (_ipFloodMap)
|
||||
{
|
||||
_ipFloodMap.put(h, new Flood());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
final long reference = System.currentTimeMillis() - (1000 * 300);
|
||||
synchronized (_ipFloodMap)
|
||||
{
|
||||
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
|
||||
while (it.hasNext())
|
||||
{
|
||||
final Flood f = it.next().getValue();
|
||||
if (f.lastAccess < reference)
|
||||
{
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Thread.sleep(SLEEP_TIME);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user