Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nullability warnings #971

Merged
merged 6 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions SteamKit2/SteamKit2/Networking/Steam3/UdpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Net.Sockets;
Expand Down Expand Up @@ -87,8 +86,8 @@ private enum State
/// </summary>
private uint inSeqHandled;

[NotNull] private List<UdpPacket>? outPackets;
[NotNull] private Dictionary<uint, UdpPacket>? inPackets;
private List<UdpPacket> outPackets;
private Dictionary<uint, UdpPacket> inPackets;

private ILogContext log;

Expand All @@ -102,6 +101,9 @@ public UdpConnection(ILogContext log)
sock.Bind(localEndPoint);

state = (int)State.Disconnected;

outPackets = new List<UdpPacket>();
inPackets = new Dictionary<uint, UdpPacket>();
}

public event EventHandler<NetMsgEventArgs>? NetMsgReceived;
Expand All @@ -121,8 +123,8 @@ public UdpConnection(ILogContext log)
/// <param name="timeout">Timeout in milliseconds</param>
public void Connect(EndPoint endPoint, int timeout)
{
outPackets = new List<UdpPacket>();
inPackets = new Dictionary<uint, UdpPacket>();
outPackets.Clear();
inPackets.Clear();

CurrentEndPoint = null;
remoteConnId = 0;
Expand Down
24 changes: 17 additions & 7 deletions SteamKit2/SteamKit2/Networking/Steam3/UdpPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/// <summary>
Expand Down Expand Up @@ -121,16 +125,22 @@ public void SetPayload(MemoryStream ms)
/// <param name="ms">The payload.</param>
/// <param name="length">The length.</param>
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;
}

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions SteamKit2/SteamKit2/Steam/Discovery/BasicServerListProto.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using ProtoBuf;
using ProtoBuf;

namespace SteamKit2.Discovery
{
[ProtoContract]
class BasicServerListProto
{
[ProtoMember(1)]
[DisallowNull, NotNull]
public string? Address { get; set; }
yaakov-h marked this conversation as resolved.
Show resolved Hide resolved

[ProtoMember(2)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public Task<IEnumerable<ServerRecord>> FetchServerListAsync()
{
using (FileStream fileStream = File.OpenRead(filename))
{
return Serializer.DeserializeItems<BasicServerListProto>(fileStream, PrefixStyle.Base128, 1)
.Select(item =>
{
return ServerRecord.CreateServer(item.Address, item.Port, item.Protocols);
})
return Serializer.DeserializeItems<BasicServerListProto>(fileStream, PrefixStyle.Base128, 1 )
.Select( item => ServerRecord.CreateServer(
item.Address ?? throw new InvalidDataException( "Serialized server had null address" ),
yaakov-h marked this conversation as resolved.
Show resolved Hide resolved
item.Port,
item.Protocols ) )
.ToList();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public Task<IEnumerable<ServerRecord>> FetchServerListAsync()
using (var fileStream = isolatedStorage.OpenFile(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
return Serializer.DeserializeItems<BasicServerListProto>(fileStream, PrefixStyle.Base128, 1)
.Select(item =>
{
return ServerRecord.CreateServer(item.Address, item.Port, item.Protocols);
})
.Select(item => ServerRecord.CreateServer(
item.Address ?? throw new InvalidDataException( "Serialized server had null address" ),
item.Port,
item.Protocols ))
.ToList();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -101,30 +99,26 @@ public string RpcName
/// <summary>
/// Gets the full name of the service method.
/// </summary>
[DisallowNull, NotNull]
public string? MethodName { get; private set; }
public string MethodName { get; private set; }

/// <summary>
/// Gets the protobuf notification body.
/// </summary>
[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 } );
yaakov-h marked this conversation as resolved.
Show resolved Hide resolved
}

void Setup<T>( IPacketMsg packetMsg )
static (string methodName, object body) Setup<T>( IPacketMsg packetMsg )
where T : IExtensible, new()
{
var clientMsg = new ClientMsgProtobuf<T>( packetMsg );

MethodName = clientMsg.Header.Proto.target_job_name;
Body = clientMsg.Body;
return (clientMsg.Header.Proto.target_job_name, clientMsg.Body);
}
}
}
Expand Down