Skip to content

Commit 2a8f519

Browse files
committed
problem: get string might throw
1 parent 17a910c commit 2a8f519

File tree

3 files changed

+81
-50
lines changed

3 files changed

+81
-50
lines changed

src/NetMQ/ReceiveThreadSafeSocketExtensions.cs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static byte[] ReceiveBytes([NotNull] this IThreadSafeInSocket socket)
3232
msg.Close();
3333
return data;
3434
}
35-
35+
3636
#endregion
3737

3838
#region Immediate
@@ -61,7 +61,8 @@ public static bool TryReceiveBytes([NotNull] this IThreadSafeInSocket socket, ou
6161
/// <param name="timeout">The maximum period of time to wait for a message to become available.</param>
6262
/// <param name="bytes">The content of the received message, or <c>null</c> if no message was available.</param>
6363
/// <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)
6566
{
6667
var msg = new Msg();
6768
msg.InitEmpty();
@@ -80,9 +81,9 @@ public static bool TryReceiveBytes([NotNull] this IThreadSafeInSocket socket, Ti
8081
}
8182

8283
#endregion
83-
84+
8485
#region Async
85-
86+
8687
/// <summary>
8788
/// Receive a bytes from <paramref name="socket"/> asynchronously.
8889
/// </summary>
@@ -92,16 +93,16 @@ public static ValueTask<byte[]> ReceiveBytesAsync([NotNull] this IThreadSafeInSo
9293
{
9394
if (TryReceiveBytes(socket, out var bytes))
9495
return new ValueTask<byte[]>(bytes);
95-
96+
9697
// TODO: this is a hack, eventually we need kind of IO ThreadPool for thread-safe socket to wait on asynchronously
9798
// and probably implement IValueTaskSource
9899
return new ValueTask<byte[]>(Task.Factory.StartNew(socket.ReceiveBytes, TaskCreationOptions.LongRunning));
99100
}
100-
101+
101102
#endregion
102-
103+
103104
#endregion
104-
105+
105106
#region Receiving string
106107

107108
#region Blocking
@@ -128,15 +129,19 @@ public static string ReceiveString([NotNull] this IThreadSafeInSocket socket, [N
128129
msg.InitEmpty();
129130

130131
socket.Receive(ref msg);
131-
132-
var str = msg.Size > 0
133-
? msg.GetString(encoding)
134-
: string.Empty;
135132

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+
}
138143
}
139-
144+
140145
#endregion
141146

142147
#region Immediate
@@ -161,7 +166,8 @@ public static bool TryReceiveString([NotNull] this IThreadSafeInSocket socket, o
161166
/// <param name="encoding">The encoding used to convert the data to a string.</param>
162167
/// <param name="str">The content of the received message, or <c>null</c> if no message was available.</param>
163168
/// <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)
165171
{
166172
return socket.TryReceiveString(TimeSpan.Zero, encoding, out str);
167173
}
@@ -192,19 +198,25 @@ public static bool TryReceiveString([NotNull] this IThreadSafeInSocket socket, T
192198
/// <param name="encoding">The encoding used to convert the data to a string.</param>
193199
/// <param name="str">The content of the received message, or <c>null</c> if no message was available.</param>
194200
/// <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)
196203
{
197204
var msg = new Msg();
198205
msg.InitEmpty();
199206

200207
if (socket.TryReceive(ref msg, timeout))
201208
{
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+
}
208220
}
209221

210222
str = null;
@@ -213,9 +225,9 @@ public static bool TryReceiveString([NotNull] this IThreadSafeInSocket socket, T
213225
}
214226

215227
#endregion
216-
228+
217229
#region Async
218-
230+
219231
/// <summary>
220232
/// Receive a string from <paramref name="socket"/> asynchronously.
221233
/// </summary>
@@ -225,7 +237,7 @@ public static ValueTask<string> ReceiveStringAsync([NotNull] this IThreadSafeInS
225237
{
226238
if (TryReceiveString(socket, out var msg))
227239
return new ValueTask<string>(msg);
228-
240+
229241
// TODO: this is a hack, eventually we need kind of IO ThreadPool for thread-safe socket to wait on asynchronously
230242
// and probably implement IValueTaskSource
231243
return new ValueTask<string>(Task.Factory.StartNew(socket.ReceiveString, TaskCreationOptions.LongRunning));

src/NetMQ/ReceivingSocketExtensions.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,16 @@ public static string ReceiveFrameString([NotNull] this IReceivingSocket socket,
315315

316316
more = msg.HasMore;
317317

318-
var str = msg.Size > 0
319-
? msg.GetString(encoding)
320-
: string.Empty;
321-
322-
msg.Close();
323-
return str;
318+
try
319+
{
320+
return msg.Size > 0
321+
? msg.GetString(encoding)
322+
: string.Empty;
323+
}
324+
finally
325+
{
326+
msg.Close();
327+
}
324328
}
325329

326330
#endregion
@@ -443,12 +447,17 @@ public static bool TryReceiveFrameString([NotNull] this IReceivingSocket socket,
443447
{
444448
more = msg.HasMore;
445449

446-
frameString = msg.Size > 0
447-
? msg.GetString(encoding)
448-
: string.Empty;
449-
450-
msg.Close();
451-
return true;
450+
try
451+
{
452+
frameString = msg.Size > 0
453+
? msg.GetString(encoding)
454+
: string.Empty;
455+
return true;
456+
}
457+
finally
458+
{
459+
msg.Close();
460+
}
452461
}
453462

454463
frameString = null;

src/NetMQ/RoutingIdSocketExtensions.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,17 @@ public static (uint, string) ReceiveString([NotNull] this IRoutingIdSocket socke
386386

387387
var routingId = msg.RoutingId;
388388

389-
var str = msg.Size > 0
390-
? msg.GetString(encoding)
391-
: string.Empty;
392-
393-
msg.Close();
394-
return (routingId, str);
389+
try
390+
{
391+
var str = msg.Size > 0
392+
? msg.GetString(encoding)
393+
: string.Empty;
394+
return (routingId, str);
395+
}
396+
finally
397+
{
398+
msg.Close();
399+
}
395400
}
396401

397402
#endregion
@@ -462,12 +467,17 @@ public static bool TryReceiveString([NotNull] this IRoutingIdSocket socket, Time
462467
{
463468
routingId = msg.RoutingId;
464469

465-
str = msg.Size > 0
466-
? msg.GetString(encoding)
467-
: string.Empty;
468-
469-
msg.Close();
470-
return true;
470+
try
471+
{
472+
str = msg.Size > 0
473+
? msg.GetString(encoding)
474+
: string.Empty;
475+
return true;
476+
}
477+
finally
478+
{
479+
msg.Close();
480+
}
471481
}
472482

473483
str = null;

0 commit comments

Comments
 (0)