Example #1
0
        /// <summary>
        /// Sends the next instruction from the outgoing-queue.
        /// </summary>
        protected override void AsyncInstructionSendNext()
        {
            base.AsyncInstructionSendNext();

            if (!HandlerData.HaltActive)
            {
                try
                {
                    if ((HandlerData.OutgoingInstructions[0].Receiver as NetComCData).Authenticated ||
                        HandlerData.OutgoingInstructions[0].GetType() != typeof(InstructionLibraryEssentials.KeyExchangeServer2Client) ||
                        HandlerData.OutgoingInstructions[0].GetType() != typeof(InstructionLibraryEssentials.AuthenticationServer2Client))
                    {
                        Socket current = HandlerData.OutgoingInstructions[0]?.Receiver.LocalSocket;
                        byte[] data;

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

                        try
                        {
                            current.Send(data);
                        }
                        catch (Exception)
                        {
                            Handler.Debug("Client disconnected > Connection lost. (101)", DebugType.Warning);
                            current.Close();
                            UserGroups.Disconnect(current);
                            ConnectedClients.Remove(current);
                            return;
                        }

                        Handler.Debug($"Sent Message to {HandlerData.OutgoingInstructions[0].Receiver.ToString()}.", DebugType.Info);
                        Handler.Debug(HandlerData.OutgoingInstructions[0].ToString(), DebugType.Info);
                        HandlerData.OutgoingInstructions.RemoveAt(0);
                    }
                    else
                    {
                        Handler.Debug($"Could not send Message to {HandlerData.OutgoingInstructions[0].Receiver.ToString()}. Authentication not valid.", DebugType.Error);
                        Handler.Debug($"Sending Authentication-Reminder...", DebugType.Info);

                        // Queues a authentication-reminder
                        Send(new InstructionLibraryEssentials.AuthenticationReminder(this, HandlerData.OutgoingInstructions[0].Receiver));

                        // moves the current instruction to the back
                        HandlerData.OutgoingInstructions.Add(HandlerData.OutgoingInstructions[0].Clone());
                        HandlerData.OutgoingInstructions.RemoveAt(0);
                    }
                }
                catch (Exception ex)
                {
                    Handler.Debug("Halting (08)", DebugType.Warning);
                    if (HandlerData.ShowExceptions)
                    {
                        Handler.Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception);
                    }
                    if (HandlerData.TryRestartOnCrash)
                    {
                        HaltAllThreads();
                    }
                }
            }
            else
            {
                Handler.Debug("Could not send message. Server is in halt-mode. Waiting for 5 seconds...", DebugType.Error);
                Thread.Sleep(5000);
            }
        }
Example #2
0
        /// <summary>
        /// Gets called when a message is received.
        /// </summary>
        /// <param name="AR">IAsyncResult</param>
        private void ReceiveCallback(IAsyncResult AR)
        {
            try
            {
                Socket current = (Socket)AR.AsyncState;
                int    received;

                try
                {
                    received = current.EndReceive(AR);
                }
                catch (SocketException)
                {
                    Handler.Debug("Client disconnected > Connection lost. (102)", DebugType.Warning);
                    // Don't shutdown because the socket may be disposed and its disconnected anyway.
                    current.Close();
                    UserGroups.Disconnect(current);
                    ConnectedClients.Remove(current);
                    return;
                }

                byte[] recBuf = new byte[received];
                Array.Copy(buffer, recBuf, received);
                string text = Encoding.UTF8.GetString(recBuf);

                Console.ForegroundColor = ConsoleColor.Cyan;
                //Debug("Received message: " + text);
                Handler.Debug("Received message.", DebugType.Info);
                Console.ForegroundColor = ConsoleColor.White;

                InstructionBase[] instructionList = null;

                try
                {
                    instructionList = InstructionOperations.Parse(this, current, text, ConnectedClients).ToArray();

                    // Check for group-Addignment
                    foreach (InstructionBase instr in instructionList)
                    {
                        HandlerData.IncommingInstructions.Add(instr);

                        if (instr.GetType() != typeof(InstructionLibraryEssentials.KeyExchangeClient2Server) && !GroupAddRecords.Contains(instr.Sender.Username))
                        {
                            GroupAddRecords.Add(instr.Sender.Username);
                            UserGroups.TryGroupAdd(instr.Sender);
                        }
                    }
                }
                catch (NetComAuthenticationException ex)
                {
                    Handler.Debug("Authentication-Error (Instruction-Parsing).", DebugType.Error);
                    if (HandlerData.ShowExceptions)
                    {
                        Handler.Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception);
                    }
                }
                catch (Exception ex)
                {
                    Handler.Debug($"Error occured (Instruction-Parsing). ({HandlerData.LogErrorCounter})", DebugType.Error);
                    if (HandlerData.ShowExceptions)
                    {
                        Handler.Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception);
                    }
                    HandlerData.LogErrorCounter++;
                }



                try
                {
                    current.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, current);
                }
                catch (SocketException)
                {
                    Handler.Debug("Client disconnected > Connection lost. (103)", DebugType.Warning);
                    // Don't shutdown because the socket may be disposed and its disconnected anyway.
                    current.Close();
                    UserGroups.Disconnect(current);
                    ConnectedClients.Remove(current);
                    return;
                }
            }
            catch (Exception ex)
            {
                Handler.Debug("Halting (05)", DebugType.Warning);
                if (HandlerData.ShowExceptions)
                {
                    Handler.Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception);
                }
                if (HandlerData.TryRestartOnCrash)
                {
                    HaltAllThreads();
                }
            }
        }