public void QueueWarpMessage(ByteArray messageData)
 {
     lock (newWarpMessages)
     {
         ByteArray queueByteArray = ByteRecycler.GetObject(messageData.Length);
         Array.Copy(messageData.data, 0, queueByteArray.data, 0, messageData.Length);
         newWarpMessages.Enqueue(queueByteArray);
     }
 }
Exemple #2
0
 public void QueueMessage(ByteArray data)
 {
     lock (messageQueue)
     {
         ByteArray queueByteArray = ByteRecycler.GetObject(data.Length);
         Array.Copy(data.data, 0, queueByteArray.data, 0, data.Length);
         messageQueue.Enqueue(queueByteArray);
     }
 }
 public void HandleModpackMessage(ByteArray messageData)
 {
     lock (messageQueue)
     {
         ByteArray queueByteArray = ByteRecycler.GetObject(messageData.Length);
         Array.Copy(messageData.data, 0, queueByteArray.data, 0, messageData.Length);
         messageQueue.Enqueue(queueByteArray);
     }
 }
 /// <summary>
 /// Queues to cache. This method is non-blocking, using SaveToCache for a blocking method.
 /// </summary>
 /// <param name="fileData">File data.</param>
 public void QueueToCache(ByteArray fileData)
 {
     lock (incomingQueue)
     {
         ByteArray incomingBytes = ByteRecycler.GetObject(fileData.Length);
         Array.Copy(fileData.data, 0, incomingBytes.data, 0, fileData.Length);
         incomingQueue.Enqueue(incomingBytes);
     }
     incomingEvent.Set();
 }
Exemple #5
0
        public object ReadByteArrayFromStream(Stream inputStream)
        {
            inputStream.Read(intReverser, 0, 4);
            ReverseIfLittleEndian(intReverser);
            int       length      = BitConverter.ToInt32(intReverser, 0);
            ByteArray returnBytes = ByteRecycler.GetObject(length);

            inputStream.Read(returnBytes.data, 0, length);
            return(returnBytes);
        }
        public ByteArray Serialize(ConfigNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            ByteArray retVal;
            int       retValSize = 0;

            //Call the insides of what ConfigNode would have called if we said Save(filename)
            using (MemoryStream stream = new MemoryStream(configNodeBuffer))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    //we late bind to the instance by passing the instance as the first argument
                    WriteNodeThunk(node, writer);
                    retValSize = (int)stream.Position;
                }
            }
            retVal = ByteRecycler.GetObject(retValSize);
            Array.Copy(configNodeBuffer, 0, retVal.data, 0, retValSize);
            return(retVal);
        }
        public void StartPlayback()
        {
            int       messagesLoaded = 0;
            bool      firstMessage   = true;
            ByteArray headerBytes    = ByteRecycler.GetObject(8);

            using (FileStream fs = new FileStream(recordPath, FileMode.Open))
            {
                while (fs.Position < fs.Length)
                {
                    messagesLoaded++;
                    fs.Read(headerBytes.data, 0, 8);
                    using (MessageReader mr = new MessageReader(headerBytes.data))
                    {
                        ClientMessageType messageType = (ClientMessageType)mr.Read <int>();
                        int       length    = mr.Read <int>();
                        ByteArray dataBytes = ByteRecycler.GetObject(length);
                        fs.Read(dataBytes.data, 0, length);
                        using (MessageReader timeReader = new MessageReader(dataBytes.data))
                        {
                            //Planet time is the first part of the message for the three types we care about here
                            double planetTime = timeReader.Read <double>();
                            lastTime = planetTime;
                            if (firstMessage)
                            {
                                firstTime    = planetTime;
                                firstMessage = false;
                                Planetarium.SetUniversalTime(planetTime - 5d);
                                warpWorker.SendNewSubspace();
                            }
                        }
                        using (MessageReader mrignore = new MessageReader(dataBytes.data))
                        {
                            //Planet time, don't care here
                            mrignore.Read <double>();
                            string vesselID = mrignore.Read <string>();
                            vesselWorker.IgnoreVessel(new Guid(vesselID));
                        }
                        switch (messageType)
                        {
                        case ClientMessageType.VESSEL_PROTO:
                            HandleProtoUpdate(dataBytes);
                            break;

                        case ClientMessageType.VESSEL_UPDATE:
                            HandleVesselUpdate(dataBytes, false);
                            break;

                        case ClientMessageType.VESSEL_REMOVE:
                            HandleVesselRemove(dataBytes);
                            break;

                        default:
                            break;
                        }
                        ByteRecycler.ReleaseObject(dataBytes);
                    }
                }
                ByteRecycler.ReleaseObject(headerBytes);
            }

            playbackQueue = new Queue <VesselUpdate>();
            using (FileStream fs = new FileStream(recordVectorPath, FileMode.Open))
            {
                while (fs.Position < fs.Length)
                {
                    fs.Read(headerBytesInt, 0, 4);
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(headerBytesInt);
                    }
                    int       updateLength = BitConverter.ToInt32(headerBytesInt, 0);
                    ByteArray updateBytes  = ByteRecycler.GetObject(updateLength);
                    fs.Read(updateBytes.data, 0, updateLength);
                    VesselUpdate vu = networkWorker.VeselUpdateFromBytes(updateBytes.data, false);
                    playbackQueue.Enqueue(vu);
                    ByteRecycler.ReleaseObject(updateBytes);
                }
            }

            ScreenMessages.PostScreenMessage("Loaded " + messagesLoaded + " saved updates.", 5f, ScreenMessageStyle.UPPER_CENTER);
            screenMessage = ScreenMessages.PostScreenMessage("Playback 0 / " + (int)(lastTime - firstTime) + " seconds.", float.MaxValue, ScreenMessageStyle.UPPER_CENTER);
            playback      = true;
        }
        private void RealHandleMessage(ByteArray messageData)
        {
            using (MessageReader mr = new MessageReader(messageData.data))
            {
                ModpackDataMessageType type = (ModpackDataMessageType)mr.Read <int>();
                switch (type)
                {
                case ModpackDataMessageType.CKAN:
                {
                    modpackMode = ModpackMode.CKAN;
                    ByteArray receiveData = mr.Read <ByteArray>();
                    ByteArray oldData     = null;
                    if (File.Exists(ckanDataPath))
                    {
                        using (FileStream fs = new FileStream(ckanDataPath, FileMode.Open))
                        {
                            oldData = ByteRecycler.GetObject((int)fs.Length);
                            fs.Read(oldData.data, 0, oldData.Length);
                        }
                    }
                    if (!BytesMatch(oldData, receiveData))
                    {
                        missingWarnFile = true;
                        DarkLog.Debug("Ckan file changed");
                        File.Delete(ckanDataPath);
                        using (FileStream fs = new FileStream(ckanDataPath, FileMode.OpenOrCreate))
                        {
                            fs.Write(receiveData.data, 0, receiveData.Length);
                        }
                    }
                    if (!registeredChatCommand)
                    {
                        registeredChatCommand = true;
                        chatWorker.RegisterChatCommand("upload", UploadCKANToServer, "Upload DarkMultiPlayer.ckan to the server");
                    }
                    if (oldData != null)
                    {
                        ByteRecycler.ReleaseObject(oldData);
                    }
                    ByteRecycler.ReleaseObject(receiveData);
                }
                break;

                case ModpackDataMessageType.MOD_LIST:
                {
                    modFilesToHash    = null;
                    modFilesToHashPos = 0;
                    serverPathCache.Clear();
                    noWarnSha.Clear();
                    modpackMode = ModpackMode.GAMEDATA;
                    string[] files = mr.Read <string[]>();
                    string[] sha   = mr.Read <string[]>();
                    if (File.Exists(gameDataServerCachePath))
                    {
                        File.Delete(gameDataServerCachePath);
                    }
                    using (StreamWriter sw = new StreamWriter(gameDataServerCachePath))
                    {
                        for (int i = 0; i < files.Length; i++)
                        {
                            bool skipFile = false;
                            foreach (string ignoreString in ignoreList)
                            {
                                if (files[i].ToLower().StartsWith(ignoreString, StringComparison.Ordinal))
                                {
                                    skipFile = true;
                                }
                            }
                            foreach (string ignoreString in containsIgnoreList)
                            {
                                if (files[i].ToLower().Contains(ignoreString))
                                {
                                    skipFile = true;
                                }
                            }
                            if (skipFile)
                            {
                                continue;
                            }
                            sw.WriteLine("{0}={1}", files[i], sha[i]);
                            serverPathCache.Add(files[i], sha[i]);
                        }
                    }
                    LoadAuto();
                    if (!registeredChatCommand)
                    {
                        registeredChatCommand = true;
                        chatWorker.RegisterChatCommand("upload", UploadToServer, "Upload GameData to the server");
                    }
                }
                break;

                case ModpackDataMessageType.REQUEST_OBJECT:
                {
                    modFilesToUpload    = mr.Read <string[]>();
                    modFilesToUploadPos = 0;
                    DarkLog.Debug("Server requested " + modFilesToUpload.Length + " files");
                }
                break;

                case ModpackDataMessageType.RESPONSE_OBJECT:
                {
                    string sha256sum = mr.Read <string>();
                    filesDownloaded++;
                    if (mr.Read <bool>())
                    {
                        syncString = "Syncing files " + filesDownloaded + "/" + requestList.Count + " (" + (serverPathCache.Count - requestList.Count) + " cached)";
                        ByteArray fileBytes = mr.Read <ByteArray>();
                        string    filePath  = Path.Combine(cacheDataPath, sha256sum + ".bin");
                        if (!File.Exists(filePath))
                        {
                            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
                            {
                                fs.Write(fileBytes.data, 0, fileBytes.Length);
                            }
                        }
                        ByteRecycler.ReleaseObject(fileBytes);
                    }
                    else
                    {
                        ScreenMessages.PostScreenMessage("DMP Server has an out of date hash list. Tell the admin to run /reloadmods", float.PositiveInfinity, ScreenMessageStyle.UPPER_CENTER);
                        networkWorker.Disconnect("Syncing files error");
                    }
                    if (filesDownloaded == requestList.Count)
                    {
                        if (missingWarnFile)
                        {
                            networkWorker.Disconnect("Syncing files " + filesDownloaded + "/" + requestList.Count + " (" + (serverPathCache.Count - requestList.Count) + " cached)");
                            ScreenMessages.PostScreenMessage("Please run DMPModpackUpdater or reconnect to ignore", float.PositiveInfinity, ScreenMessageStyle.UPPER_CENTER);
                        }
                        else
                        {
                            synced = true;
                        }
                    }
                }
                break;

                case ModpackDataMessageType.MOD_DONE:
                {
                    if ((!missingWarnFile && requestList.Count == 0) || secondModSync)
                    {
                        synced = true;
                    }
                    else
                    {
                        if (modpackMode == ModpackMode.CKAN)
                        {
                            ScreenMessages.PostScreenMessage("Please install CKAN update at KSP/DarkMultiPlayer.ckan or reconnect to ignore", float.PositiveInfinity, ScreenMessageStyle.UPPER_CENTER);
                            networkWorker.Disconnect("Synced DarkMultiPlayer.ckan");
                        }
                        if (modpackMode == ModpackMode.GAMEDATA && requestList.Count == 0)
                        {
                            ScreenMessages.PostScreenMessage("Please run DMPModpackUpdater or reconnect to ignore", float.PositiveInfinity, ScreenMessageStyle.UPPER_CENTER);
                            syncString = "Synced files (" + serverPathCache.Count + " cached)";
                            networkWorker.Disconnect(syncString);
                        }
                    }
                    secondModSync = true;
                }
                break;
                }
            }
        }