This commit is contained in:
112
trunk/java/com/l2jserver/util/network/BaseRecievePacket.java
Normal file
112
trunk/java/com/l2jserver/util/network/BaseRecievePacket.java
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2014 L2J Server
|
||||
*
|
||||
* This file is part of L2J Server.
|
||||
*
|
||||
* L2J Server 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.
|
||||
*
|
||||
* L2J Server 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.l2jserver.util.network;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* This class ...
|
||||
* @version $Revision: 1.2.4.1 $ $Date: 2005/03/27 15:30:12 $
|
||||
*/
|
||||
public abstract class BaseRecievePacket
|
||||
{
|
||||
private static final Logger _log = Logger.getLogger(BaseRecievePacket.class.getName());
|
||||
|
||||
private final byte[] _decrypt;
|
||||
private int _off;
|
||||
|
||||
public BaseRecievePacket(byte[] decrypt)
|
||||
{
|
||||
_decrypt = decrypt;
|
||||
_off = 1; // skip packet type id
|
||||
}
|
||||
|
||||
public int readD()
|
||||
{
|
||||
int result = _decrypt[_off++] & 0xff;
|
||||
result |= (_decrypt[_off++] << 8) & 0xff00;
|
||||
result |= (_decrypt[_off++] << 0x10) & 0xff0000;
|
||||
result |= (_decrypt[_off++] << 0x18) & 0xff000000;
|
||||
return result;
|
||||
}
|
||||
|
||||
public int readC()
|
||||
{
|
||||
int result = _decrypt[_off++] & 0xff;
|
||||
return result;
|
||||
}
|
||||
|
||||
public int readH()
|
||||
{
|
||||
int result = _decrypt[_off++] & 0xff;
|
||||
result |= (_decrypt[_off++] << 8) & 0xff00;
|
||||
return result;
|
||||
}
|
||||
|
||||
public double readF()
|
||||
{
|
||||
long result = _decrypt[_off++] & 0xff;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 8L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 16L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 24L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 32L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 40L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 48L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 56L;
|
||||
return Double.longBitsToDouble(result);
|
||||
}
|
||||
|
||||
public String readS()
|
||||
{
|
||||
String result = null;
|
||||
try
|
||||
{
|
||||
result = new String(_decrypt, _off, _decrypt.length - _off, "UTF-16LE");
|
||||
result = result.substring(0, result.indexOf(0x00));
|
||||
_off += (result.length() * 2) + 2;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.warning(getClass().getSimpleName() + ": " + e.getMessage());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public final byte[] readB(int length)
|
||||
{
|
||||
byte[] result = new byte[length];
|
||||
System.arraycopy(_decrypt, _off, result, 0, length);
|
||||
_off += length;
|
||||
return result;
|
||||
}
|
||||
|
||||
public long readQ()
|
||||
{
|
||||
long result = _decrypt[_off++] & 0xff;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 8L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 16L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 24L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 32L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 40L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 48L;
|
||||
result |= (_decrypt[_off++] & 0xffL) << 56L;
|
||||
return result;
|
||||
}
|
||||
}
|
139
trunk/java/com/l2jserver/util/network/BaseSendablePacket.java
Normal file
139
trunk/java/com/l2jserver/util/network/BaseSendablePacket.java
Normal file
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2014 L2J Server
|
||||
*
|
||||
* This file is part of L2J Server.
|
||||
*
|
||||
* L2J Server 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.
|
||||
*
|
||||
* L2J Server 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.l2jserver.util.network;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* This class ...
|
||||
* @version $Revision: 1.2.4.1 $ $Date: 2005/03/27 15:30:11 $
|
||||
*/
|
||||
public abstract class BaseSendablePacket
|
||||
{
|
||||
private static final Logger _log = Logger.getLogger(BaseSendablePacket.class.getName());
|
||||
|
||||
private final ByteArrayOutputStream _bao;
|
||||
|
||||
protected BaseSendablePacket()
|
||||
{
|
||||
_bao = new ByteArrayOutputStream();
|
||||
}
|
||||
|
||||
protected void writeD(int value)
|
||||
{
|
||||
_bao.write(value & 0xff);
|
||||
_bao.write((value >> 8) & 0xff);
|
||||
_bao.write((value >> 16) & 0xff);
|
||||
_bao.write((value >> 24) & 0xff);
|
||||
}
|
||||
|
||||
protected void writeH(int value)
|
||||
{
|
||||
_bao.write(value & 0xff);
|
||||
_bao.write((value >> 8) & 0xff);
|
||||
}
|
||||
|
||||
protected void writeC(int value)
|
||||
{
|
||||
_bao.write(value & 0xff);
|
||||
}
|
||||
|
||||
protected void writeF(double org)
|
||||
{
|
||||
long value = Double.doubleToRawLongBits(org);
|
||||
_bao.write((int) (value & 0xff));
|
||||
_bao.write((int) ((value >> 8) & 0xff));
|
||||
_bao.write((int) ((value >> 16) & 0xff));
|
||||
_bao.write((int) ((value >> 24) & 0xff));
|
||||
_bao.write((int) ((value >> 32) & 0xff));
|
||||
_bao.write((int) ((value >> 40) & 0xff));
|
||||
_bao.write((int) ((value >> 48) & 0xff));
|
||||
_bao.write((int) ((value >> 56) & 0xff));
|
||||
}
|
||||
|
||||
protected void writeS(String text)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (text != null)
|
||||
{
|
||||
_bao.write(text.getBytes("UTF-16LE"));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.warning(getClass().getSimpleName() + ": " + e.getMessage());
|
||||
}
|
||||
|
||||
_bao.write(0);
|
||||
_bao.write(0);
|
||||
}
|
||||
|
||||
protected void writeB(byte[] array)
|
||||
{
|
||||
try
|
||||
{
|
||||
_bao.write(array);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_log.warning(getClass().getSimpleName() + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeQ(long value)
|
||||
{
|
||||
_bao.write((int) (value & 0xff));
|
||||
_bao.write((int) ((value >> 8) & 0xff));
|
||||
_bao.write((int) ((value >> 16) & 0xff));
|
||||
_bao.write((int) ((value >> 24) & 0xff));
|
||||
_bao.write((int) ((value >> 32) & 0xff));
|
||||
_bao.write((int) ((value >> 40) & 0xff));
|
||||
_bao.write((int) ((value >> 48) & 0xff));
|
||||
_bao.write((int) ((value >> 56) & 0xff));
|
||||
}
|
||||
|
||||
public int getLength()
|
||||
{
|
||||
return _bao.size() + 2;
|
||||
}
|
||||
|
||||
public byte[] getBytes()
|
||||
{
|
||||
// if (this instanceof Init)
|
||||
// writeD(0x00); //reserve for XOR initial key
|
||||
|
||||
writeD(0x00); // reserve for checksum
|
||||
|
||||
int padding = _bao.size() % 8;
|
||||
if (padding != 0)
|
||||
{
|
||||
for (int i = padding; i < 8; i++)
|
||||
{
|
||||
writeC(0x00);
|
||||
}
|
||||
}
|
||||
|
||||
return _bao.toByteArray();
|
||||
}
|
||||
|
||||
public abstract byte[] getContent() throws IOException;
|
||||
}
|
Reference in New Issue
Block a user