Search Unity

DarkRift Networking 2

Discussion in 'Assets and Asset Store' started by Jamster, Feb 7, 2018.

  1. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    It does, just through the third party listener rather than being built in!
     
  2. xpxilom

    xpxilom

    Joined:
    Aug 28, 2014
    Posts:
    30
    Where can I find a basic DarkRift tutorial? Does it work in HTML5?
     
  3. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    You can find a basic tutorial here on the website!

    DR works on WebGL via a community network listener you can get from here :)
     
  4. tamhuynh-ce03

    tamhuynh-ce03

    Joined:
    Mar 6, 2019
    Posts:
    3
    Hi Jamster, Do ENET Listener support Android and iOS Mobile?

    I used ENET with https://github.com/nxrighthere/ENet-CSharp/releases was mentioned in that link, but I got nothing libs for android and ios (just saw x86_64 for PC).
    So when I build apk file for androind and run, it throw the error DllNotFound enet.
     
    Last edited: Feb 19, 2021
  5. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    I don't know the definite answer, it might be worth asking the original author.

    My guess would be not as I doubt ENET is compiled for Android/iOS but I don't know
     
  6. Escoce

    Escoce

    Joined:
    Dec 9, 2020
    Posts:
    5
    You can get the sources for Enet-Csharp from nxrighthere's github. It has makefiles for android and iOS so you should be able to compile it for native Android and iOS support.

    https://github.com/nxrighthere/ENet-CSharp

    You don't have to use the precompiled releases. Here is the snippit from his README file for building:

    Building
    To build the native library appropriate software is required:

    For desktop platforms CMake with GNU Make or Visual Studio.

    For mobile platforms NDK for Android and Xcode for iOS. Make sure that all compiled libraries are assigned to appropriate platforms and CPU architectures.

    To build the library for Nintendo Switch, follow this guide.

    A managed assembly can be built using any available compiling platform that supports C# 3.0 or higher.
     
    Jamster likes this.
  7. xpxilom

    xpxilom

    Joined:
    Aug 28, 2014
    Posts:
    30
    Hola, donde puedo encontrar un tutorial para principiante de darkrift 2 ?
     
  8. Escoce

    Escoce

    Joined:
    Dec 9, 2020
    Posts:
    5
  9. rg_software

    rg_software

    Joined:
    Apr 29, 2017
    Posts:
    10
    Hello. I have a quick help request. I am testing DarkRift2 in my home network, and so far it works fine. However I still can't make outside connections running. I set up port forwarding on my router, and it is supposed to redirect both TCP and UDP calls. In reality, only TCP seem to work, while "unreliable" calls don't go through. Am I missing something?

    P.S. I am positive that UDP packets do go through the router: I am able to send and receive packets between certain two machines, but if I send a message from one of these machines to the DarkRift2 server process, it is not delivered as far as I can tell.
     
    Last edited: Feb 21, 2021
  10. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    If you're able to connect then UDP packets have made it through, DR senda a packet to the server and back again to verifiy on connection.

    Is it all unreliable messages you're losing or just a few/some? If you switch to all reliable do you still see any issues?
     
  11. rg_software

    rg_software

    Joined:
    Apr 29, 2017
    Posts:
    10
    I think all "unreliable" messages are lost. Basically, it works like this: when I connect to the internal IP address, everything works fine; when I connect to the extenal IP address, all Reliable messages are still there, but Unreliable are not delivered anymore. Are there any checkups I can try out?

    Update. It seems that DarkRift2 server works fine (including "unreliable" messages) if I access it from the outside! In other words, I have to use my internal IP when I work from my home network, but my external IP is OK for outside computers. This isn't very convenient but at least it works. Any thoughts, though? :)
     
    Last edited: Feb 22, 2021
  12. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Well, I've not heard that one before! I suspect it's just a router with very odd configuration!
     
  13. rg_software

    rg_software

    Joined:
    Apr 29, 2017
    Posts:
    10
    I am truly sorry to bug you again, but this issue drives me mad :) I tried playing with a friend outside, and for him the situation is reverse: when he sends "Reliable" packets, it always ends with "System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host", while unreliable packets are delivered all right. Also I tried sniffing my traffic with Wireshark, and I see UDP packets from my machine going outside (to my external IP) and back to DarkRift2 server, but for some reason the server doesn't see them. Built-in DR2 sniffers doesn't see them as well! Should "sniffer add -a" report any udp packet sent to the port 4296 or its more complex than that?
     
  14. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    I'd suggest trying to run the server on a VM in the cloud. Consumer routers can be hit and miss for hosting and it does seem very suspicious that you can connect fine and then not receive any data! It's also odd you get a "forcibly closed by the remote host", that indicates a TCP RST packet being sent and that usually only happens with network issues or the server exiting.

    The sniffer will show any packets DR receives that are associated with a client, i.e. the IP address matches the client's. I guess it's possible that your router is changing the IP address but I'd be surprised. Have a look in the server log file for trace level logs that look out of place, for example "UDP connection from XYZ had no associated TCP connection."
     
  15. drallcom3

    drallcom3

    Joined:
    Feb 12, 2017
    Posts:
    165
    I have some questions about DR chat. I'm looking for a solution for guild chats. Many users connected (tens of thousands hopefully), but very low usage. Message persistence is important. If it can also double as a global chat, even better (but that one is optional).
    1) I assume I have to host my chat server myself?
    2) Is there a limit on how many users can be connected at the same time (any limit, even if super high like 65535)?
    3) Is there a limit for number of rooms?
    4) Is there message persistance?
    5) Does the persistance carry over if I restart the server?

    Thanks!
     
  16. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Hey!

    1. Yes, DR servers are all self hosted so you can use your favourite hosting provider :)
    2. Per server, 65K, however the throughput of messages you'll get will depend on your hardware and logic independent of the connection count. If you build a cluster of DR servers you can get higher counts and higher throughputs
    3, 4, 5.These things are outside the scope of DR so the answer is: As many as you like and yes, if you build your server like that. Normally youd use a database of some form if you want persistence and in which case you can have persistence across restarts/difference servers etc.

    Hope that helps!
     
    drallcom3 likes this.
  17. drallcom3

    drallcom3

    Joined:
    Feb 12, 2017
    Posts:
    165
    It sure does. Thank you for your quick response!

    It also just dawned me that I can just download the free version and try to break the limits myself. Horray for free base versions!
     
  18. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    DarkRift 2.9.1 is now available on the website!

    This update contains a lot of bug fixes and a lot of improvements to error messages! I've been looking at the most common questions on the Discord and tried to clarify many of the DR error messages to be clearer, provide more information and whenever possible link you straight to the right page on the docs! Configuration errors even have line numbers now!

    upload_2021-3-7_16-24-26.png

    The update is already available on the website here, or through the DarkRift CLI tool and will be available on the asset store in the coming few days!
    https://www.darkriftnetworking.com/DarkRift2/Releases/Free/2.9.1.unitypackage
    https://www.darkriftnetworking.com/DarkRift2/Releases/Pro/2.9.1.unitypackage?invoice=<your invoice number>

     
    knxrb likes this.
  19. Bestnet

    Bestnet

    Joined:
    Nov 6, 2019
    Posts:
    3
    Using Darkrift 2.9.1 Pro. I am trying to host 2 Darkrift servers from same computer with same Ip addresses but different port. Server1 ip:127.0.0.1 Port:4296, Server2 ip:127.0.0.1 Port:4297. I am getting error
    Unhandled Exception: System.Net.HttpListenerException: Failed to listen on prefix 'http://localhost:10666/' because it conflicts with an existing registration on the machine.
    at System.Net.HttpListener.AddAllPrefixes()
    at System.Net.HttpListener.Start()
    at DarkRift.Server.Plugins.HealthCheck.HttpHealthCheck.Loaded(LoadedEventArgs args)
    at DarkRift.Server.ExtendedPluginManagerBase`1.Loaded()
    at DarkRift.Server.DarkRiftServer..ctor(ServerSpawnData spawnData, ClusterSpawnData clusterSpawnData)
    at DarkRift.Server.DarkRiftServer..ctor(ServerSpawnData spawnData)
    at DarkRift.Server.Console.Program.Main(String[] args)

    Any help?
     
  20. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Looks like the health check on the servers are on the same port. Have a look here. You can use the snippets there to change the ports or set them to not load all together.
     
  21. Bestnet

    Bestnet

    Joined:
    Nov 6, 2019
    Posts:
    3
    Setting <plugin type="HttpHealthCheck" load="false"> solved problem.
    Thank you.
     
  22. Gamedel

    Gamedel

    Joined:
    May 7, 2017
    Posts:
    6
    I want to buy this asset as a replacement for Photon, but I do not yet understand what difficulties I may face. Does this asset support server migration in matchmaking mode?
     
  23. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Hi, I think I answered you in Discord already but here's the response for anyone else looking!

    Grab the free version and have a play around before you buy, DR is rather different from Photon!

    DR's matchmaking is very different to Photon's. It doesn't do the rooms side of things like Photon does, so server migration isn't something built in
     
  24. tamhuynh-ce03

    tamhuynh-ce03

    Joined:
    Mar 6, 2019
    Posts:
    3
    Hi Jamie,
    I started my server with DR2 on linux ubuntu ago 3 months as well.
    But today, I have the problem that my server keeps crashing with the following message:
    ==============
    Client Id = 180 disconnected: ,error=ConnectionReset,Exception=System.Net.Sockets.SocketException (0x80004005): Connection reset by peer
    (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

    mmap(PROT_NONE) failed
    Caught fatal signal - signo:6 code:-6 errno:0 addr:0x201722
    Obtained 26 stack frames.
    #0 0x007f4e55da93c0 in (Unknown)
    #1 0x007f4e55be818b in (Unknown)
    #2 0x007f4e55bc7859 in (Unknown)
    #3 0x007f4e55648356 in (Unknown)
    #4 0x007f4e556483c3 in (Unknown)
    #5 0x007f4e5564b2c3 in (Unknown)
    #6 0x007f4e5564b690 in (Unknown)
    #7 0x007f4e5564dd50 in (Unknown)
    #8 0x007f4e5564e7aa in (Unknown)
    #9 0x007f4e555d85c4 in (Unknown)
    #10 0x007f4e555d86fb in (Unknown)
    #11 0x007f4e555d8743 in (Unknown)
    #12 0x000000406beed7 in (wrapper managed-to-native) object:__icall_wrapper_ves_icall_object_new_specific (intptr)
    #13 0x000000407bfe40 in DarkRift.Server.Client:HandleIncomingDataBuffer (DarkRift.MessageBuffer,DarkRift.SendMode)
    #14 0x000000407bfd87 in DarkRift.Server.NetworkServerConnection:HandleMessageReceived (DarkRift.MessageBuffer,DarkRift.SendMode)
    #15 0x000000407b0e83 in System.Net.Sockets.SocketAsyncResult/<>c:<Complete>b__27_0 (object)
    #16 0x000000407ad81c in System.Threading._ThreadPoolWaitCallback:performWaitCallback ()
    #17 0x007f4e554675b5 in (Unknown)
    #18 0x007f4e555d6b34 in (Unknown)
    #19 0x007f4e555f27b9 in (Unknown)
    #20 0x007f4e5556e8ca in (Unknown)
    #21 0x007f4e555f0829 in (Unknown)
    #22 0x007f4e55652067 in (Unknown)
    #23 0x007f4e55646dd2 in (Unknown)
    #24 0x007f4e55d9d609 in (Unknown)
    #25 0x007f4e55cc4293 in (Unknown)
    ==============

    I restarted my server after that, but it caused the same issue after 2 hours. So I restarted again, again... I'm not sure the server can happen the issue again or not.

    Do you have any idea what the problem could be?

    I just buy a Pro version and not sure Pro version can resolve that issue.

    Thanks,
    TamH
     
  25. tamhuynh-ce03

    tamhuynh-ce03

    Joined:
    Mar 6, 2019
    Posts:
    3
    My server just happen the same issue again with below logs:
    ============
    New client connected Id = 1354
    (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

    mmap(PROT_NONE) failed
    Caught fatal signal - signo:6 code:-6 errno:0 addr:0x202bb0
    Obtained 27 stack frames.
    #0 0x007f9a507c93c0 in (Unknown)
    #1 0x007f9a5060818b in (Unknown)
    #2 0x007f9a505e7859 in (Unknown)
    #3 0x007f999fabf356 in (Unknown)
    #4 0x007f999fabf3c3 in (Unknown)
    #5 0x007f999fac22c3 in (Unknown)
    #6 0x007f999fac2690 in (Unknown)
    #7 0x007f999fac4d50 in (Unknown)
    #8 0x007f999fac57aa in (Unknown)
    #9 0x007f999fa4f5c4 in (Unknown)
    #10 0x007f999fa4f6fb in (Unknown)
    #11 0x007f999fa4f743 in (Unknown)
    #12 0x00000041537ed7 in (wrapper managed-to-native) object:__icall_wrapper_ves_icall_object_new_specific (intptr)
    #13 0x000000416399e5 in DarkRift.Server.DarkRiftThreadHelper:DispatchIfNeeded (System.Action,DarkRift.Dispatching.ActionDispatchCompleteCallback)
    #14 0x00000041638e60 in DarkRift.Server.Client:HandleIncomingDataBuffer (DarkRift.MessageBuffer,DarkRift.SendMode)
    #15 0x00000041638da7 in DarkRift.Server.NetworkServerConnection:HandleMessageReceived (DarkRift.MessageBuffer,DarkRift.SendMode)
    #16 0x00000041629c73 in System.Net.Sockets.SocketAsyncResult/<>c:<Complete>b__27_0 (object)
    #17 0x0000004162681c in System.Threading._ThreadPoolWaitCallback:performWaitCallback ()
    #18 0x007f999f8de5b5 in (Unknown)
    #19 0x007f999fa4db34 in (Unknown)
    #20 0x007f999fa697b9 in (Unknown)
    #21 0x007f999f9e58ca in (Unknown)
    #22 0x007f999fa67829 in (Unknown)
    #23 0x007f999fac9067 in (Unknown)
    #24 0x007f999fabddd2 in (Unknown)
    #25 0x007f9a507bd609 in (Unknown)
    #26 0x007f9a506e4293 in (Unknown)
    ./start_server.sh: line 13: 2108336 Aborted (core dumped) /root/Desktop/linux_dracula/dracula.x86_64
    ============
     
  26. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Hey TamH,

    I'm afraid I've not got much idea about this. Looks like it's coming from some fairly random places: DarkRift.Server.DarkRiftThreadHelper.DispatchIfNeeded and DarkRift.Server.Client:HandleIncomingDataBuffer.

    I think this is probably more of an IL2CPP crash rather than a DR issue so might be worth updating your Unity version if you can

    Apologies!
     
  27. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    DarkRift 2.10.0 is now available on the website!

    This update adds builds for .NET Core 3.1 and .NET 5!

    Additionally there are a number of bug fixes for issues you guys have submitted!

    The update is already available on the website here, or through the DarkRift CLI tool and will be available on the asset store in the coming few days!
    https://www.darkriftnetworking.com/DarkRift2/Releases/2.10.0/Free/
    https://darkriftnetworking.com/DarkRift2/Releases/2.10.0/Pro/?invoice=<your invoice number>

    Do note the DR CLI tool has not yet been updated to pull .NET Core 3.1 or .NET 5 builds yet!

     
    Nyarlathothep, Brodus and knxrb like this.
  28. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    DarkRift 2.10.1 is now available on the website!

    This version features a number of bug fixes and some minor improvements from feedback from the community.

    The update is already available on the website here, or through the DarkRift CLI tool and will be available on the asset store in the coming few days!
    https://www.darkriftnetworking.com/DarkRift2/Releases/2.10.1/Free/
    https://darkriftnetworking.com/DarkRift2/Releases/2.10.1/Pro/?invoice=<your invoice number>

     
  29. stickylab

    stickylab

    Joined:
    Mar 16, 2016
    Posts:
    92
    the sadnest is there is no example project
     
  30. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    There are some very basic examples included in the package but they are indeed very basic! The official tutorial has a complete example and some of our community written tutorials have even better examples, like this one!

    But you are right, larger teams like Photon and Unity are able to put out much better example projects than we can, we've always chosen to focus our limited resources on improving DarkRift over building fully fledged examples!
     
  31. stickylab

    stickylab

    Joined:
    Mar 16, 2016
    Posts:
    92

    no ,, there is no example like 2d platformer and etc ,,,,the example even so bad ,,there is no camera and its seeems like not example ,
     
  32. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    DarkRift 2.10.1 is now available on the Asset Store!
     
    knxrb likes this.
  33. rg_software

    rg_software

    Joined:
    Apr 29, 2017
    Posts:
    10
    Hello!
    I need an opinion. I am trying to figure out how powerful should be a machine to manage, say, hundreds of concurrent clients. I decided to start with a very basic experiment. I am running DR2 server in "demo" mode, and I use a slightly modified "chat" example to send around two messages per sec to the server and print out results.

    The problem is: after ~50 concurrent connections the server stops accepting new connections. What I get is

    System.Net.Sockets.SocketException: 0x80004005 an existing connection was forcibly closed.

    Sometimes I cannot even connect, but this isn't common. I also received a message "ObjectCacheMonitor: 3 AutoRecyclingArray object were finalized last period. This is usually a sign that you are not recycling objects correctly." several times, but I don't know whether "you" means the client or the server.

    Any ideas what can be behind this all?
     
  34. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    This seems very strange, even a fairly low power console-based DarkRift server should be able to handle 50 concurrent clients at that rate of messages (unless your trying to send some crazy amount of data perhaps!)

    The exception means a TCP RST packet was received, it could be that DR sent the client this in which case you would see some logs in the log file (not on the console by default) saying why the connection was closed, or it could be the network connection between the server and client was interrupted.

    Can you confirm what provider you're using and what spec machine you're running?

    The latter log message is also a bit strange as that should never be emitted if you're simply using the demo command. It indicates that some DarkRift objects (in theis case AutoRecyclingArrays) were not correctly returned to the object pool on the server, but DR shouldn't really get itself into a state like that. It's nothing to worry about, but probably does indicate that when the connections are getting closed DR is getting a shock and ends up dropping some of the objects!

    Hope that helps!
     
  35. pop2518

    pop2518

    Joined:
    Mar 13, 2020
    Posts:
    6
    Hi, I got messages like:
    [Warning] ObjectCacheMonitor 392 AutoRecyclingArray objects were finalized last period. This is usually a sign that you are not recycling objects correctly.

    What should I do to recycle the objects correctly?
     
  36. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Hey!
    To recycle objects correctly you just need to be wrapping the right objects in using statements so that when scope leaves the using statement they are returned to DarkRift to reuse:
    Code (CSharp):
    1. using (DarkRiftWriter newPlayerWriter = DarkRiftWriter.Create())
    2. {
    3.     newPlayerWriter.Write(newPlayer.ID);
    4.    
    5.     using (Message newPlayerMessage = Message.Create(0, newPlayerWriter))
    6.     {
    7.         client.SendMessage(newPlayerMessage, SendMode.Reliable);
    8.     }
    9. }
    AutoRecyclingArrays in particular are internal to DarkRift so you'll see other warnings for other objects that use ARAs which should be addressed first. When you fix the other warnings the warning for ARAs will go as well.

    If object pooling isn't a concern for you then you can ignore the warnings safely too.
     
    pop2518 likes this.
  37. mgw2021

    mgw2021

    Joined:
    May 22, 2021
    Posts:
    4
    Hi Guys, Is there any version of Server Console Executer file work at both Mac/Linux platform?
     
  38. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Hey,

    If you have DarkRift Free you can use mono or if you have DarkRift Pro just use one of the .NET 3.1 or.NET 5 builds!
     
  39. Deleted User

    Deleted User

    Guest

    Hi,
    just installed the latest version.
    Whoever get the following issue
    Code (CSharp):
    1. SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.
    When running multiple servers.
    Make sure you add
    Code (CSharp):
    1. <plugin type="HttpHealthCheck" load="false"/>
    inside <plugins></plugins>
     
    Jamster likes this.
  40. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    324
  41. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
  42. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    324
    That was quick. Thank you :)
     
  43. Dobrbobr

    Dobrbobr

    Joined:
    Feb 6, 2019
    Posts:
    1
  44. rgsoftware

    rgsoftware

    Joined:
    Oct 25, 2017
    Posts:
    3
    Hello,

    A quick question: is it worth the effort to do something with undelivered messages in "reliable" mode? I mean, suppose SendMessage() returns false as a result.

    My current guess/understanding is that SendMessage() will retry for some time and then generate OnDisconnected event anyway in case of failure. And it would probably mean a really serious network disruption.
     
  45. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Usually if you get false back from SendMessage it doesn't mean it's undelivered but that it couldn't even send it. For example, if you try to send to a client that has already disconnected, or the buffer on the socket is full and can't take any more data (rare). It's kind of up to you how you handle it but in general if you're getting false from SendMessage then the Disconnected event has (or is just about to be) invoked.
     
  46. MotoSV

    MotoSV

    Joined:
    Mar 4, 2015
    Posts:
    23
    Hello

    I'm attempting to create a DarkRift Server within a console app and all seems to be working fine, but I'm unsure how I can stop the server other than CTRL+C.

    Is there a built in way to do this?

    I have a ProcessConsoleLoop that is run on a separate thread, very similar to examples I've seen elsewhere, and in this loop I have the following code:

    Code (CSharp):
    1.  
    2. if(string.IsNullOrWhiteSpace(input) ||
    3.    string.Equals(input, "stop", StringComparison.OrdinalIgnoreCase) ||
    4.    string.Equals(input, "exit", StringComparison.OrdinalIgnoreCase))
    5. {
    6.     m_server.Dispose();
    7.     return;
    8. }
    But this does not stop the server from running. I then updated the main loop to the following:

    Code (CSharp):
    1.  
    2. m_server.DispatcherWaitHandle.WaitOne(TimeSpan.FromSeconds(10));
    3. if(!m_server.Disposed)
    4. {
    5.     m_server.ExecuteDispatcherTasks();
    6. }
    After this change the server stopped and the console application exited.

    But my question is are there any side-effects or consequences to passing a timeout to WaitOne? If so, is there a better way of achieving this?

    Thanks
     
  47. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    I think that's the right way, exiting the console loop (going by the normal server console code) won't cause the program to execute as it's a background thread. The only other way I could think of to solve this could be to make the console loop thread the foreground thread and the dispatcher thread (the main thread) the background thread. That would mean when the console thread exits the program exits instead of the program waiting for the dispatcher thread to exit.

    I'm a bit surprised DR doesn't release the DispatcherWaitHandle when you dispose it to be honest.
     
  48. MotoSV

    MotoSV

    Joined:
    Mar 4, 2015
    Posts:
    23
    So, I switched them around with the dispatcher loop on another thread and the console in the Main method, but ran into the same issue. When the console loop exits it reaches the end of the Main method, but the process does not end. The WaitOne prevents it from happening. If I put a timeout on WaitOne the program exits.

    This is the core of the Main method:

    Code (CSharp):
    1. while(!m_server.Disposed)
    2. {
    3.     string? input = Console.ReadLine();
    4.     if(string.IsNullOrWhiteSpace(input) || string.Equals(input, "exit", StringComparison.OrdinalIgnoreCase))
    5.     {
    6.         m_server.Dispose();
    7.         Console.WriteLine("Server has been disposed");
    8.     }
    9.     else
    10.     {
    11.         Console.WriteLine("Executing a command");
    12.         m_server.ExecuteCommand(input);
    13.     }
    14. }
    15. Console.WriteLine("End of the program");
    And this is the dispatcher loop:

    Code (CSharp):
    1. while(!m_server.Disposed)
    2. {
    3.     m_server.DispatcherWaitHandle.WaitOne(TimeSpan.FromSeconds(10));
    4.     Console.WriteLine("WaitOne Timed out");
    5.     if(!m_server.Disposed)
    6.     {
    7.         Console.WriteLine("Server has not been disposed. Calling ExecuteDispatcherTasks");
    8.         m_server.ExecuteDispatcherTasks();
    9.     }
    10. }
    11. Console.WriteLine("Exiting ProcessDispatcher method");
    When I run this and input "exit [ENTER]" the output is:
    • Main: Server has been disposed
    • Main: End of program
    • ...a few seconds later...
    • Dispatcher Loop: WaitOne Timed out
    • Dispatcher Loop: Exit ProcessDispatcher method
    • ...process ends...
    In both scenarios I need to add a timeout to WaitOne. But, if having a timeout is okay, then it's not a big issue. :)
     
  49. MotoSV

    MotoSV

    Joined:
    Mar 4, 2015
    Posts:
    23
    If I set 'IsBackground=true' on the new thread running the dispatcher loop the program exits as soon as the server has been disposed in the Main method. However, this doesn't work with the original setup, i.e., dispatcher loop in the Main method and console loop on separate thread.
     
  50. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Ah sorry I had it in my head that new threads were started as background threads by default but that's not the case! You should be able to make the main thread a background thread I think, you just need to get a handle to it with Thread.CurrentThread so you can set properties on it.