forked from RevenantX/LiteNetLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetserializerusage.html
More file actions
266 lines (241 loc) · 10 KB
/
netserializerusage.html
File metadata and controls
266 lines (241 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>NetPacketProcessor </title>
<meta name="viewport" content="width=device-width">
<meta name="title" content="NetPacketProcessor ">
<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.min.css">
<link rel="stylesheet" href="../styles/docfx.css">
<link rel="stylesheet" href="../styles/main.css">
<meta property="docfx:navrel" content="../toc.html">
<meta property="docfx:tocrel" content="toc.html">
</head>
<body data-spy="scroll" data-target="#affix" data-offset="120">
<div id="wrapper">
<header>
<nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="../index.html">
<img id="logo" class="svg" src="../logo.svg" alt="">
</a>
</div>
<div class="collapse navbar-collapse" id="navbar">
<form class="navbar-form navbar-right" role="search" id="search">
<div class="form-group">
<input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
</div>
</form>
</div>
</div>
</nav>
<div class="subnav navbar navbar-default">
<div class="container hide-when-search" id="breadcrumb">
<ul class="breadcrumb">
<li></li>
</ul>
</div>
</div>
</header>
<div role="main" class="container body-content hide-when-search">
<div class="sidenav hide-when-search">
<a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
<div class="sidetoggle collapse" id="sidetoggle">
<div id="sidetoc"></div>
</div>
</div>
<div class="article row grid-right">
<div class="col-md-10">
<article class="content wrap" id="_content" data-uid="">
<h1 id="netpacketprocessor">NetPacketProcessor</h1>
<p>Fast specialized for network purposes serializer.<br>
It supports <strong>classes</strong> with <strong>public properties with "get" and "set"</strong> methods or <strong>classes/structs which implements <code>INetSerializable</code></strong>.<br>
Serializer adds some overhead to packet size: 64 bit hash of class name and namespace (8 bytes). All other class fields will be as is in resulting packet.</p>
<h2 id="serialization-speed-comparsion">Serialization speed comparsion</h2>
<p>Serialization 100000 times of simple structure from <a href="https://github.com/RevenantX/LiteNetLib/blob/master/LibSample/SerializerBenchmark.cs">example</a> (<code>NET 4.5</code>):
Serializer|Time|Size
---|---|---|
BinaryFormatter|3334 ms|1096 bytes
NetSerializer (first run)|45 ms|204 bytes
NetSerializer (second run)|37 ms|204 bytes
Raw|24 ms|204 bytes</p>
<h2 id="supported-property-types">Supported property types</h2>
<pre><code class="lang-csharp">byte sbyte short ushort int uint long ulong float double bool string char IPEndPoint
</code></pre>
<p>Arrays of all these types (and custom types) are also supported. <br>
Enums are supported but work a bit slower than other types.</p>
<h2 id="custom-types">Custom types</h2>
<p>NetPacketProcessor doesn't support nested structs or classes, but you can register your own custom type processors.<br>
That useful for game engine types such as Vector3 and Quaternion (in Unity3d).</p>
<pre><code class="lang-csharp">// Your packet that will be sent over network
class SamplePacket
{
// Both property and array are supported
public MyType SomeMyType { get; set; }
public MyType[] SomeMyTypes { get; set; }
}
// Some custom type variant 1: Basic struct
struct MyType
{
public int Value1;
public string Value2;
public static void Serialize(NetDataWriter writer, SomeMyType mytype)
{
writer.Put(mytype.Value1);
writer.Put(mytype.Value2);
}
public static MyType Deserialize(NetDataReader reader)
{
MyType res = new MyType();
res.Value1 = reader.GetInt();
res.Value2 = reader.GetString();
return res;
}
}
...
netPacketProcessor = new NetPacketProcessor();
netPacketProcessor.RegisterNestedType( MyType.Serialize, MyType.Deserialize ); // Supply Serialization methods
</code></pre>
<p>You can also implement INetSerializable interface:</p>
<pre><code class="lang-csharp">// Some custom type variant 2: INetSerializable struct
struct MyType : INetSerializable
{
public int Value1;
public string Value2;
public void Serialize(NetDataWriter writer)
{
writer.Put(Value1);
writer.Put(Value2);
}
public void Deserialize(NetDataReader reader)
{
Value1 = reader.GetInt();
Value2 = reader.GetString();
}
}
...
netPacketProcessor = new NetPacketProcessor();
netPacketProcessor.RegisterNestedType<MyType>(); // Serialization handled automatically thanks to INetSerializable
</code></pre>
<p>If you want to use a class instead of a struct you must implement the INetSerializable interface and provide a constructor:</p>
<pre><code class="lang-csharp">// Some custom type variant 3: Class, must implement INetSerializable
class MyType : INetSerializable
{
public int Value1;
public string Value2;
public void Serialize(NetDataWriter writer)
{
writer.Put(Value1);
writer.Put(Value2);
}
public void Deserialize(NetDataReader reader)
{
Value1 = reader.GetInt();
Value2 = reader.GetString();
}
}
...
netPacketProcessor = new NetPacketProcessor();
netPacketProcessor.RegisterNestedType<MyType>(() => { return new SomeMyType(); }); // Must provide constructor
</code></pre>
<h2 id="usage-example">Usage example</h2>
<p>For full example look at source <a href="https://github.com/RevenantX/LiteNetLib/blob/master/LibSample/SerializerBenchmark.cs">SerializerBenchmark</a></p>
<h3 id="packet">Packet</h3>
<pre><code class="lang-csharp">class SamplePacket
{
// All of these will be automatically serialized and deserialized
public string SomeString { get; set; }
public float SomeFloat { get; set; }
public int[] SomeIntArray { get; set; }
}
</code></pre>
<h3 id="sending--recieving">Sending / recieving</h3>
<pre><code class="lang-csharp">// Client
class SomeClientListener : INetEventListener
{
private readonly NetPacketProcessor _netPacketProcessor = new NetPacketProcessor();
...
public void OnPeerConnected(NetPeer peer)
{
// After connection is established you will have the server as a NetPeer
SamplePacket packet = new SamplePacket
{
SomeFloat = 3.42f,
SomeIntArray = new[] {6, 5, 4},
SomeString = "Test String",
}
// Serialize the packet with NetSerializer and send it to the peer (server)
peer.Send(_netPacketProcessor.Write(packet), DeliveryMethod.ReliableOrdered);
//You can also use _netPacketProcessor.Send(peer, packet, DeliveryMethod.ReliableOrdered);
}
}
// Server
class SomeServerListener : INetEventListener
{
private readonly NetPacketProcessor _netPacketProcessor = new NetPacketProcessor();
public SomeServerListener()
{
// Subscribe to recieving packets.
_netPacketProcessor.SubscribeReusable<SamplePacket, NetPeer>(OnSamplePacketReceived);
}
// Handler for SamplePacket, registered in constructor above
private void OnSamplePacketReceived(SamplePacket samplePacket, NetPeer peer)
{
Console.WriteLine("[Server] ReceivedPacket:\n" + samplePacket.SomeString);
}
// INetEventListener function.
public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
{
Console.WriteLine("[Server] received data. Processing...");
// Deserializes packet and calls the handler registered in constructor
_netPacketProcessor.ReadAllPackets(reader, peer);
}
}
</code></pre>
<h3 id="mini-faq">Mini FAQ</h3>
<p>Q: <code>NetPacketProcessor</code> throws "<code>Undefined packet in NetDataReader</code>" but all packets are registered. <br>
A: This can happen when packet definitions resides in different namespaces. Check that registered packet classes/structs are in the same namespace on both ends.
To avoid this error altogether, use shared code/-assembly for packets.</p>
</article>
</div>
<div class="hidden-sm col-md-2" role="complementary">
<div class="sideaffix">
<div class="contribution">
<ul class="nav">
</ul>
</div>
<nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
<h5>In This Article</h5>
<div></div>
</nav>
</div>
</div>
</div>
</div>
<footer>
<div class="grad-bottom"></div>
<div class="footer">
<div class="container">
<span class="pull-right">
<a href="#top">Back to top</a>
</span>
<span>Generated by <strong>DocFX</strong></span>
</div>
</div>
</footer>
</div>
<script type="text/javascript" src="../styles/docfx.vendor.min.js"></script>
<script type="text/javascript" src="../styles/docfx.js"></script>
<script type="text/javascript" src="../styles/main.js"></script>
</body>
</html>