如何通过escape键停止无休止的服务器线程



我正在尝试用c#编写一个webSocket服务器。服务器作为作业运行:

using Unity.Jobs;
using UnityEngine;
public class ServerStartup : MonoBehaviour
{
WebSocketServerJob server = new WebSocketServerJob();
JobHandle jobHandle;
// Start is called before the first frame update
void Start()
{
Debug.Log("Starting up the websocket server...");
jobHandle = server.Schedule();

Debug.Log("...the server should be running in it's own thread now :-)");
}
private void Update()
{
if (Input.GetKey(KeyCode.Escape))
{
Debug.Log("Escape key detected...stopping server!");
server.StopSwitch = true;
Debug.Log("Waiting for server tread to complete!");
jobHandle.Complete();
Debug.Log("Server tread complete...quitting app!");
Application.Quit();
}
}

}

服务器作业目前是一个无休止的作业,但我希望它在按Escape时结束。然而,仅仅从外部取消它(使用Abort或类似的方法(是不好的,因为我首先想关闭套接字并进行清理。因此,线程应该从外部得到某种通知,它将终止。


//
// csc wsserver.cs
// wsserver.exe
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using Unity.Jobs;
using UnityEngine;

public struct WebSocketServerJob : IJob
{
bool stopSwitch;
public bool StopSwitch
{
set { stopSwitch = value; }
}
public void Execute()
{
StopSwitch = false;
string ip = "127.0.0.1";
int port = 80;
var server = new TcpListener(IPAddress.Parse(ip), port);
server.Start();
Debug.Log("Server has started on " + ip + ":" + port + " Waiting for a connection...");
TcpClient client = server.AcceptTcpClient();
Debug.Log("ServerMsg: A client connected.");
NetworkStream stream = client.GetStream();
// enter to an infinite cycle to be able to handle every change in stream
while (!stopSwitch)
{
while (!stream.DataAvailable) ;
while (client.Available < 3) ; // match against "get"
byte[] bytes = new byte[client.Available];
stream.Read(bytes, 0, client.Available);
string s = Encoding.UTF8.GetString(bytes);
if (Regex.IsMatch(s, "^GET", RegexOptions.IgnoreCase))
{
Debug.Log("=====Handshaking from client=====n" + s);
// 1. Obtain the value of the "Sec-WebSocket-Key" request header without any leading or trailing whitespace
// 2. Concatenate it with "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (a special GUID specified by RFC 6455)
// 3. Compute SHA-1 and Base64 hash of the new value
// 4. Write the hash back as the value of "Sec-WebSocket-Accept" response header in an HTTP response
string swk = Regex.Match(s, "Sec-WebSocket-Key: (.*)").Groups[1].Value.Trim();
string swka = swk + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
byte[] swkaSha1 = System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(swka));
string swkaSha1Base64 = Convert.ToBase64String(swkaSha1);
// HTTP/1.1 defines the sequence CR LF as the end-of-line marker
byte[] response = Encoding.UTF8.GetBytes(
"HTTP/1.1 101 Switching Protocolsrn" +
"Connection: Upgradern" +
"Upgrade: websocketrn" +
"Sec-WebSocket-Accept: " + swkaSha1Base64 + "rnrn");
stream.Write(response, 0, response.Length);
}
else
{
bool fin = (bytes[0] & 0b10000000) != 0,
mask = (bytes[1] & 0b10000000) != 0; // must be true, "All messages from the client to the server have this bit set"
int opcode = bytes[0] & 0b00001111, // expecting 1 - text message
msglen = bytes[1] - 128, // & 0111 1111
offset = 2;
if (msglen == 126)
{
// was ToUInt16(bytes, offset) but the result is incorrect
msglen = BitConverter.ToUInt16(new byte[] { bytes[3], bytes[2] }, 0);
offset = 4;
}
else if (msglen == 127)
{
Debug.Log("TODO: msglen == 127, needs qword to store msglen");
// i don't really know the byte order, please edit this
// msglen = BitConverter.ToUInt64(new byte[] { bytes[5], bytes[4], bytes[3], bytes[2], bytes[9], bytes[8], bytes[7], bytes[6] }, 0);
// offset = 10;
}
if (msglen == 0)
Debug.Log("msglen == 0");
else if (mask)
{
byte[] decoded = new byte[msglen];
byte[] masks = new byte[4] { bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3] };
offset += 4;
for (int i = 0; i < msglen; ++i)
decoded[i] = (byte)(bytes[offset + i] ^ masks[i % 4]);
string text = Encoding.UTF8.GetString(decoded);
Debug.Log(text);
}
else
Debug.Log("mask bit not set");
//Debug.Log();
//StopSwitch = false;
}
}

Console.WriteLine("server thread interrupted by main thread.");
stream.Close();
Debug.Log("stream closed...");
client.Close();
Debug.Log("TcpClient closed...");



}
}

我考虑过使用一个外部静态变量";停止";并将其设置为"0";真";并将WHILE子句替换为类似于";而"(!stop(";,但该文档警告不要这样做,那么解决方案是什么呢?

查看Microsoft文档中关于Thread的示例。打断方法我看到了他们是如何使用公共变量与线程通信的,并将代码更改为使用"server.stopSwitch";。看看cpu的使用情况,它似乎停止了线程,但Unity编辑器也被卡住了。。。所以我必须从任务管理器终止任务:-/

如果您可以使用CancellationTokenSource类,并且可以为服务器提供一些参数。然后进行

var cts = new CancellationTokenSource();
var token = cts.Token;
// somehow give the token to the server
jobHandle = server.Schedule();

然后服务器可以像一样检查他是否应该停止吗

while (!token.IsCancellationRequested) { ... }

要停止服务器执行

cts.Cancel();

您可以使用Interlocked类从long变量读取和写入线程保存。

所以在某个地方定义变量long isRunning

然后在开始或在你安排服务器之前,你只需要isRunning到0

...
isRunning = 0;
...
jobHandle = server.Schedule();
...

服务器可以从这个变量中读取,以检查是否应该运行

while (Interlocked.Read(ref isRunning) == 0) { .. }

要告知服务器应停止使用Interlocked.Increment

Interlocked.Increment(ref isRunning);

在当前循环迭代完成后,服务器将停止工作。

相关内容

最新更新