/// <summary>
        /// Receives and adds the next incomming instruction to the incomming-queue.
        /// </summary>
        protected void AsyncInstructionReceiveNext()
        {
            buffer = new byte[bufferSize];

            int received = LocalSocket.Receive(buffer, SocketFlags.None);

            if (received == 0)
            {
                return;
            }

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

            Debug("Received Message.", DebugType.Info);
            try
            {
                InstructionBase[] instructionList = InstructionOperations.Parse(this, null, text).ToArray();
                foreach (InstructionBase instr in instructionList)
                {
                    incommingInstructions.Add(instr);
                }
            }
            catch (NetComAuthenticationException ex)
            {
                Debug("Authentication-Error.", DebugType.Error);
                if (ShowExceptions)
                {
                    Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception);
                }
            }
            catch (Exception ex)
            {
                Debug($"Error occured. ({_logErrorCount})", DebugType.Error);
                if (ShowExceptions)
                {
                    Debug($"({ex.GetType().Name}) {ex.Message}", DebugType.Exception);
                }
                _logErrorCount++;
            }
        }
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();
                }
            }
        }