public bool Add(IVNC_Socket h, VNC_repeater.Utility.Host_Type t) { lock (_HostGuard) { if (Hosts[(int)t] == null) { Hosts[(int)t] = h; Last_Time_Heard = DateTime.Now; return(true); } } return(false); }
private bool Add(IVNC_Socket h, int id, VNC_repeater.Utility.Host_Type t) { Debug.WriteLine("Add " + Enum.GetName(t.GetType(), t) + " for id " + id); //first check to see if any hosts already connected with the same id and host type for (int i = 0; i < VNC_Proxy_Connections.Length; i++) { if (VNC_Proxy_Connections[i] == null) { continue; } var pair = VNC_Proxy_Connections[i]; if (pair.ID == id) { var ret = pair.Add(h, t);//this is atomic internally to guard against multiple threads working on the same object if (!ret) { Debug.WriteLine(id + " is already in used!"); } else { Debug.WriteLine("Pairing found, starting to service the pair: " + id); } return(ret); } } var unusedid = -1; Debug.WriteLine("First connect request with the id of " + id); if (Unused_IDs.TryDequeue(out unusedid)) { var tmp = new VNC_Pair(); tmp.Service_Running = true; tmp.ID = id; tmp.Add(h, t);//this should always succeed VNC_Proxy_Connections[unusedid] = tmp; System.Threading.Tasks.Task.Factory.StartNew(() => {//start a thread to service this connection and wait for a pairing tmp.Service_Connections(); }); return(true); } Debug.WriteLine("No more slots available to service the incomming connection request."); return(false); }