Refactoring of BNetServer
This commit is contained in:
@@ -0,0 +1,245 @@
|
||||
/*
|
||||
* Copyright (C) 2012-2020 CypherCore <http://github.com/CypherCore>
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
using Framework.Constants;
|
||||
using Framework.IO;
|
||||
using Game.Entities;
|
||||
using System;
|
||||
|
||||
namespace Game.Networking
|
||||
{
|
||||
public abstract class ClientPacket : IDisposable
|
||||
{
|
||||
protected ClientPacket(WorldPacket worldPacket)
|
||||
{
|
||||
_worldPacket = worldPacket;
|
||||
}
|
||||
|
||||
public abstract void Read();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_worldPacket.Dispose();
|
||||
}
|
||||
|
||||
public ClientOpcodes GetOpcode() { return (ClientOpcodes)_worldPacket.GetOpcode(); }
|
||||
|
||||
public void LogPacket(WorldSession session)
|
||||
{
|
||||
Log.outDebug(LogFilter.Network, "Received ClientOpcode: {0} From: {1}", GetOpcode(), session != null ? session.GetPlayerInfo() : "Unknown IP");
|
||||
}
|
||||
|
||||
protected WorldPacket _worldPacket;
|
||||
}
|
||||
|
||||
public abstract class ServerPacket
|
||||
{
|
||||
protected ServerPacket(ServerOpcodes opcode)
|
||||
{
|
||||
connectionType = ConnectionType.Realm;
|
||||
_worldPacket = new WorldPacket(opcode);
|
||||
}
|
||||
|
||||
protected ServerPacket(ServerOpcodes opcode, ConnectionType type = ConnectionType.Realm)
|
||||
{
|
||||
connectionType = type;
|
||||
_worldPacket = new WorldPacket(opcode);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_worldPacket.Clear();
|
||||
buffer = null;
|
||||
}
|
||||
|
||||
public ServerOpcodes GetOpcode()
|
||||
{
|
||||
return (ServerOpcodes)_worldPacket.GetOpcode();
|
||||
}
|
||||
|
||||
public byte[] GetData()
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public void LogPacket(WorldSession session)
|
||||
{
|
||||
Log.outDebug(LogFilter.Network, "Sent ServerOpcode: {0} To: {1}", GetOpcode(), session != null ? session.GetPlayerInfo() : "");
|
||||
}
|
||||
|
||||
public abstract void Write();
|
||||
|
||||
public void WritePacketData()
|
||||
{
|
||||
if (buffer != null)
|
||||
return;
|
||||
|
||||
Write();
|
||||
|
||||
buffer = _worldPacket.GetData();
|
||||
_worldPacket.Dispose();
|
||||
}
|
||||
|
||||
public ConnectionType GetConnection() { return connectionType; }
|
||||
|
||||
byte[] buffer;
|
||||
ConnectionType connectionType;
|
||||
protected WorldPacket _worldPacket;
|
||||
}
|
||||
|
||||
public class WorldPacket : ByteBuffer
|
||||
{
|
||||
public WorldPacket(ServerOpcodes opcode = ServerOpcodes.None)
|
||||
{
|
||||
this.opcode = (uint)opcode;
|
||||
}
|
||||
|
||||
public WorldPacket(byte[] data) : base(data)
|
||||
{
|
||||
opcode = ReadUInt16();
|
||||
}
|
||||
|
||||
public ObjectGuid ReadPackedGuid()
|
||||
{
|
||||
var loLength = ReadUInt8();
|
||||
var hiLength = ReadUInt8();
|
||||
var low = ReadPackedUInt64(loLength);
|
||||
return new ObjectGuid(ReadPackedUInt64(hiLength), low);
|
||||
}
|
||||
|
||||
private ulong ReadPackedUInt64(byte length)
|
||||
{
|
||||
if (length == 0)
|
||||
return 0;
|
||||
|
||||
var guid = 0ul;
|
||||
|
||||
for (var i = 0; i < 8; i++)
|
||||
if ((1 << i & length) != 0)
|
||||
guid |= (ulong)ReadUInt8() << (i * 8);
|
||||
|
||||
return guid;
|
||||
}
|
||||
|
||||
public Position ReadPosition()
|
||||
{
|
||||
return new Position(ReadFloat(), ReadFloat(), ReadFloat());
|
||||
}
|
||||
|
||||
public void WritePackedGuid(ObjectGuid guid)
|
||||
{
|
||||
if (guid.IsEmpty())
|
||||
{
|
||||
WriteUInt8(0);
|
||||
WriteUInt8(0);
|
||||
return;
|
||||
}
|
||||
|
||||
byte lowMask, highMask;
|
||||
byte[] lowPacked, highPacked;
|
||||
|
||||
var loSize = PackUInt64(guid.GetLowValue(), out lowMask, out lowPacked);
|
||||
var hiSize = PackUInt64(guid.GetHighValue(), out highMask, out highPacked);
|
||||
|
||||
WriteUInt8(lowMask);
|
||||
WriteUInt8(highMask);
|
||||
WriteBytes(lowPacked, loSize);
|
||||
WriteBytes(highPacked, hiSize);
|
||||
}
|
||||
|
||||
public void WritePackedUInt64(ulong guid)
|
||||
{
|
||||
byte mask;
|
||||
byte[] packed;
|
||||
var packedSize = PackUInt64(guid, out mask, out packed);
|
||||
|
||||
WriteUInt8(mask);
|
||||
WriteBytes(packed, packedSize);
|
||||
}
|
||||
|
||||
uint PackUInt64(ulong value, out byte mask, out byte[] result)
|
||||
{
|
||||
uint resultSize = 0;
|
||||
mask = 0;
|
||||
result = new byte[8];
|
||||
|
||||
for (byte i = 0; value != 0; ++i)
|
||||
{
|
||||
if ((value & 0xFF) != 0)
|
||||
{
|
||||
mask |= (byte)(1 << i);
|
||||
result[resultSize++] = (byte)(value & 0xFF);
|
||||
}
|
||||
|
||||
value >>= 8;
|
||||
}
|
||||
|
||||
return resultSize;
|
||||
}
|
||||
|
||||
public void WriteBytes(WorldPacket data)
|
||||
{
|
||||
FlushBits();
|
||||
WriteBytes(data.GetData());
|
||||
}
|
||||
|
||||
public void WriteXYZ(Position pos)
|
||||
{
|
||||
if (pos == null)
|
||||
return;
|
||||
|
||||
float x, y, z;
|
||||
pos.GetPosition(out x, out y, out z);
|
||||
WriteFloat(x);
|
||||
WriteFloat(y);
|
||||
WriteFloat(z);
|
||||
}
|
||||
public void WriteXYZO(Position pos)
|
||||
{
|
||||
float x, y, z, o;
|
||||
pos.GetPosition(out x, out y, out z, out o);
|
||||
WriteFloat(x);
|
||||
WriteFloat(y);
|
||||
WriteFloat(z);
|
||||
WriteFloat(o);
|
||||
}
|
||||
|
||||
public uint GetOpcode() { return opcode; }
|
||||
|
||||
uint opcode;
|
||||
}
|
||||
|
||||
public class PacketHeader
|
||||
{
|
||||
public int Size;
|
||||
public byte[] Tag = new byte[12];
|
||||
|
||||
public void Read(byte[] buffer)
|
||||
{
|
||||
Size = BitConverter.ToInt32(buffer, 0);
|
||||
Buffer.BlockCopy(buffer, 4, Tag, 0, 12);
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer byteBuffer)
|
||||
{
|
||||
byteBuffer.WriteInt32(Size);
|
||||
byteBuffer.WriteBytes(Tag, 12);
|
||||
}
|
||||
|
||||
public bool IsValidSize() { return Size < 0x40000; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user