/// <summary> /// Creates a new thread dedicated to message receiving. /// </summary> protected void ReceiveMessages() { ReceiverThread = new Thread(new ThreadStart(delegate { while (true) { try { // TODO: This throws an error when killing the network connection. byte[] messageByteArray = Receiver.Receive(ref ReceivingEndPoint); DistributeMessage(messageByteArray); } catch (System.Exception ex) { LoggingUtilities.LogFormat( "Message: ({0})\nInner Exception: ({1})\nData: ({2})\nHelplink: ({3})\nHResult: ({4})\nSource: ({5})\nTargetSite: ({6})\nStack Trace: ({7})", ex.Message, ex.InnerException, ex.Data, ex.HelpLink, ex.HResult, ex.Source, ex.TargetSite, ex.StackTrace); } } })); ReceiverThread.Start(); }
public BackUpSettings Load(string settingsPath) { // loads selected settings. string json = File.ReadAllText(settingsPath); LoadedSettings = JsonUtility.FromJson <BackUpSettings>(json); LoggingUtilities.LogFormat("Loaded Settings: ({0})\n", json); return(LoadedSettings); }
/// <summary> /// Sends a message across the network. /// </summary> public void SendMessage(T message) { string msg = JsonUtility.ToJson(message); byte[] messageByteArray = Encoding.ASCII.GetBytes(msg); SendMessage(messageByteArray); LoggingUtilities.LogFormat("Sent message (\"{0}\") to ip ({1}) using port ({2})", msg, SendingEndPoint.Address, SendingEndPoint.Port); }
/// <summary> /// Closes used port and message receiving thread. /// </summary> public virtual void Kill() { LoggingUtilities.LogFormat("Killing UDPMaster"); SendingSocket.Close(); Receiver.Close(); ReceiverThread.Abort(); IsInitialized = false; }
/// <summary> /// Calls the networkListeners. /// </summary> protected virtual void DistributeMessage(byte[] message) { foreach (INetworkListener networkListener in NetworkListeners) { networkListener.OnMessageReceived(message); } LoggingUtilities.LogFormat( "Received message (\"{0}\") from ip ({1}) using port ({2})", message.ToString(), ReceivingEndPoint.Address.ToString(), ReceivingPort); }
/// <summary> /// Deletes the file with the specified name. /// </summary> public static bool Remove(string name) { string path = Path.ChangeExtension(Path.Combine(savePath, name), Extention); try { File.Delete(path); return(true); } catch (System.Exception ex) { LoggingUtilities.LogFormat("Couldn't delete file ({0}). Halted with error ({1})", path, ex.Message); return(false); } }
/// <inheritdoc /> protected override void DistributeMessage(byte[] message) { base.DistributeMessage(message); string serializedMessage = Encoding.ASCII.GetString(message); UDPMessage udpMessage = (UDPMessage)JsonUtility.FromJson(serializedMessage, typeof(T)); udpMessage.SenderIP = ReceivingEndPoint.Address.ToString(); LoggingUtilities.LogFormat("Message Received: ({0})", serializedMessage); foreach (IAppliedNetworkListener appliedListeners in appliedNetworkListeners) { appliedListeners.OnMessageReceived(udpMessage); } }
private void StartBackUp(string userSettings) { LoadBackingUpMenu(); backUpThread = new Thread(() => { LoggingUtilities.SetAppDataRoot(AppDataRoot); try { LoadingIcon(); LoggingUtilities.LogFormat("\nInitiating Simple Back-Up ({0})\n", DateTime.Now.ToString()); BackUpSettings loadedSettings = settings.Load(userSettings); backUp = new BackUp(); backUp.Start(loadedSettings); } catch (Exception ex) { LoggingUtilities.Log("ERROR\n"); LoggingUtilities.Log(ex.StackTrace + "\n"); LoggingUtilities.Log(ex.Message + "\n"); LoggingUtilities.Log(ex.Data.ToString() + "\n"); Console.Beep(200, 500); Console.Beep(200, 500); Console.Beep(200, 500); } finally { if (loadIconThread != null) { loadIconThread.Abort(); loadIconThread = null; trayIcon.Icon = Resources.AppIcon; } Reloadmenu(); Console.Beep(400, 500); LoggingUtilities.LogFormat("Finished AFB ({0})\n", DateTime.Now.ToString()); } }); backUpThread.Start(); }
/// <summary> /// Sets the UDPMaster up for sending messages, /// and start listening to messages. /// </summary> public virtual void Initialize(string sendingAddress, int sendingPort = 11000, int receivingPort = 11001) { this.ReceivingPort = receivingPort; this.SendingPort = sendingPort; this.SendingAddress = IPAddress.Parse(sendingAddress); SendingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); SendingEndPoint = new IPEndPoint(this.SendingAddress, this.SendingPort); Receiver = new UdpClient(this.ReceivingPort); ReceivingEndPoint = new IPEndPoint(IPAddress.Any, this.ReceivingPort); ReceiveMessages(); IsInitialized = true; LoggingUtilities.LogFormat("UDPMaster initialized with ip ({0}) and sending port ({1}) receiving port ({2})", SendingEndPoint.Address, sendingPort, this.ReceivingPort); }
/// <summary> /// Saves the provided byte[] to the set path. /// </summary> public static FileInfo Save(byte[] data, string name) { if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } if (EncryptData) { FileEncryption.Encrypt(ref data); } string path = Path.Combine(savePath, name); path = Path.ChangeExtension(path, Extention); File.WriteAllBytes(path, data); LoggingUtilities.LogFormat("Saved at: {0}", path); return(new FileInfo(path)); }
public static string ToJson(object obj, Type t) { try { JsonSerializer jsonSerializer = new JsonSerializer(); return(jsonSerializer.ToJson(obj, t)); } catch (Exception ex) { LoggingUtilities.LogFormat( "Json serialization halted with error message: ({0})\nInner Exception: ({1})\nData: ({2})\nHelplink: ({3})\nHResult: ({4})\nSource: ({5})\nTargetSite: ({6})\nStack Trace: ({7})", ex.Message, ex.InnerException, ex.Data, ex.HelpLink, ex.HResult, ex.Source, ex.TargetSite, ex.StackTrace); throw ex; } }
public static T FromJson <T>(string json) where T : new() { try { JsonDeserializer jsonDeserializer = new JsonDeserializer(); int i = 0; return((T)jsonDeserializer.DeserializeObject(json, ref i, typeof(T))); } catch (Exception ex) { LoggingUtilities.LogFormat( "Json deserialization halted with error message: ({0})\nInner Exception: ({1})\nData: ({2})\nHelplink: ({3})\nHResult: ({4})\nSource: ({5})\nTargetSite: ({6})\nStack Trace: ({7})", ex.Message, ex.InnerException, ex.Data, ex.HelpLink, ex.HResult, ex.Source, ex.TargetSite, ex.StackTrace); throw ex; } }
private void Copy( string originDirectory, string targetDirectory) { DirectoryUtilities.EnsureDirectory(targetDirectory); DirectoryUtilities.ForeachFolderAt(originDirectory, (string childDir) => Copy(childDir, Path.Combine(targetDirectory, Path.GetFileName(childDir)))); DirectoryUtilities.ForeachFileAt(originDirectory, (FileInfo file) => { // TODO: add sub folders to settings to autoskip (e.g. ".git" folders). string targetPath = Path.Combine(targetDirectory, file.Name); // if it's the same file, but a different version. if (File.Exists(targetPath) && file.LastWriteTime != File.GetLastWriteTime(targetPath)) { LoggingUtilities.LogFormat("DUPLICATE FILE: {0}\n", targetPath); bool overwrite = overwriteState == ActionState.Yes; if (overwriteState == ActionState.Unset) { overwrite = RequestActionState(ref overwriteState, "overwrite", targetPath); } if (!overwrite) { LoggingUtilities.LogFormat("Skipped\n"); return; } LoggingUtilities.LogFormat("Overwritten\n"); } try { File.Copy(file.FullName, targetPath, true); } catch (Exception e) { LoggingUtilities.LogFormat("ERROR: {0} | {1}\n", e.GetType(), e.Message); bool skip = skipState == ActionState.Yes; if (skipState == ActionState.Unset) { skip = RequestActionState(ref skipState, string.Format("Can't reach file due to error ({0}). Skip", e.Message), targetPath); } if (skip) { LoggingUtilities.Log("Skipped\n"); return; } } FileAttributes attributes = File.GetAttributes(targetPath); if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { attributes = attributes & ~FileAttributes.ReadOnly; File.SetAttributes(targetPath, attributes); } }); }
/// <summary> /// Updates the targetIP to the /// provided target. /// </summary> public void UpdateTargetIP(string ipAddress) { SendingEndPoint.Address = IPAddress.Parse(ipAddress); LoggingUtilities.LogFormat("Updating target IP to: {0}", ipAddress); }