示例#1
0
        private void HandleWarpMessage(byte[] messageData)
        {
            using (MessageReader mr = new MessageReader(messageData))
            {
                WarpMessageType messageType = (WarpMessageType)mr.Read <int>();
                switch (messageType)
                {
                case WarpMessageType.REQUEST_VOTE:
                {
                    voteMaster = mr.Read <string>();
                    long expireTime = mr.Read <long>();
                    voteExpireTime = Client.realtimeSinceStartup + ((expireTime - timeSyncer.GetServerClock()) / 10000000d);
                }
                break;

                case WarpMessageType.REPLY_VOTE:
                {
                    int incomingVoteYesCount = mr.Read <int>();
                    int incomingVoteNoCount  = mr.Read <int>();
                    HandleReplyVote(incomingVoteYesCount, incomingVoteNoCount);
                }
                break;

                case WarpMessageType.SET_CONTROLLER:
                {
                    string newController = mr.Read <string>();
                    long   expireTime    = mr.Read <long>();
                    HandleSetController(newController, expireTime);
                }
                break;

                case WarpMessageType.CHANGE_WARP:
                {
                    string fromPlayer  = mr.Read <string>();
                    bool   isPhysWarp  = mr.Read <bool>();
                    int    rateIndex   = mr.Read <int>();
                    long   serverClock = mr.Read <long>();
                    double planetTime  = mr.Read <double>();
                    HandleChangeWarp(fromPlayer, isPhysWarp, rateIndex, serverClock, planetTime);
                }
                break;

                case WarpMessageType.NEW_SUBSPACE:
                {
                    int    newSubspaceID   = mr.Read <int>();
                    long   serverTime      = mr.Read <long>();
                    double planetariumTime = mr.Read <double>();
                    float  gameSpeed       = mr.Read <float>();
                    timeSyncer.AddNewSubspace(newSubspaceID, serverTime, planetariumTime, gameSpeed);
                }
                break;

                case WarpMessageType.CHANGE_SUBSPACE:
                {
                    string fromPlayer = mr.Read <string>();
                    if (fromPlayer != dmpSettings.playerName)
                    {
                        int changeSubspaceID = mr.Read <int>();
                        clientSubspaceList[fromPlayer] = changeSubspaceID;
                        if (changeSubspaceID != -1)
                        {
                            if (clientWarpList.ContainsKey(fromPlayer))
                            {
                                clientWarpList.Remove(fromPlayer);
                            }
                        }
                    }
                }
                break;

                case WarpMessageType.RELOCK_SUBSPACE:
                {
                    int    subspaceID      = mr.Read <int>();
                    long   serverTime      = mr.Read <long>();
                    double planetariumTime = mr.Read <double>();
                    float  gameSpeed       = mr.Read <float>();
                    timeSyncer.RelockSubspace(subspaceID, serverTime, planetariumTime, gameSpeed);
                }
                break;

                case WarpMessageType.REPORT_RATE:
                {
                    string fromPlayer = mr.Read <string>();
                    clientSkewList[fromPlayer] = mr.Read <float>();
                }
                break;

                default:
                {
                    DarkLog.Debug("Unhandled WARP_MESSAGE type: " + messageType);
                    break;
                }
                }
            }
        }
        public static void HandleWarpControl(ClientObject client, byte[] messageData)
        {
            ServerMessage newMessage = new ServerMessage();

            newMessage.type = ServerMessageType.WARP_CONTROL;
            newMessage.data = messageData;
            using (MessageReader mr = new MessageReader(messageData))
            {
                WarpMessageType warpType   = (WarpMessageType)mr.Read <int>();
                string          fromPlayer = mr.Read <string>();
                if (fromPlayer == client.playerName)
                {
                    if (warpType == WarpMessageType.NEW_SUBSPACE)
                    {
                        int newSubspaceID = mr.Read <int>();
                        if (subspaces.ContainsKey(newSubspaceID))
                        {
                            DarkLog.Debug("Kicked for trying to create an existing subspace");
                            Messages.ConnectionEnd.SendConnectionEnd(client, "Kicked for trying to create an existing subspace");
                            return;
                        }
                        else
                        {
                            Subspace newSubspace = new Subspace();
                            newSubspace.serverClock   = mr.Read <long>();
                            newSubspace.planetTime    = mr.Read <double>();
                            newSubspace.subspaceSpeed = mr.Read <float>();
                            subspaces.Add(newSubspaceID, newSubspace);
                            client.subspace = newSubspaceID;
                            SaveLatestSubspace();
                        }
                    }
                    if (warpType == WarpMessageType.CHANGE_SUBSPACE)
                    {
                        client.subspace = mr.Read <int>();
                    }
                    if (warpType == WarpMessageType.REPORT_RATE)
                    {
                        int reportedSubspace = mr.Read <int>();
                        if (client.subspace != reportedSubspace)
                        {
                            DarkLog.Debug("Warning, setting client " + client.playerName + " to subspace " + client.subspace);
                            client.subspace = reportedSubspace;
                        }
                        float newSubspaceRate = mr.Read <float>();
                        client.subspaceRate = newSubspaceRate;
                        foreach (ClientObject otherClient in ClientHandler.GetClients())
                        {
                            if (otherClient.authenticated && otherClient.subspace == reportedSubspace)
                            {
                                if (newSubspaceRate > otherClient.subspaceRate)
                                {
                                    newSubspaceRate = otherClient.subspaceRate;
                                }
                            }
                        }
                        if (newSubspaceRate < 0.3f)
                        {
                            newSubspaceRate = 0.3f;
                        }
                        if (newSubspaceRate > 1f)
                        {
                            newSubspaceRate = 1f;
                        }
                        //Relock the subspace if the rate is more than 3% out of the average
                        if (Math.Abs(subspaces[reportedSubspace].subspaceSpeed - newSubspaceRate) > 0.03f)
                        {
                            UpdateSubspace(reportedSubspace);
                            subspaces[reportedSubspace].subspaceSpeed = newSubspaceRate;
                            ServerMessage relockMessage = new ServerMessage();
                            relockMessage.type = ServerMessageType.WARP_CONTROL;
                            using (MessageWriter mw = new MessageWriter())
                            {
                                mw.Write <int>((int)WarpMessageType.RELOCK_SUBSPACE);
                                mw.Write <string>(Settings.settingsStore.consoleIdentifier);
                                mw.Write <int>(reportedSubspace);
                                mw.Write <long>(subspaces[reportedSubspace].serverClock);
                                mw.Write <double>(subspaces[reportedSubspace].planetTime);
                                mw.Write <float>(subspaces[reportedSubspace].subspaceSpeed);
                                relockMessage.data = mw.GetMessageBytes();
                            }
                            SaveLatestSubspace();
                            //DarkLog.Debug("Subspace " + client.subspace + " locked to " + newSubspaceRate + "x speed.");
                            ClientHandler.SendToClient(client, relockMessage, true);
                            ClientHandler.SendToAll(client, relockMessage, true);
                        }
                    }
                }
                else
                {
                    DarkLog.Debug(client.playerName + " tried to send an update for " + fromPlayer + ", kicking.");
                    Messages.ConnectionEnd.SendConnectionEnd(client, "Kicked for sending an update for another player");
                    return;
                }
            }
            ClientHandler.SendToAll(client, newMessage, true);
        }
示例#3
0
        private void HandleWarpMessage(byte[] messageData)
        {
            using (MessageReader mr = new MessageReader(messageData, false))
            {
                WarpMessageType messageType = (WarpMessageType)mr.Read <int>();
                string          fromPlayer  = mr.Read <string>();
                switch (messageType)
                {
                case WarpMessageType.REQUEST_VOTE:
                {
                    if (warpMode == WarpMode.MCW_VOTE)
                    {
                        if (voteMaster == "")
                        {
                            voteMaster = fromPlayer;
                        }
                        else
                        {
                            //Freak out and tell everyone to reset their warp votes - This can happen if 2 clients start a vote at the exact same time.
                            ReleaseWarpMaster();
                        }
                    }
                }
                break;

                case WarpMessageType.REPLY_VOTE:
                {
                    if (warpMode == WarpMode.MCW_VOTE)
                    {
                        if (voteMaster == Settings.fetch.playerName && warpMaster == "")
                        {
                            if (!voteList.ContainsKey(fromPlayer))
                            {
                                bool vote = mr.Read <bool>();
                                DarkLog.Debug(fromPlayer + " voted " + vote);
                                voteList.Add(fromPlayer, vote);
                            }
                            voteNoCount  = 0;
                            voteYesCount = 0;
                            foreach (KeyValuePair <string, bool> vote in voteList)
                            {
                                if (vote.Value)
                                {
                                    voteYesCount++;
                                }
                                else
                                {
                                    voteNoCount++;
                                }
                            }

                            //We have enough votes
                            if (voteYesCount >= voteNeededCount)
                            {
                                //Vote has passed.
                                warpMasterOwnerTime = UnityEngine.Time.realtimeSinceStartup;
                                warpMaster          = Settings.fetch.playerName;
                                using (MessageWriter mw = new MessageWriter())
                                {
                                    mw.Write <int>((int)WarpMessageType.SET_CONTROLLER);
                                    mw.Write <string>(Settings.fetch.playerName);
                                    mw.Write <string>(Settings.fetch.playerName);
                                    NetworkWorker.fetch.SendWarpMessage(mw.GetMessageBytes());
                                }
                            }
                            //We have enough votes
                            if (voteNoCount >= voteFailedCount)
                            {
                                //Vote has failed.
                                ReleaseWarpMaster();
                                DisplayMessage("Vote failed!", 5f);
                            }
                        }
                    }
                }
                break;

                case WarpMessageType.SET_CONTROLLER:
                {
                    if (warpMode == WarpMode.MCW_FORCE || warpMode == WarpMode.MCW_VOTE || warpMode == WarpMode.MCW_LOWEST)
                    {
                        string newController = mr.Read <string>();
                        warpMaster = newController;
                        if (warpMode == WarpMode.MCW_FORCE && newController == "")
                        {
                            warpMasterOwnerTime = 0f;
                        }
                        if (warpMode == WarpMode.MCW_VOTE && newController == "")
                        {
                            TimeWarp.SetRate(0, true);
                            CancelVote();
                        }
                    }
                }
                break;

                case WarpMessageType.CHANGE_WARP:
                {
                    if (warpMode == WarpMode.MCW_FORCE || warpMode == WarpMode.MCW_VOTE || warpMode == WarpMode.MCW_LOWEST || warpMode == WarpMode.SUBSPACE)
                    {
                        bool newPhysWarp  = mr.Read <bool>();
                        int  newRateIndex = mr.Read <int>();
                        if (clientWarpList.ContainsKey(fromPlayer))
                        {
                            clientWarpList[fromPlayer].isPhysWarp = newPhysWarp;
                            clientWarpList[fromPlayer].rateIndex  = newRateIndex;
                        }
                        else
                        {
                            PlayerWarpRate newPlayerWarpRate = new PlayerWarpRate();
                            newPlayerWarpRate.isPhysWarp = newPhysWarp;
                            newPlayerWarpRate.rateIndex  = newRateIndex;
                            clientWarpList.Add(fromPlayer, newPlayerWarpRate);
                        }
                        //DarkLog.Debug(fromPlayer + " warp rate changed, Physwarp: " + newPhysWarp + ", Index: " + newRateIndex);
                    }
                }
                break;

                case WarpMessageType.NEW_SUBSPACE:
                {
                    int    newSubspaceID   = mr.Read <int>();
                    long   serverTime      = mr.Read <long>();
                    double planetariumTime = mr.Read <double>();
                    float  gameSpeed       = mr.Read <float>();
                    TimeSyncer.fetch.LockNewSubspace(newSubspaceID, serverTime, planetariumTime, gameSpeed);
                    if (((warpMode == WarpMode.MCW_VOTE) || (warpMode == WarpMode.MCW_FORCE)) && (warpMaster == fromPlayer))
                    {
                        TimeSyncer.fetch.LockSubspace(newSubspaceID);
                    }
                    if (!TimeSyncer.fetch.locked && TimeSyncer.fetch.currentSubspace == newSubspaceID)
                    {
                        TimeSyncer.fetch.LockSubspace(newSubspaceID);
                    }
                }
                break;

                case WarpMessageType.CHANGE_SUBSPACE:
                {
                    int changeSubspaceID = mr.Read <int>();
                    clientSubspaceList[fromPlayer] = changeSubspaceID;
                }
                break;

                case WarpMessageType.RELOCK_SUBSPACE:
                {
                    if (fromPlayer == "Server")
                    {
                        int    subspaceID      = mr.Read <int>();
                        long   serverTime      = mr.Read <long>();
                        double planetariumTime = mr.Read <double>();
                        float  gameSpeed       = mr.Read <float>();
                        TimeSyncer.fetch.RelockSubspace(subspaceID, serverTime, planetariumTime, gameSpeed);
                    }
                }
                break;

                case WarpMessageType.REPORT_RATE:
                {
                    //Not interested in subspace.
                    mr.Read <int>();
                    clientSkewList[fromPlayer] = mr.Read <float>();
                }
                break;

                default:
                {
                    DarkLog.Debug("Unhandled WARP_MESSAGE type: " + messageType);
                    break;
                }
                }
            }
        }
示例#4
0
        public static void HandleWarpControl(ClientObject client, byte[] messageData)
        {
            using (MessageReader mr = new MessageReader(messageData))
            {
                WarpMessageType warpType = (WarpMessageType)mr.Read <int>();
                switch (warpType)
                {
                case WarpMessageType.REQUEST_CONTROLLER:
                {
                    HandleRequestController(client);
                }
                break;

                case WarpMessageType.RELEASE_CONTROLLER:
                {
                    HandleReleaseController(client);
                }
                break;

                case WarpMessageType.REPLY_VOTE:
                {
                    bool voteReply = mr.Read <bool>();
                    HandleReplyVote(client, voteReply);
                }
                break;

                case WarpMessageType.NEW_SUBSPACE:
                {
                    long   serverTime   = mr.Read <long>();
                    double planetTime   = mr.Read <double>();
                    float  subspaceRate = mr.Read <float>();
                    HandleNewSubspace(client, serverTime, planetTime, subspaceRate);
                }
                break;

                case WarpMessageType.CHANGE_SUBSPACE:
                {
                    int newSubspace = mr.Read <int>();
                    HandleChangeSubspace(client, newSubspace);
                }
                break;

                case WarpMessageType.REPORT_RATE:
                {
                    float newSubspaceRate = mr.Read <float>();
                    HandleReportRate(client, newSubspaceRate);
                }
                break;

                case WarpMessageType.CHANGE_WARP:
                {
                    bool   physWarp    = mr.Read <bool>();
                    int    rateIndex   = mr.Read <int>();
                    long   serverClock = mr.Read <long>();
                    double planetTime  = mr.Read <double>();
                    HandleChangeWarp(client, physWarp, rateIndex, serverClock, planetTime);
                }
                break;

                        #if DEBUG
                default:
                    throw new NotImplementedException("Warp type not implemented");
                        #endif
                }
            }
        }