How to detect NetPeer shutdown?

142 views
Skip to first unread message

Siyuan Chen

unread,
Nov 9, 2016, 5:13:16 AM11/9/16
to lidgren-network-gen3
Hi all,
I'm using Lidgren to develop a application.
In my application, I will create and delete several lidgren NetPeers (NetClient/NetServer).
My question is how to detect NetPeer shutdown?

For example:
When I new a NetPeer, I will call NetPeer.Start() immediately.
NetPeer.Start() method will internally sleep(50) to ensure background thread start, that's OK.
But when I want to dispose a NetPeer, Can I safely call NetPeer.Shutdown() and set its reference to null immediately?
I found there is no sleep operation in NetPeer.Shutdown():
    public void Shutdown(string bye)
    {
      if (this.m_socket == null)
        return;
      this.m_shutdownReason = bye;
      this.m_status = NetPeerStatus.ShutdownRequested;
    }

So I can only think of another way: Pulling message in main loop.
But unfortunately.... there is no PeerStatusChanged message in current lidgren version.

NetIncomingMessage inc;
    while ((inc = lidgrenServer.ReadMessage()) != null)
    {
        try
        {
            switch (inc.MessageType)
            {
                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.ErrorMessage:
                case NetIncomingMessageType.ConnectionApproval:
                case NetIncomingMessageType.StatusChanged: // <------ This is
StatusChanged of connection not peer!
                case NetIncomingMessageType.Data:
                // No PeerStatusChanged!!!
            }
        }
        finally
        {
           
lidgrenServer.Recycle(inc);
        }
    }

After reading lidgren source code, I found that when lidgren NetPeer shutdown: ExecutePeerShutdown will call LogDebug("Shutdown complete");
So I want to hook
LogDebug("Shutdown complete"), but LogDebug has a Condition Attribute [Conditional("DEBUG")], so it won't be call in release version.....

Now, how to detect NetPeer shutdown event?
Can someone help me ?
Very thanks.
Reply all
Reply to author
Forward
0 new messages