-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.cs
More file actions
85 lines (70 loc) · 1.75 KB
/
Request.cs
File metadata and controls
85 lines (70 loc) · 1.75 KB
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
using System;
using System.Collections.Generic;
using LibTx.Internal;
using Sandbox;
namespace LibTx;
public class Request
{
public readonly string Command;
public float CreationTime { get; }
public float Timeout { get; private init; }
public int Identifier { get; }
private Action<Response> _onComplete;
private Request( string command )
{
Command = command;
CreationTime = RealTime.Now;
Identifier = Game.Random.Int( 0, 999999 );
Client.RegisterRequest( this );
}
private void Send( IEnumerable<object> args )
{
ConsoleSystem.Run( Core.ConCmdNameData,
Command, Identifier, Core.ToDataString( args ) );
}
private void Send()
{
ConsoleSystem.Run( Core.ConCmdName, Command, Identifier );
}
internal void InvokeComplete( Response response )
{
try
{
_onComplete?.Invoke( response );
}
catch ( Exception e )
{
Log.Warning( $"Caught exception invoking complete action {e}" );
}
}
public struct RequestCreator
{
private readonly string _command;
private float _timeout = 3.0f;
private Action<Response> _onComplete;
public RequestCreator( string command ) => _command = command;
public RequestCreator WithTimeout( float value )
{
_timeout = value;
return this;
}
public RequestCreator WhenComplete( Action<Response> action )
{
_onComplete = action;
return this;
}
public Request Send( params object[] args )
{
var instance = new Request( _command ) { Timeout = _timeout, _onComplete = _onComplete };
instance.Send( args );
return instance;
}
public Request Send()
{
var instance = new Request( _command ) { Timeout = _timeout, _onComplete = _onComplete };
instance.Send();
return instance;
}
}
public static RequestCreator Make( string command ) => new(command);
}