-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIpc.cs
72 lines (60 loc) · 2.17 KB
/
Ipc.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Mpv.JsonIpc
{
public class Ipc : IIpc
{
private readonly IManager _manager;
public Ipc(IManager manager)
{
_manager = manager;
}
public async Task<Response<T>> GetProperty<T>(Property property, params string[] args)
{
var request = CreateCommand(new[] {"get_property", property.GetStringValue()}, args);
return await ExecuteCommand<T>(request);
}
public async Task<Response<T>> SetProperty<T>(Property property, params object[] args)
{
var request = CreateCommand(new[] {"set_property", property.GetStringValue()}, args);
return await ExecuteCommand<T>(request);
}
public async Task<Response<T>> SetPropertyString<T>(Property property, params string[] args)
{
var request = CreateCommand(new[] {"set_property_string", property.GetStringValue()}, args);
return await ExecuteCommand<T>(request);
}
public async Task<Response<T>> CycleProperty<T>(Property property)
{
var request = CreateCommand(new[] {"cycle", property.GetStringValue()});
return await ExecuteCommand<T>(request);
}
public Request CreateCommand(IEnumerable<object> command)
{
return CreateCommand(command, Array.Empty<object>());
}
public Request CreateCommand(IEnumerable<object> command, object arg)
{
return CreateCommand(command, new[] {arg});
}
public async Task<Response<T>> ExecuteCommand<T>(Request request)
{
return await _manager.Execute<T>(request);
}
public Request CreateCommand(IEnumerable<object> command, IEnumerable<object> args)
{
return new Request
{
Command = command.Concat(args).ToArray(),
RequestId = GenerateNewRequestId(),
};
}
private static int GenerateNewRequestId()
{
return 10;
// return _random.Next(0, 10000);
}
}
}