Ejemplo n.º 1
0
        private void IncomingListener()
        {
            keepalive = true;
            InstanceProvider.GetServiceLogger().AppendLine("Established connection! Listening for incoming packets!");
            int AvailBytes = 0;
            int byteCount  = 0;

            while (InstanceProvider.GetClientServiceAlive())
            {
                try
                {
                    byte[] buffer = new byte[4];
                    AvailBytes = client.Client.Available;
                    while (AvailBytes != 0) // Recieve data from client.
                    {
                        byteCount = stream.Read(buffer, 0, 4);
                        int expectedLen = BitConverter.ToInt32(buffer, 0);
                        buffer    = new byte[expectedLen];
                        byteCount = stream.Read(buffer, 0, expectedLen);
                        NetworkMessageSource      msgSource = (NetworkMessageSource)buffer[0];
                        NetworkMessageDestination msgDest   = (NetworkMessageDestination)buffer[1];
                        NetworkMessageTypes       msgType   = (NetworkMessageTypes)buffer[2];
                        NetworkMessageStatus      msgStatus = (NetworkMessageStatus)buffer[3];
                        string   data      = GetOffsetString(buffer);
                        string[] dataSplit = null;
                        switch (msgDest)
                        {
                        case NetworkMessageDestination.Server:

                            JsonParser message;
                            switch (msgType)
                            {
                            case NetworkMessageTypes.PropUpdate:

                                message = JsonParser.Deserialize(data);
                                List <Property> propList = message.Value.ToObject <List <Property> >();
                                Property        prop     = propList.First(p => p.KeyName == "server-name");
                                InstanceProvider.GetBedrockServer(prop.Value).serverInfo.ServerPropList = propList;
                                InstanceProvider.GetConfigManager().SaveServerProps(InstanceProvider.GetBedrockServer(prop.Value).serverInfo, true);
                                InstanceProvider.GetConfigManager().LoadConfigs();
                                InstanceProvider.GetBedrockServer(prop.Value).CurrentServerStatus = BedrockServer.ServerStatus.Stopping;
                                while (InstanceProvider.GetBedrockServer(prop.Value).CurrentServerStatus == BedrockServer.ServerStatus.Stopping)
                                {
                                    Thread.Sleep(100);
                                }
                                InstanceProvider.GetBedrockServer(prop.Value).StartControl(InstanceProvider.GetBedrockService()._hostControl);
                                SendData(NetworkMessageSource.Server, NetworkMessageDestination.Client, NetworkMessageTypes.PropUpdate);

                                break;

                            case NetworkMessageTypes.Restart:

                                RestartServer(data, false);
                                break;

                            case NetworkMessageTypes.Backup:

                                RestartServer(data, true);
                                break;

                            case NetworkMessageTypes.Command:

                                dataSplit = data.Split(';');
                                InstanceProvider.GetBedrockServer(dataSplit[0]).StdInStream.WriteLine(dataSplit[1]);
                                InstanceProvider.GetServiceLogger().AppendLine($"Sent command {dataSplit[1]} to stdInput stream");

                                break;
                            }
                            break;

                        case NetworkMessageDestination.Service:
                            switch (msgType)
                            {
                            case NetworkMessageTypes.Connect:

                                InstanceProvider.GetHostInfo().ServiceLog = InstanceProvider.GetServiceLogger().Log;
                                string jsonString    = JsonParser.Serialize(JsonParser.FromValue(InstanceProvider.GetHostInfo()));
                                byte[] stringAsBytes = GetBytes(jsonString);
                                SendData(stringAsBytes, NetworkMessageSource.Service, NetworkMessageDestination.Client, NetworkMessageTypes.Connect);
                                heartbeatRecieved = false;

                                break;

                            case NetworkMessageTypes.Disconnect:

                                DisconnectClient();

                                break;

                            case NetworkMessageTypes.Heartbeat:

                                if (InstanceProvider.GetHeartbeatThreadAlive())
                                {
                                    heartbeatRecieved = true;
                                }
                                else
                                {
                                    InstanceProvider.InitHeartbeatThread(new ThreadStart(SendBackHeatbeatSignal)).Start();
                                    Thread.Sleep(500);
                                    heartbeatRecieved = true;
                                }

                                break;

                            case NetworkMessageTypes.ConsoleLogUpdate:

                                StringBuilder srvString = new StringBuilder();
                                string[]      split     = data.Split('|');
                                for (int i = 0; i < split.Length; i++)
                                {
                                    dataSplit = split[i].Split(';');
                                    string srvName = dataSplit[0];
                                    int    srvTextLen;
                                    int    clientCurLen;
                                    int    loop;
                                    if (srvName != "Service")
                                    {
                                        ServerLogger srvText = InstanceProvider.GetBedrockServer(srvName).serverInfo.ConsoleBuffer;
                                        srvTextLen   = srvText.Count();
                                        clientCurLen = int.Parse(dataSplit[1]);
                                        loop         = clientCurLen;
                                        while (loop < srvTextLen)
                                        {
                                            srvString.Append($"{srvName};{srvText.FromIndex(loop)};{loop}|");
                                            loop++;
                                        }
                                    }
                                    else
                                    {
                                        ServiceLogger srvText = InstanceProvider.GetServiceLogger();
                                        srvTextLen   = srvText.Count();
                                        clientCurLen = int.Parse(dataSplit[1]);
                                        loop         = clientCurLen;
                                        while (loop < srvTextLen)
                                        {
                                            srvString.Append($"{srvName};{srvText.FromIndex(loop)};{loop}|");
                                            loop++;
                                        }
                                    }
                                }
                                if (srvString.Length > 1)
                                {
                                    srvString.Remove(srvString.Length - 1, 1);
                                    stringAsBytes = GetBytes(srvString.ToString());
                                    SendData(stringAsBytes, NetworkMessageSource.Server, NetworkMessageDestination.Client, NetworkMessageTypes.ConsoleLogUpdate);
                                }
                                break;
                            }
                            break;
                        }
                        AvailBytes = client.Client.Available;
                    }
                    Thread.Sleep(200);
                }
                catch (OutOfMemoryException)
                {
                    InstanceProvider.GetServiceLogger().AppendLine("");
                }
                catch (ObjectDisposedException e)
                {
                    InstanceProvider.GetServiceLogger().AppendLine("Client was disposed! Killing thread...");
                    break;
                }
                catch (ThreadAbortException) { }

                catch (JsonException e)
                {
                    InstanceProvider.GetServiceLogger().AppendLine($"Error parsing json array: {e.Message}");
                    InstanceProvider.GetServiceLogger().AppendLine($"Stacktrace: {e.InnerException}");
                }
                catch (Exception e)
                {
                    //InstanceProvider.GetServiceLogger().AppendLine($"Error: {e.Message} {e.StackTrace}");
                    //InstanceProvider.GetServiceLogger().AppendLine($"Error: {e.Message}: {AvailBytes}, {byteCount}\n{e.StackTrace}");
                }
                AvailBytes = client.Client.Available;
                if (InstanceProvider.GetClientService().ThreadState == ThreadState.Aborted)
                {
                    keepalive = false;
                }
            }
            InstanceProvider.GetServiceLogger().AppendLine("IncomingListener thread exited.");
        }