mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
First pass of moving stuff out of UNetwork. (#481)
This commit is contained in:
parent
d39f999f0f
commit
2d37a9e87c
50
Assets/Mirror/Runtime/MessagePacker.cs
Normal file
50
Assets/Mirror/Runtime/MessagePacker.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace Mirror
|
||||
{
|
||||
// message packing all in one place, instead of constructing headers in all
|
||||
// kinds of different places
|
||||
//
|
||||
// MsgType (1-n bytes)
|
||||
// Content (ContentSize bytes)
|
||||
//
|
||||
// -> we use varint for headers because most messages will result in 1 byte
|
||||
// type/size headers then instead of always
|
||||
// using 2 bytes for shorts.
|
||||
// -> this reduces bandwidth by 10% if average message size is 20 bytes
|
||||
// (probably even shorter)
|
||||
public static class MessagePacker
|
||||
{
|
||||
// PackMessage is in hot path. caching the writer is really worth it to
|
||||
// avoid large amounts of allocations.
|
||||
static NetworkWriter packWriter = new NetworkWriter();
|
||||
|
||||
// pack message before sending
|
||||
// -> pass writer instead of byte[] so we can reuse it
|
||||
public static byte[] PackMessage(ushort msgType, MessageBase msg)
|
||||
{
|
||||
// reset cached writer length and position
|
||||
packWriter.SetLength(0);
|
||||
|
||||
// write message type
|
||||
packWriter.WritePackedUInt32(msgType);
|
||||
|
||||
// serialize message into writer
|
||||
msg.Serialize(packWriter);
|
||||
|
||||
// return byte[]
|
||||
return packWriter.ToArray();
|
||||
}
|
||||
|
||||
// unpack message after receiving
|
||||
// -> pass NetworkReader so it's less strange if we create it in here
|
||||
// and pass it upwards.
|
||||
// -> NetworkReader will point at content afterwards!
|
||||
public static bool UnpackMessage(NetworkReader messageReader, out ushort msgType)
|
||||
{
|
||||
// read message type (varint)
|
||||
msgType = (UInt16)messageReader.ReadPackedUInt32();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Mirror/Runtime/MessagePacker.cs.meta
Normal file
11
Assets/Mirror/Runtime/MessagePacker.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2db134099f0df4d96a84ae7a0cd9b4bc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
21
Assets/Mirror/Runtime/NetworkMessage.cs
Normal file
21
Assets/Mirror/Runtime/NetworkMessage.cs
Normal file
@ -0,0 +1,21 @@
|
||||
namespace Mirror
|
||||
{
|
||||
public struct NetworkMessage
|
||||
{
|
||||
public short msgType;
|
||||
public NetworkConnection conn;
|
||||
public NetworkReader reader;
|
||||
|
||||
public TMsg ReadMessage<TMsg>() where TMsg : MessageBase, new()
|
||||
{
|
||||
TMsg msg = new TMsg();
|
||||
msg.Deserialize(reader);
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void ReadMessage<TMsg>(TMsg msg) where TMsg : MessageBase
|
||||
{
|
||||
msg.Deserialize(reader);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Mirror/Runtime/NetworkMessage.cs.meta
Normal file
11
Assets/Mirror/Runtime/NetworkMessage.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eb04e4848a2e4452aa2dbd7adb801c51
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -58,25 +58,6 @@ public enum MsgType : short
|
||||
Highest = 47
|
||||
}
|
||||
|
||||
public struct NetworkMessage
|
||||
{
|
||||
public short msgType;
|
||||
public NetworkConnection conn;
|
||||
public NetworkReader reader;
|
||||
|
||||
public TMsg ReadMessage<TMsg>() where TMsg : MessageBase, new()
|
||||
{
|
||||
TMsg msg = new TMsg();
|
||||
msg.Deserialize(reader);
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void ReadMessage<TMsg>(TMsg msg) where TMsg : MessageBase
|
||||
{
|
||||
msg.Deserialize(reader);
|
||||
}
|
||||
}
|
||||
|
||||
public enum Version
|
||||
{
|
||||
Current = 1
|
||||
@ -87,50 +68,4 @@ public static class Channels
|
||||
public const int DefaultReliable = 0;
|
||||
public const int DefaultUnreliable = 1;
|
||||
}
|
||||
|
||||
// message packing all in one place, instead of constructing headers in all
|
||||
// kinds of different places
|
||||
//
|
||||
// MsgType (1-n bytes)
|
||||
// Content (ContentSize bytes)
|
||||
//
|
||||
// -> we use varint for headers because most messages will result in 1 byte
|
||||
// type/size headers then instead of always
|
||||
// using 2 bytes for shorts.
|
||||
// -> this reduces bandwidth by 10% if average message size is 20 bytes
|
||||
// (probably even shorter)
|
||||
public static class MessagePacker
|
||||
{
|
||||
// PackMessage is in hot path. caching the writer is really worth it to
|
||||
// avoid large amounts of allocations.
|
||||
static NetworkWriter packWriter = new NetworkWriter();
|
||||
|
||||
// pack message before sending
|
||||
// -> pass writer instead of byte[] so we can reuse it
|
||||
public static byte[] PackMessage(ushort msgType, MessageBase msg)
|
||||
{
|
||||
// reset cached writer length and position
|
||||
packWriter.SetLength(0);
|
||||
|
||||
// write message type
|
||||
packWriter.WritePackedUInt32(msgType);
|
||||
|
||||
// serialize message into writer
|
||||
msg.Serialize(packWriter);
|
||||
|
||||
// return byte[]
|
||||
return packWriter.ToArray();
|
||||
}
|
||||
|
||||
// unpack message after receiving
|
||||
// -> pass NetworkReader so it's less strange if we create it in here
|
||||
// and pass it upwards.
|
||||
// -> NetworkReader will point at content afterwards!
|
||||
public static bool UnpackMessage(NetworkReader messageReader, out ushort msgType)
|
||||
{
|
||||
// read message type (varint)
|
||||
msgType = (UInt16)messageReader.ReadPackedUInt32();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user