~ruther/NosTale-PacketLogger

ref: 676e5bb566fd796d9d023b1ccbdc751d8e0bb5c2 NosTale-PacketLogger/src/PacketLogger/ViewModels/Sender/PacketSendSubViewModel.cs -rw-r--r-- 3.8 KiB
676e5bb5 — Rutherther feat: add possibility to save and load settings to json 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
//  PacketSendSubViewModel.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.Reactive;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using NosSmooth.PacketSerializer.Abstractions.Attributes;
using PacketLogger.Models.Packets;
using ReactiveUI;

namespace PacketLogger.ViewModels.Sender;

/// <inheritdoc />
public class PacketSendSubViewModel : ViewModelBase, IDisposable
{
    private readonly IPacketProvider _sender;
    private string[]? _cachedPacketData;
    private IDisposable? _sendingTask;
    private SemaphoreSlim _semaphore;

    /// <summary>
    /// Initializes a new instance of the <see cref="PacketSendSubViewModel"/> class.
    /// </summary>
    /// <param name="source">The packet source to use.</param>
    /// <param name="sender">The sender to send packets to.</param>
    public PacketSendSubViewModel(PacketSource source, IPacketProvider sender)
    {
        _semaphore = new SemaphoreSlim(1, 1);
        Source = source;
        _sender = sender;

        SendPackets = ReactiveCommand.CreateFromTask(SendPacketData);
        ToggleRepetetiveSend = ReactiveCommand.Create(
            () =>
            {
                if (IsSending)
                {
                    _semaphore.Wait();
                    _sendingTask?.Dispose();
                    _sendingTask = null;
                    _semaphore.Release();
                }
                else
                {
                    _semaphore.Wait();
                    _cachedPacketData = null;
                    _sendingTask?.Dispose();
                    _sendingTask = Observable.Timer(DateTimeOffset.Now, TimeSpan.FromMilliseconds(RepetitionDelay))
                        .Subscribe
                        (
                            _ =>
                            {
                                SendPacketData().GetAwaiter().GetResult();
                            }
                        );
                    _semaphore.Release();
                }

                IsSending = !IsSending;
            });
    }

    /// <summary>
    /// Gets the source to send the packets as.
    /// </summary>
    public PacketSource Source { get; }

    /// <summary>
    /// Gets or sets whether current repetetively sending.
    /// </summary>
    public bool IsSending { get; private set; }

    /// <summary>
    /// Gets or sets the packets to send separated by a line.
    /// </summary>
    public string PacketsData { get; set; } = string.Empty;

    /// <summary>
    /// The delay of repetition in milliseconds.
    /// </summary>
    public int RepetitionDelay { get; set; } = 100;

    /// <summary>
    /// Gets or sets the command used to send the packets.
    /// </summary>
    public ReactiveCommand<Unit, Unit> SendPackets { get; }

    /// <summary>
    /// Gets the command used for toggling repetetive send.
    /// </summary>
    public ReactiveCommand<Unit, Unit> ToggleRepetetiveSend { get; }

    /// <inheritdoc />
    public void Dispose()
    {
        _sendingTask?.Dispose();
        SendPackets.Dispose();
        ToggleRepetetiveSend.Dispose();
    }

    private async Task SendPacketData()
    {
        if (!IsSending || _cachedPacketData is null)
        {
            _cachedPacketData = PacketsData.Split('\n', StringSplitOptions.RemoveEmptyEntries);
        }

        foreach (var line in _cachedPacketData)
        {
            await Send(line);
        }
    }

    private Task Send(string packetString)
    {
        if (Source == PacketSource.Server)
        {
            return _sender.ReceivePacket(packetString);
        }
        else
        {
            return _sender.SendPacket(packetString);
        }
    }
}
Do not follow this link