//------------------------------------------------------------------------------------------------------------------- //- MESSAGING - //------------------------------------------------------------------------------------------------------------------- #region -=[- MESSAGING -]=- /// <summary> /// Sends a message to a given client /// </summary> /// <param name="pMessage">Raw-Message to be sent</param> /// <param name="pSocket">Client-Socket</param> public void SendToClient(string pMessage, Socket pSocket) { if (pSocket != null && LClients.Authenticated(pSocket)) { Debug($"Queueing message for {pSocket.GetHashCode()}: {pMessage}", DebugParams); OutgoingInstructions.Add(pMessage, pSocket); } }
public void SendToClient(NetComClientData pClient, NetComInstruction pInstruction) { if (pClient != null) { Debug($"Queueing message for {pClient.Username}: {pInstruction}", DebugParams); OutgoingInstructions.Add(pInstruction, pClient); } }
public void Broadcast(NetComInstruction pInstruction) { foreach (NetComClientData client in LClientList) { Debug($"Queueing message for {client.Username}: {pInstruction}", DebugParams); OutgoingInstructions.Add(pInstruction, client); } }
/// <summary> /// Sends an instruction to a single client. /// </summary> /// <param name="pInstruction">Instruction to send. The receiver is set by the 'pReceiver'-parameter</param> public void Send(InstructionBase pInstruction) { if (!haltActive) { if (pInstruction.Receiver != null) { Debug($"Queueing message for {pInstruction.Receiver.ToString()}.", DebugType.Info); OutgoingInstructions.Add(pInstruction); } } }
/// <summary> /// Sends a instruction to a user-group. /// </summary> /// <param name="pInstruction">Instruction to send. Set the receiver-parameter to 'null'</param> /// <param name="pGroup">Target group</param> public void GroupSend(InstructionBase pInstruction, UserGroup pGroup) { if (!haltActive) { try { lock (pGroup) { for (int i = 0; i < pGroup.OnlineMembers.Count; i++) { if (!string.IsNullOrEmpty(pGroup.OnlineMembers[i].Username) && !string.IsNullOrEmpty(pGroup.OnlineMembers[i].Password)) { try { InstructionBase tmpInstruction = pInstruction.Clone(); tmpInstruction.Receiver = pGroup.OnlineMembers[i]; Debug($"Queueing message for {tmpInstruction.Receiver.ToString()}.", DebugType.Info); OutgoingInstructions.Add(tmpInstruction); } catch (Exception ex) { Debug("GroupSend-Error.", DebugType.Error); if (ShowExceptions) { Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception); } _logErrorCount++; } } } } } catch (Exception ex) { Debug("Halting (02)", DebugType.Warning); if (ShowExceptions) { Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception); } if (AutoRestartOnCrash) { HaltAllThreads(); } } } }
/// <summary> /// Sends an instruction to all connected users. /// </summary> /// <param name="pInstruction">Instruction to send. Set the receiver-parameter to 'null'</param> public void Broadcast(InstructionBase pInstruction) { if (!haltActive) { try { lock (ConnectedClients) { for (int i = 0; i < ConnectedClients.Count; i++) { if (!string.IsNullOrEmpty(ConnectedClients[i].Username) && !string.IsNullOrEmpty(ConnectedClients[i].Password)) { try { InstructionBase tmpInstruction = pInstruction.Clone(); tmpInstruction.Receiver = ConnectedClients[i]; Debug($"Queueing message for {tmpInstruction.Receiver.ToString()}.", DebugType.Info); OutgoingInstructions.Add(tmpInstruction); } catch (Exception ex) { Debug("Broadcast-Error.", DebugType.Error); if (ShowExceptions) { Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception); } _logErrorCount++; } } } } } catch (Exception ex) { Debug("Halting (04)", DebugType.Warning); if (ShowExceptions) { Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception); } if (AutoRestartOnCrash) { HaltAllThreads(); } } } }
/// <summary> /// Sends an instruction to the server. /// </summary> /// <param name="pInstruction">Instruction to be sent. Set the receiver-parameter to 'null'</param> public void Send(InstructionBase pInstruction) { try { pInstruction.SetReceiverPublicKey(serverPublicKey); OutgoingInstructions.Add(pInstruction); } catch (Exception ex) { Debug("Could not add instruction to send-queue.", DebugType.Error); if (ShowExceptions) { Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception); } } }
/// <summary> /// Sends an instruction to a range of users. /// </summary> /// <param name="pInstruction">Instruction to send. Set the receiver-parameter to 'null'</param> /// <param name="pUsers">Target users</param> public void ListSend(InstructionBase pInstruction, params NetComUser[] pUsers) { if (!haltActive) { try { for (int i = 0; i < pUsers.Length; i++) { if (!string.IsNullOrEmpty(pUsers[i].Username) && !string.IsNullOrEmpty(pUsers[i].Password)) { try { InstructionBase tmpInstruction = pInstruction.Clone(); tmpInstruction.Receiver = pUsers[i]; Debug($"Queueing message for {tmpInstruction.Receiver.ToString()}.", DebugType.Info); OutgoingInstructions.Add(tmpInstruction); } catch (Exception ex) { Debug("ListSend-Error.", DebugType.Error); if (ShowExceptions) { Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception); } _logErrorCount++; } } } } catch (Exception ex) { Debug("Halting (03)", DebugType.Warning); if (ShowExceptions) { Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception); } if (AutoRestartOnCrash) { HaltAllThreads(); } } } }
//------------------------------------------------------------------------------------------------------------------- //- SENDING - //------------------------------------------------------------------------------------------------------------------- public void Send(NetComInstruction pInstruction) { OutgoingInstructions.Add(pInstruction, null); }
//------------------------------------------------------------------------------------------------------------------- //- MESSAGING - //------------------------------------------------------------------------------------------------------------------- #region -=[- MESSAGING -]=- /// <summary> /// Sends a message to a given client /// </summary> /// <param name="pMessage">Raw-Message to be sent</param> public void SendToServer(string pMessage) { Debug($"Queueing message for Server: [{pMessage}]", DebugParams); OutgoingInstructions.Add(pMessage, null); }
/// <summary> /// Sends the next instruction from the outgoing-queue. /// </summary> protected override void AsyncInstructionSendNext() { base.AsyncInstructionSendNext(); if (!haltActive) { try { if ((OutgoingInstructions[0].Receiver as NetComCData).Authenticated || OutgoingInstructions[0].GetType() != typeof(InstructionLibraryEssentials.KeyExchangeServer2Client) || OutgoingInstructions[0].GetType() != typeof(InstructionLibraryEssentials.AuthenticationServer2Client)) { Socket current = OutgoingInstructions[0]?.Receiver.LocalSocket; byte[] data; data = Encoding.UTF8.GetBytes(OutgoingInstructions[0].Encode()); try { current.Send(data); } catch (Exception) { Debug("Client disconnected > Connection lost. (101)", DebugType.Warning); current.Close(); UserGroups.Disconnect(current); ConnectedClients.Remove(current); return; } Debug($"Sent Message to {OutgoingInstructions[0].Receiver.ToString()}.", DebugType.Info); Debug(OutgoingInstructions[0].ToString(), DebugType.Info); OutgoingInstructions.RemoveAt(0); } else { Debug($"Could not send Message to {OutgoingInstructions[0].Receiver.ToString()}. Authentication not valid.", DebugType.Error); Debug($"Sending Authentication-Reminder...", DebugType.Info); // Queues a authentication-reminder Send(new InstructionLibraryEssentials.AuthenticationReminder(this, OutgoingInstructions[0].Receiver)); // moves the current instruction to the back OutgoingInstructions.Add(OutgoingInstructions[0].Clone()); OutgoingInstructions.RemoveAt(0); } } catch (Exception ex) { Debug("Halting (08)", DebugType.Warning); if (ShowExceptions) { Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception); } if (AutoRestartOnCrash) { HaltAllThreads(); } } } else { Debug("Could not send message. Server is in halt-mode. Waiting for 5 seconds...", DebugType.Error); Thread.Sleep(5000); } }