public bool SendData(byte[] bytes, NetworkMessageSource source, NetworkMessageDestination destination, NetworkMessageTypes type, NetworkMessageStatus status) { byte[] compiled = new byte[8 + bytes.Length]; byte[] len = BitConverter.GetBytes(4 + bytes.Length); Buffer.BlockCopy(len, 0, compiled, 0, 4); compiled[4] = (byte)source; compiled[5] = (byte)destination; compiled[6] = (byte)type; compiled[7] = (byte)status; Buffer.BlockCopy(bytes, 0, compiled, 8, bytes.Length); if (keepalive) { try { stream.Write(compiled, 0, compiled.Length); stream.Flush(); return(true); } catch { InstanceProvider.GetServiceLogger().AppendLine("Error writing to network stream!"); return(false); } } return(false); }
public bool SendData(NetworkMessageSource source, NetworkMessageDestination destination, NetworkMessageTypes type, NetworkMessageStatus status) { if (SendData(new byte[0], source, destination, type, status)) { return(true); } return(false); }
public bool SendData(byte[] bytes, NetworkMessageSource source, NetworkMessageDestination destination, NetworkMessageTypes type) { if (SendData(bytes, source, destination, type, NetworkMessageStatus.None)) { return(true); } return(false); }
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."); }
public void ReceiveListener() { while (Connected) { try { byte[] buffer = new byte[4]; while (OpenedTcpClient != null && OpenedTcpClient.Client.Available != 0) { int byteCount = stream.Read(buffer, 0, 4); int expectedLen = BitConverter.ToInt32(buffer, 0); buffer = new byte[expectedLen]; byteCount = stream.Read(buffer, 0, expectedLen); NetworkMessageSource source = (NetworkMessageSource)buffer[0]; NetworkMessageDestination destination = (NetworkMessageDestination)buffer[1]; NetworkMessageTypes msgType = (NetworkMessageTypes)buffer[2]; NetworkMessageStatus msgStatus = (NetworkMessageStatus)buffer[3]; string data = GetString(buffer); if (destination != NetworkMessageDestination.Client) { continue; } int srvCurLen = 0; switch (source) { case NetworkMessageSource.Service: switch (msgType) { case NetworkMessageTypes.Connect: try { JsonParser message = JsonParser.Deserialize(data); if (message.Type == typeof(HostInfo)) { Console.WriteLine($"{message.Value}"); Console.WriteLine("Connection to Host successful!"); MainWindow.connectedHost = message.Value.ToObject <HostInfo>(); heartbeatFailTimeout = 0; HeartbeatThread = new Thread(new ThreadStart(SendHeatbeatSignal)) { IsBackground = true, Name = "HeartBeatThread" }; HeartbeatThread.Start(); } } catch (Exception e) { Console.WriteLine($"Error: ConnectMan reported error: {e.Message}\n{e.StackTrace}"); } break; case NetworkMessageTypes.Disconnect: break; case NetworkMessageTypes.Heartbeat: heartbeatRecieved = true; if (!HeartbeatThread.IsAlive) { HeartbeatThread = new Thread(new ThreadStart(SendHeatbeatSignal)); HeartbeatThread.IsBackground = true; HeartbeatThread.Name = "HeartBeatThread"; HeartbeatThread.Start(); Thread.Sleep(500); } break; } break; case NetworkMessageSource.Server: switch (msgType) { case NetworkMessageTypes.ConsoleLogUpdate: string[] strings = data.Split('|'); for (int i = 0; i < strings.Length; i++) { string[] srvSplit = strings[i].Split(';'); string srvName = srvSplit[0]; string srvText = srvSplit[1]; srvCurLen = int.Parse(srvSplit[2]); if (srvName != "Service") { ServerInfo bedrockServer = MainWindow.connectedHost.GetServerInfos().First(srv => srv.ServerName == srvName); bedrockServer.ConsoleBuffer = bedrockServer.ConsoleBuffer ?? new ServerLogger(bedrockServer.ServerName); int curCount = bedrockServer.ConsoleBuffer.Count(); if (curCount == srvCurLen) { bedrockServer.ConsoleBuffer.Append(srvText); } } else { int curCount = MainWindow.connectedHost.ServiceLog.Count; if (curCount == srvCurLen) { MainWindow.connectedHost.ServiceLog.Add(srvText); } } } break; case NetworkMessageTypes.Backup: Console.WriteLine(msgStatus.ToString()); break; } break; } } } catch (Exception e) { Console.WriteLine($"TCPClient error! Stacktrace: {e.Message}\n{e.StackTrace}"); } Thread.Sleep(200); } }