public static void AddRequest(IPAddress intefaceIpAddress, IPEndPoint serverPoint, IPEndPoint myConnectPoint) { TcpClient client = new TcpClient(new IPEndPoint(intefaceIpAddress, 0)); NetMessage msg = new NetMessage(); msg.typeRequest = NetMessage.Type.Add_Me_Request; msg.addMeData = new AddMeData(); msg.addMeData.ipAddress = myConnectPoint.Address.ToString(); msg.addMeData.port = myConnectPoint.Port.ToString(); client.Connect(serverPoint); NetFunctions.WriteString(client.GetStream(), NetMessage.ToJson(msg)); client.Close(); }
public void SendNetMessage(string msgJSON) { new Task(() => { if (ruler == null) { return; } lock (ruler) { if (resultOfConnection.IsCompleted && ruler != null) { NetFunctions.WriteString(ruler.GetStream(), msgJSON); } } }).Start(); }
public void CloseConnection() { if (ruler != null) { if (!resultOfConnection.IsCompleted) { resultOfConnection.AsyncWaitHandle.Close(); } else { //send close request NetMessage closeMsg = new NetMessage(); closeMsg.typeRequest = NetMessage.Type.Close; NetFunctions.WriteString(ruler.GetStream(), NetMessage.ToJson(closeMsg)); //read remoute clippboard data // MessageBox.Show("read clippboard text"); //new Action(() => MessageBox.Show("read clippboard text")).BeginInvoke(null, null); System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource(); var task = NetFunctions.ReadString(ruler.GetStream(), cts); if (task.Wait(10000)) //magic ten seconds { //MessageBox.Show("msg readed"); //new Action(() => MessageBox.Show("msg readed : " + task.Result)).BeginInvoke(null, null); //set new clippboard var msg = NetMessage.FromJson(task.Result); if (!string.IsNullOrEmpty(msg.textClippboard)) { //MessageBox.Show("clippboard not empty : " + msg.TextClippboard); //new Action(() => MessageBox.Show("clippboard not empty : " + msg.textClippboard)).BeginInvoke(null, null); Clipboard.SetText(msg.textClippboard); } } } resultOfConnection = null; ruler.Close(); ruler = null; } }
private void button1_Click(object sender, EventArgs e) { if (DvListBox.SelectedItem != null) { var ip = this.interfaces.GetIPAdressString(DvListBox.SelectedIndex); Listener lstnr = null; lstnr = new Listener(IPAddress.Parse(ip), (net, client) => { if (net.typeRequest == NetMessage.Type.Add_Me_Request) { return(addMeRequest(net, lstnr)); } if (net.typeRequest == NetMessage.Type.Control_Request) { return(controlRequest(net, lstnr.GetRemouteIPEndPoint())); } if (net.typeRequest == NetMessage.Type.None) { return(true); } if (net.typeRequest == NetMessage.Type.Close) { NetMessage msg = new NetMessage(); // MessageBox.Show("Recv Closse msg"); //new Action(() => MessageBox.Show("Recv Closse msg")).BeginInvoke(null, null); string text = null; SafeInvoke(this, new Action(() => { text = GetAnyClippBoardText(); })); if (!string.IsNullOrEmpty(text)) { //MessageBox.Show("Add text"); //new Action(() => MessageBox.Show("Add text")).BeginInvoke(null, null); msg.textClippboard = text; } else { //MessageBox.Show("add empty"); //new Action(() => MessageBox.Show("add empty")).BeginInvoke(null, null); msg.textClippboard = string.Empty; } string strmsg = NetMessage.ToJson(msg); //MessageBox.Show("Send : " + strmsg); //new Action(() => MessageBox.Show("Send : " + strmsg)).BeginInvoke(null, null); for (int i = 0; i < 5; ++i) //send 5 times for more safety { NetFunctions.WriteString(client.GetStream(), strmsg); } //MessageBox.Show("Invoke"); //new Action(() => MessageBox.Show("Invoke")).BeginInvoke(null, null); SafeInvoke(this, new Action(() => { notifyAboutRemouteControlling = false; controledByPanel.Visible = false; })); return(false); } return(false); }); lstnr.StartListener(); myListeners.Add(lstnr); connectPointsListBox.Items.Add(lstnr.ToString()); } else { MessageBox.Show("Please, select any device!"); } }