These are chat archives for akkadotnet/akka.net

15th
Mar 2015
jcwrequests
@jcwrequests
Mar 15 2015 01:48
PR is has now been submitted to patch the DI implementation. akkadotnet/akka.net#733. I tried using the new Git For WIndows application for pull request. Seems to work nicely. @Aaronontheweb @rogeralsing @Horusiath @HCanber if I need to make any other changes or you need additional documentation just let me know.
Bartosz Sypytkowski
@Horusiath
Mar 15 2015 09:25

@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

Robert Mircea
@robertmircea
Mar 15 2015 13:51
@Horusiath I have this async socket receive code:
//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;
    }
Basically I need to have somehow access to Context in order to stop the actor, but inside the callback Context is not available
ConnectionInfo contains an ActionRef to the actor.
I've skipped the instantiation code
Roger Johansson
@rogeralsing
Mar 15 2015 13:58
There are a few options, 1) .net async api for a taskbased, (if socket does not have that, TPL provides helpers to convert old async events to task), and then use .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;
    }
});
This message was deleted
This message was deleted
Robert Mircea
@robertmircea
Mar 15 2015 14:04
is it ok to pass a reference to Context in ConnectionInfo?
Roger Johansson
@rogeralsing
Mar 15 2015 14:07
yes, the context itself is threadsafe if you have a valid ref to it, it uses message passing internally to ensure thread safety.. so calling stop on it is ok
Robert Mircea
@robertmircea
Mar 15 2015 14:08
I'll try that :)
Roger Johansson
@rogeralsing
Mar 15 2015 14:08
but as you noticed, you can not access Context in a callback as the callback is not run in the actor concurrency constraint. but you can pass it as state to the callback