Пример #1
0
        public static void SendScenarioModules(ClientObject client)
        {
            int numberOfScenarioModules = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Scenarios", client.playerName)).Length;
            int currentScenarioModule   = 0;

            string[] scenarioNames     = new string[numberOfScenarioModules];
            byte[][] scenarioDataArray = new byte[numberOfScenarioModules][];
            foreach (string file in Directory.GetFiles(Path.Combine(Server.universeDirectory, "Scenarios", client.playerName)))
            {
                //Remove the .txt part for the name
                scenarioNames[currentScenarioModule]     = Path.GetFileNameWithoutExtension(file);
                scenarioDataArray[currentScenarioModule] = File.ReadAllBytes(file);
                currentScenarioModule++;
            }
            ServerMessage newMessage = new ServerMessage();

            newMessage.type = ServerMessageType.SCENARIO_DATA;
            using (MessageWriter mw = new MessageWriter())
            {
                mw.Write <string[]>(scenarioNames);
                foreach (byte[] scenarioData in scenarioDataArray)
                {
                    if (client.compressionEnabled)
                    {
                        mw.Write <byte[]>(Compression.CompressIfNeeded(scenarioData));
                    }
                    else
                    {
                        mw.Write <byte[]>(Compression.AddCompressionHeader(scenarioData, false));
                    }
                }
                newMessage.data = mw.GetMessageBytes();
            }
            ClientHandler.SendToClient(client, newMessage, true);
        }
Пример #2
0
        public static void SendVessel(ClientObject client, string vesselGUID, byte[] vesselData)
        {
            ServerMessage newMessage = new ServerMessage();

            newMessage.type = ServerMessageType.VESSEL_PROTO;
            using (MessageWriter mw = new MessageWriter())
            {
                mw.Write <double>(0);
                mw.Write <string>(vesselGUID);
                mw.Write <bool>(false);
                mw.Write <bool>(false);
                if (client.compressionEnabled)
                {
                    mw.Write <byte[]>(Compression.CompressIfNeeded(vesselData));
                }
                else
                {
                    mw.Write <byte[]>(Compression.AddCompressionHeader(vesselData, false));
                }
                newMessage.data = mw.GetMessageBytes();
            }
            ClientHandler.SendToClient(client, newMessage, false);
        }
Пример #3
0
        // Send a specific scenario module to all clients - only called in shared science mode.
        public static void SendScenarioModuleToClients(string scenarioName, byte[] scenarioData)
        {
            string scenarioFile = Path.Combine(Server.universeDirectory, "Scenarios", "Shared", scenarioName + ".txt");

            string[] scenarioNames     = new string[1];
            byte[][] scenarioDataArray = new byte[1][];

            scenarioNames[0]     = scenarioName;
            scenarioDataArray[0] = File.ReadAllBytes(scenarioFile);

            ServerMessage newMessageComp = new ServerMessage();

            newMessageComp.type = ServerMessageType.SCENARIO_DATA;
            using (MessageWriter mw = new MessageWriter())
            {
                mw.Write <string[]>(scenarioNames);
                foreach (byte[] thisScenarioData in scenarioDataArray)
                {
                    mw.Write <byte[]>(Compression.CompressIfNeeded(thisScenarioData));
                }
                newMessageComp.data = mw.GetMessageBytes();
            }
            ServerMessage newMessage = new ServerMessage();

            newMessage.type = ServerMessageType.SCENARIO_DATA;
            using (MessageWriter mw = new MessageWriter())
            {
                mw.Write <string[]>(scenarioNames);
                foreach (byte[] thisScenarioData in scenarioDataArray)
                {
                    mw.Write <byte[]>(Compression.AddCompressionHeader(thisScenarioData, false));
                }
                newMessage.data = mw.GetMessageBytes();
            }
            ClientHandler.SendToAllAutoCompressed(null, newMessageComp, newMessage, true);
            DarkLog.Debug("Sent " + scenarioName + " to all players.");
        }
Пример #4
0
        public static void HandleVesselProto(ClientObject client, byte[] messageData)
        {
            //TODO: Relay the message as is so we can optimize it
            //Send vessel
            using (MessageReader mr = new MessageReader(messageData))
            {
                //Don't care about planet time
                double planetTime              = mr.Read <double>();
                string vesselGuid              = mr.Read <string>();
                bool   isDockingUpdate         = mr.Read <bool>();
                bool   isFlyingUpdate          = mr.Read <bool>();
                byte[] possibleCompressedBytes = mr.Read <byte[]>();
                byte[] vesselData              = Compression.DecompressIfNeeded(possibleCompressedBytes);
                if (isFlyingUpdate)
                {
                    DarkLog.Debug("Relaying FLYING vessel " + vesselGuid + " from " + client.playerName);
                }
                else
                {
                    if (!isDockingUpdate)
                    {
                        DarkLog.Debug("Saving vessel " + vesselGuid + " from " + client.playerName);
                    }
                    else
                    {
                        DarkLog.Debug("Saving DOCKED vessel " + vesselGuid + " from " + client.playerName);
                    }
                    lock (Server.universeSizeLock)
                    {
                        File.WriteAllBytes(Path.Combine(Server.universeDirectory, "Vessels", vesselGuid + ".txt"), vesselData);
                    }
                }

                ServerMessage newCompressedMessage   = null;
                ServerMessage newDecompressedMessage = null;
                if (Compression.BytesAreCompressed(possibleCompressedBytes))
                {
                    //Relay compressed message
                    newCompressedMessage      = new ServerMessage();
                    newCompressedMessage.type = ServerMessageType.VESSEL_PROTO;
                    newCompressedMessage.data = messageData;
                    //Build decompressed message.
                    newDecompressedMessage      = new ServerMessage();
                    newDecompressedMessage.type = ServerMessageType.VESSEL_PROTO;
                    using (MessageWriter mw = new MessageWriter())
                    {
                        mw.Write <double>(planetTime);
                        mw.Write <string>(vesselGuid);
                        mw.Write <bool>(isDockingUpdate);
                        mw.Write <bool>(isFlyingUpdate);
                        mw.Write <byte[]>(Compression.AddCompressionHeader(vesselData, false));
                        newDecompressedMessage.data = mw.GetMessageBytes();
                    }
                }
                else
                {
                    //Relay decompressed message
                    newDecompressedMessage      = new ServerMessage();
                    newDecompressedMessage.type = ServerMessageType.VESSEL_PROTO;
                    newDecompressedMessage.data = messageData;
                    //Build compressed message if the message is over the threshold.
                    //This should only happen if the client has disabled compression.
                    if (vesselData.Length > Common.COMPRESSION_THRESHOLD)
                    {
                        newCompressedMessage      = new ServerMessage();
                        newCompressedMessage.type = ServerMessageType.VESSEL_PROTO;
                        using (MessageWriter mw = new MessageWriter())
                        {
                            mw.Write <double>(planetTime);
                            mw.Write <string>(vesselGuid);
                            mw.Write <bool>(isDockingUpdate);
                            mw.Write <bool>(isFlyingUpdate);
                            mw.Write <byte[]>(Compression.CompressIfNeeded(vesselData));
                            newCompressedMessage.data = mw.GetMessageBytes();
                        }
                    }
                }
                ClientHandler.SendToAllAutoCompressed(client, newCompressedMessage, newDecompressedMessage, false);
            }
        }