Esempio n. 1
0
        public void HandleMessages()
        {
            NetIncomingMessage message;

            while ((message = m_Server.ReadMessage()) != null)
            {
                switch (message.MessageType)
                {
                case NetIncomingMessageType.ConnectionApproval:
                    try
                    {
                        var packet = Packet.Read(message.Data);
                        ApproveConnection(message.SenderConnection);
                        OnConnectionApproved?.Invoke(new LidgrenMessage(message));
                    }
                    catch (Exception e)
                    {
                        DenyConnection(message.SenderConnection);
                        OnConnectionDenied?.Invoke(new LidgrenMessage(message));
                    }
                    break;

                case NetIncomingMessageType.StatusChanged:
                    switch (message.SenderConnection.Status)
                    {
                    case NetConnectionStatus.Connected:
                        OnConnected?.Invoke(new LidgrenMessage(message));
                        break;

                    case NetConnectionStatus.Disconnected:
                        OnDisconnecting?.Invoke(new LidgrenMessage(message));         //not firing from lidgren
                        OnDisconnected?.Invoke(new LidgrenMessage(message));
                        break;
                    }
                    break;

                case NetIncomingMessageType.Data:
                    TriggerCallback(new LidgrenConnection(message.SenderConnection), message.Data);
                    OnReceivedData?.Invoke(new LidgrenMessage(message));
                    break;

                case NetIncomingMessageType.DebugMessage:
                    // handle debug messages
                    // (only received when compiled in DEBUG mode)
                    Console.WriteLine("DEBUG: " + message.ReadString());
                    break;

                case NetIncomingMessageType.WarningMessage:
                    Console.WriteLine("WARNING: " + message.ReadString());
                    break;

                /* .. */
                default:
                    Console.WriteLine("unhandled message with type: "
                                      + message.MessageType);
                    break;
                }
            }
        }
 protected virtual void HandleInterfaceOnConnectonApproved(
     [NotNull] INetworkLayerInterface sender,
     [NotNull] ConnectionEventArgs connectionEventArgs
     )
 {
     Log.Info($"Connection approved [{connectionEventArgs.Connection?.Guid}].");
     OnConnectionApproved?.Invoke(sender, connectionEventArgs);
 }
 protected virtual void HandleInterfaceOnConnectonApproved(INetworkLayerInterface sender, IConnection connection)
 {
     Log.Info($"Connection approved [{connection?.Guid}].");
     OnConnectionApproved?.Invoke(sender, connection);
 }