01-15-2018, 10:17 PM
Нужна помощь с реализацией событий , поймать пакеты и разобрать смог.
Но вот реализовать не особо, может кто нибудь скинуть пример реализации например с хп.
Просто пример
Но вот реализовать не особо, может кто нибудь скинуть пример реализации например с хп.
Просто пример
BattleClientConnection
[SRC="java"]public class BattleClientConnection extends SimpleChannelInboundHandler<DatagramPacket>
{
private static final Logger log = ServiceManager.getLogger();
static byte[] data;
byte opcode;
byte slotID;
float time;
byte session;
short length;
short event1;
short event2;
ByteBuf InternalPacket;
int sub_opcode, slot, sub_length, EventType;
Room room;
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception
{
InetSocketAddress sender = msg.sender();
ByteBuf buf = msg.content().order(ByteOrder.LITTLE_ENDIAN);
opcode = buf.readByte();
slotID = buf.readByte();
time = buf.readFloat();
session = buf.readByte();
length = buf.readShort();
event1 = buf.readShort();
event2 = buf.readShort();
log.success("Opcode: " + opcode + " | slot: " + slotID + " | Time: " + time + " | Session: " + session + " | length: " + length + " | event 1,2: " + event1 + "|" + event2);
byte[] array = new byte[length - 13];
buf.readBytes(array);
InternalPacket = Unpooled.copiedBuffer(array).order(ByteOrder.LITTLE_ENDIAN);
ByteBuf originalbuffer = Unpooled.copiedBuffer(array).order(ByteOrder.LITTLE_ENDIAN);
switch (opcode)
{
case 3:
log.debug("Packet Replication Opcode 3 Slot :" + slotID);
if (this.room != null)
{
onlyDecrypt(originalbuffer, length);
sub_opcode = originalbuffer.readByte();
slot = originalbuffer.readShort();
sub_length = originalbuffer.readShort();
EventType = 0;
if (sub_opcode != 2)
{
EventType = originalbuffer.readInt();
}
System.out.println("| Sub-Opcode: " + sub_opcode + " | Slot: " + slot + "| length: " + sub_length + " | EventType: " + EventType + " |");
ByteBuf _buf = Unpooled.buffer(0, this.length).order(ByteOrder.LITTLE_ENDIAN);
_buf.writeByte(4); // OPCODE
_buf.writeByte(-1); // SLOT
_buf.writeFloat(time); // TIME
_buf.writeByte(1); // SESSION
_buf.writeShort(this.length); // LENGTH
_buf.writeShort(0); // EVENT 1
_buf.writeShort(0); // EVENT 2
_buf.writeBytes(InternalPacket.copy());
for (Player p : this.room.getPlayers().values())
{
// System.out.println(NetworkUtil.printData("Buffer OutPut 3", _buf));
ctx.writeAndFlush(new DatagramPacket(_buf.copy(), p.net));
log.warn("Relay Packet send to > " + p.slotID);
}
}
break;
case 4:
log.debug("Packet Replication Opcode 4 Slot :" + slotID);
if (this.room != null)
{
for (Player p : this.room.getPlayers().values())
{
ByteBuf _buf = Unpooled.buffer(0, this.length).order(ByteOrder.LITTLE_ENDIAN);
_buf.writeByte(4); // OPCODE
_buf.writeByte(-1); // SLOT
_buf.writeFloat(time); // TIME
_buf.writeByte(1); // SESSION
_buf.writeShort(this.length); // LENGTH
_buf.writeShort(0); // EVENT 1
_buf.writeShort(0); // EVENT 2
_buf.writeBytes(InternalPacket.copy());
ctx.writeAndFlush(new DatagramPacket(_buf, p.net));
log.warn("Relay Packet send to > " + p.slotID);
}
}
break;
case 65:
log.debug("Player Trying to connect to Server IP: " + msg.sender().getHostString() + " | Port: " + msg.sender().getPort());
onlyDecrypt(InternalPacket, length);
int unknown = InternalPacket.readInt();
int roomID = InternalPacket.readInt();
int roomSeed = InternalPacket.readInt();
log.debug("Room ID: " + roomID + " | Room SEED: " + roomSeed);
Room room = RoomController.getInstance().getRoom(roomID);
if (room != null)
{
log.debug("Room " + roomID + " is registered, Searching for Slot: " + slotID);
Player p = room.getPlayerBySlot(slotID);
if (p != null)
{
log.debug("Slot Founded !!!");
p.net = new InetSocketAddress(sender.getHostName(), sender.getPort());
//p.room = room; --временно
p.ctx = ctx;
this.room = room;
// Packet 66
ByteBuf _buf = Unpooled.buffer(0, 14).order(ByteOrder.LITTLE_ENDIAN);
_buf.writeByte(66); // OPCODE
_buf.writeByte(0); // SLOT
_buf.writeFloat(time); // TIME
_buf.writeByte(1); // SESSION
_buf.writeShort(14); // LENGTH
_buf.writeShort(0); // EVENT 1
_buf.writeShort(0); // EVENT 2
_buf.writeByte(8);
ctx.writeAndFlush(new DatagramPacket(_buf, p.net));
log.debug("Packet 66 Send");
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
}
});
t.start();
}
}
PrintInfo.getInstance().printSection("End Packet 65");
break;
default:
//log.warn("UNHANDLED PACKET");
break;
}
}
@Override
public void exceptionCaught(
ChannelHandlerContext ctx, Throwable cause)
throws Exception
{
cause.printStackTrace();
}
public static void onlyDecrypt(ByteBuf buf, int length)
{
data = buf.array();
byte lastElement = data[data.length - 1];
for (int i = data.length - 1; i > 0; i--)
{
data[i] = (byte) (((data[i - 1] & 0xFF) << (8 - getShift(length))) | ((data[i] & 0xFF) >> getShift(length)));
}
data[0] = (byte) ((lastElement << (8 - getShift(length))) | ((data[0] & 0xFF) >> getShift(length)));
// System.out.println(NetworkUtil.printData("Decoded [shift: " + getShift(length) + "]", buf));
}
public static void Shift(ByteBuf buffer, int bits)
{
data = buffer.array();
int length = data.length;
byte first = data[0];
byte current;
for (int i = 0; i < length; i++)
{
if (i >= (length - 1))
{
current = first;
} else
{
current = data[i + 1];
}
data[i] = (byte) (current >> (8 - bits) | (data[i] << bits));
}
}
public static int getShift(int length)
{
return length % 6 + 1;
}
public static void Shift(byte[] buffer, int bits)
{
int length = buffer.length;
byte first = buffer[0];
byte current;
for (int i = 0; i < length; i++)
{
if (i >= (length - 1))
{
current = first;
} else
{
current = buffer[i + 1];
}
buffer[i] = (byte) (current >> (8 - bits) | (buffer[i] << bits));
}
}
}
[/SRC]
{
private static final Logger log = ServiceManager.getLogger();
static byte[] data;
byte opcode;
byte slotID;
float time;
byte session;
short length;
short event1;
short event2;
ByteBuf InternalPacket;
int sub_opcode, slot, sub_length, EventType;
Room room;
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception
{
InetSocketAddress sender = msg.sender();
ByteBuf buf = msg.content().order(ByteOrder.LITTLE_ENDIAN);
opcode = buf.readByte();
slotID = buf.readByte();
time = buf.readFloat();
session = buf.readByte();
length = buf.readShort();
event1 = buf.readShort();
event2 = buf.readShort();
log.success("Opcode: " + opcode + " | slot: " + slotID + " | Time: " + time + " | Session: " + session + " | length: " + length + " | event 1,2: " + event1 + "|" + event2);
byte[] array = new byte[length - 13];
buf.readBytes(array);
InternalPacket = Unpooled.copiedBuffer(array).order(ByteOrder.LITTLE_ENDIAN);
ByteBuf originalbuffer = Unpooled.copiedBuffer(array).order(ByteOrder.LITTLE_ENDIAN);
switch (opcode)
{
case 3:
log.debug("Packet Replication Opcode 3 Slot :" + slotID);
if (this.room != null)
{
onlyDecrypt(originalbuffer, length);
sub_opcode = originalbuffer.readByte();
slot = originalbuffer.readShort();
sub_length = originalbuffer.readShort();
EventType = 0;
if (sub_opcode != 2)
{
EventType = originalbuffer.readInt();
}
System.out.println("| Sub-Opcode: " + sub_opcode + " | Slot: " + slot + "| length: " + sub_length + " | EventType: " + EventType + " |");
ByteBuf _buf = Unpooled.buffer(0, this.length).order(ByteOrder.LITTLE_ENDIAN);
_buf.writeByte(4); // OPCODE
_buf.writeByte(-1); // SLOT
_buf.writeFloat(time); // TIME
_buf.writeByte(1); // SESSION
_buf.writeShort(this.length); // LENGTH
_buf.writeShort(0); // EVENT 1
_buf.writeShort(0); // EVENT 2
_buf.writeBytes(InternalPacket.copy());
for (Player p : this.room.getPlayers().values())
{
// System.out.println(NetworkUtil.printData("Buffer OutPut 3", _buf));
ctx.writeAndFlush(new DatagramPacket(_buf.copy(), p.net));
log.warn("Relay Packet send to > " + p.slotID);
}
}
break;
case 4:
log.debug("Packet Replication Opcode 4 Slot :" + slotID);
if (this.room != null)
{
for (Player p : this.room.getPlayers().values())
{
ByteBuf _buf = Unpooled.buffer(0, this.length).order(ByteOrder.LITTLE_ENDIAN);
_buf.writeByte(4); // OPCODE
_buf.writeByte(-1); // SLOT
_buf.writeFloat(time); // TIME
_buf.writeByte(1); // SESSION
_buf.writeShort(this.length); // LENGTH
_buf.writeShort(0); // EVENT 1
_buf.writeShort(0); // EVENT 2
_buf.writeBytes(InternalPacket.copy());
ctx.writeAndFlush(new DatagramPacket(_buf, p.net));
log.warn("Relay Packet send to > " + p.slotID);
}
}
break;
case 65:
log.debug("Player Trying to connect to Server IP: " + msg.sender().getHostString() + " | Port: " + msg.sender().getPort());
onlyDecrypt(InternalPacket, length);
int unknown = InternalPacket.readInt();
int roomID = InternalPacket.readInt();
int roomSeed = InternalPacket.readInt();
log.debug("Room ID: " + roomID + " | Room SEED: " + roomSeed);
Room room = RoomController.getInstance().getRoom(roomID);
if (room != null)
{
log.debug("Room " + roomID + " is registered, Searching for Slot: " + slotID);
Player p = room.getPlayerBySlot(slotID);
if (p != null)
{
log.debug("Slot Founded !!!");
p.net = new InetSocketAddress(sender.getHostName(), sender.getPort());
//p.room = room; --временно
p.ctx = ctx;
this.room = room;
// Packet 66
ByteBuf _buf = Unpooled.buffer(0, 14).order(ByteOrder.LITTLE_ENDIAN);
_buf.writeByte(66); // OPCODE
_buf.writeByte(0); // SLOT
_buf.writeFloat(time); // TIME
_buf.writeByte(1); // SESSION
_buf.writeShort(14); // LENGTH
_buf.writeShort(0); // EVENT 1
_buf.writeShort(0); // EVENT 2
_buf.writeByte(8);
ctx.writeAndFlush(new DatagramPacket(_buf, p.net));
log.debug("Packet 66 Send");
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
}
});
t.start();
}
}
PrintInfo.getInstance().printSection("End Packet 65");
break;
default:
//log.warn("UNHANDLED PACKET");
break;
}
}
@Override
public void exceptionCaught(
ChannelHandlerContext ctx, Throwable cause)
throws Exception
{
cause.printStackTrace();
}
public static void onlyDecrypt(ByteBuf buf, int length)
{
data = buf.array();
byte lastElement = data[data.length - 1];
for (int i = data.length - 1; i > 0; i--)
{
data[i] = (byte) (((data[i - 1] & 0xFF) << (8 - getShift(length))) | ((data[i] & 0xFF) >> getShift(length)));
}
data[0] = (byte) ((lastElement << (8 - getShift(length))) | ((data[0] & 0xFF) >> getShift(length)));
// System.out.println(NetworkUtil.printData("Decoded [shift: " + getShift(length) + "]", buf));
}
public static void Shift(ByteBuf buffer, int bits)
{
data = buffer.array();
int length = data.length;
byte first = data[0];
byte current;
for (int i = 0; i < length; i++)
{
if (i >= (length - 1))
{
current = first;
} else
{
current = data[i + 1];
}
data[i] = (byte) (current >> (8 - bits) | (data[i] << bits));
}
}
public static int getShift(int length)
{
return length % 6 + 1;
}
public static void Shift(byte[] buffer, int bits)
{
int length = buffer.length;
byte first = buffer[0];
byte current;
for (int i = 0; i < length; i++)
{
if (i >= (length - 1))
{
current = first;
} else
{
current = buffer[i + 1];
}
buffer[i] = (byte) (current >> (8 - bits) | (buffer[i] << bits));
}
}
}
[/SRC]