DarkRift - XianWorld/xxxgame GitHub Wiki
Features:
- Plugin dll load and execute
- Simple server configuration
- Database plugin interface
- TCP
- Message object serialization extensions (BUT need to optimize)
- Full source code from Reflector
- No separate server lib but a console application
- Command line function and the commands can be extended by plugin
- Support customize message body with BinaryReader/BinaryWriter (BUT new the stream and reader/writer every times).
- Support object serializer/deserializer by BinaryFormatter.Serialze/Deserialize (BUT new a MemoryStream/BinaryFormatter every times).
Message Protocol
Message:
[StructLayout(LayoutKind.Sequential)]
public struct NetworkMessage
{
public ushort senderID;
public DistributionType distributionType;
public ushort destinationID;
public byte tag;
public ushort subject;
public object data;
internal NetworkMessage(ushort senderID, DistributionType distributionType, ushort destinationID, byte tag, ushort subject);
public NetworkMessage(ushort senderID, DistributionType distributionType, ushort destinationID, byte tag, ushort subject, object data);
public NetworkMessage(ushort senderID, DistributionType distributionType, ushort destinationID, byte tag, ushort subject, DarkRiftWriter data);
}
Encode functions:
private static byte[] EncodeMessage(NetworkMessage msg, byte version)
{
byte[] buffer;
switch (version)
{
case 0:
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(stream, msg.data);
}
catch (ArgumentNullException)
{
}
buffer = stream.ToArray();
}
break;
case 1:
{
DarkRiftWriter data = (DarkRiftWriter) msg.data;
buffer = ((MemoryStream) data.BaseStream).ToArray();
break;
}
default:
throw new VersionException(version);
}
byte[] dst = new byte[13 + buffer.Length];
Buffer.BlockCopy(BitConverter.GetBytes((uint) (dst.Length - 4)), 0, dst, 0, 4);
dst[4] = version;
Buffer.BlockCopy(BitConverter.GetBytes(msg.senderID), 0, dst, 5, 2);
dst[7] = (byte) msg.distributionType;
Buffer.BlockCopy(BitConverter.GetBytes(msg.destinationID), 0, dst, 8, 2);
dst[10] = msg.tag;
Buffer.BlockCopy(BitConverter.GetBytes(msg.subject), 0, dst, 11, 2);
Buffer.BlockCopy(buffer, 0, dst, 13, buffer.Length);
return dst;
}
Decode functions:
public static NetworkMessage DecodeMessage(byte[] bytes)
{
NetworkMessage message = DecodeMessageHeader(bytes);
message.data = DecodeMessageData(bytes);
return message;
}
public static NetworkMessage DecodeMessageHeader(byte[] bytes)
{
return new NetworkMessage(BitConverter.ToUInt16(bytes, 1), (DistributionType) bytes[3], BitConverter.ToUInt16(bytes, 4), bytes[6], BitConverter.ToUInt16(bytes, 7));
}
public static NetworkMessage DecodeMessageHeader(byte[] bytes)
{
return new NetworkMessage(BitConverter.ToUInt16(bytes, 1), (DistributionType) bytes[3], BitConverter.ToUInt16(bytes, 4), bytes[6], BitConverter.ToUInt16(bytes, 7));
}
public static object DecodeMessageData(byte[] bytes)
{
byte specifiedEncodingVersion = bytes[0];
switch (specifiedEncodingVersion)
{
case 0:
{
using (MemoryStream stream = new MemoryStream(bytes, 9, bytes.Length - 9))
{
BinaryFormatter formatter = new BinaryFormatter();
return formatter.Deserialize(stream);
}
}
case 1:
return new DarkRiftReader(new MemoryStream(bytes, 9, bytes.Length - 9, false));
}
throw new VersionException(specifiedEncodingVersion);
}