private void NewReceivedFile_ReceivedFileFinishedEvent(ReceivedFile file) { file.ReceivedFileFinishedEvent -= NewReceivedFile_ReceivedFileFinishedEvent; lock (syncRoot) { if (receivedFilesDict.ContainsKey(file.SourceInfo)) { Dictionary <string, ReceivedFile> connReceivedFileDict = receivedFilesDict[file.SourceInfo]; ReceivedFile connReceivedFile = connReceivedFileDict[file.Filename]; if (!Directory.Exists("plugins")) { Directory.CreateDirectory("plugins"); } string onlyFileName = Path.GetFileNameWithoutExtension(connReceivedFile.Filename); string randomGuid = Guid.NewGuid().ToString().Substring(0, 5); string saveFilePath = Path.Combine("plugins", onlyFileName + randomGuid + ".dll"); connReceivedFile.SaveFileToDisk(saveFilePath); connReceivedFile.Close(); lock (receivedFilesDict) { receivedFilesDict.Remove(file.SourceInfo); } moduleManager.RegisterModule(saveFilePath); } } }
private void FillFile(ReceivedFile receivedFile) { Name.Text = receivedFile.Name; OpenFile.Visibility = ViewStates.Visible; ShareFile.Visibility = ViewStates.Visible; IsFolder = false; }
public void Index(ReceivedFile incomingFiles) { try { IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0]; IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, 1800); Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listenSocket.Bind(ipEndpoint); listenSocket.Listen(1); IAsyncResult asyncAccept = listenSocket.BeginAccept(new AsyncCallback(HomeController.acceptCallback), listenSocket); } catch (Exception ee) { } }
/// <summary> /// Handles an incoming packet of type 'PartialFileData' /// </summary> /// <param name="header">Header associated with incoming packet</param> /// <param name="connection">The connection associated with incoming packet</param> /// <param name="data">The incoming data</param> private void IncomingPartialFileData(PacketHeader header, Connection connection, byte[] data) { // ######################################################################## // This method requires authentication. // If user is not authorized then send UnAuthorized and end method. if (!accountManager.Authorized(connection)) { TCPConnection.GetConnection(connection.ConnectionInfo).SendObject( PacketName.ReUnauthorized.ToString(), 1); return; } // ######################################################################## try { SendInfo info = null; ReceivedFile file = null; //Perform this in a thread safe way lock (syncRoot) { //Extract the packet sequence number from the header //The header can also user defined parameters long sequenceNumber = header.GetOption(PacketHeaderLongItems.PacketSequenceNumber); if (incomingDataInfoCache.ContainsKey(connection.ConnectionInfo) && incomingDataInfoCache[connection.ConnectionInfo].ContainsKey(sequenceNumber)) { //We have the associated SendInfo so we can add this data directly to the file info = incomingDataInfoCache[connection.ConnectionInfo][sequenceNumber]; incomingDataInfoCache[connection.ConnectionInfo].Remove(sequenceNumber); //Check to see if we have already received any files from this location if (!receivedFilesDict.ContainsKey(connection.ConnectionInfo)) { receivedFilesDict.Add(connection.ConnectionInfo, new Dictionary <string, ReceivedFile>()); } //Check to see if we have already initialised this file if (!receivedFilesDict[connection.ConnectionInfo].ContainsKey(info.Filename)) { ReceivedFile newReceivedFile = new ReceivedFile(info.Filename, connection.ConnectionInfo, info.TotalBytes); newReceivedFile.ReceivedFileFinishedEvent += NewReceivedFile_ReceivedFileFinishedEvent; receivedFilesDict[connection.ConnectionInfo].Add(info.Filename, newReceivedFile); } file = receivedFilesDict[connection.ConnectionInfo][info.Filename]; } else { //We do not yet have the associated SendInfo so we just add the data to the cache if (!incomingDataCache.ContainsKey(connection.ConnectionInfo)) { incomingDataCache.Add(connection.ConnectionInfo, new Dictionary <long, byte[]>()); } incomingDataCache[connection.ConnectionInfo].Add(sequenceNumber, data); } } //If we have everything we need we can add data to the ReceivedFile if (info != null && file != null && !file.IsCompleted) { file.AddData(info.BytesStart, 0, data.Length, data); //Perform a little clean-up file = null; data = null; GC.Collect(); } else if (info == null ^ file == null) { throw new Exception("Either both are null or both are set. Info is " + (info == null ? "null." : "set.") + " File is " + (file == null ? "null." : "set.") + " File is " + (file.IsCompleted ? "completed." : "not completed.")); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); //If an exception occurs we write to the log window and also create an error file // AddLineToLog("Exception - " + ex.ToString()); // LogTools.LogException(ex, "IncomingPartialFileDataError"); } }
/// <summary> /// Connect to a NBSP Master Server and Download Data /// </summary> /// <param name="MasterServerDNS">Specify the DNS Name for the Master Server on the NOAAPORT Broadcast System</param> /// <param name="FileReceivedCallback">Specify the callback to use when a file is successfully received from NOAAPORT</param> public static void OpenConnection(string MasterServerDNS, ReceivedFile FileReceivedCallback) { // Specify the TCP Client TcpClient nbsp = new TcpClient(); // Connect to a Tier Server nbsp.Client.Connect(MasterServerDNS, 2210); if (nbsp.Connected) { Console.WriteLine("[NBSPClient] Success! Connecting as NBS1 Slave!"); } // Connect to NOAAPORT NetworkStream ns = nbsp.GetStream(); try { do { // Advertise to the NBSP Server we are a NBS1 Slave Server byte[] strProtocol = System.Text.Encoding.ASCII.GetBytes("NBS1"); for (int i = 0; i < strProtocol.Length; i++) { ns.WriteByte(strProtocol[i]); } // Tell user if we are connected to a NBSP Server as NBS1 Slave Console.WriteLine("[NBSPClient] Connected as NBS1 Slave Server!"); // Track bytes and loops int bytes = 0; // Read the header for each NOAAPORT Broadcast // through the NBSP system. byte[] buffer = new byte[66]; bytes = ns.Read(buffer, 0, buffer.Length); // Keep track of the bytes we are reading for each // NOAAPORT product int p = 0; // Check the NBSP Server's Data Identifier. long data_id = unpack_uint32(buffer, 0); // Check to make sure NBSP Server provided us with a correct NOAAPORT // product. NBSP provides data identifier of 1 if (data_id == 1) { // Increase here. We don't need these 12 bytes. p += 12; // Determine the Sequence Number of Products long seq_number = unpack_uint32(buffer, p); p += 4; // Determine the NOAAPORT Product Type byte product_type = buffer[p]; p++; // Determine the NOAAPORT Product Category byte product_category = buffer[p]; p++; // Determine the NOAAPORT product code byte product_code = buffer[p]; p++; // Determine which NOAAPORT Channel this product is from // Example Channel 1 is mostly dedicated to text based products // from the National Weather Service byte np_channel_index = buffer[p]; p++; // Get the filename NOAAPORT wishes to use from the server string strFileName = System.Text.Encoding.ASCII.GetString(buffer, p, 37); p += 37; // Determine if the product is compressed from the NOAAPORT Stream byte f_zip = buffer[p]; p++; // Read which block we currently are on from the NOAAPORT Stream uint num_block = unpack_uint16(buffer, p); p += 2; // Read the current block number (how many blocks make up this product) uint block_number = unpack_uint16(buffer, p); p += 2; // Read the NOAAPORT Block Size long block_size = unpack_uint32(buffer, p); p += 4; // Read the data from the network stream if ((int)block_size > 0) { int data_bytes = 0; // Get the data specified to this product from NOAAPORT MemoryStream ms_data = new MemoryStream(); for (int i = 0; i < (int)block_size; i++) { byte[] data_buffer = new byte[1]; int c_bytes = ns.Read(data_buffer, 0, 1); ms_data.Write(data_buffer, 0, c_bytes); data_bytes++; } // Received the file. Get a string and provide it to the consumer. byte[] dataReceived = ms_data.ToArray(); FileReceivedCallback(strFileName, dataReceived); } } } while (true); } catch (Exception ex) { // Received an exception. Give details. Console.WriteLine("[NBSPClient] Error " + ex.Message); } finally { // Close the NBSP Client if we error to not keep the server hung nbsp.Dispose(); } // Specify the connection has been closed. if (!nbsp.Connected) { Console.WriteLine("[NBSPClient] Connection Closed!"); } }
private void DataAdapter_OpenFileRequested(object sender, ReceivedFile e) { LaunchHelper.OpenFile(this, Path.Combine(e.StorePath, e.Name)); }
private void DataAdapter_ShareFileRequested(object sender, ReceivedFile e) { ShareHelper.ShareFile(this, new Java.IO.File(Path.Combine(e.StorePath, e.Name))); }