//
// PacketSerializer.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Threading.Tasks;
using NosCore.Packets;
using NosCore.Packets.Interfaces;
using Remora.Results;
namespace NosSmooth.Core.Packets;
///
public class PacketSerializer : IPacketSerializer
{
private readonly Serializer _serializer;
private readonly Deserializer _deserializer;
///
/// Initializes a new instance of the class.
///
/// The NosCore serializer.
/// The NosCore deserializer.
public PacketSerializer(Serializer serializer, Deserializer deserializer)
{
_serializer = serializer;
_deserializer = deserializer;
}
///
public Result Serialize(IPacket packet)
{
try
{
return _serializer.Serialize(packet);
}
catch (Exception e)
{
return e;
}
}
///
public Result Deserialize(string packetString)
{
try
{
return Result.FromSuccess(_deserializer.Deserialize(packetString));
}
catch (Exception e)
{
return e;
}
}
}