/// <summary> /// Приостанавливает раздачу /// </summary> public void Pause() { // Отлаживаем потоки ServerThread.Suspend(); //ClientThreadDownload.Suspend(); //ClientThreadUpload.Suspend(); MaterialState = MaterialStatus.Paused; // обновляем статус }
/// <summary> /// Останавливаем раздачу /// </summary> public void Stop() { // Снимаем потоки с выпонения ServerThread.Abort(); foreach (Connection c in connections) { c.Thread.Interrupt(); } MaterialState = MaterialStatus.Stoped; }
public void SetStatus() { MaterialState = MaterialStatus.Finished; for (int i = 0; i < Files.Length; i++) { for (int j = 0; j < Files[i].Count_Parts_; j++) { if (!Files[i].IsPart(j)) { MaterialState = MaterialStatus.Started; break; } } } }
void CheckOre(long gridId) { _oreStatus = new MaterialStatus(); var allItems = new List <IMyTerminalBlock>(); GridTerminalSystem.GetBlocksOfType(allItems, cargo => cargo.HasInventory & cargo.CubeGrid.EntityId == gridId); foreach (var block in allItems) { if (!(block is IMyReactor)) { CheckInventoryOfBlock(block, true); } } }
public Program() { _buttonEnabled = new bool[2][]; _buttonEnabled[0] = new bool[9]; _buttonEnabled[1] = new bool[9]; _buttonText = new string[2][]; _buttonText[0] = new string[9]; _buttonText[1] = new string[9]; _buttonData = new PressedButton[2][]; _buttonData[0] = new PressedButton[9]; _buttonData[1] = new PressedButton[9]; _oreStatus = new MaterialStatus(); _ignotStatus = new MaterialStatus(); // Find LCD Displays if (!InitDisplays()) { Echo("ERROR ON STARTUP: LCD ERROR"); return; } // Get Blocks we care about _cockpit = GridTerminalSystem.GetBlockWithName("ACockpit") as IMyCockpit; _assembler = GridTerminalSystem.GetBlockWithName("Assembler") as IMyAssembler; _oreCargo = GridTerminalSystem.GetBlockWithName("Ore Cargo Bin") as IMyCargoContainer; _ignotCargo = GridTerminalSystem.GetBlockWithName("Ignot Cargo Bin") as IMyCargoContainer; _materialCargo = GridTerminalSystem.GetBlockWithName("Material Cargo Bin") as IMyCargoContainer; _bay1Connector = GridTerminalSystem.GetBlockWithName("Bay 1 Connector") as IMyShipConnector; _bay2Connector = GridTerminalSystem.GetBlockWithName("Bay 2 Connector") as IMyShipConnector; _bay3Connector = GridTerminalSystem.GetBlockWithName("Bay 3 Connector") as IMyShipConnector; _bay4Connector = GridTerminalSystem.GetBlockWithName("Bay 4 Connector") as IMyShipConnector; ////Runtime.UpdateFrequency = UpdateFrequency.Update100; init = true; Echo("Startup complete"); StartWizard(); }
/// <summary> /// Конструктор, вызываемый при создании новой раздачи /// </summary> /// <param name="name">Имя раздачи</param> /// <param name="dir">Папка, в которой находится раздача</param> /// <param name="partsize">Размер части</param> /// <param name="server">Содержит IP и порт сервера, на котором зарегистрирован материал</param> /// public Material(string name, string dir, int partsize) { Name = name; PartSize = partsize; DirectoryInfo d = new DirectoryInfo(dir); List <string> files = GetFiles(d);// получаем список файлов Files = new File[files.Count]; string str_full = d.FullName.Substring(0, d.FullName.Length - d.Name.Length); for (int i = 0; i < files.Count; i++) { Files[i] = new File(str_full + files[i], files[i], partsize); } SetSize(); filese_that_need_to_download = new int[Files.Length]; cread_map_download(); MaterialState = MaterialStatus.Finished; }
/// <summary> /// Конструктор, вызываемый при создании новой раздачи /// </summary> /// <param name="name">Имя раздачи</param> /// <param name="dir">Папка, в которой находится раздача</param> /// <param name="partsize">Размер части</param> /// <param name="server">Содержит IP и порт сервера, на котором зарегистрирован материал</param> /// public Material(string name, string dir, int partsize) { Name = name; PartSize = partsize; DirectoryInfo d = new DirectoryInfo(dir); List<string> files = GetFiles(d);// получаем список файлов Files = new File[files.Count]; string str_full = d.FullName.Substring(0, d.FullName.Length - d.Name.Length); for (int i = 0; i < files.Count; i++) Files[i] = new File(str_full + files[i], files[i], partsize); SetSize(); filese_that_need_to_download = new int[Files.Length]; cread_map_download(); MaterialState = MaterialStatus.Finished; }
public void Download(object connection) { Connection con = (Connection)connection; Socket socket_download = con.Socket; byte[] id_file_to_dowmnload = new byte[4]; byte[] part_to_download = new byte[4]; lock (connections) connections.Add(con); Config.Connections++; byte[] comand_send = new byte[1]; byte[] comand_recive = new byte[1]; reconnect: comand_send[0] = (byte)C_C_Protocol.Connect; socket_download.Send(comand_send, 1, SocketFlags.None); session: socket_download.Receive(comand_recive, 1, SocketFlags.None); switch (comand_recive[0]) { case (byte)C_C_Protocol.Connected: goto Connected; case (byte)C_C_Protocol.MaterialResponse: goto MaterialResponse; case (byte)C_C_Protocol.SendPart: goto RecivePart; default: if (filese_that_need_to_download[BitConverter.ToInt32(id_file_to_dowmnload, 0)] != 2) { filese_that_need_to_download[BitConverter.ToInt32(id_file_to_dowmnload, 0)] = 1; } goto exit; } Connected: byte[] result = new byte[1]; socket_download.Receive(result, 1, SocketFlags.None); switch (result[0]) { case 0: goto exit; case 1: comand_send[0] = (byte)C_C_Protocol.IsMaterial; socket_download.Send(comand_send, 1, SocketFlags.None); byte[] id_mat = new byte[4]; id_mat = BitConverter.GetBytes(IdMaterial); socket_download.Send(id_mat, 4, SocketFlags.None); goto session; default: goto reconnect; } MaterialResponse: byte[] result2 = new byte[5]; socket_download.Receive(result2, 1, SocketFlags.None); switch (result2[0]) { case 0: goto exit; case 1: socket_download.Receive(result2, 4, SocketFlags.None); if (BitConverter.ToInt32(result2, 0) == IdMaterial) { comand_send[0] = (byte)C_C_Protocol.GetPart; socket_download.Send(comand_send, 1, SocketFlags.None); goto GetPart; } else { goto exit; } default: goto reconnect; } GetPart: bool isfile = false; for (int i = 0; i < filese_that_need_to_download.Length; i++) { lock (filese_that_need_to_download) { if (filese_that_need_to_download[i] == 1) { id_file_to_dowmnload = BitConverter.GetBytes(i); filese_that_need_to_download[i] = 3; isfile = true; break; } } } byte[] map_parts; if (isfile) { map_parts = Files[BitConverter.ToInt32(id_file_to_dowmnload, 0)].Get_Download_Bit_Map(); for (int i = 0; i < map_parts.Length; i++) { if (map_parts[i] == 0) { part_to_download = BitConverter.GetBytes(i); break; } } socket_download.Send(BitConverter.GetBytes(IdMaterial), 4, SocketFlags.None); socket_download.Send(id_file_to_dowmnload, 4, SocketFlags.None); socket_download.Send(part_to_download, 4, SocketFlags.None); goto session; } else { MaterialState = MaterialStatus.Finished; goto exit; } RecivePart: byte[] result3 = new byte[13]; byte[] resp = new byte[1]; socket_download.Receive(resp, 1, SocketFlags.None); switch (resp[0]) { case 0: goto exit; case 1: byte[] idmat_ = new byte[4]; byte[] idfile_ = new byte[4]; byte[] idpart = new byte[4]; socket_download.Receive(idmat_, 4, SocketFlags.None); socket_download.Receive(idfile_, 4, SocketFlags.None); socket_download.Receive(idpart, 4, SocketFlags.None); if (BitConverter.ToInt32(idmat_, 0) == IdMaterial && BitConverter.ToInt32(idfile_, 0) == BitConverter.ToInt32(id_file_to_dowmnload, 0) && BitConverter.ToInt32(idpart, 0) == BitConverter.ToInt32(part_to_download, 0)) { int size = Files[BitConverter.ToInt32(result3, 9)].Part_size; byte[] part = new byte[size]; long start_recive = DateTime.Now.Ticks; NetworkStream ns = new NetworkStream(socket_download); for (int i = 0; i != size; ) { i += ns.Read(part, i, size - i); } long end_recive = DateTime.Now.Ticks; speed_Download = size / (((int)end_recive - (int)start_recive) / 1000000); Stat_Download_ += size; //socket_download.Receive(part); Files[BitConverter.ToInt32(idfile_, 0)].Set_Part(BitConverter.ToInt32(idpart, 0), part); bool state_file = true; map_parts = Files[BitConverter.ToInt32(id_file_to_dowmnload, 0)].Get_Download_Bit_Map(); for (int i = 0; i < map_parts.Length; i++) { if (map_parts[i] == 0) { state_file = false; break; } } if (state_file) { lock (filese_that_need_to_download) { filese_that_need_to_download[BitConverter.ToInt32(id_file_to_dowmnload, 0)] = 2;//Скачан } goto exit; } else { lock (filese_that_need_to_download) { filese_that_need_to_download[BitConverter.ToInt32(id_file_to_dowmnload, 0)] = 1;//Недокачан } comand_send[0] = (byte)C_C_Protocol.GetPart; socket_download.Send(comand_send, 1, SocketFlags.None); goto GetPart; } } else { lock (filese_that_need_to_download) { filese_that_need_to_download[BitConverter.ToInt32(id_file_to_dowmnload, 0)] = 1;//Недокачан } goto reconnect; } default: goto reconnect; } exit: lock (connections) { con.Socket.Close(); connections.Remove(con); } }
public void Download(object connection) { Connection con = (Connection)connection; Socket socket_download = con.Socket; byte[] id_file_to_dowmnload = new byte[4]; byte[] part_to_download = new byte[4]; lock (connections) connections.Add(con); Config.Connections++; byte[] comand_send = new byte[1]; byte[] comand_recive = new byte[1]; reconnect: comand_send[0] = (byte)C_C_Protocol.Connect; socket_download.Send(comand_send, 1, SocketFlags.None); session: socket_download.Receive(comand_recive, 1, SocketFlags.None); switch (comand_recive[0]) { case (byte)C_C_Protocol.Connected: goto Connected; case (byte)C_C_Protocol.MaterialResponse: goto MaterialResponse; case (byte)C_C_Protocol.SendPart: goto RecivePart; default: if (filese_that_need_to_download[BitConverter.ToInt32(id_file_to_dowmnload, 0)] != 2) { filese_that_need_to_download[BitConverter.ToInt32(id_file_to_dowmnload, 0)] = 1; } goto exit; } Connected: byte[] result = new byte[1]; socket_download.Receive(result, 1, SocketFlags.None); switch (result[0]) { case 0: goto exit; case 1: comand_send[0] = (byte)C_C_Protocol.IsMaterial; socket_download.Send(comand_send, 1, SocketFlags.None); byte[] id_mat = new byte[4]; id_mat = BitConverter.GetBytes(IdMaterial); socket_download.Send(id_mat, 4, SocketFlags.None); goto session; default: goto reconnect; } MaterialResponse: byte[] result2 = new byte[5]; socket_download.Receive(result2, 1, SocketFlags.None); switch (result2[0]) { case 0: goto exit; case 1: socket_download.Receive(result2, 4, SocketFlags.None); if (BitConverter.ToInt32(result2, 0) == IdMaterial) { comand_send[0] = (byte)C_C_Protocol.GetPart; socket_download.Send(comand_send, 1, SocketFlags.None); goto GetPart; } else { goto exit; } default: goto reconnect; } GetPart: bool isfile = false; for (int i = 0; i < filese_that_need_to_download.Length; i++) { lock (filese_that_need_to_download) { if (filese_that_need_to_download[i] == 1) { id_file_to_dowmnload = BitConverter.GetBytes(i); filese_that_need_to_download[i] = 3; isfile = true; break; } } } byte[] map_parts; if (isfile) { map_parts = Files[BitConverter.ToInt32(id_file_to_dowmnload, 0)].Get_Download_Bit_Map(); for (int i = 0; i < map_parts.Length; i++) { if (map_parts[i] == 0) { part_to_download = BitConverter.GetBytes(i); break; } } socket_download.Send(BitConverter.GetBytes(IdMaterial), 4, SocketFlags.None); socket_download.Send(id_file_to_dowmnload, 4, SocketFlags.None); socket_download.Send(part_to_download, 4, SocketFlags.None); goto session; } else { MaterialState = MaterialStatus.Finished; goto exit; } RecivePart: byte[] result3 = new byte[13]; byte[] resp = new byte[1]; socket_download.Receive(resp, 1, SocketFlags.None); switch (resp[0]) { case 0: goto exit; case 1: byte[] idmat_ = new byte[4]; byte[] idfile_ = new byte[4]; byte[] idpart = new byte[4]; socket_download.Receive(idmat_, 4, SocketFlags.None); socket_download.Receive(idfile_, 4, SocketFlags.None); socket_download.Receive(idpart, 4, SocketFlags.None); if (BitConverter.ToInt32(idmat_, 0) == IdMaterial && BitConverter.ToInt32(idfile_, 0) == BitConverter.ToInt32(id_file_to_dowmnload, 0) && BitConverter.ToInt32(idpart, 0) == BitConverter.ToInt32(part_to_download, 0)) { int size = Files[BitConverter.ToInt32(result3, 9)].Part_size; byte[] part = new byte[size]; long start_recive = DateTime.Now.Ticks; NetworkStream ns = new NetworkStream(socket_download); for (int i = 0; i != size;) { i += ns.Read(part, i, size - i); } long end_recive = DateTime.Now.Ticks; speed_Download = size / (((int)end_recive - (int)start_recive) / 1000000); Stat_Download_ += size; //socket_download.Receive(part); Files[BitConverter.ToInt32(idfile_, 0)].Set_Part(BitConverter.ToInt32(idpart, 0), part); bool state_file = true; map_parts = Files[BitConverter.ToInt32(id_file_to_dowmnload, 0)].Get_Download_Bit_Map(); for (int i = 0; i < map_parts.Length; i++) { if (map_parts[i] == 0) { state_file = false; break; } } if (state_file) { lock (filese_that_need_to_download) { filese_that_need_to_download[BitConverter.ToInt32(id_file_to_dowmnload, 0)] = 2; //Скачан } goto exit; } else { lock (filese_that_need_to_download) { filese_that_need_to_download[BitConverter.ToInt32(id_file_to_dowmnload, 0)] = 1; //Недокачан } comand_send[0] = (byte)C_C_Protocol.GetPart; socket_download.Send(comand_send, 1, SocketFlags.None); goto GetPart; } } else { lock (filese_that_need_to_download) { filese_that_need_to_download[BitConverter.ToInt32(id_file_to_dowmnload, 0)] = 1; //Недокачан } goto reconnect; } default: goto reconnect; } exit: lock (connections) { con.Socket.Close(); connections.Remove(con); } }
// Check inventory/gather count. checkOre = check for ore, if false, check for ignots void CheckInventoryOfBlock(IMyTerminalBlock block, bool checkOre) { MaterialStatus currentStatus = _ignotStatus; string typeId = "MyObjectBuilder_Ignot"; if (checkOre) { currentStatus = _oreStatus; typeId = "MyObjectBuilder_Ore"; } var inventory = block.GetInventory(); foreach (var item in inventory.GetItems()) { if (item.Content.TypeId.ToString() == typeId) { switch (item.Content.SubtypeId.ToString()) { case "Iron": currentStatus.SetMaterial(ItemType.Iron); currentStatus.AddAmount(ItemType.Iron, (float)item.Amount.RawValue / 1000000); break; case "Nickel": currentStatus.SetMaterial(ItemType.Nickel); currentStatus.AddAmount(ItemType.Nickel, (float)item.Amount.RawValue / 1000000); break; case "Cobalt": currentStatus.SetMaterial(ItemType.Cobalt); currentStatus.AddAmount(ItemType.Cobalt, (float)item.Amount.RawValue / 1000000); break; case "Magnesium": currentStatus.SetMaterial(ItemType.Magnesium); currentStatus.AddAmount(ItemType.Magnesium, (float)item.Amount.RawValue / 1000000); break; case "Silicon": currentStatus.SetMaterial(ItemType.Silicon); currentStatus.AddAmount(ItemType.Silicon, (float)item.Amount.RawValue / 1000000); break; case "Silver": currentStatus.SetMaterial(ItemType.Silver); currentStatus.AddAmount(ItemType.Silver, (float)item.Amount.RawValue / 1000000); break; case "Gold": currentStatus.SetMaterial(ItemType.Gold); currentStatus.AddAmount(ItemType.Gold, (float)item.Amount.RawValue / 1000000); break; case "Platinum": currentStatus.SetMaterial(ItemType.Platinum); currentStatus.AddAmount(ItemType.Platinum, (float)item.Amount.RawValue / 1000000); break; case "Uranium": currentStatus.SetMaterial(ItemType.Uranium); currentStatus.AddAmount(ItemType.Uranium, (float)item.Amount.RawValue / 1000000); break; default: break; } } } }