aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/Am/AppletAE/AppletSession.cs
blob: a3f44a45d0aab653935bed38685bcbdaaaf88e07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;

namespace Ryujinx.HLE.HOS.Services.Am.AppletAE
{
    internal class AppletSession
    {
        private readonly IAppletFifo<byte[]> _inputData;
        private readonly IAppletFifo<byte[]> _outputData;

        public event EventHandler DataAvailable;

        public int Length
        {
            get { return _inputData.Count; }
        }

        public AppletSession()
            : this(new AppletFifo<byte[]>(),
                   new AppletFifo<byte[]>())
        { }

        public AppletSession(
            IAppletFifo<byte[]> inputData,
            IAppletFifo<byte[]> outputData)
        {
            _inputData = inputData;
            _outputData = outputData;

            _inputData.DataAvailable += OnDataAvailable;
        }

        private void OnDataAvailable(object sender, EventArgs e)
        {
            DataAvailable?.Invoke(this, null);
        }

        public void Push(byte[] item)
        {
            if (!this.TryPush(item))
            {
                // TODO(jduncanator): Throw a proper exception
                throw new InvalidOperationException();
            }
        }

        public bool TryPush(byte[] item)
        {
            return _outputData.TryAdd(item);
        }

        public byte[] Pop()
        {
            if (this.TryPop(out byte[] item))
            {
                return item;
            }

            throw new InvalidOperationException("Input data empty.");
        }

        public bool TryPop(out byte[] item)
        {
            return _inputData.TryTake(out item);
        }

        /// <summary>
        /// This returns an AppletSession that can be used at the
        /// other end of the pipe. Pushing data into this new session
        /// will put it in the first session's input buffer, and vice
        /// versa.
        /// </summary>
        public AppletSession GetConsumer()
        {
            return new AppletSession(this._outputData, this._inputData);
        }
    }
}