/* * Function of clipSender thread. This thread will stay blocked waiting for a condition variable set, performed by the main thread * when the user perform a copy/cut operation. */ private void clipSenderFunction() { message msg = new message(); byte[] data = null; while (enableClipSender) /*This function runs until the Application is active or the user makes a click on StopServerButton.*/ { try { send.WaitOne(); /* Wait for a notification from the main thread */ if (!enableClipSender) /* If the main thread has set this boolean to false, this thread will terminate here. */ { break; } IPEndPoint remoteIpEndPoint = clientSocket.Client.RemoteEndPoint as IPEndPoint; /* IP address of the client actually connected */ msg.messType = messageType.REMOTE_COPY; /* The operation to perform is a RemoteCopy! */ if (Clipboard.ContainsText()) /* The clipboard contains text! */ { String text = Clipboard.GetText(); /* Here we get the text copied in the clipboard */ data = Encoding.Unicode.GetBytes(text); /* We have to get the byte array of the text, in order to send this through a socket. */ msg.cinf.ct = clipboardType.TEXT; /* We are sending text! */ } else if (Clipboard.ContainsImage()) /*The clipboard contains an Image!*/ { Image img = Clipboard.GetImage(); /*We have to get the image object..*/ data = SocketCommunication.serialize(img); /* and to serialize it! */ msg.cinf.ct = clipboardType.IMAGE; /*The data is an Image*/ } else if (Clipboard.ContainsAudio()) /* The clipboard contains an audio stream! */ { Stream audio = Clipboard.GetAudioStream(); /*We have to get the stream object..*/ data = SocketCommunication.serialize(audio); /* and to serialize it! */ msg.cinf.ct = clipboardType.AUDIO; /* The clipboard data is an audio stream! */ } else if (Clipboard.ContainsFileDropList()) /* The clipboard contains files! */ { string archPath = SocketCommunication.createArchive(); /*This function creates an archive with all files in the clipboard*/ FileInfo fi = new FileInfo(archPath); /* Object containing info about the archive */ if (fi.Length > 10 * 1024 * 1024) /* If the zip size is more than 10 MB */ { DialogResult confirmResult = MessageBox.Show("Sei davvero sicuro di voler trasferire più di 10 MB di dati?", "Conferma Trasferimento", MessageBoxButtons.YesNo); /* We have to show a confirm button, in order to avoid network overhead. */ if (confirmResult == DialogResult.No) /* If the user doesn't want to transfer the file.. */ { continue; /* We have to continue */ } } msg.cinf.ct = clipboardType.FILES; /* The clipboard data is a FileDropList! */ msg.cinf.size = (int)fi.Length; /* Size of the archive that is going to be sent */ using (clipClient = new TcpClient()) /* Socket used to send clipboard data to server */ { clipClient.Connect(remoteIpEndPoint.Address, 9999); /* We have to connect to the target server, port 9999 */ SocketCommunication.sendBufferToSocket(SocketCommunication.rawSerialize(msg), clipClient); SocketCommunication.sendFileToSocket(clipClient, archPath); } File.Delete(archPath); continue; } else { msg.cinf.ct = clipboardType.NO_VALID_DATA; data = new byte[4]; } if (data.Length > 10 * 1024 * 1024) /* If the data size is more than 10 MB */ { DialogResult confirmResult = MessageBox.Show("Sei davvero sicuro di voler trasferire più di 10 MB di dati?", "Conferma Trasferimento", MessageBoxButtons.YesNo); /* We have to show a confirm button, in order to avoid network overhead. */ if (confirmResult == DialogResult.No) /* If the user doesn't want to transfer the file.. */ { continue; /* We have to continue */ } } msg.cinf.size = data.Length; using (clipClient = new TcpClient()) { clipClient.Connect(remoteIpEndPoint.Address, 9999); SocketCommunication.sendBufferToSocket(SocketCommunication.rawSerialize(msg), clipClient); /*Struct Sending*/ SocketCommunication.sendChunks(clipClient, data); /* Data sending */ } } catch (SocketException) /* The exceptions that occurs will be catched here and the thread will not terminate! This thread has to remain active while the application ends */ { continue; } catch (IOException) { continue; } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
/* * Function of clipSender thread. This thread will stay blocked waiting for a condition variable set, performed by the main thread * when the user makes a double click on a server. */ private void clipSenderFunction() { byte[] data; while (true) /*This function runs until the Application is active.*/ { try { Invoke(new makeInvisible(makeInvisible), remotePasteProgressBar); /* If a remote paste progress bar is visible, we have to make it invisible */ send.WaitOne(); /*Wait for a notification from the main thread*/ Invoke(new makeVisible(makeVisible), remotePasteProgressBar); /* Because we have to send data toward the targetServer, we show the progressBar to inform the user on the transfer */ IPEndPoint remoteServerIpEndPoint = targetServer.Client.RemoteEndPoint as IPEndPoint; /*Get ip address of the server actually connected*/ message msg = new message(); msg.messType = messageType.REMOTE_PASTE; /* The operation to perform is a remote paste! */ if (Clipboard.ContainsText()) /* The clipboard contains text! */ { String text = Clipboard.GetText(); /* Here we get the text copied in the clipboard */ data = Encoding.Unicode.GetBytes(text); /* We have to get the byte array of the text, in order to send this through a socket. */ msg.cinf.ct = clipboardType.TEXT; /* We are sending text! */ msg.cinf.size = data.Length; /* We have to sent even the amount of text bytes in order to receive the whole array */ } else if (Clipboard.ContainsImage()) /*The clipboard contains an Image!*/ { Image img = Clipboard.GetImage(); /*We have to get the image object..*/ data = SocketCommunication.serialize(img); /* and to serialize it! */ msg.cinf.ct = clipboardType.IMAGE; /*The data is an Image*/ msg.cinf.size = data.Length; } else if (Clipboard.ContainsAudio()) /* The clipboard contains an audio stream! */ { Stream audio = Clipboard.GetAudioStream(); /*We have to get the stream object..*/ data = SocketCommunication.serialize(audio); /* and to serialize it! */ msg.cinf.ct = clipboardType.AUDIO; /* Tha clipboard data is an audio stream! */ msg.cinf.size = data.Length; } else if (Clipboard.ContainsFileDropList()) /* The clipboard contains files! */ { string archPath = SocketCommunication.createArchive(); /*This function creates an archive with all files in the clipboard*/ FileInfo fi = new FileInfo(archPath); /* Object containing info about the archive */ if (fi.Length > 10 * 1024 * 1024) /* If the zip size is more than 10 MB */ { DialogResult confirmResult = MessageBox.Show("Sei davvero sicuro di voler trasferire più di 10 MB di dati?", "Conferma Trasferimento", MessageBoxButtons.YesNo); /* We have to show a confirm button, in order to avoid network overhead. */ if (confirmResult == DialogResult.No) /* If the user doesn't want to transfer the file.. */ { continue; /* We have to continue */ } } msg.cinf.ct = clipboardType.FILES; /* The clipboard data is a FileDropList! */ msg.cinf.size = (int)fi.Length; /* Size of the archive that is going to be sent */ using (TcpClient clientSendData = new TcpClient()) /*Socket used to send clipboard data to server */ { clientSendData.Connect(remoteServerIpEndPoint.Address, 9998); /* We have to connect to the target server, port 9998 */ SocketCommunication.sendBufferToSocket(SocketCommunication.rawSerialize(msg), clientSendData); SocketCommunication.sendFileToSocket(clientSendData, archPath); clientSendData.Close(); } continue; } else { msg.cinf.ct = clipboardType.NO_VALID_DATA; data = new byte[4]; } if (data.Length > 10 * 1024 * 1024) /* If the clipboard data is bigger than 10 MB */ { DialogResult confirmResult = MessageBox.Show("Sei davvero sicuro di voler trasferire più di 10 MB di dati?", "Conferma Trasferimento", MessageBoxButtons.YesNo); /* We have to show a confirm button, in order to avoid network overhead. */ if (confirmResult == DialogResult.No) /* If the user doesn't want to transfer the file.. */ { continue; /* We have to continue */ } } using (TcpClient clientSendData = new TcpClient()) /*Socket used to send clipboard data to server */ { clientSendData.Connect(remoteServerIpEndPoint.Address, 9998); /* We have to connect to the target server, port 9998 */ SocketCommunication.sendBufferToSocket(SocketCommunication.rawSerialize(msg), clientSendData); /* Struct Sending */ SocketCommunication.sendChunks(clientSendData, data); /* Data sending */ clientSendData.Close(); } } catch (SocketException) /* The exceptions that occurs will be catched here and the thread will not terminate! This thread has to remain active while the application ends */ { continue; } catch (IOException) { continue; } catch (Exception e) { MessageBox.Show(e.Message); } } }