Пример #1
0
        private static void OnClientStatusChanged(ClientStatusChangedEventArgs args)
        {
            if (args.Status == NetConnectionStatus.Connected)
            {
                lastPlayerIndex++;

                players[args.SenderConnection].Index = lastPlayerIndex;

                Send(new LoadLevel {
                    LevelName           = currentLevel,
                    AssignedPlayerIndex = lastPlayerIndex
                }, 64, args.SenderConnection, NetDeliveryMethod.ReliableSequenced, NetworkChannels.Main);
            }
            else if (args.Status == NetConnectionStatus.Disconnected)
            {
                int index = players[args.SenderConnection].Index;

                players.Remove(args.SenderConnection);

                foreach (var to in players)
                {
                    if (to.Key == args.SenderConnection)
                    {
                        continue;
                    }

                    Send(new DestroyRemotePlayer {
                        Index  = index,
                        Reason = 1 // ToDo
                    }, 3, to.Key, NetDeliveryMethod.ReliableSequenced, NetworkChannels.PlayerGeneral);
                }
            }
        }
Пример #2
0
        private void OnClientStatusChanged(ClientStatusChangedEventArgs args)
        {
            if (args.Status == NetConnectionStatus.Connected)
            {
                lock (sync) {
                    lastPlayerIndex++;
                    players[args.SenderConnection].Index = lastPlayerIndex;
                }

                Log.Write(LogType.Verbose, "Client " + PlayerNameToConsole(players[args.SenderConnection]) + " - " + players[args.SenderConnection].UserName + " (" + args.SenderConnection.RemoteEndPoint + ") connected!");

                if (currentLevel != null)
                {
                    Send(new LoadLevel {
                        LevelName           = currentLevel,
                        LevelType           = currentLevelType,
                        AssignedPlayerIndex = lastPlayerIndex
                    }, 64, args.SenderConnection, NetDeliveryMethod.ReliableSequenced, PacketChannels.Main);
                }
            }
            else if (args.Status == NetConnectionStatus.Disconnected)
            {
                Log.Write(LogType.Verbose, "Client " + PlayerNameToConsole(players[args.SenderConnection]) + " - " + players[args.SenderConnection].UserName + " (" + args.SenderConnection.RemoteEndPoint + ") disconnected!");

                lock (sync) {
                    byte index = players[args.SenderConnection].Index;

                    Player player;
                    if (players.TryGetValue(args.SenderConnection, out player))
                    {
                        collisions.RemoveProxy(player);
                    }

                    players.Remove(args.SenderConnection);
                    playerConnections.Remove(args.SenderConnection);

                    foreach (KeyValuePair <NetConnection, Player> to in players)
                    {
                        if (to.Key == args.SenderConnection)
                        {
                            continue;
                        }

                        Send(new DestroyRemotePlayer {
                            Index  = index,
                            Reason = 1 // ToDo
                        }, 3, to.Key, NetDeliveryMethod.ReliableSequenced, PacketChannels.Main);
                    }
                }
            }
        }
        private void OnClientStatusChanged(ClientStatusChangedEventArgs args)
        {
            if (args.Status == NetConnectionStatus.Connected)
            {
                PlayerClient player;

                lock (sync) {
                    lastPlayerIndex++;

                    if (players.TryGetValue(args.SenderConnection, out player))
                    {
                        player.Index = lastPlayerIndex;
                        playersByIndex[lastPlayerIndex] = player;
                    }

                    if (serverState == ServerState.LevelReady && playerSpawningEnabled)
                    {
                        // Take a new player another 60 seconds to load level
                        countdown       = 60f;
                        countdownNotify = int.MaxValue;
                    }
                }

                Log.Write(LogType.Verbose, "Player #" + player.Index + " (" + player.UserName + " @ " + args.SenderConnection.RemoteEndPoint + ") connected");

                if (currentLevel != null)
                {
                    Send(new LoadLevel {
                        ServerName          = serverName,
                        LevelName           = currentLevel,
                        LevelType           = currentLevelType,
                        AssignedPlayerIndex = lastPlayerIndex
                    }, 64, args.SenderConnection, NetDeliveryMethod.ReliableOrdered, PacketChannels.Main);
                }
            }
            else if (args.Status == NetConnectionStatus.Disconnected)
            {
                PlayerClient player;

                lock (sync) {
                    byte index = players[args.SenderConnection].Index;

                    if (players.TryGetValue(args.SenderConnection, out player))
                    {
                        //collisions.RemoveProxy(player.ShadowActor);

                        if (player.ProxyActor != null)
                        {
                            levelHandler.RemovePlayer(player.ProxyActor);
                        }
                    }

                    players.Remove(args.SenderConnection);
                    playersByIndex[index] = null;
                    playerConnections.Remove(args.SenderConnection);

                    foreach (KeyValuePair <NetConnection, PlayerClient> to in players)
                    {
                        if (to.Key == args.SenderConnection)
                        {
                            continue;
                        }

                        Send(new DestroyRemoteActor {
                            Index = index
                        }, 5, to.Key, NetDeliveryMethod.ReliableOrdered, PacketChannels.Main);
                    }
                }

                Log.Write(LogType.Verbose, "Player #" + player.Index + " (" + player.UserName + " @ " + args.SenderConnection.RemoteEndPoint + ") disconnected");
            }
        }
Пример #4
0
        private void OnMessage()
        {
            try {
                while (true)
                {
                    server.MessageReceivedEvent.WaitOne();

                    NetIncomingMessage msg;
                    while (server.ReadMessage(out msg))
                    {
                        switch (msg.MessageType)
                        {
                        case NetIncomingMessageType.StatusChanged: {
                            NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
#if DEBUG
                            Console.ForegroundColor = ConsoleColor.DarkCyan;
                            Console.Write("    S ");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine("[" + msg.SenderEndPoint + "] " + status);
#endif
                            ClientStatusChangedEventArgs args = new ClientStatusChangedEventArgs(msg.SenderConnection, status);
                            ClientStatusChanged?.Invoke(args);
                            break;
                        }

                        case NetIncomingMessageType.Data: {
#if DEBUG__
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.Write("    R ");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine("[" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes");
#endif

                            MessageReceivedEventArgs args = new MessageReceivedEventArgs(msg, false);
                            MessageReceived?.Invoke(args);

                            break;
                        }

                        case NetIncomingMessageType.UnconnectedData: {
#if DEBUG
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.Write("    R ");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine("Unconnected [" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes");
#endif

                            MessageReceivedEventArgs args = new MessageReceivedEventArgs(msg, true);
                            MessageReceived?.Invoke(args);

                            break;
                        }

                        case NetIncomingMessageType.ConnectionApproval: {
                            ClientConnectedEventArgs args = new ClientConnectedEventArgs(msg);
                            ClientConnected?.Invoke(args);

                            if (args.Allow)
                            {
                                msg.SenderConnection.Approve();
                            }
                            else
                            {
                                msg.SenderConnection.Deny("Incompatible version");
                            }
                            break;
                        }

                        case NetIncomingMessageType.DiscoveryRequest: {
#if DEBUG
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.Write("    Q ");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine("[" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes");
#endif

                            DiscoveryRequestEventArgs args = new DiscoveryRequestEventArgs();
                            DiscoveryRequest?.Invoke(args);

                            server.SendDiscoveryResponse(args.Message, msg.SenderEndPoint);
                            break;
                        }

#if DEBUG
                        case NetIncomingMessageType.VerboseDebugMessage:
                            Console.ForegroundColor = ConsoleColor.DarkGray;
                            Console.Write("    D ");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(msg.ReadString());
                            break;

                        case NetIncomingMessageType.DebugMessage:
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.Write("    D ");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(msg.ReadString());
                            break;

                        case NetIncomingMessageType.WarningMessage:
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write("    W ");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(msg.ReadString());
                            break;

                        case NetIncomingMessageType.ErrorMessage:
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("    E ");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(msg.ReadString());
                            break;
#endif
                        }

                        server.Recycle(msg);
                    }
                }
            } catch (ThreadAbortException) {
                // Server is stopped
            }
        }
Пример #5
0
        private void OnHandleMessagesThread()
        {
            while (server.Status != NetPeerStatus.NotRunning)
            {
                server.MessageReceivedEvent.WaitOne();

                NetIncomingMessage msg;
                while (server.ReadMessage(out msg))
                {
                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.StatusChanged: {
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
#if NETWORK_DEBUG
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                        Console.Write("    S ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine("[" + msg.SenderEndPoint + "] " + status);
#endif
                        ClientStatusChangedEventArgs args = new ClientStatusChangedEventArgs(msg.SenderConnection, status);
                        ClientStatusChanged?.Invoke(args);
                        break;
                    }

                    case NetIncomingMessageType.Data: {
#if NETWORK_DEBUG__
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.Write("    R ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine("[" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes");
#endif

                        MessageReceivedEventArgs args = new MessageReceivedEventArgs(msg, false);
                        MessageReceived?.Invoke(args);

                        break;
                    }

                    case NetIncomingMessageType.UnconnectedData: {
#if NETWORK_DEBUG
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.Write("    R ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine("Unconnected [" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes");
#endif

                        MessageReceivedEventArgs args = new MessageReceivedEventArgs(msg, true);
                        MessageReceived?.Invoke(args);

                        break;
                    }

                    case NetIncomingMessageType.ConnectionApproval: {
                        ClientConnectedEventArgs args = new ClientConnectedEventArgs(msg);
                        ClientConnected?.Invoke(args);

                        if (args.DenyReason == null)
                        {
                            msg.SenderConnection.Approve();
                        }
                        else
                        {
                            msg.SenderConnection.Deny(args.DenyReason);
                        }
                        break;
                    }

                    case NetIncomingMessageType.DiscoveryRequest: {
#if NETWORK_DEBUG
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.Write("    Q ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine("[" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes");
#endif

                        DiscoveryRequestEventArgs args = new DiscoveryRequestEventArgs();
                        DiscoveryRequest?.Invoke(args);

                        server.SendDiscoveryResponse(args.Message, msg.SenderEndPoint);
                        break;
                    }

#if NETWORK_DEBUG
                    case NetIncomingMessageType.VerboseDebugMessage:
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Console.Write("    D ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(msg.ReadString());
                        break;

                    case NetIncomingMessageType.DebugMessage:
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write("    D ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(msg.ReadString());
                        break;

                    case NetIncomingMessageType.WarningMessage:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("    W ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(msg.ReadString());
                        break;

                    case NetIncomingMessageType.ErrorMessage:
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("    E ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(msg.ReadString());
                        break;
#endif
                    }

                    server.Recycle(msg);
                }
            }

            server = null;

            Debug.WriteLine("ServerConnection: OnHandleMessagesThread exited!");
        }