Exemplo n.º 1
0
        //-------------------------------------------------------------------------------------------------------------------
        //-         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);
            }
        }
Exemplo n.º 2
0
 public void BroadcastRSA(NetComInstruction pInstruction)
 {
     foreach (NetComClientData client in LClientList)
     {
         Debug($"Queueing message for {client.Username}: {pInstruction}", DebugParams);
         OutgoingInstructions.AddRSA(pInstruction, client);
     }
 }
Exemplo n.º 3
0
 public void SendToClientRSA(NetComClientData pClient, NetComInstruction pInstruction)
 {
     if (pClient != null)
     {
         Debug($"Queueing message for {pClient.Username}: {pInstruction}", DebugParams);
         OutgoingInstructions.AddRSA(pInstruction, pClient);
     }
 }
Exemplo n.º 4
0
 /// <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);
         }
     }
 }
Exemplo n.º 5
0
        public void SendNextQueueItem()
        {
            if (OutgoingInstructions.Count > 0)
            {
                Debug($"Start sending message to Server: [{OutgoingInstructions[0].Key}]", DebugParams);

                Data = Encoding.ASCII.GetBytes(EncodeMessage(OutgoingInstructions[0].Key));

                ClientSocket.Send(Data);

                OutgoingInstructions.RemoveAt(0);
            }
        }
Exemplo n.º 6
0
        public void SendNextQueueItem()
        {
            if (OutgoingInstructions.Count > 0 && SendStreamOpen)
            {
                SendStreamOpen = false;
                Debug($"Start sending message to {OutgoingInstructions[0].Value.GetHashCode()}: [{OutgoingInstructions[0].Key}]", DebugParams);

                Data = Encoding.ASCII.GetBytes(EncodeMessage(OutgoingInstructions[0].Key));
                OutgoingInstructions[0].Value.BeginSend(Data, 0, Data.Length, SocketFlags.None, new AsyncCallback(OnClientSend), OutgoingInstructions[0].Value);
                Start();

                OutgoingInstructions.RemoveAt(0);
            }
        }
Exemplo n.º 7
0
        /// <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();
                    }
                }
            }
        }
Exemplo n.º 8
0
        /// <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();
                    }
                }
            }
        }
Exemplo n.º 9
0
        /// <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);
                }
            }
        }
Exemplo n.º 10
0
        /// <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();
                    }
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Sends the next instruction from the outgoing-queue.
        /// </summary>
        protected override void AsyncInstructionSendNext()
        {
            base.AsyncInstructionSendNext();

            if (!haltActive)
            {
                byte[] buffer;

                buffer = Encoding.UTF8.GetBytes(OutgoingInstructions[0].Encode());

                LocalSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);

                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. Client is in halt-mode. Waiting for 5 seconds...", DebugType.Error);
                Thread.Sleep(5000);
            }
        }
Exemplo n.º 12
0
        private void SendNextInstruction()
        {
            if (OutgoingInstructions.Count > 0)
            {
                Socket current = OutgoingInstructions[0]?.Client?.Socket;
                byte[] data;

                if (OutgoingInstructions[0].RSAEncrypted && OutgoingInstructions[0].Client.PublicKey != null)
                {
                    data = Encoding.UTF8.GetBytes(OutgoingInstructions[0].Instruction.Encode(true, OutgoingInstructions[0].Client.PublicKey));
                }
                else
                {
                    data = Encoding.UTF8.GetBytes(OutgoingInstructions[0].Instruction.Encode(false));
                }

                current.Send(data);

                Debug($"Sent Message to {OutgoingInstructions[0].Client.Username}: {OutgoingInstructions[0].Instruction}.", DebugParams);

                OutgoingInstructions.RemoveAt(0);
            }
        }
Exemplo n.º 13
0
        private void SendNextInstruction()
        {
            if (OutgoingInstructions.Count > 0)
            {
                byte[] buffer;

                string instruction;

                if (OutgoingInstructions[0].RSAEncrypted && ServerPublicKey != null)
                {
                    instruction = OutgoingInstructions[0].Instruction.Encode(true, ServerPublicKey);
                }
                else
                {
                    instruction = OutgoingInstructions[0].Instruction.Encode(false);
                }

                buffer = Encoding.UTF8.GetBytes(instruction);

                ClientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
                Debug($"Sent Message: {instruction}.", DebugParams);
                OutgoingInstructions.RemoveAt(0);
            }
        }
Exemplo n.º 14
0
 public void SendRSA(NetComInstruction pInstruction)
 {
     OutgoingInstructions.AddRSA(pInstruction, null);
 }
Exemplo n.º 15
0
        //-------------------------------------------------------------------------------------------------------------------
        //-         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);
        }
Exemplo n.º 16
0
        /// <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);
            }
        }