public byte[] OnDeployCommand(CoreNetworkServer server, CoreNetworkOpcode opcode, byte[] payload)
        {
            //Get the parameters
            CoreNetworkServerType serverType = (CoreNetworkServerType)BinaryTool.ReadInt16(payload, 0);
            string configString = Encoding.UTF8.GetString(payload, 4, payload.Length - 4);
            byte   count        = payload[2];

            //Validate that we support this
            if (!Program.server_types.ContainsKey(serverType))
            {
                return(CreateFailResponse("This server manager is not configured to support this type of server."));
            }

            //Get the config data
            JObject serverConfig;

            try
            {
                serverConfig = JsonConvert.DeserializeObject <JObject>(configString);
            } catch
            {
                return(CreateFailResponse("Config JSON is invalid."));
            }

            //Create servers
            List <ManagerInstance> instances = new List <ManagerInstance>();

            for (int i = 0; i < count; i++)
            {
                ManagerInstance instance;
                try
                {
                    instance = Program.server_types[serverType].CreateInstance(i == count - 1, serverConfig);
                }
                catch (Exception ex)
                {
                    return(CreateFailResponse($"Failed to create instance: {ex.Message} {ex.StackTrace}"));
                }

                //Start instance
                instance.StartProcess();
                instances.Add(instance);
            }

            //Validate instances is running
            Thread.Sleep(500);
            foreach (var i in instances)
            {
                if (!i.IsProcessRunning())
                {
                    return(CreateFailResponse($"A spawned instance ({i.settings.server_id}) ended early. More may have failed."));
                }
            }

            //Create response payload
            byte[] response = new byte[3];
            response[0] = 0x00;
            BinaryTool.WriteUInt16(response, 1, (ushort)instances[0].settings.server_id);
            return(response);
        }
Пример #2
0
        /// <summary>
        /// Writes a standard response: the only thing this will ever respond with
        /// </summary>
        /// <param name="index"></param>
        /// <param name="opcode"></param>
        /// <returns></returns>
        private async Task WriteResponse(ulong index, ushort opcode)
        {
            //This includes just 10 bytes:
            //  8 bytes: The index of the request
            //  2 bytes: The response opcode. 0 means that all is good

            //Create payload
            byte[] payload = new byte[10];
            BinaryTool.WriteUInt64(payload, 0, index);
            BinaryTool.WriteUInt16(payload, 8, opcode);

            //Send
            await SendData(payload);
        }