public ListInstanceItem(SimInstance instance) { // empty context Instance = instance; Refresh(); }
public ListInstanceItem(OpCore core) { Core = core; UI = new CoreUI(core); Instance = core.Sim; Core.Exited += Core_Exited; Refresh(); }
public DeOpsContext(SimInstance sim, string startupPath, Icon defaultIcon) { StartupPath = startupPath; DefaultIcon = defaultIcon; LookupConfig = new LookupSettings(startupPath); // starting up simulated context context->simulator->instances[]->context Sim = sim; }
private void LoadDirectory(string dirpath) { Sim.LoadedPath = dirpath; if (Sim.UseTimeFile) { TimeFile = new FileStream(dirpath + Path.DirectorySeparatorChar + "time.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); if (TimeFile.Length >= 8) { byte[] startTime = new byte[8]; TimeFile.Read(startTime, 0, 8); Sim.TimeNow = DateTime.FromBinary(BitConverter.ToInt64(startTime, 0)); Sim.StartTime = Sim.TimeNow; } } string[] dirs = Directory.GetDirectories(dirpath); LoadProgress.Visible = true; LoadProgress.Maximum = dirs.Length; LoadProgress.Value = 0; foreach (string dir in dirs) { string[] paths = Directory.GetFiles(dir, "*.dop"); foreach (string path in paths) { string filename = Path.GetFileNameWithoutExtension(path); string[] parts = filename.Split('-'); string op = parts[0].Trim(); string name = parts[1].Trim(); // if instance with same user name, who has not joined this operation - add to same instance SimInstance instance = null; Sim.Instances.LockReading(() => instance = Sim.Instances.Where(i => i.Name == name && !i.Ops.Contains(op)).FirstOrDefault()); if (instance != null) { Login(instance, path); } else { Sim.StartInstance(path); } } LoadProgress.Value = LoadProgress.Value + 1; Application.DoEvents(); } LoadProgress.Visible = false; }
public void StartInstance(string path) { SimInstance instance = new SimInstance(this, InstanceCount++); instance.Context = new DeOpsContext(instance, StartupPath, DefaultIcon); // ip byte[] ipbytes = new byte[4] { (byte)RndGen.Next(99), (byte)RndGen.Next(99), (byte)RndGen.Next(99), (byte)RndGen.Next(99) }; if (LAN) { ipbytes[0] = 10; } else if (Utilities.IsLocalIP(new IPAddress(ipbytes))) { ipbytes[0] = (byte)RndGen.Next(30, 70); // make non lan ip } instance.RealIP = new IPAddress(ipbytes); // firewall instance.RealFirewall = FirewallType.Open; int num = RndGen.Next(100); if (num < PercentBlocked) { instance.RealFirewall = FirewallType.Blocked; } else if (num < PercentBlocked + PercentNAT) { instance.RealFirewall = FirewallType.NAT; } // processor instance.ThreadIndex = NextAffinity++; if (NextAffinity >= Environment.ProcessorCount) { NextAffinity = 0; } // hook instance into maps SimMap[instance.RealIP] = instance; if (LoadOnline) { Login(instance, path); } Instances.SafeAdd(instance); InstanceChange(instance, InstanceChangeType.Add); }
void OnInstanceChange(SimInstance instance, InstanceChangeType type) { if (Thread.CurrentThread.ManagedThreadId != UiThreadId) { BeginInvoke(Sim.InstanceChange, instance, type); return; } // add if (type == InstanceChangeType.Add) { AddItem(instance); } // refresh else if (type == InstanceChangeType.Refresh) { ListInstances.Items.Clear(); Sim.Instances.SafeForEach(i => { AddItem(i); }); LabelInstances.Text = Sim.Instances.SafeCount.ToString() + " Instances"; } // update else if (type == InstanceChangeType.Update) { foreach (ListInstanceItem item in ListInstances.Items) { if (item.Instance == instance) { item.Refresh(); break; } } } // remove else if (type == InstanceChangeType.Remove) { foreach (ListInstanceItem item in ListInstances.Items) { if (item.Instance == instance) { ListInstances.Items.Remove(item); break; } } } }
private void Login(SimInstance instance, string path) { try { Sim.Login(instance, path); } catch (Exception ex) { MessageBox.Show(this, instance.Name + ": " + ex.Message); return; } }
private void AddItem(SimInstance instance) { instance.Context.Cores.LockReading(delegate() { foreach (OpCore core in instance.Context.Cores) { ListInstances.Items.Add(new ListInstanceItem(core)); } if (instance.Context.Cores.Count == 0) { ListInstances.Items.Add(new ListInstanceItem(instance)); } }); }
public void Login(SimInstance instance, string path) { string filename = Path.GetFileNameWithoutExtension(path); string[] parts = filename.Split('-'); string op = parts[0].Trim(); string name = parts[1].Trim(); instance.Name = name; instance.Ops.Add(op); string pass = name.Split(' ')[0].ToLower(); // lowercase firstname instance.LastPath = path; instance.Context.LoadCore(path, pass); if (InstanceChange != null) { InstanceChange(instance, InstanceChangeType.Update); } }
// initializing lookup network (from the settings of a loaded operation) public OpCore(DeOpsContext context) { Context = context; Sim = context.Sim; StartTime = TimeNow; GuiProtocol = new G2Protocol(); Network = new DhtNetwork(this, true); // for each core, re-load the lookup cache items Context.Cores.LockReading(() => { foreach (OpCore core in Context.Cores) core.User.Load(LoadModeType.LookupCache); }); ServiceBandwidth[DhtServiceID] = new BandwidthLog(RecordBandwidthSeconds); // get cache from all loaded cores AddService(new LookupService(this)); if (Sim != null) Sim.Internet.RegisterAddress(this); CoreThread = new Thread(RunCore); CoreThread.Name = "Lookup Thread"; if (Sim == null || Sim.Internet.TestCoreThread) CoreThread.Start(); }
// initializing operation network public OpCore(DeOpsContext context, string userPath, string pass) { Context = context; Sim = context.Sim; StartTime = TimeNow; GuiProtocol = new G2Protocol(); User = new OpUser(userPath, pass, this); User.Load(LoadModeType.Settings); Network = new DhtNetwork(this, false); TunnelID = (ushort)RndGen.Next(1, ushort.MaxValue); Test test = new Test(); // should be empty unless running a test User.Load(LoadModeType.AllCaches); // delete data dirs if frsh start indicated if (Sim != null && Sim.Internet.FreshStart) for (int service = 1; service < 20; service++ ) // 0 is temp folder, cleared on startup { string dirpath = User.RootPath + Path.DirectorySeparatorChar + "Data" + Path.DirectorySeparatorChar + service.ToString(); if (Directory.Exists(dirpath)) Directory.Delete(dirpath, true); } if (Sim != null) KeyMax = 32; Context.KnownServices[DhtServiceID] = "Dht"; ServiceBandwidth[DhtServiceID] = new BandwidthLog(RecordBandwidthSeconds); // permanent - order is important here AddService(new TransferService(this)); AddService(new LocationService(this)); AddService(new LocalSync(this)); AddService(new BuddyService(this)); AddService(new UpdateService(this)); if (!User.Settings.GlobalIM) AddService(new TrustService(this)); // optional AddService(new IMService(this)); AddService(new ChatService(this)); AddService(new ShareService(this)); if (Type.GetType("Mono.Runtime") == null) AddService(new VoiceService(this)); if (!User.Settings.GlobalIM) { AddService(new ProfileService(this)); AddService(new MailService(this)); AddService(new BoardService(this)); AddService(new PlanService(this)); AddService(new StorageService(this)); } if (Sim != null) Sim.Internet.RegisterAddress(this); CoreThread = new Thread(RunCore); CoreThread.Name = User.Settings.Operation + " Thread"; if (Sim == null || Sim.Internet.TestCoreThread) CoreThread.Start(); #if DEBUG DebugWindowsActive = true; #endif }
public int SendPacket(SimPacketType type, DhtNetwork network, byte[] packet, System.Net.IPEndPoint target, TcpConnect tcp) { if (type == SimPacketType.Tcp) { if (!TcpEndPoints.ContainsKey(target)) { //this is what actually happens -> throw new Exception("Disconnected"); return(-1); } } if (!UdpEndPoints.ContainsKey(target) || !SimMap.ContainsKey(target.Address) || !SimMap.ContainsKey(network.Core.Sim.RealIP)) { return(0); } DhtNetwork targetNet = (type == SimPacketType.Tcp) ? TcpEndPoints[target] : UdpEndPoints[target]; if (network.IsLookup != targetNet.IsLookup) { Debug.Assert(false); } IPEndPoint source = new IPEndPoint(network.Core.Sim.RealIP, 0); source.Port = (type == SimPacketType.Udp) ? network.UdpControl.ListenPort : network.TcpControl.ListenPort; SimInstance sourceInstance = SimMap[source.Address]; SimInstance destInstance = SimMap[target.Address]; if (packet != null) { sourceInstance.BytesSent += (ulong)packet.Length; } // tcp connection must be present to send tcp if (type == SimPacketType.Tcp) { if (!TcpSourcetoDest.SafeContainsKey(tcp)) { //this is what actually happens -> throw new Exception("Disconnected"); return(-1); } } // add destination to nat table if (type == SimPacketType.Udp && sourceInstance.RealFirewall == FirewallType.NAT) { sourceInstance.NatTable[target] = true; } // if destination blocked drop udp / tcp connect requests if (destInstance.RealFirewall == FirewallType.Blocked) { if (type == SimPacketType.Udp || type == SimPacketType.TcpConnect) { return(0); } } // if destination natted drop udp (unless in nat table) / tcp connect requests else if (destInstance.RealFirewall == FirewallType.NAT) { if (type == SimPacketType.TcpConnect) { return(0); } if (type == SimPacketType.Udp && !destInstance.NatTable.ContainsKey(source)) { return(0); } } // randomly test tcp send buffer full if (TestTcpFullBuffer) { if (type == SimPacketType.Tcp && packet.Length > 4 && RndGen.Next(2) == 1) { int newlength = packet.Length / 2; byte[] newpacket = new byte[newlength]; Buffer.BlockCopy(packet, 0, newpacket, 0, newlength); packet = newpacket; } } if (packet != null && packet.Length == 0) { Debug.Assert(false, "Empty Packet"); } int index = destInstance.ThreadIndex; lock (OutPackets[index]) OutPackets[index].Add(new SimPacket(type, source, packet, targetNet, tcp, network.Local.UserID)); if (packet == null) { return(0); } return(packet.Length); }
public void ExitInstance(SimInstance instance) { SimMap.Remove(instance.RealIP); }
public void StartInstance(string path) { SimInstance instance = new SimInstance(this, InstanceCount++); instance.Context = new DeOpsContext(instance, StartupPath, DefaultIcon); // ip byte[] ipbytes = new byte[4] { (byte)RndGen.Next(99), (byte)RndGen.Next(99), (byte)RndGen.Next(99), (byte)RndGen.Next(99) }; if (LAN) ipbytes[0] = 10; else if (Utilities.IsLocalIP(new IPAddress(ipbytes))) ipbytes[0] = (byte)RndGen.Next(30, 70); // make non lan ip instance.RealIP = new IPAddress(ipbytes); // firewall instance.RealFirewall = FirewallType.Open; int num = RndGen.Next(100); if (num < PercentBlocked) instance.RealFirewall = FirewallType.Blocked; else if (num < PercentBlocked + PercentNAT) instance.RealFirewall = FirewallType.NAT; // processor instance.ThreadIndex = NextAffinity++; if (NextAffinity >= Environment.ProcessorCount) NextAffinity = 0; // hook instance into maps SimMap[instance.RealIP] = instance; if (LoadOnline) Login(instance, path); Instances.SafeAdd(instance); InstanceChange(instance, InstanceChangeType.Add); }
public void Login(SimInstance instance, string path) { string filename = Path.GetFileNameWithoutExtension(path); string[] parts = filename.Split('-'); string op = parts[0].Trim(); string name = parts[1].Trim(); instance.Name = name; instance.Ops.Add(op); string pass = name.Split(' ')[0].ToLower(); // lowercase firstname instance.LastPath = path; instance.Context.LoadCore(path, pass); if(InstanceChange != null) InstanceChange(instance, InstanceChangeType.Update); }