0

Basically what I'm doing is I'm creating a temporary buffer that writes data to a byte[] and then returns the size of the buffer; I'm then using this to attempt to de-segmentate my code over a network. (Trying to get my C# Client to work properly with Netty's FrameDecoder class)

Basically, I'm storing all of my Actions in a List to be called over the network once I find the buffer-size(Dynamic)

      public void SendBuffer(DataOutputStream ClientOuput)
      {
                ClientOuput.WriteInt(GetBufferSize());
                foreach (Action a in executionList)
                {
                         // What Do?
                }
      }

My problem is I need to Invoke the method inside of the DataOutputStream that's passed through the SendBuffer parameters, so something like

ClientOutput.a.invoke();

What's the best way to do this?

Hobbyist
  • 15,888
  • 9
  • 46
  • 98

1 Answers1

0

It's not that clear what you are asking but I try to answer anyway. So you have bunch of actions (defined somewhere) that you want to be executed by instance of DataOutputStream? Then you could do something like:

public async void SendBuffer(DataOutputStream clientOuput)
{
    var executionList = new List<Action>()
        {
            () => { Debug.WriteLine("whatyousay"); Thread.Sleep(1500); },
            () => { Debug.WriteLine("allyourbase"); Thread.Sleep(1500); },
        };

    clientOuput.WriteInt(1);
    foreach (Action action in executionList)
        await clientOuput.Execute(action);

    Debug.WriteLine("arebelongtous");
}

Here DataOutputStream is just

public class DataOutputStream
{
    // await and actions will be executed in given order, non-blocking
    public Task Execute(Action action)
    {
        return Task.Run(action);
    }

    // fire & forget, non-blocking
    public void BeginInvoke(Action action)
    {
        action.BeginInvoke(callback => {}, null);
    }

    // blocking
    public void Invoke(Action action)
    {
        action.Invoke();
    }

    public void WriteInt(int integer)
    {
        Debug.WriteLine("int:{0}", integer);
    }
}
Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
  • 1
    Maybe it's just me but comment would be more constructive than -1 out of nowhere. – Mikko Viitala Oct 23 '14 at 09:34
  • 1
    Just one thing (i'm not the downvoter, just passing by), I think the example itself won't compile because the method is not marked as `async` and it has an `await` inside its body. – Conrad Clark Oct 23 '14 at 09:52
  • Thx, I just wrapped the stuff around OP method. Changed. – Mikko Viitala Oct 23 '14 at 09:53
  • I downvoted because it is not at all clear that this is what the OP wants/needs. So far this is a thin wrapper around the thread-pool. Under what circumstances could this help the OP? I can't think of any. – usr Oct 23 '14 at 10:08
  • I have no idea if it helps but I hope we hear soon enough. This answers to the part where he wants to do "something like" `ClientOutput.a.invoke();` where `a` is `action`. – Mikko Viitala Oct 23 '14 at 10:14