Пример #1
0
        /// <summary>
        /// The SetJson.
        /// </summary>
        /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
        /// <param name="tostream">The tostream<see cref="Stream"/>.</param>
        /// <returns>The <see cref="int"/>.</returns>
        public static int SetJson(this DealHeader thdr, Stream tostream)
        {
            BinaryWriter binwriter = new BinaryWriter(tostream);

            binwriter.Write(thdr.SetJsonString());
            return((int)tostream.Length);
        }
Пример #2
0
        /// <summary>
        /// The CltRecHead.
        /// </summary>
        /// <param name="buffer">The buffer<see cref="ISerialBuffer"/>.</param>
        private void CltRecHead(ISerialBuffer buffer)
        {
            DealHeader headerObject = (DealHeader)transaction.MyHeader.Deserialize(buffer);

            if (headerObject != null)
            {
                transaction.HeaderReceived = headerObject;

                transaction.MyHeader.Context.Identity.Key    = null;
                transaction.MyHeader.Context.Identity.Name   = null;
                transaction.MyHeader.Context.Identity.UserId = transaction.HeaderReceived.Context.Identity.UserId;
                transaction.MyHeader.Context.Identity.Token  = transaction.HeaderReceived.Context.Identity.Token;
                transaction.MyHeader.Context.Identity.DeptId = transaction.HeaderReceived.Context.Identity.DeptId;

                object reciveContent = transaction.HeaderReceived.Content;

                Type[] ifaces = reciveContent.GetType().GetInterfaces();
                if (ifaces.Contains(typeof(ISerialFormatter)) && ifaces.Contains(typeof(ISerialObject)))
                {
                    if (transaction.MyHeader.Content == null)
                    {
                        transaction.MyHeader.Content = ((ISerialObject)reciveContent).Locate();
                    }

                    object myContent = transaction.MyHeader.Content;

                    ((ISerialObject)myContent).Merge(reciveContent);

                    int objectCount = transaction.HeaderReceived.Context.ObjectsCount;
                    if (objectCount == 0)
                    {
                        transferContext.ReceiveMessage = false;
                    }
                    else
                    {
                        transaction.MessageReceived = new DealMessage(transaction, DirectionType.Receive, myContent);
                    }
                }
                else if (reciveContent is Hashtable)
                {
                    Hashtable hashTable = (Hashtable)reciveContent;
                    if (hashTable.Contains("Register"))
                    {
                        transferContext.Denied = !(bool)hashTable["Register"];
                        if (transferContext.Denied)
                        {
                            transferContext.Close          = true;
                            transferContext.ReceiveMessage = false;
                            transferContext.SendMessage    = false;
                        }
                    }
                }
                else
                {
                    transferContext.SendMessage = false;
                }
            }
        }
Пример #3
0
 /// <summary>
 /// The SetJsonBag.
 /// </summary>
 /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
 /// <returns>The <see cref="IDictionary{string, object}"/>.</returns>
 public static IDictionary <string, object> SetJsonBag(this DealHeader thdr)
 {
     return(new Dictionary <string, object>()
     {
         { "DealHeader", JsonParserProperties.GetJsonProperties(typeof(DealHeader), thdr.Context.Complexity)
           .Select(k => new KeyValuePair <string, object>(k.Name, k.GetValue(thdr, null)))
           .ToDictionary(k => k.Key, v => v.Value) }
     });
 }
Пример #4
0
        /// <summary>
        /// The SetBinary.
        /// </summary>
        /// <param name="bank">The bank<see cref="DealHeader"/>.</param>
        /// <param name="tostream">The tostream<see cref="Stream"/>.</param>
        /// <returns>The <see cref="int"/>.</returns>
        public static int SetBinary(this DealHeader bank, Stream tostream)
        {
            if (tostream == null)
            {
                tostream = new MemoryStream();
            }
            BinaryFormatter binform = new BinaryFormatter();

            binform.Serialize(tostream, bank);
            return((int)tostream.Length);
        }
Пример #5
0
 /// <summary>
 /// The GetBinary.
 /// </summary>
 /// <param name="bank">The bank<see cref="DealHeader"/>.</param>
 /// <param name="fromstream">The fromstream<see cref="Stream"/>.</param>
 /// <returns>The <see cref="DealHeader"/>.</returns>
 public static DealHeader GetBinary(this DealHeader bank, Stream fromstream)
 {
     try
     {
         BinaryFormatter binform = new BinaryFormatter();
         return((DealHeader)binform.Deserialize(fromstream));
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
Пример #6
0
 /// <summary>
 /// The GetJson.
 /// </summary>
 /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
 /// <param name="jsonstring">The jsonstring<see cref="string"/>.</param>
 /// <returns>The <see cref="DealHeader"/>.</returns>
 public static DealHeader GetJson(this DealHeader thdr, string jsonstring)
 {
     try
     {
         DealHeader trs = thdr.GetJsonObject(jsonstring)["DealHeader"];
         return(trs);
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
Пример #7
0
        /// <summary>
        /// The SetBinary.
        /// </summary>
        /// <param name="bank">The bank<see cref="DealHeader"/>.</param>
        /// <param name="buffer">The buffer<see cref="ISerialBuffer"/>.</param>
        /// <returns>The <see cref="int"/>.</returns>
        public static int SetBinary(this DealHeader bank, ISerialBuffer buffer)
        {
            MemoryStream ms = new MemoryStream();

            ms.Write(new byte[buffer.BlockOffset], 0, buffer.BlockOffset);
            BinaryFormatter binform = new BinaryFormatter();

            binform.Serialize(ms, bank);
            buffer.SerialBlock = ms.ToArray();
            ms.Dispose();
            return(buffer.SerialBlock.Length);
        }
Пример #8
0
 /// <summary>
 /// The GetJson.
 /// </summary>
 /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
 /// <param name="stringbuilder">The stringbuilder<see cref="StringBuilder"/>.</param>
 /// <returns>The <see cref="DealHeader"/>.</returns>
 public static DealHeader GetJson(this DealHeader thdr, StringBuilder stringbuilder)
 {
     try
     {
         StringBuilder sb  = stringbuilder;
         DealHeader    trs = thdr.GetJsonObject(sb.ToString())["DealHeader"];
         return(trs);
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
Пример #9
0
        /// <summary>
        /// The GetJsonObject.
        /// </summary>
        /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
        /// <param name="_bag">The _bag<see cref="IDictionary{string, object}"/>.</param>
        /// <returns>The <see cref="Dictionary{string, DealHeader}"/>.</returns>
        public static Dictionary <string, DealHeader> GetJsonObject(this DealHeader thdr, IDictionary <string, object> _bag)
        {
            Dictionary <string, DealHeader> result = new Dictionary <string, DealHeader>();
            IDictionary <string, object>    bags   = _bag;

            foreach (KeyValuePair <string, object> bag in bags)
            {
                object inst = new object();
                IEnumerable <PropertyInfo> map = JsonParser.PrepareInstance(out inst, typeof(DealHeader));
                JsonParser.DeserializeType(map, (IDictionary <string, object>)bag.Value, inst);
                DealHeader deck = (DealHeader)inst;
                result.Add(bag.Key, deck);
            }
            return(result);
        }
Пример #10
0
 /// <summary>
 /// The GetBinary.
 /// </summary>
 /// <param name="bank">The bank<see cref="DealHeader"/>.</param>
 /// <param name="buffer">The buffer<see cref="ISerialBuffer"/>.</param>
 /// <returns>The <see cref="DealHeader"/>.</returns>
 public static DealHeader GetBinary(this DealHeader bank, ISerialBuffer buffer)
 {
     try
     {
         MemoryStream    ms      = new MemoryStream(buffer.DeserialBlock);
         BinaryFormatter binform = new BinaryFormatter();
         DealHeader      _bank   = (DealHeader)binform.Deserialize(ms);
         ms.Dispose();
         return(_bank);
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="DealTransfer"/> class.
        /// </summary>
        /// <param name="identity">The identity<see cref="MemberIdentity"/>.</param>
        /// <param name="message">The message<see cref="object"/>.</param>
        /// <param name="context">The context<see cref="ITransferContext"/>.</param>
        public DealTransfer(MemberIdentity identity, object message = null, ITransferContext context = null)
        {
            Context = context;
            if (Context != null)
            {
                MyHeader = new DealHeader(this, Context, identity);
            }
            else
            {
                MyHeader = new DealHeader(this, identity);
            }

            Identity  = identity;
            Manager   = new TransferManager(this);
            MyMessage = new DealMessage(this, DirectionType.Send, message);
        }
Пример #12
0
        /// <summary>
        /// The SetJson.
        /// </summary>
        /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
        /// <param name="buffer">The buffer<see cref="ISerialBuffer"/>.</param>
        /// <param name="offset">The offset<see cref="int"/>.</param>
        /// <returns>The <see cref="int"/>.</returns>
        public static int SetJson(this DealHeader thdr, ISerialBuffer buffer, int offset = 0)
        {
            if (offset > 0)
            {
                byte[] jsonBytes   = Encoding.UTF8.GetBytes(thdr.SetJsonString());
                byte[] serialBytes = new byte[jsonBytes.Length + offset];
                jsonBytes.CopyTo(serialBytes, offset);
                buffer.SerialBlock = serialBytes;
                jsonBytes          = null;
            }
            else
            {
                buffer.SerialBlock = Encoding.UTF8.GetBytes(thdr.SetJsonString());
            }

            return(buffer.SerialBlock.Length);
        }
Пример #13
0
        /// <summary>
        /// The GetJson.
        /// </summary>
        /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
        /// <param name="buffer">The buffer<see cref="ISerialBuffer"/>.</param>
        /// <returns>The <see cref="DealHeader"/>.</returns>
        public static DealHeader GetJson(this DealHeader thdr, ISerialBuffer buffer)
        {
            try
            {
                DealHeader trs = null;

                byte[]        _fromarray = buffer.DeserialBlock;
                StringBuilder sb         = new StringBuilder();

                sb.Append(_fromarray.ToChars(CharEncoding.UTF8));
                trs = thdr.GetJsonObject(sb.ToString())["DealHeader"];

                _fromarray = null;
                sb         = null;
                return(trs);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Пример #14
0
 /// <summary>
 /// The GetJson.
 /// </summary>
 /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
 /// <param name="fromstream">The fromstream<see cref="Stream"/>.</param>
 /// <returns>The <see cref="DealHeader"/>.</returns>
 public static DealHeader GetJson(this DealHeader thdr, Stream fromstream)
 {
     try
     {
         fromstream.Position = 0;
         byte[]        array = new byte[4096];
         int           read  = 0;
         StringBuilder sb    = new StringBuilder();
         while ((read = fromstream.Read(array, 0, array.Length)) > 0)
         {
             sb.Append(array.Select(b => (char)b).ToArray());
         }
         DealHeader trs = thdr.GetJsonObject(sb.ToString())["DealHeader"];
         sb = null;
         fromstream.Dispose();
         return(trs);
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DealTransfer"/> class.
 /// </summary>
 public DealTransfer()
 {
     MyHeader  = new DealHeader(this);
     Manager   = new TransferManager(this);
     MyMessage = new DealMessage(this, DirectionType.Send, null);
 }
Пример #16
0
        /// <summary>
        /// The SrvRecPostDealHeader.
        /// </summary>
        /// <param name="buffer">The buffer<see cref="ISerialBuffer"/>.</param>
        /// <returns>The <see cref="bool"/>.</returns>
        private bool SrvRecPostDealHeader(ISerialBuffer buffer)
        {
            bool   isError      = false;
            string errorMessage = "";

            try
            {
                DealHeader headerObject = (DealHeader)transaction.MyHeader.Deserialize(buffer, SerialFormat.Json);
                headerObject.Context.Identity.Ip = transaction.MyHeader.Context.RemoteEndPoint.Address.ToString();
                if (DealServer.Security.Register(headerObject.Context.Identity, true))
                {
                    transaction.HeaderReceived = (headerObject != null) ? headerObject : null;
                    transaction.MyHeader.Context.Complexity = headerObject.Context.Complexity;
                    transaction.MyHeader.Context.Identity   = headerObject.Context.Identity;

                    if (headerObject.Context.ContentType != null)
                    {
                        object instance = new object();
                        JsonParser.PrepareInstance(out instance, headerObject.Context.ContentType);
                        object content = headerObject.Content;
                        object result  = ((ISerialFormatter)instance).Deserialize(buffer, SerialFormat.Json);
                        transaction.HeaderReceived.Content = result;
                        object _content = transaction.HeaderReceived.Content;

                        Type[] ifaces = _content.GetType().GetInterfaces();
                        if (ifaces.Contains(typeof(ISerialFormatter)) && ifaces.Contains(typeof(ISerialObject)))
                        {
                            int objectCount = buffer.DeserialBlockId;

                            object myheader = ((ISerialObject)_content).Locate();

                            //Settings clientConfig = ((ISettings)_content).Config;

                            if (myheader != null)
                            {
                                if (objectCount == 0)
                                {
                                    transferContext.ReceiveMessage = false;

                                    //if (clientConfig.DealerId.IsNotEmpty)
                                    //    transaction.MyHeader.Content = ((ISerialObject)myheader).Merge(_content);
                                    //else
                                    //{
                                    //  ((ISettings)myheader).State.Expeled = true;
                                    transaction.MyHeader.Content = myheader;
                                    // }
                                }
                                else
                                {
                                    transaction.MyHeader.Content = ((ISerialObject)myheader).Merge(_content);
                                    transaction.MessageReceived  = new DealMessage(transaction, DirectionType.Receive, transaction.MyHeader.Content);
                                }
                            }
                            else
                            {
                                isError       = true;
                                errorMessage += "Prime not exist - incorrect object target ";
                            }
                        }
                        else
                        {
                            isError       = true;
                            errorMessage += "Incorrect DPOT object - deserialization error ";
                        }
                    }
                    else
                    {
                        transaction.MyHeader.Content = new Hashtable()
                        {
                            { "Register", true }
                        };
                        transaction.MyHeader.Context.Echo += "Registration success - ContentType: null ";
                    }
                }
                else
                {
                    isError = true;
                    transaction.MyHeader.Content = new Hashtable()
                    {
                        { "Register", false }
                    };
                    transaction.MyHeader.Context.Echo += "Registration failed - access denied ";
                }
            }
            catch (Exception ex)
            {
                isError       = true;
                errorMessage += ex.ToString();
            }

            if (isError)
            {
                transaction.Context.SendMessage = false;
                if (errorMessage != "")
                {
                    transaction.MyHeader.Content      += errorMessage;
                    transaction.MyHeader.Context.Echo += errorMessage;
                }
                transaction.MyHeader.Context.Errors++;
            }
            return(isError);
        }
Пример #17
0
 /// <summary>
 /// The SetJsonString.
 /// </summary>
 /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
 /// <param name="jsonbag">The jsonbag<see cref="IDictionary{string, object}"/>.</param>
 /// <returns>The <see cref="string"/>.</returns>
 public static string SetJsonString(this DealHeader thdr, IDictionary <string, object> jsonbag)
 {
     return(JsonParser.ToJson(jsonbag, thdr.Context.Complexity));
 }
Пример #18
0
        /// <summary>
        /// The SetJsonString.
        /// </summary>
        /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
        /// <returns>The <see cref="string"/>.</returns>
        public static string SetJsonString(this DealHeader thdr)
        {
            IDictionary <string, object> toJson = thdr.SetJsonBag();

            return(JsonParser.ToJson(toJson, thdr.Context.Complexity));
        }
Пример #19
0
 /// <summary>
 /// The SetJson.
 /// </summary>
 /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
 /// <param name="stringbuilder">The stringbuilder<see cref="StringBuilder"/>.</param>
 /// <returns>The <see cref="int"/>.</returns>
 public static int SetJson(this DealHeader thdr, StringBuilder stringbuilder)
 {
     stringbuilder.AppendLine(thdr.SetJsonString());
     return(stringbuilder.Length);
 }
Пример #20
0
 /// <summary>
 /// The GetJsonBag.
 /// </summary>
 /// <param name="thdr">The thdr<see cref="DealHeader"/>.</param>
 /// <param name="JsonString">The JsonString<see cref="string"/>.</param>
 /// <param name="_bag">The _bag<see cref="IDictionary{string, object}"/>.</param>
 public static void GetJsonBag(this DealHeader thdr, string JsonString, IDictionary <string, object> _bag)
 {
     _bag.Add(JsonParser.FromJson(JsonString));
 }
Пример #21
0
        /// <summary>
        /// The SrvRecHead.
        /// </summary>
        /// <param name="buffer">The buffer<see cref="ISerialBuffer"/>.</param>
        private void SrvRecHead(ISerialBuffer buffer)
        {
            bool   isError      = false;
            string errorMessage = "";

            try
            {
                DealHeader headerObject = (DealHeader)transaction.MyHeader.Deserialize(buffer);
                if (headerObject != null)
                {
                    transaction.HeaderReceived = headerObject;

                    if (DealServer.Security.Register(transaction.HeaderReceived.Context.Identity, true))
                    {
                        transaction.MyHeader.Context.Identity.UserId = transaction.HeaderReceived.Context.Identity.UserId;
                        transaction.MyHeader.Context.Identity.Token  = transaction.HeaderReceived.Context.Identity.Token;
                        transaction.MyHeader.Context.Identity.DeptId = transaction.HeaderReceived.Context.Identity.DeptId;

                        if (transaction.HeaderReceived.Context.ContentType != null)
                        {
                            object _content = transaction.HeaderReceived.Content;
                            //((ISettings)_content).State.Synced = false;

                            Type[] ifaces = _content.GetType().GetInterfaces();
                            if (ifaces.Contains(typeof(ISerialFormatter)) && ifaces.Contains(typeof(ISerialObject)))
                            {
                                int objectCount = transaction.HeaderReceived.Context.ObjectsCount;
                                transferContext.Synchronic = transaction.HeaderReceived.Context.Synchronic;

                                object myheader = ((ISerialObject)_content).Locate();

                                if (myheader != null)
                                {
                                    if (objectCount == 0)
                                    {
                                        transferContext.ReceiveMessage = false;

                                        //if (((ISettings)_content).Config.DealerId.IsNotEmpty)
                                        //    transaction.MyHeader.Content = ((ISerialObject)myheader).Merge(_content);
                                        //else
                                        //{
                                        //    ((ISettings)myheader).State.Expeled = true;
                                        transaction.MyHeader.Content = myheader;
                                        // }
                                    }
                                    else
                                    {
                                        transaction.MyHeader.Content = ((ISerialObject)myheader).Merge(_content);
                                        transaction.MessageReceived  = new DealMessage(transaction, DirectionType.Receive, transaction.MyHeader.Content);
                                    }
                                }
                                else
                                {
                                    isError       = true;
                                    errorMessage += "Prime not exist - incorrect object target ";
                                }
                            }
                            else
                            {
                                isError       = true;
                                errorMessage += "Incorrect DPOT object - deserialization error ";
                            }
                        }
                        else
                        {
                            transaction.MyHeader.Content = new Hashtable()
                            {
                                { "Register", true }
                            };
                            transaction.MyHeader.Context.Echo += "Registration success - ContentType: null ";
                        }
                    }
                    else
                    {
                        isError = true;
                        transaction.MyHeader.Content = new Hashtable()
                        {
                            { "Register", false }
                        };
                        transaction.MyHeader.Context.Echo += "Registration failed - access denied ";
                    }
                }
                else
                {
                    isError       = true;
                    errorMessage += "Incorrect DPOT object - deserialization error ";
                }
            }
            catch (Exception ex)
            {
                isError       = true;
                errorMessage += ex.ToString();
            }

            if (isError)
            {
                transaction.Context.Close          = true;
                transaction.Context.ReceiveMessage = false;
                transaction.Context.SendMessage    = false;

                if (errorMessage != "")
                {
                    transaction.MyHeader.Content      += errorMessage;
                    transaction.MyHeader.Context.Echo += errorMessage;
                }
                transaction.MyHeader.Context.Errors++;
            }
        }