Skip to content

Instantly share code, notes, and snippets.

@idiotandrobot
Last active August 11, 2023 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idiotandrobot/be47c8a6ee57e533d2ce6e1ee7673647 to your computer and use it in GitHub Desktop.
Save idiotandrobot/be47c8a6ee57e533d2ce6e1ee7673647 to your computer and use it in GitHub Desktop.
var result = List.Select((x, i) => new { Key = i, Value = x }).ToDictionary(x => x.Key, x => x.Value);
// 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());
// 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