Пример #1
0
        public static string CreateCommand(string signature, string input, IKeySym privateKey)
        {
            IParser<ParamArgs> parser = new CommandParser(); 
            ParamArgs output = parser.Parse(input);
            if (output.Count == 0) throw new OperationCanceledException("Could not parse command.");

            Header header = new Header(signature, "", output.Get<string>("Command"));
            string jsonHeader = Newtonsoft.Json.JsonConvert.SerializeObject(header);

            DynamicData detail = new DynamicData();
            foreach (string key in output.ToDictionary().Keys)
            {
                if (!key.Equals("Command"))
                {
                    detail.Add(key, output.Get<object>(key));
                }
            }
            string jsonDetail = Newtonsoft.Json.JsonConvert.SerializeObject(detail.GetDictionary());
            Crypter crypter = new Crypter(privateKey);

            string final = string.Format(Format, jsonHeader, jsonDetail);
            return crypter.Encrypt(final); 
        }
Пример #2
0
        private void TimerProcessingRequest_Elapsed(object sender, EventArgs e)
        {  
            ////ensures that one thread enter a processing request section, in the same time reading sms section may will be occured.
            ////If another thread tries to enter a locked code, it will wait, block, until the object is released.
            lock(portColletion)
            {
                PacketEventArgs workItem = workerPoolManager.GetJob();
                if (workItem != null)
                { 
                    BasicInformation currentConnection = portColletion.Get();
                    if (currentConnection != null)
                    {
                        List<string> command = new List<string>();
                        ParameterizedMap parsingCommand;
                        try
                        {
                            string dataEncode = Encoding.GetEncoding("ibm850").GetString(workItem.Data);

                            Request request = Newtonsoft.Json.JsonConvert.DeserializeObject<Request>(dataEncode, new Newtonsoft.Json.JsonSerializerSettings
                            {
                                TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects
                            });  
                            ICommandParser<ParameterizedMap> parser = new CommandParser();
                            parsingCommand = parser.Parse(request.QueueWorkItem.Command);

                            DynamicData parameters = new DynamicData();
                            foreach (string key in parsingCommand.ToDictionary().Keys)
                            {
                                if (!key.Equals("Command"))
                                {
                                    parameters.Add(key, parsingCommand.TryGet<object>(key));
                                }
                            }
                            parameters.Add("id", request.QueueWorkItem.SeqNbr == null ? "" : request.QueueWorkItem.SeqNbr);
                            command = parsingCommand.TryGet<string>("Command").Split('.').ToList();

                            ParameterizedMap param = new ParameterizedMap();
                            param.Add("base", currentConnection);
                            param.Add("packet", workItem);
                            param.Add("command", command);
                            param.Add("parameters", parameters); 

                            string responseOut;
                            string encrypted;

                            Worker.IPipeline pipe = ObjectPool.Instance.Resolve<Worker.IPipeline>();

                            pipe.BeforePush(param);
                            responseOut = pipe.Push(param); 
                            OnDataSent(responseOut.ToString());
                            pipe.AfterPush(param);

                            // send back to client if request workItem is send from tcp
                            if (workItem.Client != null)
                            {
                                Crypter crypter = new Crypter(new DefaultConfigurationKey());
                                encrypted = crypter.Encrypt(responseOut.ToString());
                                try
                                {
                                    this.Send(((IPEndPoint)workItem.Client.RemoteEndPoint).Port, ASCIIEncoding.ASCII.GetBytes(encrypted.ToString()));
                                }
                                catch (System.Net.Sockets.SocketException)
                                { 
                                    // client was closed, dont raise error. 
                                }
                                catch (ObjectDisposedException)
                                { 
                                    // client was closed, dont raise error.  
                                }  
                            }   
                        }
                        catch (Exception ex)
                        {
                            RaiseError(currentConnection, ex, "process request");
                        }  
                    }
                    else
                    {
                        workerPoolManager.CurrentWorker.Add(workItem);
                    }
                }
            } 
        }