diff --git a/SteamKit2/SteamKit2/Networking/Steam3/UdpConnection.cs b/SteamKit2/SteamKit2/Networking/Steam3/UdpConnection.cs index 7f85b4454..7d8a4eb5d 100644 --- a/SteamKit2/SteamKit2/Networking/Steam3/UdpConnection.cs +++ b/SteamKit2/SteamKit2/Networking/Steam3/UdpConnection.cs @@ -7,7 +7,6 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.IO; using System.Net; using System.Net.Sockets; @@ -87,8 +86,8 @@ private enum State /// private uint inSeqHandled; - [NotNull] private List? outPackets; - [NotNull] private Dictionary? inPackets; + private List outPackets; + private Dictionary inPackets; private ILogContext log; @@ -102,6 +101,9 @@ public UdpConnection(ILogContext log) sock.Bind(localEndPoint); state = (int)State.Disconnected; + + outPackets = new List(); + inPackets = new Dictionary(); } public event EventHandler? NetMsgReceived; @@ -121,8 +123,8 @@ public UdpConnection(ILogContext log) /// Timeout in milliseconds public void Connect(EndPoint endPoint, int timeout) { - outPackets = new List(); - inPackets = new Dictionary(); + outPackets.Clear(); + inPackets.Clear(); CurrentEndPoint = null; remoteConnId = 0; diff --git a/SteamKit2/SteamKit2/Networking/Steam3/UdpPacket.cs b/SteamKit2/SteamKit2/Networking/Steam3/UdpPacket.cs index 3d649a8bd..4b865f27a 100644 --- a/SteamKit2/SteamKit2/Networking/Steam3/UdpPacket.cs +++ b/SteamKit2/SteamKit2/Networking/Steam3/UdpPacket.cs @@ -53,13 +53,17 @@ public UdpPacket(MemoryStream ms) } catch ( Exception ) { + Payload = new MemoryStream(); return; } if ( this.Header.Magic != UdpHeader.MAGIC ) + { + Payload = new MemoryStream(); return; + } - SetPayload(ms, Header.PayloadSize); + Payload = GetPayloadAndUpdateHeader( ms, Header.PayloadSize, Header ); } /// @@ -121,16 +125,22 @@ public void SetPayload(MemoryStream ms) /// The payload. /// The length. public void SetPayload(MemoryStream ms, long length) + { + Payload = GetPayloadAndUpdateHeader( ms, length, Header ); + } + + static MemoryStream GetPayloadAndUpdateHeader(MemoryStream ms, long length, UdpHeader header) { if ( length > MAX_PAYLOAD ) - throw new ArgumentException("Payload length exceeds 0x4DC maximum"); + throw new ArgumentException( "Payload length exceeds 0x4DC maximum" ); - byte[] buf = new byte[length]; - ms.Read(buf, 0, buf.Length); + byte[] buf = new byte[ length ]; + ms.Read( buf, 0, buf.Length ); - Payload = new MemoryStream(buf); - Header.PayloadSize = (ushort) Payload.Length; - Header.MsgSize = (uint) Payload.Length; + var payload = new MemoryStream( buf ); + header.PayloadSize = ( ushort )payload.Length; + header.MsgSize = ( uint )payload.Length; + return payload; } /// diff --git a/SteamKit2/SteamKit2/Steam/Discovery/BasicServerListProto.cs b/SteamKit2/SteamKit2/Steam/Discovery/BasicServerListProto.cs index e651cfe7b..c29b51fef 100644 --- a/SteamKit2/SteamKit2/Steam/Discovery/BasicServerListProto.cs +++ b/SteamKit2/SteamKit2/Steam/Discovery/BasicServerListProto.cs @@ -1,14 +1,12 @@ -using System.Diagnostics.CodeAnalysis; -using ProtoBuf; +using ProtoBuf; namespace SteamKit2.Discovery { [ProtoContract] class BasicServerListProto { - [ProtoMember(1)] - [DisallowNull, NotNull] - public string? Address { get; set; } + [ProtoMember( 1 )] + public string Address { get; set; } = string.Empty; [ProtoMember(2)] public int Port { get; set; } diff --git a/SteamKit2/SteamKit2/Steam/Discovery/FileStorageServerListProvider.cs b/SteamKit2/SteamKit2/Steam/Discovery/FileStorageServerListProvider.cs index 1da39e9a6..e46683191 100644 --- a/SteamKit2/SteamKit2/Steam/Discovery/FileStorageServerListProvider.cs +++ b/SteamKit2/SteamKit2/Steam/Discovery/FileStorageServerListProvider.cs @@ -35,10 +35,7 @@ public Task> FetchServerListAsync() using (FileStream fileStream = File.OpenRead(filename)) { return Serializer.DeserializeItems(fileStream, PrefixStyle.Base128, 1) - .Select(item => - { - return ServerRecord.CreateServer(item.Address, item.Port, item.Protocols); - }) + .Select(item => ServerRecord.CreateServer(item.Address, item.Port, item.Protocols)) .ToList(); } } diff --git a/SteamKit2/SteamKit2/Steam/Discovery/IsolatedStorageServerListProvider.cs b/SteamKit2/SteamKit2/Steam/Discovery/IsolatedStorageServerListProvider.cs index 089ef31eb..fc7a53342 100644 --- a/SteamKit2/SteamKit2/Steam/Discovery/IsolatedStorageServerListProvider.cs +++ b/SteamKit2/SteamKit2/Steam/Discovery/IsolatedStorageServerListProvider.cs @@ -38,10 +38,7 @@ public Task> FetchServerListAsync() using (var fileStream = isolatedStorage.OpenFile(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { return Serializer.DeserializeItems(fileStream, PrefixStyle.Base128, 1) - .Select(item => - { - return ServerRecord.CreateServer(item.Address, item.Port, item.Protocols); - }) + .Select(item => ServerRecord.CreateServer(item.Address, item.Port, item.Protocols)) .ToList(); } } diff --git a/SteamKit2/SteamKit2/Steam/Handlers/SteamUnifiedMessages/Callbacks.cs b/SteamKit2/SteamKit2/Steam/Handlers/SteamUnifiedMessages/Callbacks.cs index b7d46d398..206aa57b5 100644 --- a/SteamKit2/SteamKit2/Steam/Handlers/SteamUnifiedMessages/Callbacks.cs +++ b/SteamKit2/SteamKit2/Steam/Handlers/SteamUnifiedMessages/Callbacks.cs @@ -3,13 +3,11 @@ * file 'license.txt', which is part of this source code package. */ -using ProtoBuf; -using System.IO; -using System.Linq; -using SteamKit2.Internal; using System; +using System.IO; using System.Reflection; -using System.Diagnostics.CodeAnalysis; +using ProtoBuf; +using SteamKit2.Internal; namespace SteamKit2 { @@ -101,30 +99,26 @@ public string RpcName /// /// Gets the full name of the service method. /// - [DisallowNull, NotNull] - public string? MethodName { get; private set; } + public string MethodName { get; private set; } /// /// Gets the protobuf notification body. /// - [DisallowNull, NotNull] - public object? Body { get; private set; } + public object Body { get; private set; } internal ServiceMethodNotification( Type messageType, IPacketMsg packetMsg ) { // Bounce into generic-land. - var setupMethod = GetType().GetMethod( nameof(Setup), BindingFlags.Instance | BindingFlags.NonPublic ).MakeGenericMethod( messageType ); - setupMethod.Invoke( this, new[] { packetMsg } ); + var setupMethod = GetType().GetMethod( nameof(Setup), BindingFlags.Static | BindingFlags.NonPublic ).MakeGenericMethod( messageType ); + (MethodName, Body) = ((string, object))setupMethod.Invoke( this, new[] { packetMsg } ); } - void Setup( IPacketMsg packetMsg ) + static (string methodName, object body) Setup( IPacketMsg packetMsg ) where T : IExtensible, new() { var clientMsg = new ClientMsgProtobuf( packetMsg ); - - MethodName = clientMsg.Header.Proto.target_job_name; - Body = clientMsg.Body; + return (clientMsg.Header.Proto.target_job_name, clientMsg.Body); } } }