Пример #1
0
        private static void ReceiveResponse()
        {
            string text;

            do
            {
                int received = ClientSocket.Receive(buffer, SocketFlags.None);
                if (received == 0)
                {
                    return;
                }
                var data = new byte[received];
                Array.Copy(buffer, data, received);
                text = Encoding.ASCII.GetString(data);
                var cleanText = text.Substring(33);
                var md5       = text.Substring(0, 32);
                if (CheksumGenerator.CreateMD5(cleanText) != md5)
                {
                    Console.WriteLine("Wystapil blad polaczenia");
                    continue;
                }
                Console.WriteLine(cleanText);
                text = cleanText;
            }while(!text.StartsWith("_OK_") && !text.StartsWith("_ERROR_"));
        }
Пример #2
0
        /// <summary>
        /// Sends a string to the server with ASCII encoding.
        /// </summary>
        private static void SendString(string text)
        {
            var md5request = $"{CheksumGenerator.CreateMD5(text)} {text}";

            byte[] buffer = Encoding.ASCII.GetBytes(md5request);
            ClientSocket.Send(buffer);
        }
Пример #3
0
        public async Task Handle(string request)
        {
            string response = null;

            try {
                var splitedRequest = request.Split();
                var md5cheksum     = splitedRequest[0];
                if (CheksumGenerator.CreateMD5(request.Substring(33)) != md5cheksum)
                {
                    throw new MiniCloudException("Incomplete rquest");
                }
                if (splitedRequest.Length < 3)
                {
                    throw new MiniCloudException("Invalid Request");
                }
                var controllerName = splitedRequest[1];
                var controllerType = GetControllerType(controllerName);
                if (controllerType == null)
                {
                    throw new MiniCloudException($"Controller {controllerName} doesn't exists");
                }

                var methodName = splitedRequest[2];
                var methodInfo = GetMethod(controllerType, methodName);
                if (methodInfo == null)
                {
                    throw new MiniCloudException($"Method {methodName} doesn't exists in {controllerName} controller");
                }

                var parameters = methodInfo.GetParameters();
                var arguments  = splitedRequest.Skip(3);
                if (arguments.Count() != parameters.Count() || arguments.Any(x => String.IsNullOrWhiteSpace(x)))
                {
                    var correctUsage = GenerateCorrectUsageInfo(controllerName, methodName, parameters.Select(x => x.Name));
                    throw new MiniCloudException(correctUsage);
                }

                //var controller=Activator.CreateInstance(controllerType,_currentClient.Session);
                var controller = Bootstraper.Container.ResolveNamed <IController>(controllerType.Name);
                controller.SetSession(_currentClient.Session);

                string result;
                if (methodInfo.ReturnType == typeof(Task <string>))
                {
                    var   task = (Task <string>)methodInfo.Invoke(controller, arguments.ToArray());
                    await task;
                    result = task.Result;
                }
                else
                {
                    throw new Exception("Wrong return type");
                }

                response = $"_OK_: {result}";
            }
            catch (MiniCloudException ex)
            {
                response = $"_ERROR_: {ex.Message}";
            }
            catch (Exception ex)
            {
                response = "_ERROR_: Internal Error";
            }
            var md5response = $"{CheksumGenerator.CreateMD5(response)} {response}";

            _currentClient.SendText(md5response);
        }