forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPCServer.cs
62 lines (53 loc) · 1.67 KB
/
IPCServer.cs
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
// Copyright(c) 2019-2022 pypy, Natsumi and individual contributors.
// All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Threading.Tasks;
namespace VRCX
{
internal class IPCServer
{
public static readonly IPCServer Instance;
public static readonly List<IPCClient> Clients = new List<IPCClient>();
static IPCServer()
{
Instance = new IPCServer();
}
public void Init()
{
new IPCServer().CreateIPCServer();
}
public static void Send(IPCPacket ipcPacket)
{
foreach (var client in Clients)
{
client.Send(ipcPacket);
}
}
public void CreateIPCServer()
{
var ipcServer = new NamedPipeServerStream("vrcx-ipc", PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
ipcServer.BeginWaitForConnection(DoAccept, ipcServer);
}
private void DoAccept(IAsyncResult asyncResult)
{
var ipcServer = (NamedPipeServerStream)asyncResult.AsyncState;
try
{
ipcServer.EndWaitForConnection(asyncResult);
}
catch (Exception e)
{
Console.WriteLine(e);
}
var ipcClient = new IPCClient(ipcServer);
Clients.Add(ipcClient);
ipcClient.BeginRead();
CreateIPCServer();
}
}
}