Skip to content

Commit

Permalink
Merge pull request #1036 from xPaw/servicemethod
Browse files Browse the repository at this point in the history
Use non-legacy service methods (and some refactoring of messages/packets handling)
  • Loading branch information
yaakov-h authored Nov 14, 2021
2 parents e58b8d4 + d07a59e commit 9811319
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 174 deletions.
165 changes: 74 additions & 91 deletions SteamKit2/SteamKit2/Base/ClientMsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ internal ClientMsgProtobuf( EMsg eMsg, int payloadReserve = 64 )
public ClientMsgProtobuf( IPacketMsg msg )
: this( msg.GetMsgTypeWithNullCheck( nameof(msg) ) )
{
DebugLog.Assert(msg.IsProto, "ClientMsgProtobuf", "ClientMsgProtobuf used for non-proto message!");
if ( !( msg is PacketClientMsgProtobuf packetMsgProto ) )
{
throw new InvalidDataException( "ClientMsgProtobuf used for non-proto message!" );
}

Deserialize( msg.GetData() );
Header = packetMsgProto.Header;
}


Expand All @@ -123,21 +126,6 @@ public override byte[] Serialize()
{
throw new NotSupportedException( "ClientMsgProtobuf is for reading only. Use ClientMsgProtobuf<T> for serializing messages." );
}

/// <summary>
/// Initializes this client message by deserializing the specified data.
/// </summary>
/// <param name="data">The data representing a client message.</param>
public override void Deserialize( byte[] data )
{
if ( data == null )
{
throw new ArgumentNullException( nameof(data) );
}

using MemoryStream ms = new MemoryStream( data );
Header.Deserialize( ms );
}
}

/// <summary>
Expand All @@ -147,10 +135,16 @@ public override void Deserialize( byte[] data )
public sealed class ClientMsgProtobuf<TBody> : ClientMsgProtobuf
where TBody : IExtensible, new()
{
private TBody _body;

/// <summary>
/// Gets the body structure of this message.
/// </summary>
public TBody Body { get; private set; }
public TBody Body
{
get => _body;
set => _body = value ?? throw new ArgumentNullException( nameof( value ) );
}


/// <summary>
Expand All @@ -162,7 +156,7 @@ public sealed class ClientMsgProtobuf<TBody> : ClientMsgProtobuf
public ClientMsgProtobuf( EMsg eMsg, int payloadReserve = 64 )
: base( payloadReserve )
{
Body = new TBody();
_body = new TBody();

// set our emsg
Header.Msg = eMsg;
Expand Down Expand Up @@ -190,9 +184,27 @@ public ClientMsgProtobuf( EMsg eMsg, MsgBase<MsgHdrProtoBuf> msg, int payloadRes
public ClientMsgProtobuf( IPacketMsg msg )
: this( msg.GetMsgTypeWithNullCheck( nameof(msg) ) )
{
DebugLog.Assert( msg.IsProto, "ClientMsgProtobuf<>", $"ClientMsgProtobuf<{typeof(TBody).FullName}> used for non-proto message!" );
if ( !( msg is PacketClientMsgProtobuf packetMsg ) )
{
throw new InvalidDataException( $"ClientMsgProtobuf<{typeof(TBody).FullName}> used for non-proto message!" );
}

Header = packetMsg.Header;

Deserialize( msg.GetData() );
var data = packetMsg.GetData();
var offset = (int)packetMsg.BodyOffset;
using MemoryStream ms = new MemoryStream( data, offset, data.Length - offset );

Body = Serializer.Deserialize<TBody>( ms );

// the rest of the data is the payload
int payloadLen = ( int )( ms.Length - ms.Position );

if ( payloadLen > 0 )
{
ms.CopyTo( Payload, payloadLen );
Payload.Seek( 0, SeekOrigin.Begin );
}
}

/// <summary>
Expand All @@ -210,28 +222,6 @@ public override byte[] Serialize()

return ms.ToArray();
}
/// <summary>
/// Initializes this client message by deserializing the specified data.
/// </summary>
/// <param name="data">The data representing a client message.</param>
public override void Deserialize( byte[] data )
{
if ( data == null )
{
throw new ArgumentNullException( nameof(data) );
}

using MemoryStream ms = new MemoryStream( data );
Header.Deserialize( ms );
Body = Serializer.Deserialize<TBody>( ms );

// the rest of the data is the payload
int payloadOffset = ( int )ms.Position;
int payloadLen = ( int )( ms.Length - ms.Position );

Payload.Write( data, payloadOffset, payloadLen );
Payload.Seek( 0, SeekOrigin.Begin );
}
}

/// <summary>
Expand Down Expand Up @@ -357,9 +347,27 @@ public ClientMsg( IPacketMsg msg )
throw new ArgumentNullException( nameof(msg) );
}

DebugLog.Assert( !msg.IsProto, "ClientMsg", $"ClientMsg<{typeof(TBody).FullName}> used for proto message!" );
if ( !( msg is PacketClientMsg packetMsg ) )
{
throw new InvalidDataException( $"ClientMsg<{typeof( TBody ).FullName}> used for proto message!" );
}

Header = packetMsg.Header;

var data = packetMsg.GetData();
var offset = (int)packetMsg.BodyOffset;
using MemoryStream ms = new MemoryStream( data, offset, data.Length - offset );

Body.Deserialize( ms );

Deserialize( msg.GetData() );
// the rest of the data is the payload
int payloadLen = ( int )( ms.Length - ms.Position );

if ( payloadLen > 0 )
{
ms.CopyTo( Payload, payloadLen );
Payload.Seek( 0, SeekOrigin.Begin );
}
}

/// <summary>
Expand All @@ -377,28 +385,6 @@ public override byte[] Serialize()

return ms.ToArray();
}
/// <summary>
/// Initializes this client message by deserializing the specified data.
/// </summary>
/// <param name="data">The data representing a client message.</param>
public override void Deserialize( byte[] data )
{
if ( data == null )
{
throw new ArgumentNullException( nameof(data) );
}

using MemoryStream ms = new MemoryStream( data );
Header.Deserialize( ms );
Body.Deserialize( ms );

// the rest of the data is the payload
int payloadOffset = ( int )ms.Position;
int payloadLen = ( int )( ms.Length - ms.Position );

Payload.Write( data, payloadOffset, payloadLen );
Payload.Seek( 0, SeekOrigin.Begin );
}
}

/// <summary>
Expand Down Expand Up @@ -515,7 +501,27 @@ public Msg( IPacketMsg msg )
throw new ArgumentNullException( nameof(msg) );
}

Deserialize( msg.GetData() );
if ( !( msg is PacketMsg packetMsg ) )
{
throw new InvalidDataException( $"ClientMsg<{typeof( TBody ).FullName}> used for proto message!" );
}

Header = packetMsg.Header;

var data = packetMsg.GetData();
var offset = (int)packetMsg.BodyOffset;
using MemoryStream ms = new MemoryStream( data, offset, data.Length - offset );

Body.Deserialize( ms );

// the rest of the data is the payload
int payloadLen = ( int )( ms.Length - ms.Position );

if ( payloadLen > 0 )
{
ms.CopyTo( Payload, payloadLen );
Payload.Seek( 0, SeekOrigin.Begin );
}
}


Expand All @@ -534,28 +540,5 @@ public override byte[] Serialize()

return ms.ToArray();
}
/// <summary>
/// Initializes this client message by deserializing the specified data.
/// </summary>
/// <param name="data">The data representing a client message.</param>
public override void Deserialize( byte[] data )
{
if ( data == null )
{
throw new ArgumentNullException( nameof(data) );
}

using MemoryStream ms = new MemoryStream( data );
Header.Deserialize( ms );
Body.Deserialize( ms );

// the rest of the data is the payload
int payloadOffset = ( int )ms.Position;
int payloadLen = ( int )( ms.Length - ms.Position );

Payload.Write( data, payloadOffset, payloadLen );
Payload.Seek( 0, SeekOrigin.Begin );
}

}
}
13 changes: 1 addition & 12 deletions SteamKit2/SteamKit2/Base/MsgBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ public interface IClientMsg
/// </summary>
/// <returns>Data representing a client message.</returns>
byte[] Serialize();
/// <summary>
/// Initializes this client message by deserializing the specified data.
/// </summary>
/// <param name="data">The data representing a client message.</param>
void Deserialize( byte[] data );
}

/// <summary>
Expand Down Expand Up @@ -492,7 +487,7 @@ public abstract class MsgBase<THeader> : MsgBase, IClientMsg
/// <summary>
/// Gets the header for this message type.
/// </summary>
public THeader Header { get; }
public THeader Header { get; internal set; }


/// <summary>
Expand All @@ -513,11 +508,5 @@ public MsgBase( int payloadReserve = 0 )
/// Data representing a client message.
/// </returns>
public abstract byte[] Serialize();
/// <summary>
/// Initializes this client message by deserializing the specified data.
/// </summary>
/// <param name="data">The data representing a client message.</param>
public abstract void Deserialize( byte[] data );

}
}
Loading

0 comments on commit 9811319

Please sign in to comment.