在多线程套接字服务器上使用事件 [英] Using events on multithreading socket server

查看:54
本文介绍了在多线程套接字服务器上使用事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个新的游戏项目制作一个服务器,我正在为客户端和服务器使用 C# (.NET Framework v4.5).

Im making a server for a new game proyect and im using C# (.NET Framework v4.5) for both client and server.

我目前正在使用这样的东西来管理我的球员.

Im currently using something like this to manage my players.

new Thread(() =>
{
   connection.ListenAndSend();
}).Start();

public void ListenAndSend()
{
    while(working)
    {
        if(someThingToRead)
        {
        //Listening logic here

        //Here i call my event for dataReceived with the data i just read.
        }
        else if (queue.Count > 0)
        {
            Send(queue.Dequeue().Data); //Send data from the Queue
        }
    }

使用队列是因为我不应该从不同的线程访问流(我认为它会抛出 InvalidOperationException)所以我实现了队列以从同一线程发送数据.

The use of a Queue is because i should not access the stream from different threads (it throws InvalidOperationException i think) so i implemented the queue to send data from the same thread.

我认为这基本上没问题,但我担心这些事件.

I think this is mostly OK but im worried about the events.

事件正在触发并且工作正常,但我还没有对多个客户端进行测试,所以...

The events are firing and working fine but i have not tested yet with multiple clients so...

  • 事件是否在与侦听器线程相同的线程中执行?
  • 我是否会遇到多个线程同时修改字段的问题?(例如,我在 Java 方面更有经验,我还记得关于同步接口的一些事情吗?)
  • 现在还没有完成,但稍后可能某些事件最终会调用发送方法,因此将数据添加到发送队列(但这应该不是问题吧?)

如果重要的话,这款游戏永远不会适合大量玩家(可能在 2 到 8 人之间).

This game will never be for a large number of players (probably between 2 and 8) if that matters.

有什么我没有看到的严重问题吗?

Is there some serious problem im not seeing?

当我使用队列时,我认为哪个线程添加数据并不重要,但是在执行事件代码时我的监听会停止吗?(如果它真的在同一个线程上)虽然这真的不是问题,但它会同时访问多个线程的字段吗?

As im using the Queue im thinking it does not matter what thread do add the data but will my listening be stopped while it is doing the event code? (If its really on the same thread) and while that could not really be an issue will it be simultaneously accesing fields from multiple threads?

更新:

我可以只用 async/await 来完成所有这些吗??我看不出如何以这种方式进行监听循环,但如果真的有可能,请在答案中进行扩展.

Can i make all of this just with async/await?? i cant see how to make the listen loop that way but if its really possible please expand in an answer.

此外,这个答案 说反了.

更新 2:

现在我已经测试了我的代码并且我知道它工作正常.

Now that i have tested my code and i know its working fine.

正如评论中所指出的,我可以使用 async/await 完成此操作,但线程要少得多,但是:

As pointed out in comments i could have done this using async/await with a lot less threads, but:

其中最好使用 async/await 而不是线程,请记住我的游戏服务器应该能够根据需要占用尽可能多的 cpu/内存,如果可以的话就可以了(当然线程正在调用 sleep不工作时).我希望我的服务器运行得尽可能接近实时.

In which it would be better to use async/await instead of threads, keep in mind my game server should be able to take as much cpu/memory as needed and its fine if it does (Of course the threads are calling sleep when not doing job). And i would like my server to run the closer to real time as possible.

我还想指出,当我第一次开始处理我的逻辑时,游戏地图的序列化(大约 60k 个对象)花费了大约 600 毫秒(这将适用于每个客户端)所以我最终移动了将作业序列化到套接字线程.

I also would like to point, the first time i made it to start processing my logic the serializing of the game map (around 60k objects) took around 600ms (and that will be for each client) so i ended up moving the serialize job to the socket thread.

考虑到这一点,谁认为使用 Async/Await 而不是新线程会更好

With this in mind, who thinks it would be better to use Async/Await instead of new Threads

PD:我只说我的服务器,游戏客户端确实在使用 async/await 与服务器通信.

PD: Im only talking about my server, the game client is indeed using async/await to communicate with the server.

推荐答案

You have 2 options , ether lock the variable (不推荐)如何锁定在多个线程中使用的变量

You have 2 options , ether lock the variable (which is not recommended) How to lock a variable used in multiple threads

或使用调度程序从另一个线程调用方法使用 C# 调度程序

or use dispatcher for calling a method from another thread Using the C# Dispatcher

如果您不使用 WPF,您可以查看此链接:如何将 wpf 调度程序转换为 winforms

if you are not using WPF, you might take a look to this link : how do I convert wpf dispatcher to winforms

这篇关于在多线程套接字服务器上使用事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆