Skip to content

Commit 2ce6504

Browse files
committed
Exception policy RevenantX#89: added typed exceptions
1 parent 6267384 commit 2ce6504

10 files changed

Lines changed: 124 additions & 9 deletions

File tree

LiteNetLib/LiteNetLib.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<ItemGroup>
4444
<Compile Include="INetEventListener.cs" />
4545
<Compile Include="NetDebug.cs" />
46+
<Compile Include="NetExceptions.cs" />
4647
<Compile Include="NetPacketPool.cs" />
4748
<Compile Include="NetPeerCollection.cs" />
4849
<Compile Include="NetStatistics.cs" />
@@ -65,6 +66,7 @@
6566
<Compile Include="Utils\NetDataWriter.cs" />
6667
<Compile Include="Utils\NetPacketProcessor.cs" />
6768
<Compile Include="Utils\NetSerializer.cs" />
69+
<Compile Include="Utils\NetSerializerExceptions.cs" />
6870
</ItemGroup>
6971
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
7072
</Project>

LiteNetLib/NetEndPoint.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public override int GetHashCode()
4040
return EndPoint.GetHashCode();
4141
}
4242

43+
/// <param name="hostStr">A valid host string that can be resolved by DNS or parsed as an IP address</param>
44+
/// <param name="port">Port of the end point</param>
45+
/// <exception cref="ArgumentException"> <paramref name="hostStr"/> contains an invalid IP address</exception>>
46+
/// <exception cref="ArgumentOutOfRangeException">
47+
/// <paramref name="port"/> is less than IPEndPoint.MinPort or port is greater than IPEndPoint.MaxPort</exception>
4348
public NetEndPoint(string hostStr, int port)
4449
{
4550
IPAddress addr = GetFromString(hostStr);
@@ -69,7 +74,7 @@ internal static IPAddress GetFromString(string hostStr)
6974
}
7075
if (ipAddress == null)
7176
{
72-
throw new Exception("Invalid address: " + hostStr);
77+
throw new ArgumentException("Invalid address: " + hostStr);
7378
}
7479

7580
return ipAddress;

LiteNetLib/NetExceptions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
namespace LiteNetLib
3+
{
4+
public class InvalidPacketException: ArgumentException
5+
{
6+
public InvalidPacketException()
7+
{
8+
}
9+
10+
public InvalidPacketException(string message): base(message)
11+
{
12+
}
13+
14+
public InvalidPacketException(string message, Exception innerException): base(message, innerException)
15+
{
16+
}
17+
}
18+
19+
public class TooBigPacketException : InvalidPacketException
20+
{
21+
public TooBigPacketException()
22+
{
23+
}
24+
25+
public TooBigPacketException(string message) : base(message)
26+
{
27+
}
28+
29+
public TooBigPacketException(string message, Exception innerException) : base(message, innerException)
30+
{
31+
}
32+
}
33+
}

LiteNetLib/NetManager.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,7 @@ public void PollEvents()
974974
/// <param name="port">Server Port</param>
975975
/// <param name="key">Connection key</param>
976976
/// <returns>Null if connections limit reached, New NetPeer if new connection, Old NetPeer if already connected</returns>
977+
/// <exception cref="InvalidOperationException">Manager is not running. Call <see cref="Start()"/></exception>
977978
public NetPeer Connect(string address, int port, string key)
978979
{
979980
var ep = new NetEndPoint(address, port);
@@ -987,6 +988,7 @@ public NetPeer Connect(string address, int port, string key)
987988
/// <param name="port">Server Port</param>
988989
/// <param name="connectionData">Additional data for remote peer</param>
989990
/// <returns>Null if connections limit reached, New NetPeer if new connection, Old NetPeer if already connected</returns>
991+
/// <exception cref="InvalidOperationException">Manager is not running. Call <see cref="Start()"/></exception>
990992
public NetPeer Connect(string address, int port, NetDataWriter connectionData)
991993
{
992994
var ep = new NetEndPoint(address, port);
@@ -999,6 +1001,7 @@ public NetPeer Connect(string address, int port, NetDataWriter connectionData)
9991001
/// <param name="target">Server end point (ip and port)</param>
10001002
/// <param name="key">Connection key</param>
10011003
/// <returns>Null if connections limit reached, New NetPeer if new connection, Old NetPeer if already connected</returns>
1004+
/// <exception cref="InvalidOperationException">Manager is not running. Call <see cref="Start()"/></exception>
10021005
public NetPeer Connect(NetEndPoint target, string key)
10031006
{
10041007
return Connect(target, NetDataWriter.FromString(key));
@@ -1010,11 +1013,12 @@ public NetPeer Connect(NetEndPoint target, string key)
10101013
/// <param name="target">Server end point (ip and port)</param>
10111014
/// <param name="connectionData">Additional data for remote peer</param>
10121015
/// <returns>Null if connections limit reached, New NetPeer if new connection, Old NetPeer if already connected</returns>
1016+
/// <exception cref="InvalidOperationException">Manager is not running. Call <see cref="Start()"/></exception>
10131017
public NetPeer Connect(NetEndPoint target, NetDataWriter connectionData)
10141018
{
10151019
if (!IsRunning)
10161020
{
1017-
throw new Exception("Client is not running");
1021+
throw new InvalidOperationException("Client is not running");
10181022
}
10191023
lock (_peers)
10201024
{

LiteNetLib/NetPeer.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ public int GetMaxSinglePacketSize(DeliveryMethod options)
291291
/// </summary>
292292
/// <param name="data">Data</param>
293293
/// <param name="options">Send options (reliable, unreliable, etc.)</param>
294+
/// <exception cref="TooBigPacketException">
295+
/// <para>If size exceeds maximum limit:</para>
296+
/// <para>MTU - headerSize bytes for Unreliable</para>
297+
/// <para>Fragment count exceeded ushort.MaxValue</para>
298+
/// </exception>
294299
public void Send(byte[] data, DeliveryMethod options)
295300
{
296301
Send(data, 0, data.Length, options);
@@ -301,6 +306,11 @@ public void Send(byte[] data, DeliveryMethod options)
301306
/// </summary>
302307
/// <param name="dataWriter">DataWriter with data</param>
303308
/// <param name="options">Send options (reliable, unreliable, etc.)</param>
309+
/// <exception cref="TooBigPacketException">
310+
/// <para>If size exceeds maximum limit:</para>
311+
/// <para>MTU - headerSize bytes for Unreliable</para>
312+
/// <para>Fragment count exceeded ushort.MaxValue</para>
313+
/// </exception>
304314
public void Send(NetDataWriter dataWriter, DeliveryMethod options)
305315
{
306316
Send(dataWriter.Data, 0, dataWriter.Length, options);
@@ -313,6 +323,11 @@ public void Send(NetDataWriter dataWriter, DeliveryMethod options)
313323
/// <param name="start">Start of data</param>
314324
/// <param name="length">Length of data</param>
315325
/// <param name="options">Send options (reliable, unreliable, etc.)</param>
326+
/// <exception cref="TooBigPacketException">
327+
/// <para>If size exceeds maximum limit:</para>
328+
/// <para>MTU - headerSize bytes for Unreliable</para>
329+
/// <para>Fragment count exceeded ushort.MaxValue</para>
330+
/// </exception>
316331
public void Send(byte[] data, int start, int length, DeliveryMethod options)
317332
{
318333
if (_connectionState == ConnectionState.ShutdownRequested ||
@@ -329,7 +344,7 @@ public void Send(byte[] data, int start, int length, DeliveryMethod options)
329344
{
330345
if (options == DeliveryMethod.Sequenced || options == DeliveryMethod.Unreliable)
331346
{
332-
throw new ArgumentException("Unreliable packet size > allowed (" + (mtu - headerSize) + ")");
347+
throw new TooBigPacketException("Unreliable packet size exceeded maximum of " + (_mtu - headerSize) + " bytes");
333348
}
334349

335350
int packetFullSize = mtu - headerSize;
@@ -351,7 +366,7 @@ public void Send(byte[] data, int start, int length, DeliveryMethod options)
351366

352367
if (totalPackets > ushort.MaxValue)
353368
{
354-
throw new Exception("Too many fragments: " + totalPackets + " > " + ushort.MaxValue);
369+
throw new TooBigPacketException("Data was split in " + totalPackets + " fragments, which exceeds " + ushort.MaxValue);
355370
}
356371

357372
int dataOffset = headerSize + NetConstants.FragmentHeaderSize;
@@ -485,7 +500,7 @@ private void SendPacket(NetPacket packet)
485500
_packetPool.Recycle(packet);
486501
break;
487502
default:
488-
throw new Exception("Unknown packet property: " + packet.Property);
503+
throw new InvalidPacketException("Unknown packet property: " + packet.Property);
489504
}
490505
}
491506

LiteNetLib/Utils/NetPacketProcessor.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected virtual SubscrieDelegate GetCallbackFromData(NetDataReader reader)
3737
SubscrieDelegate action;
3838
if (!_callbacks.TryGetValue(hash, out action))
3939
{
40-
throw new Exception("Undefined packet in NetDataReader");
40+
throw new ParseException("Undefined packet in NetDataReader");
4141
}
4242
return action;
4343
}
@@ -95,6 +95,7 @@ public void ReadAllPackets(NetDataReader reader)
9595
/// </summary>
9696
/// <param name="reader">NetDataReader with packets data</param>
9797
/// <param name="userData">Argument that passed to OnReceivedEvent</param>
98+
/// <exception cref="ParseException">Malformed packet</exception>
9899
public void ReadAllPackets(NetDataReader reader, object userData)
99100
{
100101
while (reader.AvailableBytes > 0)
@@ -107,6 +108,7 @@ public void ReadAllPackets(NetDataReader reader, object userData)
107108
/// Reads one packet from NetDataReader and calls OnReceive delegate
108109
/// </summary>
109110
/// <param name="reader">NetDataReader with packet</param>
111+
/// <exception cref="ParseException">Malformed packet</exception>
110112
public void ReadPacket(NetDataReader reader)
111113
{
112114
ReadPacket(reader, null);
@@ -173,6 +175,7 @@ public byte[] WriteNetSerializable<T>(T packet) where T : INetSerializable
173175
/// </summary>
174176
/// <param name="reader">NetDataReader with packet</param>
175177
/// <param name="userData">Argument that passed to OnReceivedEvent</param>
178+
/// <exception cref="ParseException">Malformed packet</exception>
176179
public void ReadPacket(NetDataReader reader, object userData)
177180
{
178181
GetCallbackFromData(reader)(reader, userData);
@@ -183,6 +186,7 @@ public void ReadPacket(NetDataReader reader, object userData)
183186
/// </summary>
184187
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
185188
/// <param name="packetConstructor">Method that constructs packet intead of slow Activator.CreateInstance</param>
189+
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
186190
public void Subscribe<T>(Action<T> onReceive, Func<T> packetConstructor) where T : class, new()
187191
{
188192
_netSerializer.Register<T>();
@@ -199,6 +203,7 @@ public void ReadPacket(NetDataReader reader, object userData)
199203
/// </summary>
200204
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
201205
/// <param name="packetConstructor">Method that constructs packet intead of slow Activator.CreateInstance</param>
206+
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
202207
public void Subscribe<T, TUserData>(Action<T, TUserData> onReceive, Func<T> packetConstructor) where T : class, new()
203208
{
204209
_netSerializer.Register<T>();
@@ -215,6 +220,7 @@ public void ReadPacket(NetDataReader reader, object userData)
215220
/// This metod will overwrite last received packet class on receive (less garbage)
216221
/// </summary>
217222
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
223+
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
218224
public void SubscribeReusable<T>(Action<T> onReceive) where T : class, new()
219225
{
220226
_netSerializer.Register<T>();
@@ -231,6 +237,7 @@ public void ReadPacket(NetDataReader reader, object userData)
231237
/// This metod will overwrite last received packet class on receive (less garbage)
232238
/// </summary>
233239
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
240+
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
234241
public void SubscribeReusable<T, TUserData>(Action<T, TUserData> onReceive) where T : class, new()
235242
{
236243
_netSerializer.Register<T>();

LiteNetLib/Utils/NetSerializer.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private StructInfo RegisterInternal<T>()
196196
int propsCount = props.Length;
197197
if (props == null || propsCount == 0)
198198
{
199-
throw new ArgumentException("Type does not contain acceptable fields");
199+
throw new InvalidTypeException("Type does not contain acceptable fields");
200200
}
201201

202202
info = new StructInfo(propsCount);
@@ -241,7 +241,7 @@ private StructInfo RegisterInternal<T>()
241241
}
242242
else
243243
{
244-
throw new Exception("Not supported enum underlying type: " + underlyingType.Name);
244+
throw new InvalidTypeException("Not supported enum underlying type: " + underlyingType.Name);
245245
}
246246
}
247247
else if (propertyType == typeof(string))
@@ -471,7 +471,7 @@ private StructInfo RegisterInternal<T>()
471471
}
472472
else
473473
{
474-
throw new Exception("Unknown property type: " + propertyType.FullName);
474+
throw new InvalidTypeException("Unknown property type: " + propertyType.FullName);
475475
}
476476
}
477477
}
@@ -480,6 +480,7 @@ private StructInfo RegisterInternal<T>()
480480
return info;
481481
}
482482

483+
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
483484
public void Register<T>()
484485
{
485486
RegisterInternal<T>();
@@ -490,6 +491,7 @@ public void Register<T>()
490491
/// </summary>
491492
/// <param name="reader">NetDataReader with packet</param>
492493
/// <returns>Returns packet if packet in reader is matched type</returns>
494+
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
493495
public T Deserialize<T>(NetDataReader reader) where T : class, new()
494496
{
495497
var info = RegisterInternal<T>();
@@ -511,6 +513,7 @@ public void Register<T>()
511513
/// <param name="reader">NetDataReader with packet</param>
512514
/// <param name="target">Deserialization target</param>
513515
/// <returns>Returns true if packet in reader is matched type</returns>
516+
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
514517
public bool Deserialize<T>(NetDataReader reader, T target) where T : class, new()
515518
{
516519
var info = RegisterInternal<T>();
@@ -531,6 +534,7 @@ public void Register<T>()
531534
/// </summary>
532535
/// <param name="writer">Serialization target NetDataWriter</param>
533536
/// <param name="obj">Object to serialize</param>
537+
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
534538
public void Serialize<T>(NetDataWriter writer, T obj) where T : class, new()
535539
{
536540
RegisterInternal<T>().Write(writer, obj);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
namespace LiteNetLib.Utils
3+
{
4+
public class InvalidTypeException : ArgumentException
5+
{
6+
public InvalidTypeException()
7+
{
8+
}
9+
10+
public InvalidTypeException(string message) : base(message)
11+
{
12+
}
13+
14+
public InvalidTypeException(string message, Exception innerException) : base(message, innerException)
15+
{
16+
}
17+
18+
public InvalidTypeException(string message, string paramName) : base(message, paramName)
19+
{
20+
}
21+
22+
public InvalidTypeException(string message, string paramName, Exception innerException) : base(message, paramName, innerException)
23+
{
24+
}
25+
}
26+
27+
public class ParseException : Exception
28+
{
29+
public ParseException()
30+
{
31+
}
32+
33+
public ParseException(string message) : base(message)
34+
{
35+
}
36+
37+
public ParseException(string message, Exception innerException) : base(message, innerException)
38+
{
39+
}
40+
}
41+
}

LiteNetLibNetCore/LiteNetLibNetCore.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<Compile Include="..\LiteNetLib\NetConstants.cs" Link="NetConstants.cs" />
2424
<Compile Include="..\LiteNetLib\NetDebug.cs" Link="NetDebug.cs" />
2525
<Compile Include="..\LiteNetLib\NetEndPoint.cs" Link="NetEndPoint.cs" />
26+
<Compile Include="..\LiteNetLib\NetExceptions.cs" Link="NetExceptions.cs" />
2627
<Compile Include="..\LiteNetLib\NetManager.cs" Link="NetManager.cs" />
2728
<Compile Include="..\LiteNetLib\NetPacket.cs" Link="NetPacket.cs" />
2829
<Compile Include="..\LiteNetLib\NetPacketPool.cs" Link="NetPacketPool.cs" />
@@ -41,6 +42,7 @@
4142
<Compile Include="..\LiteNetLib\Utils\NetDataWriter.cs" Link="Utils\NetDataWriter.cs" />
4243
<Compile Include="..\LiteNetLib\Utils\NetPacketProcessor.cs" Link="Utils\NetPacketProcessor.cs" />
4344
<Compile Include="..\LiteNetLib\Utils\NetSerializer.cs" Link="Utils\NetSerializer.cs" />
45+
<Compile Include="..\LiteNetLib\Utils\NetSerializerExceptions.cs" Link="Utils\NetSerializerExceptions.cs" />
4446
</ItemGroup>
4547

4648
<ItemGroup>

LiteNetLibStandard/LiteNetLibStandard.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</PropertyGroup>
2222

2323
<ItemGroup>
24+
<Compile Include="..\LiteNetLib\NetExceptions.cs" Link="NetExceptions.cs" />
2425
<Compile Include="..\LiteNetLib\NetStatistics.cs" Link="NetStatistics.cs" />
2526
<Compile Include="..\LiteNetLib\Properties\AssemblyInfo.cs" />
2627
<Compile Include="..\LiteNetLib\INetEventListener.cs" Link="INetEventListener.cs" />
@@ -45,6 +46,7 @@
4546
<Compile Include="..\LiteNetLib\Utils\NetDataWriter.cs" Link="Utils\NetDataWriter.cs" />
4647
<Compile Include="..\LiteNetLib\Utils\NetPacketProcessor.cs" Link="Utils\NetPacketProcessor.cs" />
4748
<Compile Include="..\LiteNetLib\Utils\NetSerializer.cs" Link="Utils\NetSerializer.cs" />
49+
<Compile Include="..\LiteNetLib\Utils\NetSerializerExceptions.cs" Link="Utils\NetSerializerExceptions.cs" />
4850
</ItemGroup>
4951

5052
<ItemGroup>

0 commit comments

Comments
 (0)