1
1
using System ;
2
- using System . Collections . Generic ;
3
2
using System . Text . Json ;
4
3
using System . Threading . Tasks ;
5
4
using TelnetNegotiationCore . Functional ;
@@ -19,112 +18,115 @@ namespace TelnetNegotiationCore.Handlers;
19
18
/// </param>
20
19
public class MSDPServerHandler ( MSDPServerModel model )
21
20
{
22
- public MSDPServerModel Data { get ; private init ; } = model ;
21
+ /// <summary>
22
+ /// Current Data.
23
+ /// </summary>
24
+ public MSDPServerModel Data { get ; private init ; } = model ;
23
25
24
- public async ValueTask HandleAsync ( TelnetInterpreter telnet , string clientJson )
25
- {
26
- var json = JsonSerializer . Deserialize < dynamic > ( clientJson ) ;
26
+ private protected record MSDPModel
27
+ {
28
+ public string ? LIST { get ; init ; }
29
+ public string ? REPORT { get ; init ; }
30
+ public string ? RESET { get ; init ; }
31
+ public string ? SEND { get ; init ; }
32
+ public string ? UNREPORT { get ; init ; }
33
+ }
34
+
35
+ public ValueTask HandleAsync ( TelnetInterpreter telnet , string clientJson )
36
+ {
37
+ var json = JsonSerializer . Deserialize < MSDPModel > ( clientJson ) ;
38
+ return json switch
39
+ {
40
+ { LIST : not null } =>
41
+ HandleListRequestAsync ( telnet , json . LIST ) ,
42
+ { REPORT : not null } =>
43
+ HandleReportRequestAsync ( telnet , json . REPORT ) ,
44
+ { RESET : not null } =>
45
+ HandleResetRequestAsync ( json . RESET ) ,
46
+ { SEND : not null } =>
47
+ HandleSendRequestAsync ( telnet , json . SEND ) ,
48
+ { UNREPORT : not null } =>
49
+ HandleUnReportRequestAsync ( json . UNREPORT ) ,
50
+ _ => ValueTask . CompletedTask
51
+ } ;
52
+ }
27
53
28
- if ( json . LIST != null )
29
- {
30
- await HandleListRequestAsync ( telnet , ( string ) json . LIST ) ;
31
- }
32
- else if ( json . REPORT != null )
33
- {
34
- await HandleReportRequestAsync ( telnet , ( string ) json . REPORT ) ;
35
- }
36
- else if ( json . RESET != null )
37
- {
38
- await HandleResetRequestAsync ( ( string ) json . RESET ) ;
39
- }
40
- else if ( json . SEND != null )
41
- {
42
- await HandleSendRequestAsync ( telnet , ( string ) json . SEND ) ;
43
- }
44
- else if ( json . UNREPORT != null )
45
- {
46
- await HandleUnReportRequestAsync ( ( string ) json . UNREPORT ) ;
47
- }
54
+ /// <summary>
55
+ /// Handles a request to LIST a single item. For safety, also supports a list of lists.
56
+ /// Should at least support:
57
+ /// "COMMANDS" Request an array of commands supported by the server.
58
+ /// "LISTS" Request an array of lists supported by the server.
59
+ /// "CONFIGURABLE_VARIABLES" Request an array of variables the client can configure.
60
+ /// "REPORTABLE_VARIABLES" Request an array of variables the server will report.
61
+ /// "REPORTED_VARIABLES" Request an array of variables currently being reported.
62
+ /// "SENDABLE_VARIABLES" Request an array of variables the server will send.
63
+ /// </summary>
64
+ /// <param name="telnet">Telnet Interpreter to callback with.</param>
65
+ /// <param name="item">The item to report</param>
66
+ private async ValueTask HandleListRequestAsync ( TelnetInterpreter telnet , string item ) =>
67
+ await ExecuteOnAsync ( item , async ( val ) =>
68
+ await ( Data . Lists . TryGetValue ( val , out var value )
69
+ ? telnet . CallbackNegotiationAsync (
70
+ MSDPLibrary . Report ( JsonSerializer . Serialize ( value ( ) ) ,
71
+ telnet . CurrentEncoding ) )
72
+ : ValueTask . CompletedTask ) ) ;
48
73
49
- await Task . CompletedTask ;
50
- }
74
+ private async ValueTask HandleReportRequestAsync ( TelnetInterpreter telnet , string item ) =>
75
+ await ExecuteOnAsync ( item , async ( val ) =>
76
+ {
77
+ await HandleSendRequestAsync ( telnet , val ) ;
78
+ Data . Report ( val , async ( newVal ) => await HandleSendRequestAsync ( telnet , newVal ) ) ;
79
+ } ) ;
51
80
52
- /// <summary>
53
- /// Handles a request to LIST a single item. For safety, also supports a list of lists.
54
- /// Should at least support:
55
- /// "COMMANDS" Request an array of commands supported by the server.
56
- /// "LISTS" Request an array of lists supported by the server.
57
- /// "CONFIGURABLE_VARIABLES" Request an array of variables the client can configure.
58
- /// "REPORTABLE_VARIABLES" Request an array of variables the server will report.
59
- /// "REPORTED_VARIABLES" Request an array of variables currently being reported.
60
- /// "SENDABLE_VARIABLES" Request an array of variables the server will send.
61
- /// </summary>
62
- /// <param name="telnet">Telnet Interpreter to callback with.</param>
63
- /// <param name="item">The item to report</param>
64
- private async ValueTask HandleListRequestAsync ( TelnetInterpreter telnet , string item ) =>
65
- await ExecuteOnAsync ( item , async ( val ) =>
66
- await ( Data . Lists . TryGetValue ( val , out var value )
67
- ? telnet . CallbackNegotiationAsync (
68
- MSDPLibrary . Report ( JsonSerializer . Serialize ( value ( ) ) ,
69
- telnet . CurrentEncoding ) )
70
- : ValueTask . CompletedTask ) ) ;
81
+ /// <summary>
82
+ /// The RESET command works like the LIST command, and can be used to reset groups of variables to their initial state.
83
+ /// Most commonly RESET will be called with REPORTABLE_VARIABLES or REPORTED_VARIABLES as the argument,
84
+ /// though any LIST option can be used.
85
+ /// </summary>
86
+ /// <param name="item">Item to reset</param>
87
+ private async ValueTask HandleResetRequestAsync ( string item ) =>
88
+ await ExecuteOnAsync ( item , ( var ) =>
89
+ {
90
+ var found = Data . Reportable_Variables ( ) . TryGetValue ( var , out var list ) ;
91
+ return ValueTask . CompletedTask ;
92
+ } ) ;
71
93
72
- private async ValueTask HandleReportRequestAsync ( TelnetInterpreter telnet , string item ) =>
73
- await ExecuteOnAsync ( item , async ( val ) =>
74
- {
75
- await HandleSendRequestAsync ( telnet , val ) ;
76
- Data . Report ( val , async ( newVal ) => await HandleSendRequestAsync ( telnet , newVal ) ) ;
77
- } ) ;
94
+ /// <summary>
95
+ /// The SEND command can be used by either side, but should typically be used by the client.
96
+ /// After the client has received a list of variables, or otherwise knows which variables exist,
97
+ /// it can request the server to send those variables and their values with the SEND command.
98
+ /// The value of the SEND command should be a list of variables the client wants returned.
99
+ /// </summary>
100
+ /// <param name="telnet">Telnet interpreter to send back negotiation with</param>
101
+ /// <param name="item">The item to send</param>
102
+ private async ValueTask HandleSendRequestAsync ( TelnetInterpreter telnet , string item ) =>
103
+ await ExecuteOnAsync ( item , async ( var ) =>
104
+ {
105
+ var found = Data . Sendable_Variables ( ) . TryGetValue ( var , out var val ) ;
106
+ var jsonString = $ "{{{var}:{ ( found ? val : "null" ) } }}";
107
+ await telnet . CallbackNegotiationAsync ( MSDPLibrary . Report ( jsonString , telnet . CurrentEncoding ) ) ;
108
+ } ) ;
78
109
79
- /// <summary>
80
- /// The RESET command works like the LIST command, and can be used to reset groups of variables to their initial state.
81
- /// Most commonly RESET will be called with REPORTABLE_VARIABLES or REPORTED_VARIABLES as the argument,
82
- /// though any LIST option can be used.
83
- /// </summary>
84
- /// <param name="item">Item to reset</param>
85
- private async ValueTask HandleResetRequestAsync ( string item ) =>
86
- await ExecuteOnAsync ( item , ( var ) =>
87
- {
88
- var found = Data . Reportable_Variables ( ) . TryGetValue ( var , out var list ) ;
89
- return ValueTask . CompletedTask ;
90
- } ) ;
110
+ /// <summary>
111
+ /// The UNREPORT command is used to remove the report status of variables after the use of the REPORT command.
112
+ /// </summary>
113
+ /// <param name="item">The item to stop reporting on</param>
114
+ private async ValueTask HandleUnReportRequestAsync ( string item ) =>
115
+ await ExecuteOnAsync ( item , ( var ) =>
116
+ {
117
+ Data . UnReport ( var ) ;
118
+ return ValueTask . CompletedTask ;
119
+ } ) ;
91
120
92
- /// <summary>
93
- /// The SEND command can be used by either side, but should typically be used by the client.
94
- /// After the client has received a list of variables, or otherwise knows which variables exist,
95
- /// it can request the server to send those variables and their values with the SEND command.
96
- /// The value of the SEND command should be a list of variables the client wants returned.
97
- /// </summary>
98
- /// <param name="telnet">Telnet interpreter to send back negotiation with</param>
99
- /// <param name="item">The item to send</param>
100
- private async ValueTask HandleSendRequestAsync ( TelnetInterpreter telnet , string item ) =>
101
- await ExecuteOnAsync ( item , async ( var ) =>
102
- {
103
- var found = Data . Sendable_Variables ( ) . TryGetValue ( var , out var val ) ;
104
- var jsonString = $ "{{{var}:{ ( found ? val : "null" ) } }}";
105
- await telnet . CallbackNegotiationAsync ( MSDPLibrary . Report ( jsonString , telnet . CurrentEncoding ) ) ;
106
- } ) ;
121
+ private async ValueTask ExecuteOnAsync ( string item , Func < string , ValueTask > function )
122
+ {
123
+ var items = item . StartsWith ( '[' )
124
+ ? JsonSerializer . Deserialize < string [ ] > ( item )
125
+ : [ item ] ;
107
126
108
- /// <summary>
109
- /// The UNREPORT command is used to remove the report status of variables after the use of the REPORT command.
110
- /// </summary>
111
- /// <param name="item">The item to stop reporting on</param>
112
- private async ValueTask HandleUnReportRequestAsync ( string item ) =>
113
- await ExecuteOnAsync ( item , ( var ) =>
114
- {
115
- Data . UnReport ( var ) ;
116
- return ValueTask . CompletedTask ;
117
- } ) ;
118
-
119
- private async ValueTask ExecuteOnAsync ( string item , Func < string , ValueTask > function )
120
- {
121
- var items = item . StartsWith ( '[' )
122
- ? JsonSerializer . Deserialize < string [ ] > ( item )
123
- : [ item ] ;
124
-
125
- foreach ( var val in items )
126
- {
127
- await function ( val ) ;
128
- }
129
- }
130
- }
127
+ foreach ( var val in items ?? [ ] )
128
+ {
129
+ await function ( val ) ;
130
+ }
131
+ }
132
+ }
0 commit comments