Пример #1
0
        private void UseContext(HttpListenerContext context)
        {
            void Task1()
            {
                context.Response.StatusCode        = (int)HttpStatusCode.OK;
                context.Response.StatusDescription = HttpStatusCode.OK.ToString();
                byte[] text = MySave.BotSettings.ConfirmMode ? Encoding.UTF8.GetBytes(MySave.BotSettings.ConfirmString) : OkArray;
                context.Response.OutputStream.Write(text, 0, text.Length);
                try
                {
                    Console.WriteLine($"{DateTime.Now}: Отправлен ответ \"OK\" ВК");
                }
                catch (Exception e)
                {
                    lock (Console.Out)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine($"{DateTime.Now}: {e}");
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                }
            }

            void Task2()
            {
                DateTime t1 = DateTime.Now;
                MyCallbackStruct <MyMessageStruct> callback = (MyCallbackStruct <MyMessageStruct>) MyCallbackStruct <MyMessageStruct> .SerializerMessage.ReadObject(context.Request.InputStream);

                if (callback.secret != MySave.BotSettings.SecretCode)
                {
                    Console.WriteLine($"{DateTime.Now}: Неверный секретный код");
                    return;
                }

                if (!ApiCommands.ContainsKey([email protected]_id))
                {
                    ApiCommands.Add([email protected]_id, new MyCommand([email protected]_id));
                }
                string mes = ApiCommands[[email protected]_id].ExecuteCommand([email protected], out string title);

                try
                {
                    SendMessage([email protected]_id, mes, [email protected], title);
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(e);
                    Console.ForegroundColor = ConsoleColor.Green;
                    throw;
                }

                DateTime t2 = DateTime.Now;

                try
                {
                    Console.WriteLine($"{DateTime.Now}: Затрачено мс: {(t2 - t1).TotalMilliseconds}");
                }
                catch (Exception e)
                {
                    lock (Console.Out)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine($"{DateTime.Now}: {e}");
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                }
            }

            Parallel.Invoke(Task1, Task2);
            try
            {
                context.Response.Close();
                context.Request.InputStream.Close();
            }
            catch (Exception)
            {
                lock (Console.Out)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine($"{DateTime.Now}: Ошибка при отправке данных в ВК");
                    Console.ForegroundColor = ConsoleColor.Green;
                }
            }
        }
Пример #2
0
 public bool Equals(MyCallbackStruct <T> other) => type == other.type && EqualityComparer <T> .Default.Equals(@object, other.@object) && group_id == other.group_id && secret == other.secret;