@@ -32,7 +32,7 @@ public static byte[] ReceiveBytes([NotNull] this IThreadSafeInSocket socket)
32
32
msg . Close ( ) ;
33
33
return data ;
34
34
}
35
-
35
+
36
36
#endregion
37
37
38
38
#region Immediate
@@ -61,7 +61,8 @@ public static bool TryReceiveBytes([NotNull] this IThreadSafeInSocket socket, ou
61
61
/// <param name="timeout">The maximum period of time to wait for a message to become available.</param>
62
62
/// <param name="bytes">The content of the received message, or <c>null</c> if no message was available.</param>
63
63
/// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns>
64
- public static bool TryReceiveBytes ( [ NotNull ] this IThreadSafeInSocket socket , TimeSpan timeout , out byte [ ] bytes )
64
+ public static bool TryReceiveBytes ( [ NotNull ] this IThreadSafeInSocket socket , TimeSpan timeout ,
65
+ out byte [ ] bytes )
65
66
{
66
67
var msg = new Msg ( ) ;
67
68
msg . InitEmpty ( ) ;
@@ -80,9 +81,9 @@ public static bool TryReceiveBytes([NotNull] this IThreadSafeInSocket socket, Ti
80
81
}
81
82
82
83
#endregion
83
-
84
+
84
85
#region Async
85
-
86
+
86
87
/// <summary>
87
88
/// Receive a bytes from <paramref name="socket"/> asynchronously.
88
89
/// </summary>
@@ -92,16 +93,16 @@ public static ValueTask<byte[]> ReceiveBytesAsync([NotNull] this IThreadSafeInSo
92
93
{
93
94
if ( TryReceiveBytes ( socket , out var bytes ) )
94
95
return new ValueTask < byte [ ] > ( bytes ) ;
95
-
96
+
96
97
// TODO: this is a hack, eventually we need kind of IO ThreadPool for thread-safe socket to wait on asynchronously
97
98
// and probably implement IValueTaskSource
98
99
return new ValueTask < byte [ ] > ( Task . Factory . StartNew ( socket . ReceiveBytes , TaskCreationOptions . LongRunning ) ) ;
99
100
}
100
-
101
+
101
102
#endregion
102
-
103
+
103
104
#endregion
104
-
105
+
105
106
#region Receiving string
106
107
107
108
#region Blocking
@@ -128,15 +129,19 @@ public static string ReceiveString([NotNull] this IThreadSafeInSocket socket, [N
128
129
msg . InitEmpty ( ) ;
129
130
130
131
socket . Receive ( ref msg ) ;
131
-
132
- var str = msg . Size > 0
133
- ? msg . GetString ( encoding )
134
- : string . Empty ;
135
132
136
- msg . Close ( ) ;
137
- return str ;
133
+ try
134
+ {
135
+ return msg . Size > 0
136
+ ? msg . GetString ( encoding )
137
+ : string . Empty ;
138
+ }
139
+ finally
140
+ {
141
+ msg . Close ( ) ;
142
+ }
138
143
}
139
-
144
+
140
145
#endregion
141
146
142
147
#region Immediate
@@ -161,7 +166,8 @@ public static bool TryReceiveString([NotNull] this IThreadSafeInSocket socket, o
161
166
/// <param name="encoding">The encoding used to convert the data to a string.</param>
162
167
/// <param name="str">The content of the received message, or <c>null</c> if no message was available.</param>
163
168
/// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns>
164
- public static bool TryReceiveString ( [ NotNull ] this IThreadSafeInSocket socket , [ NotNull ] Encoding encoding , out string str )
169
+ public static bool TryReceiveString ( [ NotNull ] this IThreadSafeInSocket socket , [ NotNull ] Encoding encoding ,
170
+ out string str )
165
171
{
166
172
return socket . TryReceiveString ( TimeSpan . Zero , encoding , out str ) ;
167
173
}
@@ -192,19 +198,25 @@ public static bool TryReceiveString([NotNull] this IThreadSafeInSocket socket, T
192
198
/// <param name="encoding">The encoding used to convert the data to a string.</param>
193
199
/// <param name="str">The content of the received message, or <c>null</c> if no message was available.</param>
194
200
/// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns>
195
- public static bool TryReceiveString ( [ NotNull ] this IThreadSafeInSocket socket , TimeSpan timeout , [ NotNull ] Encoding encoding , out string str )
201
+ public static bool TryReceiveString ( [ NotNull ] this IThreadSafeInSocket socket , TimeSpan timeout ,
202
+ [ NotNull ] Encoding encoding , out string str )
196
203
{
197
204
var msg = new Msg ( ) ;
198
205
msg . InitEmpty ( ) ;
199
206
200
207
if ( socket . TryReceive ( ref msg , timeout ) )
201
208
{
202
- str = msg . Size > 0
203
- ? msg . GetString ( encoding )
204
- : string . Empty ;
205
-
206
- msg . Close ( ) ;
207
- return true ;
209
+ try
210
+ {
211
+ str = msg . Size > 0
212
+ ? msg . GetString ( encoding )
213
+ : string . Empty ;
214
+ return true ;
215
+ }
216
+ finally
217
+ {
218
+ msg . Close ( ) ;
219
+ }
208
220
}
209
221
210
222
str = null ;
@@ -213,9 +225,9 @@ public static bool TryReceiveString([NotNull] this IThreadSafeInSocket socket, T
213
225
}
214
226
215
227
#endregion
216
-
228
+
217
229
#region Async
218
-
230
+
219
231
/// <summary>
220
232
/// Receive a string from <paramref name="socket"/> asynchronously.
221
233
/// </summary>
@@ -225,7 +237,7 @@ public static ValueTask<string> ReceiveStringAsync([NotNull] this IThreadSafeInS
225
237
{
226
238
if ( TryReceiveString ( socket , out var msg ) )
227
239
return new ValueTask < string > ( msg ) ;
228
-
240
+
229
241
// TODO: this is a hack, eventually we need kind of IO ThreadPool for thread-safe socket to wait on asynchronously
230
242
// and probably implement IValueTaskSource
231
243
return new ValueTask < string > ( Task . Factory . StartNew ( socket . ReceiveString , TaskCreationOptions . LongRunning ) ) ;
0 commit comments