Aaronontheweb on dev
Fix #4083 - Endpoint receive bu… (compare)
@robertmircea I think your question is too general. Depending on what you want to get done, different solutions applies.
I was trying to port Akka.IO from JVM to .NET - I remember, that I've got serious problems with integrating akka threading model with async sockets (first tried a way similar to cannonical Akka, but Java NIO cannot be reflected directly in .NET) and that @Aaronontheweb already encouraged them when he was creating Helios, but I don't remember specifics
//this code is part of the actor
var receiveSaea = new SocketAsyncEventArgs();
receiveSaea.Completed += ReceivedCompleted;
receiveSaea.AcceptSocket = socket;
socket.ReceiveAsync(receiveSaea);
[...]
//later, this callback is called when socket read is completed
private void ReceivedCompleted(object sender, SocketAsyncEventArgs e)
{
var connectionInfo = e.UserToken as ConnectionInfo;
if (connectionInfo == null)
throw new InvalidOperationException("Could not find a reference to ConnectionInfo");
if (e.SocketError != SocketError.Success)
{
log.Error("IO error while receiving data over socket: " + e.SocketError);
//TODO: I need to tell the calling actor to shutdown itself, but I cannot use Context.Stop(connectionInfo.Sender) here
return;
}
ConnectionInfo
contains an ActionRef to the actor.
.PipeTo(Self)
on that task.. 2) use a closure that binds a copy of Context in a local var.. 3) download the latest source and use our new TPL support
var context = Context;
var receiveSaea = new SocketAsyncEventArgs();
receiveSaea.Completed += ReceivedCompleted;
receiveSaea.AcceptSocket = socket;
socket.ReceiveAsync((sender,e) => {
var connectionInfo = e.UserToken as ConnectionInfo;
if (connectionInfo == null)
throw new InvalidOperationException("Could not find a reference to ConnectionInfo");
if (e.SocketError != SocketError.Success)
{
log.Error("IO error while receiving data over socket: " + e.SocketError);
Context.Stop(connectionInfo.Sender)
return;
}
});