Skip to content

Instantly share code, notes, and snippets.

@rdavisau
rdavisau / BetterUdpSocket
Created April 20, 2015 10:15
Wrapper for sockets-for-pcl UdpSocketReceiver that provides a blocking `Receive` call
public class BetterUdpSocket : IDisposable
{
private BlockingCollection<byte[]> _readBuffer;
private readonly UdpSocketReceiver _backingUdpSocketReceiver = new UdpSocketReceiver();
public BetterUdpSocket()
{
_backingUdpSocketReceiver.MessageReceived += OnMessageReceived;
}
@rdavisau
rdavisau / BetterTcpSocketClient
Last active August 29, 2015 14:19
ITcpSocketClient implementation with disconnected detection
public class BetterTcpSocketClient : ITcpSocketClient
{
private TcpSocketClient _backingTcpSocketClient = new TcpSocketClient();
private readonly Stream _readBuffer = new BlockingMemoryStream();
private CancellationTokenSource _canceller;
public EventHandler<TcpSocketConnectedEventArgs> Connected { get; set; }
public EventHandler<TcpSocketDisconnectedEventArgs> Disconnected { get; set; }
public EventHandler<ErrorEventArgs> Errored { get; set; }
public Stream ReadStream
@rdavisau
rdavisau / TaskTimeout.cs
Created April 17, 2015 00:30
Timeout extension method for Task<T>
// I don't remember where this is taken/adapted from.
public static class AsyncExtensions
{
public static async Task<T> TimeoutAfter<T>(this Task<T> task, TimeSpan timeout, CancellationTokenSource cancellationTokenSource = null)
{
if (task == await Task.WhenAny(task, Task.Delay(timeout)))
return await task;
else
{
if (cancellationTokenSource != null)
@rdavisau
rdavisau / InlineTableViewSource
Last active August 29, 2015 14:15
InlineTableViewSource
/*
Snippet Name: InlineTableViewSource
Platform: iOS
Function: A subclass of UITableViewSource that allows you to define UITableViewDataSource and UITableViewDelegate methods inline, rather than subclassing. Lets you take advantage of closures and use tableviews where you can't create subclasses, like in Xamarin Sketches (compile and reference).
Funcs/Actions are prefixed with "_", feel free to alter if this disagrees with your styling guidelines.
Usage:
var cellId = new NSString("cell");
@rdavisau
rdavisau / CocosExtensions.cs
Last active August 29, 2015 14:15
CocosSharp Extension Methods - Helpers for actions, layout, sizing, drawing
/*
Snippet Name: CocosSharp Extension Methods
Platform: All
Function: CocosSharp helpers for actions, layout, sizing, drawing
Includes:
- Awaitable CCNode.RunActionsWithTask - allows you to await the running of a set of actions (for example, in order to not proceed with subsequent code till an animation completes), without having to explicitly calculate the duration of the actions or use a callback.
- Easy relative positioning with CCNode.PlaceAt - allows you to position a CCNode relative to the boundaries of a parent CCNode. Fluent style, so can be used at initialisation time.
- Easy filled colours with CCDrawNode.FillWith and quick sprite initialisation with CCNode.WithSprite, also fluent.
- Quick insetting or outsetting with CCSize.MultiplyBy.