Last active
August 11, 2023 07:38
-
-
Save idiotandrobot/be47c8a6ee57e533d2ce6e1ee7673647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var result = List.Select((x, i) => new { Key = i, Value = x }).ToDictionary(x => x.Key, x => x.Value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IObservable<byte> (socket.Receiver) to IObservable<string> (Receiver) | |
Receiver = from header in socket.Receiver.Buffer(sizeof(int)) | |
let length = BitConverter.ToInt32(header.ToArray(), 0) | |
let body = socket.Receiver.Take(length) | |
select encoding.GetString(body.ToEnumerable().ToArray()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Syntax with exception handling | |
// IObservable<byte> (socket.Receiver) to IObservable<string> (Receiver) | |
Receiver = socket.Receiver.Buffer(sizeof(int)) | |
.Select(header => | |
{ | |
try | |
{ | |
var length = BitConverter.ToInt32(header.ToArray(), 0); | |
var body = socket.Receiver.Take(length); | |
return body; | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine(ex); | |
return Observable.Empty<byte>(); | |
} | |
}) | |
.Select(body => | |
{ | |
return encoding.GetString(body.ToEnumerable().ToArray()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment