Пример #1
0
        private void CreateNewServerMgr(Gametype type)
        {
            mgr = new ServerMgr();
            mgr.Init(type, server);

            if (GameStartedCallback != null)
            {
                GameStartedCallback(type == Gametype.TOURNAMENT_GAME);
            }

            mgr.CloseCallback = ManagerClosed;

            Thread serverThread = new Thread(new ThreadStart(mgr.Run));

            serverThread.IsBackground = false;
            serverThread.Name         = "Server Thread";
            serverThread.Start();

            switch (type)
            {
            case Gametype.SOLO_GAME:
                // v solo se nastavi tournament settings primo pres herni aplikaci
                break;

            case Gametype.MULTIPLAYER_GAME:
            {
                TournamentSettings s = new TournamentSettings();
                s.MMType     = MatchManagerType.QUICK_GAME;
                s.Level      = GameLevel.BASIC_MAP;
                s.RoundCount = 1;
                s.BotCount   = 0;
                mgr.Enqueue(new Action(() => mgr.TournamentSettings = s));
            }
            break;

            case Gametype.TOURNAMENT_GAME:
                // v tournamentu se poslou setting pozdeji, az je hrac potvrdi
                break;
            }
        }
Пример #2
0
        private void GotMessage(object peer)
        {
            server = peer as NetServer;
            NetIncomingMessage msg = server.ReadMessage();

            switch (msg.MessageType)
            {
            case NetIncomingMessageType.VerboseDebugMessage:
            case NetIncomingMessageType.DebugMessage:
            case NetIncomingMessageType.WarningMessage:
            case NetIncomingMessageType.ErrorMessage:
                Logger.Debug(msg.ReadString());
                break;

            case NetIncomingMessageType.UnconnectedData:
                PacketType packetType = (PacketType)msg.ReadInt32();
                switch (packetType)
                {
                case PacketType.AVAILABLE_TOURNAMENTS_REQUEST:
                    if (!CheckVersion(msg.ReadString(), msg.SenderEndPoint))
                    {
                        break;
                    }

                    SendAvailableTournamentsResponse(msg.SenderEndPoint);
                    break;

                case PacketType.AVAILABLE_RECONNECT_REQUEST:
                    CheckAvailableReconnect(msg.SenderEndPoint, msg.ReadString());
                    break;
                }
                break;

            case NetIncomingMessageType.ConnectionApproval:
                if (msg.ReadInt32() != (int)PacketType.PLAYER_CONNECT)
                {
                    break;
                }

                string clientVersion = msg.ReadString();
                if (!CheckVersion(clientVersion, msg.SenderEndPoint))
                {
                    msg.SenderConnection.Deny("version mismatch");
                    break;
                }

                Gametype type         = (Gametype)msg.ReadByte();
                int      serverId     = msg.ReadInt32();
                string   name         = msg.ReadString();
                string   playerHashId = msg.ReadString();

                // v pripade ze jeho connection je uz prirazene k nejakemu manageru
                if (connections.TryGetValue(msg.SenderConnection, out mgr))
                {
                    mgr.Enqueue(new Action(() => mgr.PlayerConnectionApproval(msg, name, playerHashId)));
                    Logger.Info("Player " + name + " has manager assigned already (during ConnectionApproval)");
                    return;
                }

                if (PlayerConnectedCallback != null)
                {
                    PlayerConnectedCallback();
                }

                mgr = GetServerForReconnectionIfAny(playerHashId);

                // novy manager se vytvori pokud je zakladan novy turnaj (id 0) nebo pokud neni nalezen vhodny server
                if (mgr == null && ((type == Gametype.TOURNAMENT_GAME && serverId == 0) ||
                                    !FindAvailableServerManager(msg.SenderConnection, type, serverId)))
                {
                    CreateNewServerMgr(type);
                }

                connections.Add(msg.SenderConnection, mgr);
                mgr.Enqueue(new Action(() => mgr.PlayerConnectionApproval(msg, name, playerHashId)));
                return;     //musi se provest return, jinak je zprava zrecyklovana jeste pred zpracovanim, o recyklaci se stara ServerMgr

            case NetIncomingMessageType.Data:
                if (!connections.TryGetValue(msg.SenderConnection, out mgr))
                {
                    Logger.Warn("received data message from unknown connection -> skipped: " + msg.SenderConnection);
                    break;
                }
                mgr.EnqueueReceivedMessage(msg);
                return;     //musi se provest return, jinak je zprava zrecyklovana jeste pred zpracovanim, o recyklaci se stara ServerMgr

            case NetIncomingMessageType.StatusChanged:
                switch (msg.SenderConnection.Status)
                {
                case NetConnectionStatus.None:
                case NetConnectionStatus.InitiatedConnect:
                case NetConnectionStatus.RespondedAwaitingApproval:
                case NetConnectionStatus.RespondedConnect:
                    break;

                case NetConnectionStatus.Disconnected:
                case NetConnectionStatus.Disconnecting:
                    if (PlayerDisconnectedCallback != null)
                    {
                        PlayerDisconnectedCallback();
                    }

                    ServerMgr tempMgr = null;
                    if (!connections.TryGetValue(msg.SenderConnection, out tempMgr))
                    {
                        Logger.Warn("disconnecting unknown connection -> skipped: " + msg.SenderConnection);
                        break;
                    }

                    long uid = msg.SenderConnection.RemoteUniqueIdentifier;
                    tempMgr.Enqueue(new Action(() => tempMgr.PlayerDisconnected(uid)));
                    connections.Remove(msg.SenderConnection);
                    //msg.SenderConnection.Disconnect("x");
                    break;

                case NetConnectionStatus.Connected:
                    break;
                }

                // NOTE: Disconnecting and Disconnected are not instant unless client is shutdown with disconnect()
                //Logger.Debug(msg.SenderConnection.ToString() + " status changed to: " + msg.SenderConnection.Status);
                break;

            default:
                Logger.Debug("non-handled message type: " + msg.MessageType);
                break;
            }
            server.Recycle(msg);
        }