2023-03-17 12:14:50 +00:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Common.Memory;
|
2020-09-22 04:50:40 +00:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
2020-12-01 23:23:43 +00:00
|
|
|
using Ryujinx.HLE.HOS.Kernel;
|
2018-12-18 05:33:36 +00:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Ipc;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2019-03-15 03:37:54 +00:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2023-01-04 22:15:45 +00:00
|
|
|
using Ryujinx.Horizon.Common;
|
2018-02-04 23:08:20 +00:00
|
|
|
using System;
|
2023-03-17 12:14:50 +00:00
|
|
|
using System.Buffers;
|
2020-12-01 23:23:43 +00:00
|
|
|
using System.Buffers.Binary;
|
|
|
|
using System.Collections.Generic;
|
2018-02-04 23:08:20 +00:00
|
|
|
using System.IO;
|
2020-12-01 23:23:43 +00:00
|
|
|
using System.Threading;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2020-09-22 04:50:40 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Services
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2021-06-29 17:37:13 +00:00
|
|
|
class ServerBase : IDisposable
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2020-12-01 23:23:43 +00:00
|
|
|
// Must be the maximum value used by services (highest one know is the one used by nvservices = 0x8000).
|
|
|
|
// Having a size that is too low will cause failures as data copy will fail if the receiving buffer is
|
|
|
|
// not large enough.
|
|
|
|
private const int PointerBufferSize = 0x8000;
|
|
|
|
|
2023-03-17 11:55:19 +00:00
|
|
|
private readonly static uint[] DefaultCapabilities = new uint[]
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2020-12-01 23:23:43 +00:00
|
|
|
0x030363F7,
|
|
|
|
0x1FFFFFCF,
|
|
|
|
0x207FFFEF,
|
|
|
|
0x47E0060F,
|
|
|
|
0x0048BFFF,
|
|
|
|
0x01007FFF
|
|
|
|
};
|
2020-09-22 04:50:40 +00:00
|
|
|
|
2023-03-19 10:30:04 +00:00
|
|
|
private readonly object _handleLock = new();
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
private readonly KernelContext _context;
|
2020-12-09 22:20:05 +00:00
|
|
|
private KProcess _selfProcess;
|
2020-12-01 23:23:43 +00:00
|
|
|
|
|
|
|
private readonly List<int> _sessionHandles = new List<int>();
|
|
|
|
private readonly List<int> _portHandles = new List<int>();
|
|
|
|
private readonly Dictionary<int, IpcService> _sessions = new Dictionary<int, IpcService>();
|
2021-05-05 21:44:26 +00:00
|
|
|
private readonly Dictionary<int, Func<IpcService>> _ports = new Dictionary<int, Func<IpcService>>();
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
private readonly MemoryStream _requestDataStream;
|
|
|
|
private readonly BinaryReader _requestDataReader;
|
|
|
|
|
|
|
|
private readonly MemoryStream _responseDataStream;
|
|
|
|
private readonly BinaryWriter _responseDataWriter;
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
public ManualResetEvent InitDone { get; }
|
|
|
|
public string Name { get; }
|
2022-06-11 17:58:30 +00:00
|
|
|
public Func<IpcService> SmObjectFactory { get; }
|
|
|
|
|
2022-06-12 07:29:12 +00:00
|
|
|
public ServerBase(KernelContext context, string name, Func<IpcService> smObjectFactory = null)
|
2020-12-01 23:23:43 +00:00
|
|
|
{
|
2022-06-11 17:58:30 +00:00
|
|
|
_context = context;
|
2023-03-17 12:14:50 +00:00
|
|
|
|
|
|
|
_requestDataStream = MemoryStreamManager.Shared.GetStream();
|
|
|
|
_requestDataReader = new BinaryReader(_requestDataStream);
|
|
|
|
|
|
|
|
_responseDataStream = MemoryStreamManager.Shared.GetStream();
|
|
|
|
_responseDataWriter = new BinaryWriter(_responseDataStream);
|
|
|
|
|
|
|
|
InitDone = new ManualResetEvent(false);
|
2020-12-01 23:23:43 +00:00
|
|
|
Name = name;
|
2021-05-11 22:57:21 +00:00
|
|
|
SmObjectFactory = smObjectFactory;
|
2020-12-01 23:23:43 +00:00
|
|
|
|
|
|
|
const ProcessCreationFlags flags =
|
|
|
|
ProcessCreationFlags.EnableAslr |
|
|
|
|
ProcessCreationFlags.AddressSpace64Bit |
|
|
|
|
ProcessCreationFlags.Is64Bit |
|
|
|
|
ProcessCreationFlags.PoolPartitionSystem;
|
|
|
|
|
|
|
|
ProcessCreationInfo creationInfo = new ProcessCreationInfo("Service", 1, 0, 0x8000000, 1, flags, 0, 0);
|
|
|
|
|
2022-06-11 17:58:30 +00:00
|
|
|
KernelStatic.StartInitialProcess(context, creationInfo, DefaultCapabilities, 44, Main);
|
2020-09-22 04:50:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 21:44:26 +00:00
|
|
|
private void AddPort(int serverPortHandle, Func<IpcService> objectFactory)
|
2020-12-01 23:23:43 +00:00
|
|
|
{
|
2023-03-19 10:30:04 +00:00
|
|
|
lock (_handleLock)
|
|
|
|
{
|
|
|
|
_portHandles.Add(serverPortHandle);
|
|
|
|
}
|
2021-05-05 21:44:26 +00:00
|
|
|
_ports.Add(serverPortHandle, objectFactory);
|
2020-12-01 23:23:43 +00:00
|
|
|
}
|
2020-09-22 04:50:40 +00:00
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
public void AddSessionObj(KServerSession serverSession, IpcService obj)
|
2020-09-22 04:50:40 +00:00
|
|
|
{
|
2021-06-29 17:37:13 +00:00
|
|
|
// Ensure that the sever loop is running.
|
|
|
|
InitDone.WaitOne();
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
_selfProcess.HandleTable.GenerateHandle(serverSession, out int serverSessionHandle);
|
|
|
|
AddSessionObj(serverSessionHandle, obj);
|
2020-09-22 04:50:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
public void AddSessionObj(int serverSessionHandle, IpcService obj)
|
2020-09-22 04:50:40 +00:00
|
|
|
{
|
2023-03-19 10:30:04 +00:00
|
|
|
lock (_handleLock)
|
|
|
|
{
|
|
|
|
_sessionHandles.Add(serverSessionHandle);
|
|
|
|
}
|
2020-12-01 23:23:43 +00:00
|
|
|
_sessions.Add(serverSessionHandle, obj);
|
2020-09-22 04:50:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 17:58:30 +00:00
|
|
|
private void Main()
|
|
|
|
{
|
|
|
|
ServerLoop();
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
private void ServerLoop()
|
2020-09-22 04:50:40 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
_selfProcess = KernelStatic.GetCurrentProcess();
|
|
|
|
|
2021-05-05 21:44:26 +00:00
|
|
|
if (SmObjectFactory != null)
|
2020-12-01 23:23:43 +00:00
|
|
|
{
|
2022-01-29 21:18:03 +00:00
|
|
|
_context.Syscall.ManageNamedPort(out int serverPortHandle, "sm:", 50);
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2021-05-05 21:44:26 +00:00
|
|
|
AddPort(serverPortHandle, SmObjectFactory);
|
2020-12-01 23:23:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 17:37:13 +00:00
|
|
|
InitDone.Set();
|
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
KThread thread = KernelStatic.GetCurrentThread();
|
2020-12-01 23:23:43 +00:00
|
|
|
ulong messagePtr = thread.TlsAddress;
|
2022-01-29 21:18:03 +00:00
|
|
|
_context.Syscall.SetHeapSize(out ulong heapAddr, 0x200000);
|
2020-12-01 23:23:43 +00:00
|
|
|
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x0, 0);
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x4, 2 << 10);
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x8, heapAddr | ((ulong)PointerBufferSize << 48));
|
|
|
|
|
|
|
|
int replyTargetHandle = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2023-03-19 10:30:04 +00:00
|
|
|
int handleCount;
|
|
|
|
int portHandleCount;
|
|
|
|
int[] handles;
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2023-03-19 10:30:04 +00:00
|
|
|
lock (_handleLock)
|
|
|
|
{
|
|
|
|
portHandleCount = _portHandles.Count;
|
|
|
|
handleCount = portHandleCount + _sessionHandles.Count;
|
|
|
|
|
|
|
|
handles = ArrayPool<int>.Shared.Rent(handleCount);
|
|
|
|
|
|
|
|
_portHandles.CopyTo(handles, 0);
|
|
|
|
_sessionHandles.CopyTo(handles, portHandleCount);
|
|
|
|
}
|
2020-09-22 04:50:40 +00:00
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
// We still need a timeout here to allow the service to pick up and listen new sessions...
|
2023-03-17 12:14:50 +00:00
|
|
|
var rc = _context.Syscall.ReplyAndReceive(out int signaledIndex, handles.AsSpan(0, handleCount), replyTargetHandle, 1000000L);
|
2020-12-01 23:23:43 +00:00
|
|
|
|
|
|
|
thread.HandlePostSyscall();
|
|
|
|
|
|
|
|
if (!thread.Context.Running)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
replyTargetHandle = 0;
|
|
|
|
|
2023-03-19 10:30:04 +00:00
|
|
|
if (rc == Result.Success && signaledIndex >= portHandleCount)
|
2020-12-01 23:23:43 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
// We got a IPC request, process it, pass to the appropriate service if needed.
|
2020-12-01 23:23:43 +00:00
|
|
|
int signaledHandle = handles[signaledIndex];
|
|
|
|
|
|
|
|
if (Process(signaledHandle, heapAddr))
|
|
|
|
{
|
|
|
|
replyTargetHandle = signaledHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-04 22:15:45 +00:00
|
|
|
if (rc == Result.Success)
|
2020-12-09 22:20:05 +00:00
|
|
|
{
|
|
|
|
// We got a new connection, accept the session to allow servicing future requests.
|
2023-01-04 22:15:45 +00:00
|
|
|
if (_context.Syscall.AcceptSession(out int serverSessionHandle, handles[signaledIndex]) == Result.Success)
|
2020-12-09 22:20:05 +00:00
|
|
|
{
|
2021-05-05 21:44:26 +00:00
|
|
|
IpcService obj = _ports[handles[signaledIndex]].Invoke();
|
|
|
|
|
|
|
|
AddSessionObj(serverSessionHandle, obj);
|
2020-12-09 22:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x0, 0);
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x4, 2 << 10);
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x8, heapAddr | ((ulong)PointerBufferSize << 48));
|
|
|
|
}
|
2023-03-17 12:14:50 +00:00
|
|
|
|
|
|
|
ArrayPool<int>.Shared.Return(handles);
|
2020-12-01 23:23:43 +00:00
|
|
|
}
|
2021-06-29 17:37:13 +00:00
|
|
|
|
|
|
|
Dispose();
|
2020-12-01 23:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool Process(int serverSessionHandle, ulong recvListAddr)
|
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
KProcess process = KernelStatic.GetCurrentProcess();
|
|
|
|
KThread thread = KernelStatic.GetCurrentThread();
|
2020-12-01 23:23:43 +00:00
|
|
|
ulong messagePtr = thread.TlsAddress;
|
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
IpcMessage request = ReadRequest(process, messagePtr);
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
IpcMessage response = new IpcMessage();
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
ulong tempAddr = recvListAddr;
|
|
|
|
int sizesOffset = request.RawData.Length - ((request.RecvListBuff.Count * 2 + 3) & ~3);
|
|
|
|
|
|
|
|
bool noReceive = true;
|
|
|
|
|
|
|
|
for (int i = 0; i < request.ReceiveBuff.Count; i++)
|
|
|
|
{
|
|
|
|
noReceive &= (request.ReceiveBuff[i].Position == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noReceive)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < request.RecvListBuff.Count; i++)
|
|
|
|
{
|
2022-02-22 13:32:10 +00:00
|
|
|
ulong size = (ulong)BinaryPrimitives.ReadInt16LittleEndian(request.RawData.AsSpan(sizesOffset + i * 2, 2));
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
response.PtrBuff.Add(new IpcPtrBuffDesc(tempAddr, (uint)i, size));
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
request.RecvListBuff[i] = new IpcRecvListBuffDesc(tempAddr, size);
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
tempAddr += size;
|
2020-12-01 23:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldReply = true;
|
2021-05-05 21:44:26 +00:00
|
|
|
bool isTipcCommunication = false;
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
_requestDataStream.SetLength(0);
|
|
|
|
_requestDataStream.Write(request.RawData);
|
|
|
|
_requestDataStream.Position = 0;
|
|
|
|
|
|
|
|
if (request.Type == IpcMessageType.HipcRequest ||
|
|
|
|
request.Type == IpcMessageType.HipcRequestWithContext)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
response.Type = IpcMessageType.HipcResponse;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
_responseDataStream.SetLength(0);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
ServiceCtx context = new ServiceCtx(
|
|
|
|
_context.Device,
|
|
|
|
process,
|
|
|
|
process.CpuMemory,
|
|
|
|
thread,
|
|
|
|
request,
|
|
|
|
response,
|
|
|
|
_requestDataReader,
|
|
|
|
_responseDataWriter);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
_sessions[serverSessionHandle].CallHipcMethod(context);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
response.RawData = _responseDataStream.ToArray();
|
|
|
|
}
|
|
|
|
else if (request.Type == IpcMessageType.HipcControl ||
|
|
|
|
request.Type == IpcMessageType.HipcControlWithContext)
|
|
|
|
{
|
|
|
|
uint magic = (uint)_requestDataReader.ReadUInt64();
|
|
|
|
uint cmdId = (uint)_requestDataReader.ReadUInt64();
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
switch (cmdId)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
case 0:
|
|
|
|
FillHipcResponse(response, 0, _sessions[serverSessionHandle].ConvertToDomain());
|
|
|
|
break;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
case 3:
|
|
|
|
FillHipcResponse(response, 0, PointerBufferSize);
|
|
|
|
break;
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
// TODO: Whats the difference between IpcDuplicateSession/Ex?
|
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
int unknown = _requestDataReader.ReadInt32();
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
_context.Syscall.CreateSession(out int dupServerSessionHandle, out int dupClientSessionHandle, false, 0);
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
AddSessionObj(dupServerSessionHandle, _sessions[serverSessionHandle]);
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
response.HandleDesc = IpcHandleDesc.MakeMove(dupClientSessionHandle);
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
FillHipcResponse(response, 0);
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
break;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
default: throw new NotImplementedException(cmdId.ToString());
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
2023-03-17 12:14:50 +00:00
|
|
|
}
|
|
|
|
else if (request.Type == IpcMessageType.HipcCloseSession || request.Type == IpcMessageType.TipcCloseSession)
|
|
|
|
{
|
|
|
|
_context.Syscall.CloseHandle(serverSessionHandle);
|
2023-03-19 10:30:04 +00:00
|
|
|
lock (_handleLock)
|
|
|
|
{
|
|
|
|
_sessionHandles.Remove(serverSessionHandle);
|
|
|
|
}
|
2023-03-17 12:14:50 +00:00
|
|
|
IpcService service = _sessions[serverSessionHandle];
|
|
|
|
(service as IDisposable)?.Dispose();
|
|
|
|
_sessions.Remove(serverSessionHandle);
|
|
|
|
shouldReply = false;
|
|
|
|
}
|
|
|
|
// If the type is past 0xF, we are using TIPC
|
|
|
|
else if (request.Type > IpcMessageType.TipcCloseSession)
|
|
|
|
{
|
|
|
|
isTipcCommunication = true;
|
2021-04-13 22:01:24 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
// Response type is always the same as request on TIPC.
|
|
|
|
response.Type = request.Type;
|
2021-04-13 22:01:24 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
_responseDataStream.SetLength(0);
|
2021-04-13 22:01:24 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
ServiceCtx context = new ServiceCtx(
|
|
|
|
_context.Device,
|
|
|
|
process,
|
|
|
|
process.CpuMemory,
|
|
|
|
thread,
|
|
|
|
request,
|
|
|
|
response,
|
|
|
|
_requestDataReader,
|
|
|
|
_responseDataWriter);
|
2021-04-13 22:01:24 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
_sessions[serverSessionHandle].CallTipcMethod(context);
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
response.RawData = _responseDataStream.ToArray();
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
using var responseStream = response.GetStreamTipc();
|
|
|
|
process.CpuMemory.Write(messagePtr, responseStream.GetReadOnlySequence());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new NotImplementedException(request.Type.ToString());
|
|
|
|
}
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
if (!isTipcCommunication)
|
|
|
|
{
|
|
|
|
using var responseStream = response.GetStream((long)messagePtr, recvListAddr | ((ulong)PointerBufferSize << 48));
|
|
|
|
process.CpuMemory.Write(messagePtr, responseStream.GetReadOnlySequence());
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
2023-03-17 12:14:50 +00:00
|
|
|
|
|
|
|
return shouldReply;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
private static IpcMessage ReadRequest(KProcess process, ulong messagePtr)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
const int messageSize = 0x100;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
byte[] reqData = ArrayPool<byte>.Shared.Rent(messageSize);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
Span<byte> reqDataSpan = reqData.AsSpan(0, messageSize);
|
|
|
|
reqDataSpan.Clear();
|
|
|
|
|
|
|
|
process.CpuMemory.Read(messagePtr, reqDataSpan);
|
|
|
|
|
|
|
|
IpcMessage request = new IpcMessage(reqDataSpan, (long)messagePtr);
|
|
|
|
|
|
|
|
ArrayPool<byte>.Shared.Return(reqData);
|
|
|
|
|
|
|
|
return request;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
private void FillHipcResponse(IpcMessage response, long result)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
FillHipcResponse(response, result, ReadOnlySpan<byte>.Empty);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
private void FillHipcResponse(IpcMessage response, long result, int value)
|
|
|
|
{
|
|
|
|
Span<byte> span = stackalloc byte[sizeof(int)];
|
|
|
|
BinaryPrimitives.WriteInt32LittleEndian(span, value);
|
|
|
|
FillHipcResponse(response, result, span);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
private void FillHipcResponse(IpcMessage response, long result, ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
response.Type = IpcMessageType.HipcResponse;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
_responseDataStream.SetLength(0);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
_responseDataStream.Write(IpcMagic.Sfco);
|
|
|
|
_responseDataStream.Write(result);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
_responseDataStream.Write(data);
|
|
|
|
|
|
|
|
response.RawData = _responseDataStream.ToArray();
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
2021-06-29 17:37:13 +00:00
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
foreach (IpcService service in _sessions.Values)
|
|
|
|
{
|
|
|
|
if (service is IDisposable disposableObj)
|
|
|
|
{
|
|
|
|
disposableObj.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
service.DestroyAtExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
_sessions.Clear();
|
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
_requestDataReader.Dispose();
|
|
|
|
_requestDataStream.Dispose();
|
|
|
|
_responseDataWriter.Dispose();
|
|
|
|
_responseDataStream.Dispose();
|
|
|
|
|
2021-06-29 17:37:13 +00:00
|
|
|
InitDone.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
2020-09-22 04:50:40 +00:00
|
|
|
}
|