/* * Function executed by the clip receiver thread. */ private void clipReceiverFunction() { message msg; clipServer = new TcpListener(IPAddress.Any, 9998); /* Socket listening for clients connections. */ clipServer.Start(); /* The clip server starts */ byte[] data; while (enableClipServer) /* This boolean is modified by the main window when the user perform a click on the StopServerButton */ { try { using (clipReceiver = clipServer.AcceptTcpClient()) { /* Here we are receiving the message with the clipboard data type that we are going to receive */ data = SocketCommunication.receiveBufferFromSocket(clipReceiver); msg = SocketCommunication.rawDeserialize <message>(data); /* If the message is not a remote paste message, there is an error, and we must throw an exception. */ if (msg.messType != messageType.REMOTE_PASTE) { throw new Exception("Wrong message format!"); } switch (msg.cinf.ct) { case clipboardType.TEXT: /* We are going to receive text */ data = SocketCommunication.receiveChunks(clipReceiver, msg.cinf.size); /* Here we set the clipboard with the text received, encoding the received bytes with unicode format. */ Invoke(new txtSet(Clipboard.SetText), Encoding.Unicode.GetString(data)); /* Here we set the clipboard with the received text */ break; case clipboardType.IMAGE: /* We are going to receive an Image */ data = SocketCommunication.receiveChunks(clipReceiver, msg.cinf.size); /*Function that receives data, saving chuncks of 1024 Bytes*/ Image i = (Image)SocketCommunication.deserialize(data); /* Received bytes deserialization. The received object is of type Image*/ Invoke(new imgSet(Clipboard.SetImage), i); /* Here We set the clipboard with the received Image */ break; case clipboardType.FILES: /* FILES case. To sent multiple files, we have to compress all files in a zip Archive. Therefore, now we must unzip the received archive */ String path = SocketCommunication.receiveFile(clipReceiver, msg.cinf.size); /* This function returns the path of the received archive */ String dirName = Path.Combine(path, "temp"); /* Here we create the path to the folder that will contain all files */ if (!Directory.Exists(dirName)) /*If this folder doesn't exist...*/ { Directory.CreateDirectory(Path.Combine(dirName)); /* we have to create it! */ } else { Directory.Delete(dirName, true); /* The directory already exists, and probably contains files...*/ Directory.CreateDirectory(dirName); /* so we have to delete the directory and to recreate it, in order to save memory. */ } ZipFile.ExtractToDirectory(Path.Combine(path, "receivedArchive.zip"), dirName); /* We unzip the archive in the folder "ReceiveTemp" */ string[] sc = Directory.GetFileSystemEntries(dirName); /*This functions returns a string array with the filenames of each element in the new folder*/ StringCollection strColl = new StringCollection(); /* Clipboard.SetFileDropList requires a StringCollection object */ strColl.AddRange(sc); /* Here we add to strColl all strings contained in sc */ Invoke(new fileSet(Clipboard.SetFileDropList), strColl); /* We have to call this functions with invoke in order to set the main thread as clipboard owner */ break; case clipboardType.AUDIO: data = SocketCommunication.receiveChunks(clipReceiver, msg.cinf.size); /*Function that receives data, saving chuncks of 1024 Bytes*/ Stream audio = (Stream)SocketCommunication.deserialize(data); /* Received bytes deserialization. The received object is of type Stream*/ Invoke(new audioSet(Clipboard.SetAudio), audio); /* Here We set the clipboard with the received Audio stream */ break; } } } catch (SocketException) { continue; } catch (IOException) { continue; } catch (ObjectDisposedException) { continue; } catch (Exception e) { MessageBox.Show(e.Message); } } }
/* * Function that receives the clipboard data and sets the client clipboard with the received data. */ public void remoteCopy(TcpClient clipClient) { message msg; /* * Here we receive the struct that contains the data type that is going to arrive. */ byte[] data = SocketCommunication.receiveBufferFromSocket(clipClient); msg = SocketCommunication.rawDeserialize <message>(data); /* * If we do not receive a messageType == remote_copy, there is an error! */ if (msg.messType != messageType.REMOTE_COPY) { throw new Exception("Formato messaggio copia errato."); } switch (msg.cinf.ct) { case clipboardType.TEXT: /*The clipboard data is text*/ data = SocketCommunication.receiveChunks(clipClient, msg.cinf.size); /*Function that receives data, saving chuncks of 1024 Bytes*/ /* We have to call this functions with an invoke in order to set the main thread as clipboard owner */ Invoke(new txtSet(Clipboard.SetText), Encoding.Unicode.GetString(data)); /* Here we set the clipboard with the text received, encoding the received bytes with unicode format. */ break; case clipboardType.IMAGE: /* The clipboard data is an Image */ data = SocketCommunication.receiveChunks(clipClient, msg.cinf.size); /*Function that receives data, saving chuncks of 1024 Bytes*/ Image i = (Image)SocketCommunication.deserialize(data); /* Received bytes deserialization. The received object is of type Image*/ Invoke(new imgSet(Clipboard.SetImage), i); /* Here We set the clipboard with the received Image */ break; case clipboardType.FILES: /* FILES case. To sent multiple files, we have to compress all files in a zip Archive. Therefore, now we must unzip the received archive */ String path = SocketCommunication.receiveFile(clipClient, msg.cinf.size); /* This function returns the path of the received archive */ String dirName = Path.Combine(path, "ReceiveTemp"); /* Here we create the path to the folder that will contain all files */ if (!Directory.Exists(dirName)) /*If this folder doesn't exist...*/ { Directory.CreateDirectory(dirName); /* we have to create it! */ } else { Directory.Delete(dirName, true); /* The directory already exists, and probably contains files...*/ Directory.CreateDirectory(dirName); /* so we have to delete the directory and to recreate it, in order to save memory. */ } ZipFile.ExtractToDirectory(Path.Combine(path, "receivedArchive.zip"), dirName); /* We unzip the archive in the folder "ReceiveTemp" */ File.Delete(Path.Combine(path, "receivedArchive.zip")); /*Then we delete the received zip, in order to save memory.*/ string[] sc = Directory.GetFileSystemEntries(dirName); /*This functions returns a string array with the filenames of each element in the new folder*/ StringCollection strColl = new StringCollection(); /* Clipboard.SetFileDropList requires a StringCollection object */ strColl.AddRange(sc); /* Here we add to strColl all strings contained in sc */ Invoke(new fileSet(Clipboard.SetFileDropList), strColl); /* We have to call this functions with an invoke in order to set the main thread as clipboard owner */ break; case clipboardType.AUDIO: /* The clipboard data is an audio stream. */ data = SocketCommunication.receiveChunks(clipClient, msg.cinf.size); /*Function that receives data, saving chuncks of 1024 Bytes*/ Stream audio = (Stream)SocketCommunication.deserialize(data); /* Received bytes deserialization. The received object is of type Stream*/ Invoke(new audioSet(Clipboard.SetAudio), audio); /* Here We set the clipboard with the received Audio stream */ break; case clipboardType.NO_VALID_DATA: /*If the clipboard doesn't contain a valid data format, we receive this message.*/ data = SocketCommunication.receiveChunks(clipClient, msg.cinf.size); break; } }