Reverting some of the latest code removals.

This commit is contained in:
MobiusDev 2015-12-04 21:20:14 +00:00
parent 1f7bc7db0e
commit 9a9365c2a5
7 changed files with 54 additions and 15 deletions

View File

@ -18,4 +18,7 @@ MaxReadPerPass = 12
# Each unfinished read/write need a TEMP storage Buffer # Each unfinished read/write need a TEMP storage Buffer
# on large player amount we need more Buffers # on large player amount we need more Buffers
# if there are not enough buffers new ones are generated but not stored for future usage # if there are not enough buffers new ones are generated but not stored for future usage
HelperBufferCount = 20 HelperBufferCount = 20
# Setting this to True will lower your ping, at the cost of an increase in bandwidth consumption.
TcpNoDelay = True

View File

@ -18,4 +18,7 @@ MaxReadPerPass = 12
# Each unfinished read/write need a TEMP storage Buffer # Each unfinished read/write need a TEMP storage Buffer
# on large player amount we need more Buffers # on large player amount we need more Buffers
# if there are not enough buffers new ones are generated but not stored for future usage # if there are not enough buffers new ones are generated but not stored for future usage
HelperBufferCount = 20 HelperBufferCount = 20
# Setting this to True will lower your ping, at the cost of an increase in bandwidth consumption.
TcpNoDelay = True

View File

@ -1002,6 +1002,7 @@ public final class Config
public static int MMO_MAX_SEND_PER_PASS; public static int MMO_MAX_SEND_PER_PASS;
public static int MMO_MAX_READ_PER_PASS; public static int MMO_MAX_READ_PER_PASS;
public static int MMO_HELPER_BUFFER_COUNT; public static int MMO_HELPER_BUFFER_COUNT;
public static boolean MMO_TCP_NODELAY;
// -------------------------------------------------- // --------------------------------------------------
// Vitality Settings // Vitality Settings
@ -1749,6 +1750,7 @@ public final class Config
MMO_MAX_SEND_PER_PASS = mmoSettings.getInt("MaxSendPerPass", 12); MMO_MAX_SEND_PER_PASS = mmoSettings.getInt("MaxSendPerPass", 12);
MMO_MAX_READ_PER_PASS = mmoSettings.getInt("MaxReadPerPass", 12); MMO_MAX_READ_PER_PASS = mmoSettings.getInt("MaxReadPerPass", 12);
MMO_HELPER_BUFFER_COUNT = mmoSettings.getInt("HelperBufferCount", 20); MMO_HELPER_BUFFER_COUNT = mmoSettings.getInt("HelperBufferCount", 20);
MMO_TCP_NODELAY = mmoSettings.getBoolean("TcpNoDelay", false);
// Load IdFactory L2Properties file (if exists) // Load IdFactory L2Properties file (if exists)
final PropertiesParser IdFactory = new PropertiesParser(ID_CONFIG_FILE); final PropertiesParser IdFactory = new PropertiesParser(ID_CONFIG_FILE);
@ -2956,6 +2958,7 @@ public final class Config
MMO_MAX_SEND_PER_PASS = mmoSettings.getInt("MaxSendPerPass", 12); MMO_MAX_SEND_PER_PASS = mmoSettings.getInt("MaxSendPerPass", 12);
MMO_MAX_READ_PER_PASS = mmoSettings.getInt("MaxReadPerPass", 12); MMO_MAX_READ_PER_PASS = mmoSettings.getInt("MaxReadPerPass", 12);
MMO_HELPER_BUFFER_COUNT = mmoSettings.getInt("HelperBufferCount", 20); MMO_HELPER_BUFFER_COUNT = mmoSettings.getInt("HelperBufferCount", 20);
MMO_TCP_NODELAY = mmoSettings.getBoolean("TcpNoDelay", false);
// Load Telnet L2Properties file (if exists) // Load Telnet L2Properties file (if exists)
final PropertiesParser telnetSettings = new PropertiesParser(TELNET_FILE); final PropertiesParser telnetSettings = new PropertiesParser(TELNET_FILE);

View File

@ -20,6 +20,7 @@ package com.l2jserver.commons.mmocore;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.CancelledKeyException; import java.nio.channels.CancelledKeyException;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
@ -48,8 +49,6 @@ public class MMOConnection<T extends MMOClient<?>>
private final SelectionKey _selectionKey; private final SelectionKey _selectionKey;
// private SendablePacket<T> _closePacket;
private ByteBuffer _readBuffer; private ByteBuffer _readBuffer;
private ByteBuffer _primaryWriteBuffer; private ByteBuffer _primaryWriteBuffer;
@ -60,7 +59,7 @@ public class MMOConnection<T extends MMOClient<?>>
private T _client; private T _client;
public MMOConnection(final SelectorThread<T> selectorThread, final Socket socket, final SelectionKey key) public MMOConnection(final SelectorThread<T> selectorThread, final Socket socket, final SelectionKey key, boolean tcpNoDelay)
{ {
_selectorThread = selectorThread; _selectorThread = selectorThread;
_socket = socket; _socket = socket;
@ -71,6 +70,15 @@ public class MMOConnection<T extends MMOClient<?>>
_selectionKey = key; _selectionKey = key;
_sendQueue = new NioNetStackList<>(); _sendQueue = new NioNetStackList<>();
try
{
_socket.setTcpNoDelay(tcpNoDelay);
}
catch (SocketException e)
{
e.printStackTrace();
}
} }
final void setClient(final T client) final void setClient(final T client)

View File

@ -51,4 +51,14 @@ public final class SelectorConfig
* 10-30 for an latency/troughput trade-off based on your needs.<BR> * 10-30 for an latency/troughput trade-off based on your needs.<BR>
*/ */
public int SLEEP_TIME = 10; 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;
} }

View File

@ -33,7 +33,7 @@ import java.util.LinkedList;
/** /**
* Parts of design based on network core from WoodenGil * Parts of design based on network core from WoodenGil
* @param <T> * @param <T>
* @author KenM, Zoey76 * @author KenM
*/ */
public final class SelectorThread<T extends MMOClient<?>> extends Thread public final class SelectorThread<T extends MMOClient<?>> extends Thread
{ {
@ -54,6 +54,7 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
private final int MAX_SEND_PER_PASS; private final int MAX_SEND_PER_PASS;
private final int MAX_READ_PER_PASS; private final int MAX_READ_PER_PASS;
private final long SLEEP_TIME; private final long SLEEP_TIME;
public boolean TCP_NODELAY;
// Main Buffers // Main Buffers
private final ByteBuffer DIRECT_WRITE_BUFFER; private final ByteBuffer DIRECT_WRITE_BUFFER;
private final ByteBuffer WRITE_BUFFER; private final ByteBuffer WRITE_BUFFER;
@ -76,6 +77,7 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
MAX_SEND_PER_PASS = sc.MAX_SEND_PER_PASS; MAX_SEND_PER_PASS = sc.MAX_SEND_PER_PASS;
MAX_READ_PER_PASS = sc.MAX_READ_PER_PASS; MAX_READ_PER_PASS = sc.MAX_READ_PER_PASS;
SLEEP_TIME = sc.SLEEP_TIME; SLEEP_TIME = sc.SLEEP_TIME;
TCP_NODELAY = sc.TCP_NODELAY;
DIRECT_WRITE_BUFFER = ByteBuffer.allocateDirect(sc.WRITE_BUFFER_SIZE).order(BYTE_ORDER); 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); WRITE_BUFFER = ByteBuffer.wrap(new byte[sc.WRITE_BUFFER_SIZE]).order(BYTE_ORDER);
@ -198,9 +200,16 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
{ {
while (!_pendingClose.isEmpty()) while (!_pendingClose.isEmpty())
{ {
con = _pendingClose.removeFirst(); try
writeClosePacket(con); {
closeConnectionImpl(con.getSelectionKey(), con); con = _pendingClose.removeFirst();
writeClosePacket(con);
closeConnectionImpl(con.getSelectionKey(), con);
}
catch (Exception e)
{
e.printStackTrace();
}
} }
} }
@ -249,7 +258,7 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
{ {
sc.configureBlocking(false); sc.configureBlocking(false);
SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ); SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
con = new MMOConnection<>(this, sc.socket(), clientKey); con = new MMOConnection<>(this, sc.socket(), clientKey, TCP_NODELAY);
con.setClient(_clientFactory.create(con)); con.setClient(_clientFactory.create(con));
clientKey.attach(con); clientKey.attach(con);
} }
@ -275,9 +284,7 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
buf = READ_BUFFER; buf = READ_BUFFER;
} }
// if we try to to do a read with no space in the buffer it will // if we try to to do a read with no space in the buffer it will read 0 bytes going into infinite loop
// read 0 bytes
// going into infinite loop
if (buf.position() == buf.limit()) if (buf.position() == buf.limit())
{ {
System.exit(0); System.exit(0);
@ -348,9 +355,9 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
switch (buf.remaining()) switch (buf.remaining())
{ {
case 0: case 0:
// buffer is full // buffer is full nothing to read
// nothing to read
return false; return false;
case 1: case 1:
// we don`t have enough data for header so we need to read // we don`t have enough data for header so we need to read
key.interestOps(key.interestOps() | SelectionKey.OP_READ); key.interestOps(key.interestOps() | SelectionKey.OP_READ);
@ -367,6 +374,7 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
buf.compact(); buf.compact();
} }
return false; return false;
default: default:
// data size excluding header size :> // data size excluding header size :>
final int dataPending = (buf.getShort() & 0xFFFF) - HEADER_SIZE; final int dataPending = (buf.getShort() & 0xFFFF) - HEADER_SIZE;
@ -398,6 +406,7 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
} }
return true; return true;
} }
// we don`t have enough bytes for the dataPacket so we need // we don`t have enough bytes for the dataPacket so we need
// to read // to read
key.interestOps(key.interestOps() | SelectionKey.OP_READ); key.interestOps(key.interestOps() | SelectionKey.OP_READ);
@ -604,6 +613,8 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
// set the write buffer // set the write buffer
sp._buf = WRITE_BUFFER; sp._buf = WRITE_BUFFER;
// set the client.
sp._client = client;
// write content to buffer // write content to buffer
sp.write(); sp.write();
// delete the write buffer // delete the write buffer

View File

@ -428,6 +428,7 @@ public final class GameServer
sc.MAX_SEND_PER_PASS = Config.MMO_MAX_SEND_PER_PASS; sc.MAX_SEND_PER_PASS = Config.MMO_MAX_SEND_PER_PASS;
sc.SLEEP_TIME = Config.MMO_SELECTOR_SLEEP_TIME; sc.SLEEP_TIME = Config.MMO_SELECTOR_SLEEP_TIME;
sc.HELPER_BUFFER_COUNT = Config.MMO_HELPER_BUFFER_COUNT; sc.HELPER_BUFFER_COUNT = Config.MMO_HELPER_BUFFER_COUNT;
sc.TCP_NODELAY = Config.MMO_TCP_NODELAY;
_gamePacketHandler = new L2GamePacketHandler(); _gamePacketHandler = new L2GamePacketHandler();
_selectorThread = new SelectorThread<>(sc, _gamePacketHandler, _gamePacketHandler, _gamePacketHandler, new IPv4Filter()); _selectorThread = new SelectorThread<>(sc, _gamePacketHandler, _gamePacketHandler, _gamePacketHandler, new IPv4Filter());