private void CloseHost(IVNC_Socket h) { if (h != null) { h.Dispose(); } }
private void Process_Connections(IVNC_Socket h, Func <IVNC_Socket, bool> process_func) { Debug.WriteLine("Connection attempt from " + h.ClientIpAddress + " to " + h.ClientPort); if (!process_func(h)) { Debug.WriteLine("Disconnecting " + h.ClientIpAddress); h.Dispose(); } }
private bool On_Server_Connect(IVNC_Socket h) { var id = GetID(h); if (id > -1) { return(Add(h, id, VNC_repeater.Utility.Host_Type.SERVER)); } return(false); }
private bool On_Viewer_Connect(IVNC_Socket h) { h.write(System.Text.Encoding.UTF8.GetBytes("RFB 000.000\n")); var id = GetID(h); if (id > -1) { return(Add(h, id, VNC_repeater.Utility.Host_Type.VIEWER)); } return(false); }
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 void write(IVNC_Socket n, byte[] buffer, int num_bytes) { if (num_bytes > 0) { if ((DateTime.Now - Second_Counter).TotalMilliseconds > 1000) { Second_Counter = DateTime.Now; _ThroughPut = 0; } Last_Time_Heard = DateTime.Now; n.write(buffer, num_bytes); _ThroughPut += num_bytes; _Total_Data_Transfered += num_bytes; } }
private int read(IVNC_Socket n, byte[] buffer) { if (n.Available) { if ((DateTime.Now - Second_Counter).TotalMilliseconds > 1000) { Second_Counter = DateTime.Now; _ThroughPut = 0; } Last_Time_Heard = DateTime.Now; var t = n.read(buffer); _ThroughPut += t; _Total_Data_Transfered += t; return(t); } return(0); }
//parses the id portion of what the host sends back and extracts it private int GetID(IVNC_Socket h) { int maxiterations = 0; while (!h.Available && maxiterations++ < 5) { System.Threading.Thread.Sleep(100); } // wait for response var bytes = new byte[250]; var i = h.read(bytes); if (i > 0) { return(Utility.ParseID(System.Text.Encoding.UTF8.GetString(bytes, 0, i))); } return(-1);//return an invalid id }
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); }
private void Process_PendingWebSockets() { while (KeepRunning) { try { IVNC_Socket h = null; if (Pending_WebSockets.TryDequeue(out h)) { Console.WriteLine("Processing websocket Connect Request"); Process_Connections(h, On_Viewer_Connect); } System.Threading.Thread.Sleep(20);//sleep for 20 milliseconds.. no need to be greedy } catch (Exception e) { Debug.WriteLine(e.Message); } } }