forked from jjohnsnaill/SubworldLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubserverLink.cs
More file actions
195 lines (172 loc) · 4.24 KB
/
SubserverLink.cs
File metadata and controls
195 lines (172 loc) · 4.24 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using Terraria;
using Terraria.ModLoader.IO;
namespace SubworldLibrary
{
internal class SubserverLink
{
private NamedPipeServerStream pipeOut;
private NamedPipeServerStream pipeIn;
private bool _connected;
private byte[] queue;
private int totalData;
public SubserverLink(string name, TagCompound data)
{
using MemoryStream stream = new MemoryStream(131070);
pipeOut = new NamedPipeServerStream(name + ".OUT", PipeDirection.In);
pipeIn = new NamedPipeServerStream(name + ".IN", PipeDirection.Out);
TagIO.ToStream(data, stream);
queue = stream.GetBuffer();
totalData = (int)stream.Length;
}
public bool Connected => _connected;
public void Close()
{
_connected = false;
pipeOut.Close();
pipeIn.Close();
}
public void Send(byte[] data)
{
if (!_connected)
{
return;
}
lock (queue)
{
while (totalData + data.Length > queue.Length)
{
Monitor.Exit(queue);
Thread.Yield();
Monitor.Enter(queue);
}
Buffer.BlockCopy(data, 0, queue, totalData, data.Length);
totalData += data.Length;
}
}
public void Send(byte[] data, int offset, int length, byte client)
{
if (!_connected)
{
return;
}
lock (queue)
{
while (totalData + length >= queue.Length)
{
Monitor.Exit(queue);
Thread.Yield();
Monitor.Enter(queue);
}
queue[totalData] = client;
Buffer.BlockCopy(data, offset, queue, totalData + 1, length);
totalData += length + 1;
}
}
public void ConnectAndSend(object id)
{
try
{
SendLoop((int)id);
}
finally
{
SubworldSystem.StopSubserver((int)id);
}
}
public void ConnectAndRead(object id)
{
try
{
ReadLoop((int)id);
}
finally
{
SubworldSystem.StopSubserver((int)id);
}
}
private void ReadLoop(int id)
{
pipeOut.WaitForConnection();
// world data has been read, packets can now be sent
_connected = true;
// prompt clients to connect to the subserver
for (int i = 0; i < 256; i++)
{
if (Netplay.Clients[i].IsConnected() && SubworldSystem.playerLocations[i] == id)
{
Netplay.Clients[i].Socket.AsyncSend(new byte[] { 5, 0, 3, (byte)i, 0 }, 0, 5, (state) => { });
}
}
while (pipeOut.IsConnected && !Netplay.Disconnect)
{
byte[] packetInfo = new byte[3];
if (pipeOut.Read(packetInfo) < 3)
{
break;
}
byte low = packetInfo[1];
byte high = packetInfo[2];
int length = (high << 8) | low;
byte[] data = new byte[length];
pipeOut.Read(data, 2, length - 2);
data[0] = low;
data[1] = high;
if (packetInfo[0] == 255 && data[2] == 255)
{
// this packet actually came from a subserver, put it in message buffer 256 for reading on the main thread
MessageBuffer buffer = NetMessage.buffer[256];
lock (buffer)
{
while (buffer.totalData + length > buffer.readBuffer.Length)
{
Monitor.Exit(buffer);
Thread.Yield();
Monitor.Enter(buffer);
}
Buffer.BlockCopy(data, 0, buffer.readBuffer, buffer.totalData, length);
buffer.totalData += length;
buffer.checkBytes = true;
}
continue;
}
// prevents a race condition where a subserver tries to send packets to a client who just left
if (SubworldSystem.playerLocations[packetInfo[0]] == id)
{
Netplay.Clients[packetInfo[0]].Socket.AsyncSend(data, 0, length, (state) => { });
}
}
}
private void SendLoop(int id)
{
pipeIn.WaitForConnection();
int sleep = 0;
while (pipeIn.IsConnected && !Netplay.Disconnect)
{
if (totalData <= 0)
{
// vanilla's server loop does this, not sure what the nuance here is
if (++sleep == 10)
{
Thread.Sleep(1);
sleep = 0;
continue;
}
Thread.Sleep(0);
continue;
}
byte[] data;
lock (queue)
{
data = new byte[totalData];
Buffer.BlockCopy(queue, 0, data, 0, totalData);
totalData = 0;
}
pipeIn.Write(data, 0, data.Length);
}
}
}
}