1
0
mirror of https://github.com/oliverbooth/TcpDotNet synced 2024-11-10 02:55:41 +00:00

refactor!: remove TSend generic parameter from SendAndReceiveAsync

This commit is contained in:
Oliver Booth 2024-02-12 17:30:56 +00:00
parent 7b74e1f3e5
commit e7dfe97c6d
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
3 changed files with 4 additions and 6 deletions

View File

@ -1,4 +1,4 @@
using System.Net; using System.Net;
using TcpDotNet; using TcpDotNet;
using TcpDotNet.ClientIntegrationTest; using TcpDotNet.ClientIntegrationTest;
using TcpDotNet.ClientIntegrationTest.PacketHandlers; using TcpDotNet.ClientIntegrationTest.PacketHandlers;
@ -17,7 +17,7 @@ Console.WriteLine($"Connected to {client.RemoteEndPoint}. My session is {client.
var ping = new PingPacket(); var ping = new PingPacket();
Console.WriteLine($"Sending ping packet with payload: {BitConverter.ToString(ping.Payload)}"); Console.WriteLine($"Sending ping packet with payload: {BitConverter.ToString(ping.Payload)}");
var pong = await client.SendAndReceiveAsync<PingPacket, PongPacket>(ping); var pong = await client.SendAndReceiveAsync<PongPacket>(ping);
Console.WriteLine($"Received pong packet with payload: {BitConverter.ToString(pong.Payload)}"); Console.WriteLine($"Received pong packet with payload: {BitConverter.ToString(pong.Payload)}");
Console.WriteLine(pong.Payload.SequenceEqual(ping.Payload) ? "Payload matches!" : "Payload does not match!"); Console.WriteLine(pong.Payload.SequenceEqual(ping.Payload) ? "Payload matches!" : "Payload does not match!");

View File

@ -221,15 +221,13 @@ public abstract class BaseClientNode : Node
/// </summary> /// </summary>
/// <param name="packetToSend">The packet to send.</param> /// <param name="packetToSend">The packet to send.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
/// <typeparam name="TSend">The type of the packet to send.</typeparam>
/// <typeparam name="TReceive">The type of the packet to return.</typeparam> /// <typeparam name="TReceive">The type of the packet to return.</typeparam>
/// <returns>The received packet.</returns> /// <returns>The received packet.</returns>
/// <remarks> /// <remarks>
/// This method will consume all incoming packets, raising their associated handlers if such packets are recognised. /// This method will consume all incoming packets, raising their associated handlers if such packets are recognised.
/// </remarks> /// </remarks>
public async Task<TReceive> SendAndReceiveAsync<TSend, TReceive>(TSend packetToSend, public async Task<TReceive> SendAndReceiveAsync<TReceive>(Packet packetToSend,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TSend : Packet
where TReceive : Packet where TReceive : Packet
{ {
var attribute = typeof(TReceive).GetCustomAttribute<PacketAttribute>(); var attribute = typeof(TReceive).GetCustomAttribute<PacketAttribute>();

View File

@ -96,7 +96,7 @@ public sealed class ProtocolClient : BaseClientNode
State = ClientState.Handshaking; State = ClientState.Handshaking;
var handshakeRequest = new HandshakeRequestPacket(ProtocolVersion); var handshakeRequest = new HandshakeRequestPacket(ProtocolVersion);
var handshakeResponse = var handshakeResponse =
await SendAndReceiveAsync<HandshakeRequestPacket, HandshakeResponsePacket>(handshakeRequest, cancellationToken); await SendAndReceiveAsync<HandshakeResponsePacket>(handshakeRequest, cancellationToken);
if (handshakeResponse.HandshakeResponse != HandshakeResponse.Success) if (handshakeResponse.HandshakeResponse != HandshakeResponse.Success)
{ {