Last active
June 2, 2017 13:35
-
-
Save creyke/c0e40caac89d00c72a6435ad622dcef3 to your computer and use it in GitHub Desktop.
Orleans Udp Stream
This file contains hidden or 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
| namespace OrleansUdpStream | |
| { | |
| using System; | |
| using System.Net.Sockets; | |
| using System.Threading.Tasks; | |
| using Orleans; | |
| using Orleans.Providers; | |
| using Orleans.Streams; | |
| public class UdpBootstrapProvider : IBootstrapProvider | |
| { | |
| private UdpClient _listener; | |
| private IStreamProvider _streamProvider; | |
| public string Name { get; private set; } | |
| public Task Close() | |
| { | |
| _listener.Dispose(); | |
| _listener = null; | |
| return TaskDone.Done; | |
| } | |
| public Task Init(string name, IProviderRuntime providerRuntime, IProviderConfiguration config) | |
| { | |
| Name = name; | |
| GrainClient.Initialize(); | |
| _streamProvider = GrainClient.GetStreamProvider("Default"); | |
| _listener = new UdpClient(config.GetIntProperty("Port", 0)); | |
| Task.Run(Listen); | |
| return TaskDone.Done; | |
| } | |
| private async Task Listen() | |
| { | |
| while (_listener != null) | |
| { | |
| await HandleResult(await _listener.ReceiveAsync()); | |
| } | |
| } | |
| private async Task HandleResult(UdpReceiveResult result) | |
| { | |
| var deviceGuid = GetDeviceGuidFromBuffer(result.Buffer); | |
| var stream = _streamProvider.GetStream<byte[]>(deviceGuid, "MyNamespace"); | |
| await stream.OnNextAsync(result.Buffer); | |
| } | |
| private Guid GetDeviceGuidFromBuffer(byte[] buffer) | |
| { | |
| // TODO: Map bytes from buffer to guid. | |
| return Guid.Empty; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment